From b6a8a25f6381f2b47af6fc35db880ed0bf25af99 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Wed, 15 Aug 2012 23:03:46 +0300 Subject: [PATCH] implement method for getting data between dates Signed-off-by: Mika Laitio --- src/plp/devicebus/DeviceBusMessageId.hh | 7 +++ src_client/DeviceManagerClient.cc | 62 +++++++++++++++++++-- src_client/DeviceManagerClient.hh | 9 +++- src_client/DeviceManagerController.cc | 24 +++++++++ src_client/DeviceManagerController.hh | 4 ++ src_server/DeviceManagerServer.cc | 72 ++++++++++++++++++++++++- src_server/DeviceManagerServer.hh | 2 + src_server/ServerListenerImpl.cc | 11 ++-- 8 files changed, 182 insertions(+), 9 deletions(-) diff --git a/src/plp/devicebus/DeviceBusMessageId.hh b/src/plp/devicebus/DeviceBusMessageId.hh index 2816598..9755470 100644 --- a/src/plp/devicebus/DeviceBusMessageId.hh +++ b/src/plp/devicebus/DeviceBusMessageId.hh @@ -12,6 +12,7 @@ #define MSG_TYPE_ID__GET_DEVICE_LIST 1 #define MSG_TYPE_ID__GET_LATEST_DATA 2 #define MSG_TYPE_ID__GET_DATA_RANGE 3 +#define MSG_TYPE_ID__GET_DATA_BETWEEN_DATES 4 #define RSP__DEVICE_LIST__DEVICE_COUNT "devcount" #define RSP__DEVICE_LIST__ID "id_" @@ -21,10 +22,16 @@ #define RSP__DEVICE_LIST__DATA "data_" #define REQ__DEVICE_ID "id" +#define REQ__DATE1 "date1" +#define REQ__DATE2 "date2" #define RSP__FIRST_DATA_VALUE "firstvalue_" #define RSP__LATEST_DATA_VALUE "lastvalue_" #define RSP__MIN_DATA_VALUE "minvalue_" #define RSP__MAX_DATA_VALUE "maxvalue_" +#define RSP__COUNT "count" +#define RSP__DATA_VALUE "dvalue" +#define RSP__DATA_UNIT "dunit" + #endif /* DEVICEBUSMESSAGEID_HH_ */ diff --git a/src_client/DeviceManagerClient.cc b/src_client/DeviceManagerClient.cc index 2ef02ee..bacbdb4 100644 --- a/src_client/DeviceManagerClient.cc +++ b/src_client/DeviceManagerClient.cc @@ -151,7 +151,7 @@ void DeviceManagerClient::parse_device_data_msg(BusMessage *msg_param, } if (data != NULL) { result->add(data); - log_debug("%s\n", description.c_str()); + //log_debug("%s\n", description.c_str()); data->printout(); delete(data); } @@ -197,7 +197,7 @@ Data *DeviceManagerClient::get_latest_data(BusClient *client_param, return ret_val; } -DataRange *DeviceManagerClient::parse_data_range_msg(BusMessage *msg_param, +DataRange *DeviceManagerClient::parse_first_and_last_data_msg(BusMessage *msg_param, int *err_flg) { DataRange *ret_val; @@ -212,6 +212,39 @@ DataRange *DeviceManagerClient::parse_data_range_msg(BusMessage *msg_param, return ret_val; } +DataRange *DeviceManagerClient::parse_data_range_msg(BusMessage *msg_param, + int *err_flg) { + DataRange *ret_val; + int ii; + int count; + string data_str; + string key_str; + string desc_str; + ostringstream key; + ostringstream desc; + + *err_flg = PLP_ERR; + ret_val = NULL; + if (msg_param != NULL) { + ret_val = new DataRange(); + count = msg_param->get_int_parameter(RSP__COUNT, err_flg); + //unit = msg_param->get_string_parameter(RSP__DATA_UNIT, err_flg); + for (ii = 0; ii < count; ii++) { + key.str(""); + key << RSP__DATA_VALUE << ii; + desc.str(""); + desc << "value_" << ii; + parse_device_data_msg(msg_param, + ret_val, + key.str(), + err_flg, + desc.str()); + } + *err_flg = PLP_OK; + } + return ret_val; +} + DataRange *DeviceManagerClient::get_data_range(BusClient *client_param, string device_id_param, int *err_flg) { @@ -222,7 +255,30 @@ DataRange *DeviceManagerClient::get_data_range(BusClient *client_param, msg_rsp = NULL; ret_val = NULL; msg_req = new BusMessage(MSG_TYPE_ID__GET_DATA_RANGE); - msg_req->add_string_parameter(REQ__DEVICE_ID, device_id_param.c_str()); + msg_req->add_string_parameter(REQ__DEVICE_ID, device_id_param); + client_param->send_message_and_wait_response(msg_req, &msg_rsp); + ret_val = parse_first_and_last_data_msg(msg_rsp, err_flg); + delete(msg_req); + delete(msg_rsp); + + return ret_val; +} + +DataRange *DeviceManagerClient::get_data(BusClient *client_param, + string device_id_param, + Date *start_date, + Date *end_date, + int *err_flg) { + BusMessage *msg_req; + BusMessage *msg_rsp; + DataRange *ret_val; + + msg_rsp = NULL; + ret_val = NULL; + msg_req = new BusMessage(MSG_TYPE_ID__GET_DATA_BETWEEN_DATES); + msg_req->add_string_parameter(REQ__DEVICE_ID, device_id_param); + msg_req->add_string_parameter(REQ__DATE1, start_date->to_string()); + msg_req->add_string_parameter(REQ__DATE2, end_date->to_string()); client_param->send_message_and_wait_response(msg_req, &msg_rsp); ret_val = parse_data_range_msg(msg_rsp, err_flg); delete(msg_req); diff --git a/src_client/DeviceManagerClient.hh b/src_client/DeviceManagerClient.hh index 587b501..3da3918 100644 --- a/src_client/DeviceManagerClient.hh +++ b/src_client/DeviceManagerClient.hh @@ -33,6 +33,11 @@ namespace plpdevicebus { DataRange *get_data_range(BusClient *client_param, string device_id_param, int *err_flg); + DataRange *get_data(BusClient *client_param, + string device_id_param, + Date *start_date, + Date *end_date, + int *err_flg); private: list *_device_list; list *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param, @@ -44,8 +49,10 @@ namespace plpdevicebus { string description); DataRange *parse_device_data_msg(BusMessage *msg_param, int *err_flg); - DataRange *parse_data_range_msg(BusMessage *msg_param, + DataRange *parse_first_and_last_data_msg(BusMessage *msg_param, int *err_flg); + DataRange *parse_data_range_msg(BusMessage *msg_param, + int *err_flg); }; } diff --git a/src_client/DeviceManagerController.cc b/src_client/DeviceManagerController.cc index fb40b58..2b1ef6e 100644 --- a/src_client/DeviceManagerController.cc +++ b/src_client/DeviceManagerController.cc @@ -114,3 +114,27 @@ DataRange *DeviceManagerController::get_data_range(string device_id_param, } return ret_val; } + +DataRange *DeviceManagerController::get_data(std::string device_id_param, + plp::Date *start_date, + plp::Date *end_date, + int *err_flg) { + DataRange *ret_val; + + ret_val = NULL; + *err_flg = PLP_ERR; + try { + if (_dev_man != NULL) { + ret_val = _dev_man->get_data(_bus_client, + device_id_param, + start_date, + end_date, + err_flg); + } + } + catch(...) { + log_error("Could not get data\n"); + *err_flg = PLP_ERR; + } + return ret_val; +} diff --git a/src_client/DeviceManagerController.hh b/src_client/DeviceManagerController.hh index e7529b8..b2df19c 100644 --- a/src_client/DeviceManagerController.hh +++ b/src_client/DeviceManagerController.hh @@ -27,6 +27,10 @@ namespace plpdevicebus { int *err_flg); DataRange *get_data_range(std::string device_id_param, int *err_flg); + DataRange *get_data(std::string device_id_param, + plp::Date *start_date, + plp::Date *end_date, + int *err_flg); private: DeviceManagerClient *_dev_man; BusClient *_bus_client; diff --git a/src_server/DeviceManagerServer.cc b/src_server/DeviceManagerServer.cc index 9d96db7..1a7e172 100644 --- a/src_server/DeviceManagerServer.cc +++ b/src_server/DeviceManagerServer.cc @@ -185,8 +185,8 @@ void decode_latest_data_to_busmessage(const BusMessage *ret_val, } } -void decode_data_range_to_busmessage(const BusMessage *ret_val, - DataRange *dr) { +void decode_first_and_last_data_to_busmessage(const BusMessage *ret_val, + DataRange *dr) { Data *data; data = dr->get_first(); @@ -207,6 +207,28 @@ void decode_data_range_to_busmessage(const BusMessage *ret_val, } } +void decode_data_range_to_busmessage(const BusMessage *ret_val, + DataRange *dr) { + Data *data; + int ii; + int count; + ostringstream key; + + count = dr->get_count(); + ((BusMessage *)ret_val)->add_int_parameter(RSP__COUNT, count); + for (ii = 0; ii < count; ii++) { + data = dr->get(ii); + if (data != NULL) { + key.str(""); + key << RSP__DATA_VALUE << ii; + add_data_values_to_bus_message(ret_val, + data, + key.str()); + delete(data); + } + } +} + void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMessage *ret_val) { string id; @@ -248,6 +270,49 @@ void DeviceManagerServer::get_data_range(BusMessage *msg_req_param, reader = sensor->get_datareader(); if (reader != NULL) { dr = ((DataReader *)reader)->get_data_range(); + if (dr != NULL) { + decode_first_and_last_data_to_busmessage(ret_val, dr); + delete(dr); + } + } + } + } + } +} + +void DeviceManagerServer::get_data_between_dates(BusMessage *msg_req_param, + const BusMessage *ret_val) { + string id; + int err_flg; + Device *dev; + SensorDevice *sensor; + ostringstream key; + const DataReader *reader; + DataRange *dr; + string startdate_str; + string enddate_str; + Date sdate; + Date edate; + + log_debug("started\n"); + id = msg_req_param->get_string_parameter(REQ__DEVICE_ID, &err_flg); + if (err_flg == PLP_OK) + startdate_str = msg_req_param->get_string_parameter(REQ__DATE1, &err_flg); + if (err_flg == PLP_OK) + enddate_str = msg_req_param->get_string_parameter(REQ__DATE2, &err_flg); + log_debug("id: %s, sdate: %s, edate: %s\n", id.c_str(), startdate_str.c_str(), enddate_str.c_str()); + if (err_flg == PLP_OK) { + sdate = Date::parse_date_str(startdate_str); + edate = Date::parse_date_str(enddate_str); + log_debug("id: %s, sdate: %s, edate: %s\n", id.c_str(), startdate_str.c_str(), enddate_str.c_str()); + dev = (Device *)get_device_by_id(id); + if (dev != NULL) { + sensor = dynamic_cast(dev); + if (sensor != NULL) { + reader = sensor->get_datareader(); + if (reader != NULL) { + dr = ((DataReader *)reader)->get_data(&sdate, &edate); + log_debug("dr.size: %d\n", dr->get_count()); if (dr != NULL) { decode_data_range_to_busmessage(ret_val, dr); delete(dr); @@ -256,4 +321,7 @@ void DeviceManagerServer::get_data_range(BusMessage *msg_req_param, } } } + else { + log_error("Invalid request parameters.\n"); + } } diff --git a/src_server/DeviceManagerServer.hh b/src_server/DeviceManagerServer.hh index 180dd54..0ed97da 100644 --- a/src_server/DeviceManagerServer.hh +++ b/src_server/DeviceManagerServer.hh @@ -30,6 +30,8 @@ namespace plpdevicebus { const BusMessage *msq_rsp_param); void get_data_range(BusMessage *msg_req_param, const BusMessage *msq_rsp_param); + void get_data_between_dates(BusMessage *msg_req_param, + const BusMessage *msq_rsp_param); private: std::list _dev_lst; pthread_t _lstnr_thrd; diff --git a/src_server/ServerListenerImpl.cc b/src_server/ServerListenerImpl.cc index fc7195a..61132ff 100644 --- a/src_server/ServerListenerImpl.cc +++ b/src_server/ServerListenerImpl.cc @@ -36,17 +36,22 @@ int ServerListenerImpl::request_received(const BusMessage *msg_req_param, const case MSG_TYPE_ID__GET_DEVICE_LIST: log_debug("get device list request handling started\n"); _dev_man->get_device_list(msg_rsp_param); - ret_val = 0; + ret_val = 0; break; case MSG_TYPE_ID__GET_LATEST_DATA: log_debug("get latest data request handling started\n"); _dev_man->get_latest_data((BusMessage *)msg_req_param, msg_rsp_param); - ret_val = 0; + ret_val = 0; break; case MSG_TYPE_ID__GET_DATA_RANGE: log_debug("get data range request handling started\n"); _dev_man->get_data_range((BusMessage *)msg_req_param, msg_rsp_param); - ret_val = 0; + ret_val = 0; + break; + case MSG_TYPE_ID__GET_DATA_BETWEEN_DATES: + log_debug("get data between dates handling started\n"); + _dev_man->get_data_between_dates((BusMessage *)msg_req_param, msg_rsp_param); + ret_val = 0; break; default: log_debug("unknown message type-id received: %ld\n", type_id); -- 2.41.1