]> pilppa.org Git - libplpdevicebus.git/blob - src_server/DeviceManagerServer.cc
53b5a4916a0a9ca830c74b55768e0ae59b96714f
[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
29         _dev_lst        = (list<Device *> *)thread_args_pointer;
30         while(1) {
31                 for (list_iter = _dev_lst->begin(); list_iter != _dev_lst->end(); list_iter++) {
32                         device  = (Device *)*list_iter;
33                         sensor = dynamic_cast<SensorDevice *>(device);
34                         if (sensor != NULL) {
35                                 sensor->get_data();
36                                 sleep(60);
37                         }
38                 }
39         }
40         pthread_exit(NULL);
41 }
42
43 DeviceManagerServer::DeviceManagerServer(list<Device *> dev_lst_param) {
44         //DeviceConfig::set_base_dir_name(storage_dir_param);
45         //_dev_lst      = Factory::get_device_list();
46         _dev_lst        = dev_lst_param;
47         /* In some toolchains the size is not unsigned int instead of long
48            unsigned int, and that can cause warnings/errors without typecasting 
49          */
50         log_info("device count: %lu\n", (long unsigned int)_dev_lst.size());
51         _lstnr_thrd     = 0;
52         pthread_create(&_lstnr_thrd,
53                         NULL,
54                         device_data_reader_thread,
55                         (void *)&_dev_lst);
56 }
57
58 DeviceManagerServer::~DeviceManagerServer() {
59 }
60
61 void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
62         Data                            *data;
63         list<Device *>::iterator        list_iter;
64         Device                          *device;
65         SensorDevice                    *sensor;
66         int                             indx;
67         ostringstream                   key;
68
69         indx    = 0;
70         ((BusMessage *)ret_val)->add_int_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, _dev_lst.size());
71         for (list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
72                 device  = (Device *)*list_iter;
73                 if (device != NULL) {
74                         key.str("");
75                         key << RSP__DEVICE_LIST__ID << indx;
76                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_id());
77
78                         key.str("");
79                         key << RSP__DEVICE_LIST__NAME << indx;
80                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_name());
81
82                         key.str("");
83                         key << RSP__DEVICE_LIST__TYPE << indx;
84                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_type());
85
86                         key.str("");
87                         key << RSP__DEVICE_LIST__LF_STATE << indx;
88                         ((BusMessage *)ret_val)->add_int_parameter(key.str(), device->get_lifecycle_state());
89                         sensor = dynamic_cast<SensorDevice *>(device);
90                         if (sensor != NULL) {
91                                 key.str("");
92                                 key << RSP__DEVICE_LIST__DATA << indx;
93                                 data    = sensor->get_data();
94                                 if (data != NULL) {
95                                         log_debug("returning data: %s\n", data->to_string().c_str());
96                                         ((BusMessage *)ret_val)->add_string_parameter(key.str(), data->to_string());
97                                 }
98                         }
99                         indx++;
100                 }
101         }
102 }
103
104 const Device *DeviceManagerServer::get_device_by_id(string id_param) {
105         Device                  *ret_val;
106         Device                  *device;
107         list<Device *>::iterator list_iter;
108
109         ret_val = NULL;
110         for(list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
111                 device  = (Device *)*list_iter;
112                 if (device != NULL) {
113                         if (device->get_id().compare(id_param) == 0) {
114                                 ret_val = device;
115                                 break;
116                         }
117                 }
118         }
119         return ret_val;
120 }
121
122 static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param, Data *data, string key_name_base_param) {
123         int             ii;
124         int             cnt;
125         double          val;
126         ostringstream   key;
127
128         cnt     = data->get_value_count();
129         for (ii = 0; ii < cnt; ii++) {
130                 key.str("");
131                 key << key_name_base_param.c_str() << ii;
132                 val     = data->get(ii);
133                 ((BusMessage *)msg_rsp_param)->add_double_parameter(key.str(), val);
134         }
135 }
136
137 void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMessage *ret_val) {
138         string                  id;
139         int                     err_flg;
140         Device                  *dev;
141         SensorDevice            *sensor;
142         Data                    *data;
143         ostringstream           key;
144         int                     cnt;
145         const DataReader        *reader;
146         DataRange               *dr;
147
148         id      = msg_req_param->get_string_parameter(REQ__GET_LATEST_DATA__ID, &err_flg);
149         if (err_flg == PLP_OK) {
150                 dev     = (Device *)get_device_by_id(id);
151                 if (dev != NULL) {
152                         sensor  = dynamic_cast<SensorDevice *>(dev);
153                         if (sensor != NULL) {
154                                 reader  = sensor->get_device_data();
155                                 data    = ((DataReader *)reader)->get_latest_data();
156                                 if (data != NULL) {
157                                         cnt     = data->get_value_count();
158                                         ((BusMessage *)ret_val)->add_int_parameter(RSP__GET_LATEST_DATA__VALUE_COUNT, cnt);
159                                         ((BusMessage *)ret_val)->add_string_parameter(RSP__GET_LATEST_DATA__DATE, data->get_date().to_string());
160                                         add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__VALUE);
161                                         ((BusMessage *)ret_val)->printout();
162                                         dr      = ((DataReader *)reader)->get_daily_summary(MIN);
163                                         if (dr != NULL) {
164                                                 if (dr->get_count() > 0) {
165                                                         data    = dr->get_first()->clone();;
166                                                         add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__MIN_VALUE);
167                                                 }
168                                                 delete(dr);
169                                         }
170                                         dr      = ((DataReader *)reader)->get_daily_summary(MAX);
171                                         if (dr != NULL) {
172                                                 if (dr->get_count() > 0) {
173                                                         data    = dr->get_first()->clone();
174                                                         add_data_values_to_bus_message(ret_val, data, RSP__GET_LATEST_DATA__MAX_VALUE);
175                                                 }
176                                                 delete(dr);
177                                         }
178                                 }
179                         }
180                 }
181         }
182 }