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