From: Mika Laitio Date: Wed, 15 Aug 2012 11:17:43 +0000 (+0300) Subject: implement get_data_range msgbus method X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=d640f2d16d690e9602154c8d7383adba2f2b4d6e;p=libplpdevicebus.git implement get_data_range msgbus method add method for querying the datarange for device id. datarange contains the first and last data that can be found. Signed-off-by: Mika Laitio --- diff --git a/src/plp/devicebus/DeviceBusMessageId.hh b/src/plp/devicebus/DeviceBusMessageId.hh index e7be23d..2816598 100644 --- a/src/plp/devicebus/DeviceBusMessageId.hh +++ b/src/plp/devicebus/DeviceBusMessageId.hh @@ -11,6 +11,7 @@ #define W1_SERVER_NAME "nws_w1" #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 RSP__DEVICE_LIST__DEVICE_COUNT "devcount" #define RSP__DEVICE_LIST__ID "id_" @@ -19,12 +20,11 @@ #define RSP__DEVICE_LIST__LF_STATE "lifecycle_state_" #define RSP__DEVICE_LIST__DATA "data_" -#define REQ__GET_LATEST_DATA__ID "id" +#define REQ__DEVICE_ID "id" -#define RSP__GET_LATEST_DATA__DATE "date" -#define RSP__GET_LATEST_DATA__VALUE_COUNT "count" -#define RSP__GET_LATEST_DATA__VALUE "value_" -#define RSP__GET_LATEST_DATA__MIN_VALUE "minvalue_" -#define RSP__GET_LATEST_DATA__MAX_VALUE "maxvalue_" +#define RSP__FIRST_DATA_VALUE "firstvalue_" +#define RSP__LATEST_DATA_VALUE "lastvalue_" +#define RSP__MIN_DATA_VALUE "minvalue_" +#define RSP__MAX_DATA_VALUE "maxvalue_" #endif /* DEVICEBUSMESSAGEID_HH_ */ diff --git a/src_client/DeviceManagerClient.cc b/src_client/DeviceManagerClient.cc index 9551282..2ef02ee 100644 --- a/src_client/DeviceManagerClient.cc +++ b/src_client/DeviceManagerClient.cc @@ -43,6 +43,80 @@ DeviceManagerClient::~DeviceManagerClient() { clean_device_list(_device_list); } +list *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, + int *err_flg) { + int ii; + long count; + ostringstream key; + string id; + string name; + string type; + string datastr; + EnumDeviceLifeCycleStatus state; + int state_i; + Device *dev; + Data *data; + list *ret_val; + + ret_val = new list; + count = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, err_flg); + log_debug("device count: %ld\n", count); + if (*err_flg == PLP_OK) { + for (ii = 0; ii < count; ii++) { + data = NULL; + + key.str(""); + key << RSP__DEVICE_LIST__ID << ii; + id = msg_param->get_string_parameter(key.str(), err_flg); + 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) { + log_error("Failed to parse name for the device: %s\n", id.c_str()); + break; + } + + key.str(""); + key << RSP__DEVICE_LIST__TYPE << ii; + type = msg_param->get_string_parameter(key.str(), err_flg); + if (*err_flg != PLP_OK) { + log_error("Failed to parse type for the device: %s\n", id.c_str()); + 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) { + log_error("Failed to parse life cycle state for the device: %s\n", id.c_str()); + 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) { + data = Data::parse_string(datastr); + if (data == NULL) { + log_error("No data available for the device: %s\n", id.c_str()); + } + } + else { + log_error("No data available for the device: %s\n", id.c_str()); + } + dev = new DeviceData(id, type, name, state, data); + ret_val->push_back(dev); + } + } + return ret_val; +} + const std::list *DeviceManagerClient::get_device_list(BusClient *client_param, int *err_flg) { BusMessage *msg_req; @@ -61,7 +135,7 @@ const std::list *DeviceManagerClient::get_device_list(BusClient * void DeviceManagerClient::parse_device_data_msg(BusMessage *msg_param, DataRange *result, - string key, + string key_str, int *err_flg, string description) { string data_str; @@ -71,7 +145,7 @@ void DeviceManagerClient::parse_device_data_msg(BusMessage *msg_param, data = NULL; if ((msg_param != NULL) && (result != NULL)) { - data_str = msg_param->get_string_parameter(RSP__GET_LATEST_DATA__VALUE, err_flg); + data_str = msg_param->get_string_parameter(key_str, err_flg); if (data_str.empty() == false) { data = Data::parse_string(data_str); } @@ -92,9 +166,9 @@ DataRange *DeviceManagerClient::parse_device_data_msg(BusMessage *msg_param, ret_val = NULL; if (msg_param != NULL) { ret_val = new DataRange(); - parse_device_data_msg(msg_param, ret_val, RSP__GET_LATEST_DATA__VALUE, err_flg, "latest data"); - parse_device_data_msg(msg_param, ret_val, RSP__GET_LATEST_DATA__MIN_VALUE, err_flg, "min data value"); - parse_device_data_msg(msg_param, ret_val, RSP__GET_LATEST_DATA__MAX_VALUE, err_flg, "max data value"); + parse_device_data_msg(msg_param, ret_val, RSP__LATEST_DATA_VALUE, err_flg, "latest data"); + parse_device_data_msg(msg_param, ret_val, RSP__MIN_DATA_VALUE, err_flg, "latest min data value"); + parse_device_data_msg(msg_param, ret_val, RSP__MAX_DATA_VALUE, err_flg, "latest max data value"); *err_flg = PLP_OK; } @@ -112,7 +186,7 @@ Data *DeviceManagerClient::get_latest_data(BusClient *client_param, 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()); + msg_req->add_string_parameter(REQ__DEVICE_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(); @@ -123,76 +197,36 @@ Data *DeviceManagerClient::get_latest_data(BusClient *client_param, return ret_val; } -list *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, - int *err_flg) { - int ii; - long count; - ostringstream key; - string id; - string name; - string type; - string datastr; - EnumDeviceLifeCycleStatus state; - int state_i; - Device *dev; - Data *data; - list *ret_val; - - ret_val = new list; - count = msg_param->get_long_parameter(RSP__DEVICE_LIST__DEVICE_COUNT, err_flg); - log_debug("device count: %ld\n", count); - if (*err_flg == PLP_OK) { - for (ii = 0; ii < count; ii++) { - data = NULL; - - key.str(""); - key << RSP__DEVICE_LIST__ID << ii; - id = msg_param->get_string_parameter(key.str(), err_flg); - if (*err_flg != PLP_OK) { - log_error("Failed to parse device id.\n"); - break; - } +DataRange *DeviceManagerClient::parse_data_range_msg(BusMessage *msg_param, + int *err_flg) { + DataRange *ret_val; - key.str(""); - key << RSP__DEVICE_LIST__NAME << ii; - name = msg_param->get_string_parameter(key.str(), err_flg); - if (*err_flg != PLP_OK) { - log_error("Failed to parse name for the device: %s\n", id.c_str()); - break; - } + *err_flg = PLP_ERR; + ret_val = NULL; + if (msg_param != NULL) { + ret_val = new DataRange(); + parse_device_data_msg(msg_param, ret_val, RSP__FIRST_DATA_VALUE, err_flg, "first data"); + parse_device_data_msg(msg_param, ret_val, RSP__LATEST_DATA_VALUE, err_flg, "latest data"); + *err_flg = PLP_OK; + } + return ret_val; +} - key.str(""); - key << RSP__DEVICE_LIST__TYPE << ii; - type = msg_param->get_string_parameter(key.str(), err_flg); - if (*err_flg != PLP_OK) { - log_error("Failed to parse type for the device: %s\n", id.c_str()); - break; - } +DataRange *DeviceManagerClient::get_data_range(BusClient *client_param, + string device_id_param, + int *err_flg) { + BusMessage *msg_req; + BusMessage *msg_rsp; + DataRange *ret_val; - 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) { - log_error("Failed to parse life cycle state for the device: %s\n", id.c_str()); - break; - } - state = (EnumDeviceLifeCycleStatus)state_i; + 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()); + client_param->send_message_and_wait_response(msg_req, &msg_rsp); + ret_val = parse_data_range_msg(msg_rsp, err_flg); + delete(msg_req); + delete(msg_rsp); - key.str(""); - key << RSP__DEVICE_LIST__DATA << ii; - datastr = msg_param->get_string_parameter(key.str(), err_flg); - if (*err_flg == PLP_OK) { - data = Data::parse_string(datastr); - if (data == NULL) { - log_error("No data available for the device: %s\n", id.c_str()); - } - } - else { - log_error("No data available for the device: %s\n", id.c_str()); - } - dev = new DeviceData(id, type, name, state, data); - ret_val->push_back(dev); - } - } return ret_val; } diff --git a/src_client/DeviceManagerClient.hh b/src_client/DeviceManagerClient.hh index 68fbd16..587b501 100644 --- a/src_client/DeviceManagerClient.hh +++ b/src_client/DeviceManagerClient.hh @@ -30,17 +30,22 @@ namespace plpdevicebus { Data *get_latest_data(BusClient *client_param, string device_id_param, int *err_flg); + DataRange *get_data_range(BusClient *client_param, + string device_id_param, + int *err_flg); private: 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); void parse_device_data_msg(BusMessage *msg_param, DataRange *result, string key, int *err_flg, string description); - list *parse_device_list_msg(plpbus::BusMessage *dev_list_msg_param, - int *err_flg); + DataRange *parse_device_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 9b4e89d..fb40b58 100644 --- a/src_client/DeviceManagerController.cc +++ b/src_client/DeviceManagerController.cc @@ -94,3 +94,23 @@ Data *DeviceManagerController::get_latest_data(string device_id_param, } return ret_val; } + +DataRange *DeviceManagerController::get_data_range(string device_id_param, + int *err_flg) { + DataRange *ret_val; + + ret_val = NULL; + *err_flg = PLP_ERR; + try { + if (_dev_man != NULL) { + ret_val = _dev_man->get_data_range(_bus_client, + device_id_param, + err_flg); + } + } + catch(...) { + log_error("Could not get data range\n"); + *err_flg = PLP_ERR; + } + return ret_val; +} diff --git a/src_client/DeviceManagerController.hh b/src_client/DeviceManagerController.hh index 2208189..e7529b8 100644 --- a/src_client/DeviceManagerController.hh +++ b/src_client/DeviceManagerController.hh @@ -25,6 +25,8 @@ namespace plpdevicebus { const std::list *get_device_list(int *err_flg); Data *get_latest_data(std::string device_id_param, int *err_flg); + DataRange *get_data_range(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 052234b..9d96db7 100644 --- a/src_server/DeviceManagerServer.cc +++ b/src_server/DeviceManagerServer.cc @@ -148,7 +148,7 @@ static void add_data_values_to_bus_message(const BusMessage *msg_rsp_param, ((BusMessage *)msg_rsp_param)->add_string_parameter(key_name_base_param, val_str); } -void decode_data_to_busmessage(const BusMessage *ret_val, +void decode_latest_data_to_busmessage(const BusMessage *ret_val, const DataReader *reader) { DataRange *dr; Data *data; @@ -157,7 +157,7 @@ void decode_data_to_busmessage(const BusMessage *ret_val, if (data != NULL) { add_data_values_to_bus_message(ret_val, data, - RSP__GET_LATEST_DATA__VALUE); + RSP__LATEST_DATA_VALUE); //((BusMessage *)ret_val)->printout(); delete(data); } @@ -167,7 +167,7 @@ void decode_data_to_busmessage(const BusMessage *ret_val, data = dr->get_first();; add_data_values_to_bus_message(ret_val, data, - RSP__GET_LATEST_DATA__MIN_VALUE); + RSP__MIN_DATA_VALUE); delete(data); } delete(dr); @@ -178,13 +178,35 @@ void decode_data_to_busmessage(const BusMessage *ret_val, data = dr->get_first(); add_data_values_to_bus_message(ret_val, data, - RSP__GET_LATEST_DATA__MAX_VALUE); + RSP__MAX_DATA_VALUE); delete(data); } delete(dr); } } +void decode_data_range_to_busmessage(const BusMessage *ret_val, + DataRange *dr) { + Data *data; + + data = dr->get_first(); + if (data != NULL) { + add_data_values_to_bus_message(ret_val, + data, + RSP__FIRST_DATA_VALUE); + //((BusMessage *)ret_val)->printout(); + delete(data); + } + data = dr->get_last(); + if (data != NULL) { + add_data_values_to_bus_message(ret_val, + data, + RSP__LATEST_DATA_VALUE); + //((BusMessage *)ret_val)->printout(); + delete(data); + } +} + void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, const BusMessage *ret_val) { string id; @@ -194,14 +216,43 @@ void DeviceManagerServer::get_latest_data(BusMessage *msg_req_param, ostringstream key; const DataReader *reader; - id = msg_req_param->get_string_parameter(REQ__GET_LATEST_DATA__ID, &err_flg); + id = msg_req_param->get_string_parameter(REQ__DEVICE_ID, &err_flg); + if (err_flg == PLP_OK) { + dev = (Device *)get_device_by_id(id); + if (dev != NULL) { + sensor = dynamic_cast(dev); + if (sensor != NULL) { + reader = sensor->get_datareader(); + decode_latest_data_to_busmessage(ret_val, reader); + } + } + } +} + +void DeviceManagerServer::get_data_range(BusMessage *msg_req_param, + const BusMessage *ret_val) { + string id; + int err_flg; + Device *dev; + SensorDevice *sensor; + ostringstream key; + const DataReader *reader; + DataRange *dr; + + id = msg_req_param->get_string_parameter(REQ__DEVICE_ID, &err_flg); if (err_flg == PLP_OK) { dev = (Device *)get_device_by_id(id); if (dev != NULL) { sensor = dynamic_cast(dev); if (sensor != NULL) { reader = sensor->get_datareader(); - decode_data_to_busmessage(ret_val, reader); + if (reader != NULL) { + dr = ((DataReader *)reader)->get_data_range(); + if (dr != NULL) { + decode_data_range_to_busmessage(ret_val, dr); + delete(dr); + } + } } } } diff --git a/src_server/DeviceManagerServer.hh b/src_server/DeviceManagerServer.hh index 8854c7c..180dd54 100644 --- a/src_server/DeviceManagerServer.hh +++ b/src_server/DeviceManagerServer.hh @@ -28,6 +28,8 @@ namespace plpdevicebus { void get_device_list(const BusMessage *msq_rsp_param); void get_latest_data(BusMessage *msg_req_param, const BusMessage *msq_rsp_param); + void get_data_range(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 a63a7af..fc7195a 100644 --- a/src_server/ServerListenerImpl.cc +++ b/src_server/ServerListenerImpl.cc @@ -43,6 +43,11 @@ int ServerListenerImpl::request_received(const BusMessage *msg_req_param, const _dev_man->get_latest_data((BusMessage *)msg_req_param, msg_rsp_param); 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; + break; default: log_debug("unknown message type-id received: %ld\n", type_id); break;