return _device_list;
}
-BusMessage *DeviceManagerClient::get_latest_data(BusClient *client_param, string device_id_param) {
+DataRange *DeviceManagerClient::parse_device_data_msg(BusMessage *msg_param,
+ int *err_flg) {
+ Data *data;
+ string data_str;
+ DataRange *ret_val;
+
+ *err_flg = PLP_ERR;
+ ret_val = NULL;
+ if (msg_param != NULL) {
+ data_str = msg_param->get_string_parameter(RSP__GET_LATEST_DATA__VALUE, err_flg);
+ data = Data::parse_string(data_str);
+ ret_val = new DataRange(data);
+ log_info("date: %s\n", data->get_date().to_string().c_str());
+ log_debug("latest data\n");
+ data->printout();
+
+ data_str = msg_param->get_string_parameter(RSP__GET_LATEST_DATA__MIN_VALUE, err_flg);
+ data = Data::parse_string(data_str);
+ ret_val->add(data);
+ log_debug("min data\n");
+ data->printout();
+
+ data_str = msg_param->get_string_parameter(RSP__GET_LATEST_DATA__MAX_VALUE, err_flg);
+ data = Data::parse_string(data_str);
+ ret_val->add(data);
+ log_debug("max data\n");
+ data->printout();
+
+ *err_flg = PLP_OK;
+ }
+ return ret_val;
+}
+
+Data *DeviceManagerClient::get_latest_data(BusClient *client_param,
+ string device_id_param,
+ int *err_flg) {
BusMessage *msg_req;
BusMessage *msg_rsp;
+ Data *ret_val;
+ DataRange *dr;
msg_rsp = NULL;
+ ret_val = NULL;
msg_req = new BusMessage(MSG_TYPE_ID__GET_LATEST_DATA);
msg_req->add_string_parameter(REQ__GET_LATEST_DATA__ID, device_id_param.c_str());
client_param->send_message_and_wait_response(msg_req, &msg_rsp);
+ dr = parse_device_data_msg(msg_rsp, err_flg);
+ ret_val = dr->get_first()->clone();
+ delete(dr);
delete(msg_req);
- return msg_rsp;
+ delete(msg_rsp);
+
+ return ret_val;
}
-list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, int *err_flg) {
+list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param,
+ int *err_flg) {
int ii;
long count;
ostringstream key;
key.str("");
key << RSP__DEVICE_LIST__ID << ii;
id = msg_param->get_string_parameter(key.str(), err_flg);
- if (*err_flg != PLP_OK)
+ if (*err_flg != PLP_OK) {
+ log_error("Failed to parse device id\n");
break;
+ }
key.str("");
key << RSP__DEVICE_LIST__NAME << ii;
name = msg_param->get_string_parameter(key.str(), err_flg);
- if (*err_flg != PLP_OK)
+ if (*err_flg != PLP_OK) {
+ log_error("Failed to parse device name\n");
break;
+ }
key.str("");
key << RSP__DEVICE_LIST__TYPE << ii;
type = msg_param->get_string_parameter(key.str(), err_flg);
- if (*err_flg != PLP_OK)
+ if (*err_flg != PLP_OK) {
+ log_error("Failed to parse device type\n");
break;
+ }
key.str("");
key << RSP__DEVICE_LIST__LF_STATE << ii;
state_i = msg_param->get_int_parameter(key.str(), err_flg);
- if (*err_flg != PLP_OK)
+ if (*err_flg != PLP_OK) {
+ log_error("Failed to parse device's life cycle state\n");
break;
+ }
state = (EnumDeviceLifeCycleStatus)state_i;
key.str("");
key << RSP__DEVICE_LIST__DATA << ii;
datastr = msg_param->get_string_parameter(key.str(), err_flg);
- if (*err_flg != PLP_OK)
+ if (*err_flg != PLP_OK) {
+ log_error("Failed to parse device's data\n");
break;
+ }
data = Data::parse_string(datastr);
if (data == NULL) {
+ log_error("Failed to parse device's data items.\n");
*err_flg = PLP_ERR;
break;
}
-
dev = new DeviceData(id, type, name, state, data);
ret_val->push_back(dev);
}
#include <list>
#include <string>
+#include <plp/Data.hh>
+#include <plp/Device.hh>
#include <plp/bus/BusMessage.hh>
#include <plp/bus/BusClient.hh>
-#include <plp/Device.hh>
+
+using namespace std;
+using namespace plp;
+using namespace plpbus;
namespace plpdevicebus {
class DeviceManagerClient {
public:
DeviceManagerClient();
virtual ~DeviceManagerClient();
- const std::list<plp::Device *> *get_device_list(plpbus::BusClient *client_param,
+ const list<plp::Device *> *get_device_list(plpbus::BusClient *client_param,
+ int *err_flg);
+ Data *get_latest_data(BusClient *client_param,
+ string device_id_param,
int *err_flg);
- plpbus::BusMessage *get_latest_data(plpbus::BusClient *client_param,
- std::string device_id_param);
private:
- std::list<plp::Device *> *_device_list;
- //void send_request__get_device_list(plpbus::BusClient *client_param);
+ list<plp::Device *> *_device_list;
+ DataRange *parse_device_data_msg(BusMessage *msg_param,
+ int *err_flg);
list<plp::Device *> *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param,
int *err_flg);
};
const std::list<plp::Device *> *DeviceManagerController::get_device_list(int *err_flg) {
const list<Device *> *ret_val;
- ret_val = NULL;
+ ret_val = NULL;
+ *err_flg = PLP_ERR;
try {
if (_dev_man != NULL) {
ret_val = _dev_man->get_device_list(_bus_client, err_flg);
}
-BusMessage *DeviceManagerController::get_latest_data(string device_id_param,
+Data *DeviceManagerController::get_latest_data(string device_id_param,
int *err_flg) {
- BusMessage *ret_val;
+ Data *ret_val;
- ret_val = NULL;
+ ret_val = NULL;
+ *err_flg = PLP_ERR;
try {
if (_dev_man != NULL) {
- ret_val =_dev_man->get_latest_data(_bus_client, device_id_param);
+ ret_val = _dev_man->get_latest_data(_bus_client,
+ device_id_param,
+ err_flg);
}
}
catch(...) {
log_error("Could not get latest data\n");
+ *err_flg = PLP_ERR;
}
return ret_val;
}
#define W1DEVICEMANAGERCONTROLLER_HH_
#include <plp/Device.hh>
+#include <plp/Data.hh>
#include "DeviceManagerClient.hh"
#include "ClientListenerImpl.hh"
+using namespace plp;
using namespace plpbus;
namespace plpdevicebus {
DeviceManagerController();
virtual ~DeviceManagerController();
const std::list<plp::Device *> *get_device_list(int *err_flg);
- BusMessage *get_latest_data(std::string device_id_param, int *err_flg);
+ Data *get_latest_data(std::string device_id_param,
+ int *err_flg);
private:
DeviceManagerClient *_dev_man;
BusClient *_bus_client;
DeviceManagerServer::~DeviceManagerServer() {
}
+void decode_device_to_busmessage(const BusMessage *ret_val,
+ Device *device,
+ int indx) {
+ ostringstream key;
+ SensorDevice *sensor;
+ Data *data;
+ string data_str;
+
+ if ((ret_val != NULL) &&
+ (device != NULL)) {
+ key.str("");
+ key << RSP__DEVICE_LIST__ID << indx;
+ ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_id());
+
+ key.str("");
+ key << RSP__DEVICE_LIST__NAME << indx;
+ ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_name());
+
+ key.str("");
+ key << RSP__DEVICE_LIST__TYPE << indx;
+ ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_type());
+
+ key.str("");
+ key << RSP__DEVICE_LIST__LF_STATE << indx;
+ ((BusMessage *)ret_val)->add_int_parameter(key.str(), device->get_lifecycle_state());
+ sensor = dynamic_cast<SensorDevice *>(device);
+ if (sensor != NULL) {
+ key.str("");
+ key << RSP__DEVICE_LIST__DATA << indx;
+ data = sensor->get_data();
+ if (data != NULL) {
+ data_str = data->to_string();
+ log_debug("returning data: %s\n", data_str.c_str());
+ ((BusMessage *)ret_val)->add_string_parameter(key.str(), data_str);
+ delete(data);
+ }
+ }
+ }
+}
+
void DeviceManagerServer::get_device_list(const BusMessage *ret_val) {
- Data *data;
list<Device *>::iterator list_iter;
Device *device;
- SensorDevice *sensor;
int indx;
- ostringstream key;
- string data_str;
indx = 0;
((BusMessage *)ret_val)->add_int_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, _dev_lst.size());
for (list_iter = _dev_lst.begin(); list_iter != _dev_lst.end(); list_iter++) {
device = (Device *)*list_iter;
- if (device != NULL) {
- key.str("");
- key << RSP__DEVICE_LIST__ID << indx;
- ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_id());
-
- key.str("");
- key << RSP__DEVICE_LIST__NAME << indx;
- ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_name());
-
- key.str("");
- key << RSP__DEVICE_LIST__TYPE << indx;
- ((BusMessage *)ret_val)->add_string_parameter(key.str(), device->get_type());
-
- key.str("");
- key << RSP__DEVICE_LIST__LF_STATE << indx;
- ((BusMessage *)ret_val)->add_int_parameter(key.str(), device->get_lifecycle_state());
- sensor = dynamic_cast<SensorDevice *>(device);
- if (sensor != NULL) {
- key.str("");
- key << RSP__DEVICE_LIST__DATA << indx;
- data = sensor->get_data();
- if (data != NULL) {
- data_str = data->to_string();
- log_debug("returning data: %s\n", data_str.c_str());
- ((BusMessage *)ret_val)->add_string_parameter(key.str(), data_str);
- delete(data);
- }
- }
- indx++;
- }
+ decode_device_to_busmessage(ret_val,
+ device,
+ indx);
+ indx++;
}
}
return ret_val;
}
-static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param, Data *data, string key_name_base_param) {
- int ii;
- int cnt;
- double val;
- ostringstream key;
+static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param,
+ Data *data,
+ string key_name_base_param) {
+ string val_str;
- cnt = data->get_value_count();
- for (ii = 0; ii < cnt; ii++) {
- key.str("");
- key << key_name_base_param.c_str() << ii;
- val = data->get(ii);
- ((BusMessage *)msg_rsp_param)->add_double_parameter(key.str(), val);
+ val_str = data->to_string();
+ ((BusMessage *)msg_rsp_param)->add_string_parameter(key_name_base_param, val_str);
+}
+
+void decode_data_to_busmessage(const BusMessage *ret_val,
+ const DataReader *reader) {
+ DataRange *dr;
+ Data *data;
+
+ data = ((DataReader *)reader)->get_latest_data();
+ if (data != NULL) {
+ add_data_values_to_bus_message(ret_val,
+ data,
+ RSP__GET_LATEST_DATA__VALUE);
+ //((BusMessage *)ret_val)->printout();
+ delete(data);
+ }
+ dr = ((DataReader *)reader)->get_daily_summary(MIN);
+ if (dr != NULL) {
+ if (dr->get_count() > 0) {
+ data = dr->get_first();;
+ add_data_values_to_bus_message(ret_val,
+ data,
+ RSP__GET_LATEST_DATA__MIN_VALUE);
+ delete(data);
+ }
+ delete(dr);
+ }
+ dr = ((DataReader *)reader)->get_daily_summary(MAX);
+ if (dr != NULL) {
+ if (dr->get_count() > 0) {
+ data = dr->get_first();
+ add_data_values_to_bus_message(ret_val,
+ data,
+ RSP__GET_LATEST_DATA__MAX_VALUE);
+ delete(data);
+ }
+ delete(dr);
}
}
-void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMessage *ret_val) {
+void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param,
+ const BusMessage *ret_val) {
string id;
int err_flg;
Device *dev;
SensorDevice *sensor;
- Data *data;
ostringstream key;
- int cnt;
const DataReader *reader;
- DataRange *dr;
id = msg_req_param->get_string_parameter(REQ__GET_LATEST_DATA__ID, &err_flg);
if (err_flg == PLP_OK) {
sensor = dynamic_cast<SensorDevice *>(dev);
if (sensor != NULL) {
reader = sensor->get_device_data();
- data = ((DataReader *)reader)->get_latest_data();
- if (data != NULL) {
- cnt = data->get_value_count();
- ((BusMessage *)ret_val)->add_int_parameter(RSP__GET_LATEST_DATA__VALUE_COUNT, cnt);
- ((BusMessage *)ret_val)->add_string_parameter(RSP__GET_LATEST_DATA__DATE,
- data->get_date().to_string());
- add_data_values_to_bus_message(ret_val,
- data, RSP__GET_LATEST_DATA__VALUE);
- ((BusMessage *)ret_val)->printout();
- dr = ((DataReader *)reader)->get_daily_summary(MIN);
- if (dr != NULL) {
- if (dr->get_count() > 0) {
- data = dr->get_first()->clone();;
- add_data_values_to_bus_message(ret_val,
- data,
- RSP__GET_LATEST_DATA__MIN_VALUE);
- }
- delete(dr);
- }
- dr = ((DataReader *)reader)->get_daily_summary(MAX);
- if (dr != NULL) {
- if (dr->get_count() > 0) {
- data = dr->get_first()->clone();
- add_data_values_to_bus_message(ret_val,
- data,
- RSP__GET_LATEST_DATA__MAX_VALUE);
- }
- delete(dr);
- }
- }
+ decode_data_to_busmessage(ret_val, reader);
}
}
}
DeviceManagerServer(list<Device *> dev_lst_param);
virtual ~DeviceManagerServer();
void get_device_list(const BusMessage *msq_rsp_param);
- void get_latest_data(BusMessage *msg_req_param, const BusMessage *msq_rsp_param);
- //void *device_data_reader_thread(void *thread_args_pointer);
+ void get_latest_data(BusMessage *msg_req_param,
+ const BusMessage *msq_rsp_param);
private:
std::list<plp::Device *> _dev_lst;
pthread_t _lstnr_thrd;