]> pilppa.com Git - libplpdevicebus.git/commitdiff
implement get_data_range msgbus method
authorMika Laitio <lamikr@pilppa.org>
Wed, 15 Aug 2012 11:17:43 +0000 (14:17 +0300)
committerMika Laitio <lamikr@pilppa.org>
Wed, 15 Aug 2012 11:17:43 +0000 (14:17 +0300)
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 <lamikr@pilppa.org>
src/plp/devicebus/DeviceBusMessageId.hh
src_client/DeviceManagerClient.cc
src_client/DeviceManagerClient.hh
src_client/DeviceManagerController.cc
src_client/DeviceManagerController.hh
src_server/DeviceManagerServer.cc
src_server/DeviceManagerServer.hh
src_server/ServerListenerImpl.cc

index e7be23de29f7c7b9d27dce45f0f5cf49d7b70dc6..2816598b701ad503f4de40b3006338bd5904c1d4 100644 (file)
@@ -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_"
 #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_ */
index 955128239923d16578b07d00b5135fcd6795017f..2ef02eef55af04fbc92f058008423da5ad3b74a4 100644 (file)
@@ -43,6 +43,80 @@ DeviceManagerClient::~DeviceManagerClient() {
        clean_device_list(_device_list);
 }
 
+list<Device *> *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<Device *>                  *ret_val;
+
+       ret_val = new list<Device *>;
+       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<plp::Device *> *DeviceManagerClient::get_device_list(BusClient *client_param,
                                                                int *err_flg) {
        BusMessage      *msg_req;
@@ -61,7 +135,7 @@ const std::list<plp::Device *> *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<Device *> *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<Device *>                  *ret_val;
-
-       ret_val = new list<Device *>;
-       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;
 }
index 68fbd16396495ca5a314feaeddf873504ec4533e..587b5011acac65a496b93482fc4dbd34e9369d46 100644 (file)
@@ -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<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);
                        void parse_device_data_msg(BusMessage *msg_param,
                                                DataRange *result,
                                                string key,
                                                int *err_flg,
                                                string description);
-                       list<plp::Device *> *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);
        };
 }
 
index 9b4e89d8fbd7c01153265ef256629d1461755070..fb40b5850e182213179a3ca901866956be2f7165 100644 (file)
@@ -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;
+}
index 220818990bb510cb3774c405314bdc8c1e7464a2..e7529b8fd97e162c7ebee599617594850a9a530e 100644 (file)
@@ -25,6 +25,8 @@ namespace plpdevicebus {
                        const std::list<plp::Device *> *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;
index 052234b11f0b80c90c930bcc75ba43dc6f96ef4a..9d96db7aa02d9559c08b1c6df14f9c28a6ac89c6 100644 (file)
@@ -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<SensorDevice *>(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<SensorDevice *>(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);
+                                       }
+                               }
                        }
                }
        }
index 8854c7cd152df7387802b9d06f3ab0db0f83eb61..180dd5440e4b0744a9441650020cfa718bb0e20d 100644 (file)
@@ -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<plp::Device *>        _dev_lst;
                        pthread_t       _lstnr_thrd;
index a63a7aff09db26b137cbf326d7e901d728af20a7..fc7195acf80a0d77efa024fc0f2783f75de12bcf 100644 (file)
@@ -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;