]> pilppa.org Git - libplpdevicebus.git/blob - src_client/DeviceManagerClient.cc
6fabbd544bae6dc9c167f78a3ff80700f1d364ed
[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/DeviceData.hh>
14
15 #include "DeviceManagerClient.hh"
16 #include "../src/plp/devicebus/DeviceBusMessageId.hh"
17
18 using namespace std;
19 using namespace plp;
20 using namespace plpbus;
21 using namespace plpdevicebus;
22
23 DeviceManagerClient::DeviceManagerClient() {
24         _device_list    = NULL;
25 }
26
27 static void clean_device_list(list<Device *> *dev_lst) {
28         Device  *dev;
29
30         if (dev_lst != NULL) {
31                 while (dev_lst->empty() == false) {
32                         dev     = dev_lst->back();
33                         dev_lst->pop_back();
34                         delete(dev);
35                 }
36                 delete(dev_lst);
37                 dev_lst = NULL;
38         }
39 }
40
41 DeviceManagerClient::~DeviceManagerClient() {
42         clean_device_list(_device_list);
43 }
44
45 const std::list<plp::Device *> *DeviceManagerClient::get_device_list(BusClient *client_param) {
46         send_request__get_device_list(client_param);
47         return _device_list;
48 }
49
50 void DeviceManagerClient::send_request__get_device_list(BusClient *client_param) {
51         BusMessage      *msg_req;
52         BusMessage      *msg_rsp;
53
54         msg_rsp = NULL;
55         msg_req = new BusMessage(MSG_TYPE_ID__GET_DEVICE_LIST);
56         client_param->send_message_and_wait_response(msg_req, &msg_rsp);
57         clean_device_list(_device_list);
58         _device_list    = parse_device_list_msg(msg_rsp);
59         delete(msg_req);
60         delete(msg_rsp);
61 }
62
63 BusMessage *DeviceManagerClient::get_latest_data(BusClient *client_param, string device_id_param) {
64         BusMessage      *msg_req;
65         BusMessage      *msg_rsp;
66
67         msg_rsp = NULL;
68         msg_req = new BusMessage(MSG_TYPE_ID__GET_LATEST_DATA);
69         msg_req->add_string_parameter(REQ__GET_LATEST_DATA__ID, device_id_param.c_str());
70         client_param->send_message_and_wait_response(msg_req, &msg_rsp);
71         delete(msg_req);
72         return msg_rsp;
73 }
74
75 list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param) {
76         int                             ii;
77         long                            count;
78         int                             err_flg;
79         ostringstream                   key;
80         string                          id;
81         string                          name;
82         string                          type;
83         EnumDeviceLifeCycleStatus       state;
84         int                             state_i;
85         Device                          *dev;
86         list<Device *>                  *ret_val;
87
88         log_debug("parse_device_list_msg() started\n");
89         count   = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, &err_flg);
90         log_debug("count: %ld\n", count);
91         ret_val = new list<Device *>;
92         if (err_flg == PLP_OK) {
93                 for (ii = 0; ii < count; ii++) {
94                         key.str("");
95                         key << RSP__DEVICE_LIST__ID << ii;
96                         id      = msg_param->get_string_parameter(key.str(), &err_flg);
97
98                         key.str("");
99                         key << RSP__DEVICE_LIST__NAME << ii;
100                         name    = msg_param->get_string_parameter(key.str(), &err_flg);
101
102                         key.str("");
103                         key << RSP__DEVICE_LIST__TYPE << ii;
104                         type    = msg_param->get_string_parameter(key.str(), &err_flg);
105
106                         key.str("");
107                         key << RSP__DEVICE_LIST__LF_STATE << ii;
108                         state_i = msg_param->get_int_parameter(key.str(), &err_flg);
109                         state   = (EnumDeviceLifeCycleStatus)state_i;
110
111                         dev     = new DeviceData(id, type, name, state);
112                         ret_val->push_back(dev);
113                 }
114         }
115         return ret_val;
116 }