]> pilppa.org Git - libplpdevicebus.git/blob - src_server/DeviceManagerServer.cc
d83accb671f6d4c06c786ce09b868ab5801844fe
[libplpdevicebus.git] / src_server / DeviceManagerServer.cc
1 /*
2  * DeviceManager.cc
3  *
4  *  Created on: Mar 3, 2011
5  *      Author: lamikr
6  */
7 #include <sstream>
8 #include <typeinfo>
9
10 #include <plp/log.h>
11 #include <plp/retval.h>
12
13 #include <plp/Data.hh>
14 #include <plp/Device.hh>
15 #include <plp/SensorDevice.hh>
16 #include <plp/DeviceConfig.hh>
17
18 #include "DeviceManagerServer.hh"
19 #include "../src/plp/devicebus/DeviceBusMessageId.hh"
20
21 using namespace plpdevicebus;
22
23 static void *device_data_reader_thread(void *thread_args_pointer) {
24         list<Device *>                  *_dev_lst;
25         list<Device *>::iterator        list_iter;
26         Device                          *device;
27         SensorDevice                    *sensor;
28         long                            read_int_sec;
29         plp::Data                       *data;
30
31         read_int_sec    = DeviceConfig::get_read_interval_seconds();
32         if (read_int_sec < 0)
33                 read_int_sec = 600;
34         _dev_lst        = (list<Device *> *)thread_args_pointer;
35         while(1) {
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);
39                         if (sensor != NULL) {
40                                 data = sensor->get_data();
41                                 if (data != NULL)
42                                         delete(data);
43                         }
44                 }
45                 sleep(read_int_sec);
46         }
47         pthread_exit(NULL);
48 }
49
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 
56          */
57         log_info("device count: %lu\n", (long unsigned int)_dev_lst.size());
58         _lstnr_thrd     = 0;
59         pthread_create(&_lstnr_thrd,
60                         NULL,
61                         device_data_reader_thread,
62                         (void *)&_dev_lst);
63 }
64
65 DeviceManagerServer::~DeviceManagerServer() {
66 }
67
68 void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
69         Data                            *data;
70         list<Device *>::iterator        list_iter;
71         Device                          *device;
72         SensorDevice                    *sensor;
73         int                             indx;
74         ostringstream                   key;
75         string                          data_str;
76
77         indx    = 0;
78         ((BusMessage *)ret_val)->add_int_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, _dev_lst.size());
79         for (list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
80                 device  = (Device *)*list_iter;
81                 if (device != NULL) {
82                         key.str("");
83                         key << RSP__DEVICE_LIST__ID << indx;
84                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_id());
85
86                         key.str("");
87                         key << RSP__DEVICE_LIST__NAME << indx;
88                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_name());
89
90                         key.str("");
91                         key << RSP__DEVICE_LIST__TYPE << indx;
92                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_type());
93
94                         key.str("");
95                         key << RSP__DEVICE_LIST__LF_STATE << indx;
96                         ((BusMessage *)ret_val)->add_int_parameter(key.str(), device->get_lifecycle_state());
97                         sensor = dynamic_cast<SensorDevice *>(device);
98                         if (sensor != NULL) {
99                                 key.str("");
100                                 key << RSP__DEVICE_LIST__DATA << indx;
101                                 data    = sensor->get_data();
102                                 if (data != NULL) {
103                                         data_str = data->to_string();
104                                         log_debug("returning data: %s\n", data_str.c_str());
105                                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), data_str);
106                                         delete(data);
107                                 }
108                         }
109                         indx++;
110                 }
111         }
112 }
113
114 const Device *DeviceManagerServer::get_device_by_id(string id_param) {
115         Device                  *ret_val;
116         Device                  *device;
117         list<Device *>::iterator list_iter;
118
119         ret_val = NULL;
120         for(list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
121                 device  = (Device *)*list_iter;
122                 if (device != NULL) {
123                         if (device->get_id().compare(id_param) == 0) {
124                                 ret_val = device;
125                                 break;
126                         }
127                 }
128         }
129         return ret_val;
130 }
131
132 static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param, Data *data, string key_name_base_param) {
133         int             ii;
134         int             cnt;
135         double          val;
136         ostringstream   key;
137
138         cnt     = data->get_value_count();
139         for (ii = 0; ii < cnt; ii++) {
140                 key.str("");
141                 key << key_name_base_param.c_str() << ii;
142                 val     = data->get(ii);
143                 ((BusMessage *)msg_rsp_param)->add_double_parameter(key.str(), val);
144         }
145 }
146
147 void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMessage *ret_val) {
148         string                  id;
149         int                     err_flg;
150         Device                  *dev;
151         SensorDevice            *sensor;
152         Data                    *data;
153         ostringstream           key;
154         int                     cnt;
155         const DataReader        *reader;
156         DataRange               *dr;
157
158         id      = msg_req_param->get_string_parameter(REQ__GET_LATEST_DATA__ID, &err_flg);
159         if (err_flg == PLP_OK) {
160                 dev     = (Device *)get_device_by_id(id);
161                 if (dev != NULL) {
162                         sensor  = dynamic_cast<SensorDevice *>(dev);
163                         if (sensor != NULL) {
164                                 reader  = sensor->get_device_data();
165                                 data    = ((DataReader *)reader)->get_latest_data();
166                                 if (data != NULL) {
167                                         cnt     = data->get_value_count();
168                                         ((BusMessage *)ret_val)->add_int_parameter(RSP__GET_LATEST_DATA__VALUE_COUNT, cnt);
169                                         ((BusMessage *)ret_val)->add_string_parameter(RSP__GET_LATEST_DATA__DATE,
170                                                                                 data->get_date().to_string());
171                                         add_data_values_to_bus_message(ret_val,
172                                                                         data, RSP__GET_LATEST_DATA__VALUE);
173                                         ((BusMessage *)ret_val)->printout();
174                                         dr      = ((DataReader *)reader)->get_daily_summary(MIN);
175                                         if (dr != NULL) {
176                                                 if (dr->get_count() > 0) {
177                                                         data    = dr->get_first()->clone();;
178                                                         add_data_values_to_bus_message(ret_val,
179                                                                         data,
180                                                                         RSP__GET_LATEST_DATA__MIN_VALUE);
181                                                 }
182                                                 delete(dr);
183                                         }
184                                         dr      = ((DataReader *)reader)->get_daily_summary(MAX);
185                                         if (dr != NULL) {
186                                                 if (dr->get_count() > 0) {
187                                                         data    = dr->get_first()->clone();
188                                                         add_data_values_to_bus_message(ret_val,
189                                                                                 data,
190                                                                                 RSP__GET_LATEST_DATA__MAX_VALUE);
191                                                 }
192                                                 delete(dr);
193                                         }
194                                 }
195                         }
196                 }
197         }
198 }