From: Mika Laitio Date: Mon, 13 Aug 2012 17:23:29 +0000 (+0300) Subject: implemented get_latest_data method X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=5cb22566f978007d9cb037032e2fc2d0affc9199;p=libplpdevicebus.git implemented get_latest_data method - Implemented new client/server method to get data by device id. - Fixed memory leaks. Signed-off-by: Mika Laitio --- diff --git a/src_client/DeviceManagerClient.cc b/src_client/DeviceManagerClient.cc index 801e4d0..fb4e62e 100644 --- a/src_client/DeviceManagerClient.cc +++ b/src_client/DeviceManagerClient.cc @@ -61,19 +61,63 @@ const std::list *DeviceManagerClient::get_device_list(BusClient * 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 *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, int *err_flg) { +list *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, + int *err_flg) { int ii; long count; ostringstream key; @@ -95,40 +139,50 @@ list *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param 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); } diff --git a/src_client/DeviceManagerClient.hh b/src_client/DeviceManagerClient.hh index cb88c90..fbfdff9 100644 --- a/src_client/DeviceManagerClient.hh +++ b/src_client/DeviceManagerClient.hh @@ -11,22 +11,29 @@ #include #include +#include +#include #include #include -#include + +using namespace std; +using namespace plp; +using namespace plpbus; namespace plpdevicebus { class DeviceManagerClient { public: DeviceManagerClient(); virtual ~DeviceManagerClient(); - const std::list *get_device_list(plpbus::BusClient *client_param, + const list *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 *_device_list; - //void send_request__get_device_list(plpbus::BusClient *client_param); + list *_device_list; + DataRange *parse_device_data_msg(BusMessage *msg_param, + int *err_flg); list *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param, int *err_flg); }; diff --git a/src_client/DeviceManagerController.cc b/src_client/DeviceManagerController.cc index bdd2348..9b4e89d 100644 --- a/src_client/DeviceManagerController.cc +++ b/src_client/DeviceManagerController.cc @@ -57,7 +57,8 @@ DeviceManagerController::~DeviceManagerController() { const std::list *DeviceManagerController::get_device_list(int *err_flg) { const list *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); @@ -74,18 +75,22 @@ const std::list *DeviceManagerController::get_device_list(int *er } -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; } diff --git a/src_client/DeviceManagerController.hh b/src_client/DeviceManagerController.hh index 0d739a3..2208189 100644 --- a/src_client/DeviceManagerController.hh +++ b/src_client/DeviceManagerController.hh @@ -9,10 +9,12 @@ #define W1DEVICEMANAGERCONTROLLER_HH_ #include +#include #include "DeviceManagerClient.hh" #include "ClientListenerImpl.hh" +using namespace plp; using namespace plpbus; namespace plpdevicebus { @@ -21,7 +23,8 @@ namespace plpdevicebus { DeviceManagerController(); virtual ~DeviceManagerController(); const std::list *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; diff --git a/src_server/DeviceManagerServer.cc b/src_server/DeviceManagerServer.cc index d83accb..3eb8a01 100644 --- a/src_server/DeviceManagerServer.cc +++ b/src_server/DeviceManagerServer.cc @@ -65,49 +65,59 @@ DeviceManagerServer::DeviceManagerServer(list dev_lst_param) { 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(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::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(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++; } } @@ -129,31 +139,60 @@ const Device *DeviceManagerServer::get_device_by_id(string id_param) { 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) { @@ -162,36 +201,7 @@ void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMe sensor = dynamic_cast(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); } } } diff --git a/src_server/DeviceManagerServer.hh b/src_server/DeviceManagerServer.hh index 057432b..8854c7c 100644 --- a/src_server/DeviceManagerServer.hh +++ b/src_server/DeviceManagerServer.hh @@ -26,8 +26,8 @@ namespace plpdevicebus { DeviceManagerServer(list 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 _dev_lst; pthread_t _lstnr_thrd;