Prelim sensor callback implementation, should probably be moved out of the chaiscript dir at some point
This commit is contained in:
parent
855e5acd2b
commit
1919d88f4f
@ -18,6 +18,10 @@ if(Boost_FOUND)
|
||||
add_executable(chaiscript_callbacktest callbacktest.cpp)
|
||||
target_link_libraries(chaiscript_callbacktest ${Boost_LIBRARIES})
|
||||
|
||||
add_executable(sensors sensors.cpp)
|
||||
target_link_libraries(sensors ${Boost_LIBRARIES})
|
||||
|
||||
|
||||
endif()
|
||||
|
||||
|
||||
|
104
chaiscript/sensors.cpp
Normal file
104
chaiscript/sensors.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "chaiscript.hpp"
|
||||
#include "function_call.hpp"
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
struct Sensor_Manager
|
||||
{
|
||||
struct Sensor
|
||||
{
|
||||
int milliseconds;
|
||||
dispatchkit::Boxed_Value state_object;
|
||||
boost::function<double (dispatchkit::Boxed_Value)> sensor;
|
||||
boost::posix_time::ptime next_run;
|
||||
|
||||
Sensor(int t_milliseconds, dispatchkit::Boxed_Value t_state_object,
|
||||
boost::function<double (dispatchkit::Boxed_Value)> t_sensor)
|
||||
: milliseconds(t_milliseconds), state_object(t_state_object), sensor(t_sensor),
|
||||
next_run(boost::posix_time::microsec_clock::universal_time()
|
||||
+ boost::posix_time::milliseconds(milliseconds))
|
||||
{
|
||||
}
|
||||
|
||||
std::pair<boost::posix_time::ptime, double> get_value()
|
||||
{
|
||||
next_run = boost::posix_time::microsec_clock::universal_time()
|
||||
+ boost::posix_time::milliseconds(milliseconds);
|
||||
|
||||
return std::make_pair(boost::posix_time::microsec_clock::universal_time(),
|
||||
sensor(state_object));
|
||||
}
|
||||
};
|
||||
|
||||
std::map<std::string, Sensor> m_sensors;
|
||||
|
||||
//sensor_manager.add_sensor("CPU", 1000, global_state, function(state) { update_state(state); state["CPU"]; } )
|
||||
void add_sensor(const std::string &t_name, int t_milliseconds, dispatchkit::Boxed_Value t_state_object,
|
||||
boost::shared_ptr<dispatchkit::Proxy_Function> t_func)
|
||||
{
|
||||
m_sensors.insert(
|
||||
std::make_pair(t_name,
|
||||
Sensor(t_milliseconds, t_state_object,
|
||||
dispatchkit::build_function_caller<double, dispatchkit::Boxed_Value>(t_func)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::pair<std::string, double> > run_sensors()
|
||||
{
|
||||
std::vector<std::pair<std::string, double> > results;
|
||||
|
||||
boost::posix_time::ptime t(boost::posix_time::microsec_clock::universal_time());
|
||||
|
||||
for (std::map<std::string, Sensor>::iterator itr = m_sensors.begin();
|
||||
itr != m_sensors.end();
|
||||
++itr)
|
||||
{
|
||||
if (itr->second.next_run <= t)
|
||||
{
|
||||
results.push_back(std::make_pair(itr->first, itr->second.get_value().second));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
chaiscript::ChaiScript_Engine chai;
|
||||
|
||||
Sensor_Manager sensor_manager;
|
||||
chai.get_eval_engine().add_object("sensor_manager", boost::ref(sensor_manager));
|
||||
dispatchkit::register_function(chai.get_eval_engine(), &Sensor_Manager::add_sensor, "add_sensor");
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
try {
|
||||
chai.evaluate_file(argv[i]);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
std::cerr << "Could not open: " << argv[i] << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
usleep(1000);
|
||||
std::vector<std::pair<std::string, double> > sensor_data = sensor_manager.run_sensors();
|
||||
|
||||
for (std::vector<std::pair<std::string, double> >::iterator itr = sensor_data.begin();
|
||||
itr != sensor_data.end();
|
||||
++itr)
|
||||
{
|
||||
std::cout << "Sensor: " << itr->first << " value: " << itr->second << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
samples/sensors.chai
Normal file
14
samples/sensors.chai
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
|
||||
var global_state = Map()
|
||||
|
||||
global_state["CPU"] = 0.0;
|
||||
global_state["CPU0"] = 0.0;
|
||||
|
||||
def update_state(state, name)
|
||||
{
|
||||
++state[name];
|
||||
}
|
||||
|
||||
sensor_manager.add_sensor("CPU", 1000, global_state, function(state) { update_state(state, "CPU"); state["CPU"]; } )
|
||||
sensor_manager.add_sensor("CPU0", 1000, global_state, function(state) { update_state(state, "CPU0"); state["CPU0"]; } )
|
Loading…
x
Reference in New Issue
Block a user