]> pilppa.org Git - libplpdevicebus.git/blob - src_client/DeviceManagerClient.cc
801e4d0cba6c31cda08006530c184118623e0c93
[libplpdevicebus.git] / src_client / DeviceManagerClient.cc
1 /*
2  * DeviceManagerClient.cc
3  *
4  *  Created on: Mar 4, 2011
5  *      Author: lamikr
6  */
7 #include <sstream>
8 #include <string>
9
10 #include <plp/log.h>
11 #include <plp/retval.h>
12
13 #include <plp/Data.hh>
14 #include <plp/DeviceData.hh>
15
16 #include "DeviceManagerClient.hh"
17 #include "../src/plp/devicebus/DeviceBusMessageId.hh"
18
19 using namespace std;
20 using namespace plp;
21 using namespace plpbus;
22 using namespace plpdevicebus;
23
24 DeviceManagerClient::DeviceManagerClient() {
25         _device_list    = NULL;
26 }
27
28 static void clean_device_list(list<Device *> *dev_lst) {
29         Device  *dev;
30
31         if (dev_lst != NULL) {
32                 while (dev_lst->empty() == false) {
33                         dev     = dev_lst->back();
34                         dev_lst->pop_back();
35                         delete(dev);
36                 }
37                 delete(dev_lst);
38                 dev_lst = NULL;
39         }
40 }
41
42 DeviceManagerClient::~DeviceManagerClient() {
43         clean_device_list(_device_list);
44 }
45
46 const std::list<plp::Device *> *DeviceManagerClient::get_device_list(BusClient *client_param,
47                                                 int *err_flg) {
48         //send_request__get_device_list(client_param);
49
50         BusMessage      *msg_req;
51         BusMessage      *msg_rsp;
52
53         msg_rsp = NULL;
54         msg_req = new BusMessage(MSG_TYPE_ID__GET_DEVICE_LIST);
55         client_param->send_message_and_wait_response(msg_req, &msg_rsp);
56         clean_device_list(_device_list);
57         _device_list    = parse_device_list_msg(msg_rsp, err_flg);
58         delete(msg_req);
59         delete(msg_rsp);
60
61         return _device_list;
62 }
63
64 BusMessage *DeviceManagerClient::get_latest_data(BusClient *client_param, string device_id_param) {
65         BusMessage      *msg_req;
66         BusMessage      *msg_rsp;
67
68         msg_rsp = NULL;
69         msg_req = new BusMessage(MSG_TYPE_ID__GET_LATEST_DATA);
70         msg_req->add_string_parameter(REQ__GET_LATEST_DATA__ID, device_id_param.c_str());
71         client_param->send_message_and_wait_response(msg_req, &msg_rsp);
72         delete(msg_req);
73         return msg_rsp;
74 }
75
76 list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, int *err_flg) {
77         int                             ii;
78         long                            count;
79         ostringstream                   key;
80         string                          id;
81         string                          name;
82         string                          type;
83         string                          datastr;
84         EnumDeviceLifeCycleStatus       state;
85         int                             state_i;
86         Device                          *dev;
87         Data                            *data;
88         list<Device *>                  *ret_val;
89
90         count   = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, err_flg);
91         log_debug("device count: %ld\n", count);
92         ret_val = new list<Device *>;
93         if (*err_flg == PLP_OK) {
94                 for (ii = 0; ii < count; ii++) {
95                         key.str("");
96                         key << RSP__DEVICE_LIST__ID << ii;
97                         id      = msg_param->get_string_parameter(key.str(), err_flg);
98                         if (*err_flg != PLP_OK)
99                                 break;
100
101                         key.str("");
102                         key << RSP__DEVICE_LIST__NAME << ii;
103                         name    = msg_param->get_string_parameter(key.str(), err_flg);
104                         if (*err_flg != PLP_OK)
105                                 break;
106
107                         key.str("");
108                         key << RSP__DEVICE_LIST__TYPE << ii;
109                         type    = msg_param->get_string_parameter(key.str(), err_flg);
110                         if (*err_flg != PLP_OK)
111                                 break;
112
113                         key.str("");
114                         key << RSP__DEVICE_LIST__LF_STATE << ii;
115                         state_i = msg_param->get_int_parameter(key.str(), err_flg);
116                         if (*err_flg != PLP_OK)
117                                 break;
118                         state   = (EnumDeviceLifeCycleStatus)state_i;
119
120                         key.str("");
121                         key << RSP__DEVICE_LIST__DATA << ii;
122                         datastr = msg_param->get_string_parameter(key.str(), err_flg);
123                         if (*err_flg != PLP_OK)
124                                 break;
125
126                         data    = Data::parse_string(datastr);
127                         if (data == NULL) {
128                                 *err_flg = PLP_ERR;
129                                 break;
130                         }
131
132                         dev     = new DeviceData(id, type, name, state, data);
133                         ret_val->push_back(dev);
134                 }
135         }
136         return ret_val;
137 }