4 * Created on: Mar 3, 2011
11 #include <plp/retval.h>
13 #include <plp/Data.hh>
14 #include <plp/Device.hh>
15 #include <plp/SensorDevice.hh>
16 #include <plp/DeviceConfig.hh>
18 #include "DeviceManagerServer.hh"
19 #include "../src/plp/devicebus/DeviceBusMessageId.hh"
21 using namespace plpdevicebus;
23 static void *device_data_reader_thread(void *thread_args_pointer) {
24 list<Device *> *_dev_lst;
25 list<Device *>::iterator list_iter;
31 read_int_sec = DeviceConfig::get_read_interval_seconds();
34 _dev_lst = (list<Device *> *)thread_args_pointer;
36 for (list_iter = _dev_lst->begin(); list_iter != _dev_lst->end(); list_iter++) {
37 device = (Device *)*list_iter;
38 sensor = dynamic_cast<SensorDevice *>(device);
40 data = sensor->get_data();
50 DeviceManagerServer::DeviceManagerServer(list<Device *> dev_lst_param) {
51 //DeviceConfig::set_base_dir_name(storage_dir_param);
52 //_dev_lst = Factory::get_device_list();
53 _dev_lst = dev_lst_param;
54 /* In some toolchains the size is not unsigned int instead of long
55 unsigned int, and that can cause warnings/errors without typecasting
57 log_info("device count: %lu\n", (long unsigned int)_dev_lst.size());
59 pthread_create(&_lstnr_thrd,
61 device_data_reader_thread,
65 DeviceManagerServer::~DeviceManagerServer() {
68 void decode_device_to_busmessage(const BusMessage *ret_val,
76 if ((ret_val != NULL) &&
79 key << RSP__DEVICE_LIST__ID << indx;
80 ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_id());
83 key << RSP__DEVICE_LIST__NAME << indx;
84 ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_name());
87 key << RSP__DEVICE_LIST__TYPE << indx;
88 ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_type());
91 key << RSP__DEVICE_LIST__LF_STATE << indx;
92 ((BusMessage *)ret_val)->add_int_parameter(key.str(), device->get_lifecycle_state());
93 sensor = dynamic_cast<SensorDevice *>(device);
96 key << RSP__DEVICE_LIST__DATA << indx;
97 data = sensor->get_data();
99 data_str = data->to_string();
100 log_debug("returning data: %s\n", data_str.c_str());
101 ((BusMessage *)ret_val)->add_string_parameter(key.str(), data_str);
108 void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
109 list<Device *>::iterator list_iter;
114 ((BusMessage *)ret_val)->add_int_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, _dev_lst.size());
115 for (list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
116 device = (Device *)*list_iter;
117 decode_device_to_busmessage(ret_val,
124 const Device *DeviceManagerServer::get_device_by_id(string id_param) {
127 list<Device *>::iterator list_iter;
130 for(list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
131 device = (Device *)*list_iter;
132 if (device != NULL) {
133 if (device->get_id().compare(id_param) == 0) {
142 static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param,
144 string key_name_base_param) {
147 val_str = data->to_string();
148 ((BusMessage *)msg_rsp_param)->add_string_parameter(key_name_base_param, val_str);
151 void decode_data_to_busmessage(const BusMessage *ret_val,
152 const DataReader *reader) {
156 data = ((DataReader *)reader)->get_latest_data();
158 add_data_values_to_bus_message(ret_val,
160 RSP__GET_LATEST_DATA__VALUE);
161 //((BusMessage *)ret_val)->printout();
164 dr = ((DataReader *)reader)->get_daily_summary(MIN);
166 if (dr->get_count() > 0) {
167 data = dr->get_first();;
168 add_data_values_to_bus_message(ret_val,
170 RSP__GET_LATEST_DATA__MIN_VALUE);
175 dr = ((DataReader *)reader)->get_daily_summary(MAX);
177 if (dr->get_count() > 0) {
178 data = dr->get_first();
179 add_data_values_to_bus_message(ret_val,
181 RSP__GET_LATEST_DATA__MAX_VALUE);
188 void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param,
189 const BusMessage *ret_val) {
193 SensorDevice *sensor;
195 const DataReader *reader;
197 id = msg_req_param->get_string_parameter(REQ__GET_LATEST_DATA__ID, &err_flg);
198 if (err_flg == PLP_OK) {
199 dev = (Device *)get_device_by_id(id);
201 sensor = dynamic_cast<SensorDevice *>(dev);
202 if (sensor != NULL) {
203 reader = sensor->get_datareader();
204 decode_data_to_busmessage(ret_val, reader);