]> pilppa.com Git - libplpdevicebus.git/commitdiff
implement method for getting data between dates
authorMika Laitio <lamikr@pilppa.org>
Wed, 15 Aug 2012 20:03:46 +0000 (23:03 +0300)
committerMika Laitio <lamikr@pilppa.org>
Wed, 15 Aug 2012 20:03:46 +0000 (23:03 +0300)
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 2816598b701ad503f4de40b3006338bd5904c1d4..97554707ea918a498e82f91715574a726d089c09 100644 (file)
@@ -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_"
 #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_ */
index 2ef02eef55af04fbc92f058008423da5ad3b74a4..bacbdb49c408a68a8a29e2cea25b885b53e131b9 100644 (file)
@@ -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);
index 587b5011acac65a496b93482fc4dbd34e9369d46..3da39181290b2b0bd08d32a4b8c131baff079f7c 100644 (file)
@@ -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<plp::Device *>     *_device_list;
                        list<plp::Device *> *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);
        };
 }
 
index fb40b5850e182213179a3ca901866956be2f7165..2b1ef6ea03190a3222c821219593f1e2557174e5 100644 (file)
@@ -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;
+}
index e7529b8fd97e162c7ebee599617594850a9a530e..b2df19cd50a4e15995d26d369c6b99898214f486 100644 (file)
@@ -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;
index 9d96db7aa02d9559c08b1c6df14f9c28a6ac89c6..1a7e17296b85885fcff83793c91fc3d78260fdd7 100644 (file)
@@ -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<SensorDevice *>(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");
+       }
 }
index 180dd5440e4b0744a9441650020cfa718bb0e20d..0ed97da9d5c7b8ea60ef8ba4015492edf859dffb 100644 (file)
@@ -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<plp::Device *>        _dev_lst;
                        pthread_t       _lstnr_thrd;
index fc7195acf80a0d77efa024fc0f2783f75de12bcf..61132ffc2e4baaa76ebc9425fd673d51cb956eda 100644 (file)
@@ -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);