]> pilppa.com Git - libplpdevicebus.git/commitdiff
implemented get_latest_data method
authorMika Laitio <lamikr@pilppa.org>
Mon, 13 Aug 2012 17:23:29 +0000 (20:23 +0300)
committerMika Laitio <lamikr@pilppa.org>
Mon, 13 Aug 2012 17:23:29 +0000 (20:23 +0300)
- Implemented new client/server method to get
data by device id.
- Fixed memory leaks.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src_client/DeviceManagerClient.cc
src_client/DeviceManagerClient.hh
src_client/DeviceManagerController.cc
src_client/DeviceManagerController.hh
src_server/DeviceManagerServer.cc
src_server/DeviceManagerServer.hh

index 801e4d0cba6c31cda08006530c184118623e0c93..fb4e62e457c0fa4cc43c35fae66be3467c960cfa 100644 (file)
@@ -61,19 +61,63 @@ const std::list<plp::Device *> *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<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param, int *err_flg) {
+list<Device *> *DeviceManagerClient::parse_device_list_msg(BusMessage *msg_param,
+                                                       int *err_flg) {
        int                             ii;
        long                            count;
        ostringstream                   key;
@@ -95,40 +139,50 @@ list<Device *> *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);
                }
index cb88c90c1e4a8a17309ed81e2be19eac4cd7a35b..fbfdff9644d6cb2c4f3cacf4c862e3e1fe8ded58 100644 (file)
 #include <list>
 #include <string>
 
+#include <plp/Data.hh>
+#include <plp/Device.hh>
 #include <plp/bus/BusMessage.hh>
 #include <plp/bus/BusClient.hh>
-#include <plp/Device.hh>
+
+using namespace std;
+using namespace plp;
+using namespace plpbus;
 
 namespace plpdevicebus {
        class DeviceManagerClient {
                public:
                        DeviceManagerClient();
                        virtual ~DeviceManagerClient();
-                       const std::list<plp::Device *> *get_device_list(plpbus::BusClient *client_param,
+                       const list<plp::Device *> *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<plp::Device *>        *_device_list;
-                       //void send_request__get_device_list(plpbus::BusClient *client_param);
+                       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);
        };
index bdd2348bd6c67bd34f823703224b7f4cd7d14c22..9b4e89d8fbd7c01153265ef256629d1461755070 100644 (file)
@@ -57,7 +57,8 @@ DeviceManagerController::~DeviceManagerController() {
 const std::list<plp::Device *> *DeviceManagerController::get_device_list(int *err_flg) {
        const list<Device *>    *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<plp::Device *> *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;
 }
index 0d739a34662a91becfc21d2750021ff2a27c63f1..220818990bb510cb3774c405314bdc8c1e7464a2 100644 (file)
@@ -9,10 +9,12 @@
 #define W1DEVICEMANAGERCONTROLLER_HH_
 
 #include <plp/Device.hh>
+#include <plp/Data.hh>
 
 #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<plp::Device *> *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;
index d83accb671f6d4c06c786ce09b868ab5801844fe..3eb8a0106526a191691584934f7c2f11ad78f89c 100644 (file)
@@ -65,49 +65,59 @@ DeviceManagerServer::DeviceManagerServer(list<Device *> 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<SensorDevice *>(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<Device *>::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<SensorDevice *>(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<SensorDevice *>(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);
                        }
                }
        }
index 057432b63d87a0db6c20581f65eb3409e1547176..8854c7cd152df7387802b9d06f3ab0db0f83eb61 100644 (file)
@@ -26,8 +26,8 @@ namespace plpdevicebus {
                        DeviceManagerServer(list<Device *> 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<plp::Device *>        _dev_lst;
                        pthread_t       _lstnr_thrd;