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