]> pilppa.org Git - lib1wire.git/commitdiff
Api cleanups for device querying
authorMika Laitio <lamikr@pilppa.org>
Thu, 3 Mar 2011 17:42:05 +0000 (19:42 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 3 Mar 2011 17:42:05 +0000 (19:42 +0200)
Instead of having separate methods for asking the list of device-datas
stored to save directory and list of devices active, only one list of
devices is now returned which compines active devices and list of
inactive devices which have anyway data. Device has now lifecycle state
which indicates whether it's active.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
17 files changed:
src/DataReader.cc [moved from src/DeviceData.cc with 85% similarity]
src/DataReader.hh [moved from src/DeviceData.hh with 93% similarity]
src/Device.hh [moved from src/DeviceTypeGeneric.hh with 63% similarity]
src/DeviceTypes.hh [new file with mode: 0644]
src/Factory.cc
src/Factory.hh
src/Makefile.am
src/SensorDevice.hh [moved from src/DeviceTypeSensor.hh with 54% similarity]
src/StoreCache.cc
src/W1CounterDevice.cc
src/W1CounterDevice.hh
src/W1Device.cc
src/W1Device.hh
src/W1TemperatureSensor.cc
src/W1TemperatureSensor.hh
src_test/test_w1_datalog_read.cc
src_test/test_w1_datalog_write.cc

similarity index 85%
rename from src/DeviceData.cc
rename to src/DataReader.cc
index c564377a146d747b9c051092e3ef55bae5664fa1..799ead10a7fc411f6c4eaa4cfac7b006080c8418 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * DeviceData.cc
+ * DataReader.cc
  *
  *  Created on: Nov 7, 2010
  *      Author: lamikr
@@ -12,7 +12,7 @@
 #include <string.h>
 
 #include "W1Util.hh"
-#include "DeviceData.hh"
+#include "DataReader.hh"
 #include "StoreDay.hh"
 #include "StoreCache.hh"
 #include "DeviceConfig.hh"
@@ -33,7 +33,7 @@ bool string_to_number(NumberDataType& result,
        return !(iss >> format >> result).fail();
 }
 
-DeviceData::DeviceData(string device_id_param) {
+DataReader::DataReader(string device_id_param) {
        string  base_dir;
 
        device_config   = NULL;
@@ -44,14 +44,14 @@ DeviceData::DeviceData(string device_id_param) {
        device_ch_dir   = W1Util::concat_paths(device_ch_dir, device_id);
 }
 
-DeviceData::~DeviceData() {
+DataReader::~DataReader() {
        if (device_config != NULL) {
                delete(device_config);
                device_config   = NULL;
        }
 }
 
-Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
+Data *DataReader::find_newest_data(vector<string> year_name_vector_param) {
        string  year_name;
        int     size;
        Data    *ret_val;
@@ -70,7 +70,7 @@ Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
        return ret_val;
 }
 
-Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
+Data *DataReader::find_oldest_data(vector<string> year_name_vector_param) {
        int     size;
        string  year_name;
        Data    *ret_val;
@@ -89,7 +89,7 @@ Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_data_range() {
+DataRange *DataReader::get_data_range() {
        DataRange       *ret_val;
        vector<string>  y_list;
        Data            *o_data;
@@ -150,7 +150,7 @@ EnumSummaryPeriod get_period_type(Date *start_date,
        return ret_val;
 }
 
-DataRange *DeviceData::get_summary(Date *date_param,
+DataRange *DataReader::get_summary(Date *date_param,
                                EnumSummaryCalculationType calc_type_param,
                                EnumSummaryPeriod period_type_param) {
        DataRange       *ret_val;
@@ -192,12 +192,12 @@ DataRange *DeviceData::get_summary(Date *date_param,
        return ret_val;
 }
 
-DataRange *DeviceData::get_yearly_summary(Date *date,
+DataRange *DataReader::get_yearly_summary(Date *date,
                                EnumSummaryCalculationType calc_type_param) {
        return get_summary(date, calc_type_param, PERIOD_YEARLY);
 }
 
-DataRange *DeviceData::get_yearly_summary(Date *date) {
+DataRange *DataReader::get_yearly_summary(Date *date) {
        DataRange       *ret_val;
 
        if (device_config == NULL) {
@@ -208,7 +208,7 @@ DataRange *DeviceData::get_yearly_summary(Date *date) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_yearly_summary(Date *start_date,
+DataRange *DataReader::get_yearly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
        DataRange       *data;
@@ -233,12 +233,12 @@ DataRange *DeviceData::get_yearly_summary(Date *start_date,
        return ret_val;
 }
 
-DataRange *DeviceData::get_monthly_summary(Date *date,
+DataRange *DataReader::get_monthly_summary(Date *date,
                                EnumSummaryCalculationType calc_type_param) {
        return get_summary(date, calc_type_param, PERIOD_MONTHLY);
 }
 
-DataRange *DeviceData::get_monthly_summary(Date *date) {
+DataRange *DataReader::get_monthly_summary(Date *date) {
        DataRange       *ret_val;
 
        if (device_config == NULL) {
@@ -249,7 +249,7 @@ DataRange *DeviceData::get_monthly_summary(Date *date) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_monthly_summary(Date *start_date,
+DataRange *DataReader::get_monthly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
        DataRange       *data;
@@ -274,12 +274,12 @@ DataRange *DeviceData::get_monthly_summary(Date *start_date,
        return ret_val;
 }
 
-DataRange *DeviceData::get_daily_summary(Date *date,
+DataRange *DataReader::get_daily_summary(Date *date,
                                EnumSummaryCalculationType calc_type_param) {
        return get_summary(date, calc_type_param, PERIOD_DAILY);
 }
 
-DataRange *DeviceData::get_daily_summary(Date *date) {
+DataRange *DataReader::get_daily_summary(Date *date) {
        DataRange       *ret_val;
 
        if (device_config == NULL) {
@@ -290,7 +290,7 @@ DataRange *DeviceData::get_daily_summary(Date *date) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_daily_summary(Date *start_date,
+DataRange *DataReader::get_daily_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
        DataRange       *data;
@@ -315,12 +315,12 @@ DataRange *DeviceData::get_daily_summary(Date *start_date,
        return ret_val;
 }
 
-DataRange *DeviceData::get_hourly_summary(Date *date,
+DataRange *DataReader::get_hourly_summary(Date *date,
                EnumSummaryCalculationType calc_type_param) {
        return get_summary(date, calc_type_param, PERIOD_HOURLY);
 }
 
-DataRange *DeviceData::get_hourly_summary(Date *date) {
+DataRange *DataReader::get_hourly_summary(Date *date) {
        DataRange       *ret_val;
 
        if (device_config == NULL) {
@@ -331,7 +331,7 @@ DataRange *DeviceData::get_hourly_summary(Date *date) {
        return ret_val;
 }
 
-DataRange *DeviceData::get_hourly_summary(Date *start_date,
+DataRange *DataReader::get_hourly_summary(Date *start_date,
                                        Date *end_date) {
        DataRange       *ret_val;
        DataRange       *dta_lst;
@@ -363,7 +363,7 @@ DataRange *DeviceData::get_hourly_summary(Date *start_date,
        return ret_val;
 }
 
-DataRange *DeviceData::get_data(Date *start_date,
+DataRange *DataReader::get_data(Date *start_date,
                                Date *end_date) {
        DataRange               *ret_val;
        EnumSummaryPeriod       period;
@@ -399,3 +399,20 @@ DataRange *DeviceData::get_data(Date *start_date,
        }
        return ret_val;
 }
+
+string DataReader::get_device_id() {
+       return device_id;
+}
+
+/**
+ * Read device type from the device specific config file
+ */
+string DataReader::get_device_type() {
+       string  ret_val;
+
+       if (device_config == NULL) {
+               device_config   = Factory::get_device_config(device_id);
+               ret_val         = device_config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
+       }
+       return ret_val;
+}
similarity index 93%
rename from src/DeviceData.hh
rename to src/DataReader.hh
index 1ccbb4f29b9cd8ab64e2a7463692cb2f83a1b68a..ac7e13eb019136bf29ca17d06080563980e816f5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * DeviceData.hh
+ * DataReader.hh
  *
  *  Created on: Nov 7, 2010
  *      Author: lamikr
 #include <time.h>
 
 namespace plp {
-       class DeviceData {
+       class DataReader {
                public:
-                       DeviceData(std::string device_id);
-                       virtual ~DeviceData();
+                       DataReader(std::string device_id);
+                       virtual ~DataReader();
                        plp::DataRange *get_data_range();
                        /**
                         * Get monthly summary data.
@@ -48,6 +48,8 @@ namespace plp {
                        plp::DataRange *get_hourly_summary(plp::Date *date);
                        plp::DataRange *get_hourly_summary(plp::Date *start_date, plp::Date *end_date);
                        plp::DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
+                       std::string get_device_id();
+                       std::string get_device_type();
                protected:
                        std::string                     device_id;
                        std::string                     device_dir;
similarity index 63%
rename from src/DeviceTypeGeneric.hh
rename to src/Device.hh
index ba944a8c792a78aa236cf682936a6dd8edad9a6b..f5ee509671d92219c34a283eb8220c1893c86cf4 100644 (file)
 
 #include <string>
 
+#include "DataReader.hh"
+
 namespace plp {
-       class DeviceTypeGeneric {
+       enum EnumDeviceLifeCycleStatus {LIFECYCLE_STATUS__UNAVAILABLE, LIFECYCLE_STATUS__AVAILABLE};
+
+       class Device {
                public:
                        virtual std::string get_id() = 0;
                        virtual std::string get_name() = 0;
                        virtual void set_name(std::string name_param) = 0;
                        virtual std::string get_device_type() = 0;
+                       virtual plp::DataReader *get_device_data() = 0;
+                       virtual EnumDeviceLifeCycleStatus get_lifecycle_state() = 0;
                        virtual void printout() = 0;
        };
 }
diff --git a/src/DeviceTypes.hh b/src/DeviceTypes.hh
new file mode 100644 (file)
index 0000000..e37a68b
--- /dev/null
@@ -0,0 +1,15 @@
+/*
+ * DeviceTypes.hh
+ *
+ *  Created on: Mar 3, 2011
+ *      Author: lamikr
+ */
+
+#ifndef DEVICETYPES_HH_
+#define DEVICETYPES_HH_
+
+#define DEVICE_TYPE_UNKNOWN            "Unknown"
+#define DEVICE_TYPE_TEMPERATURESENSOR  "Temperature Sensor"
+#define DEVICE_TYPE_COUNTER_DEVICE     "Counter Device"
+
+#endif /* DEVICETYPES_HH_ */
index 76287eb17f553ad89f4a7b24c47fb683c6fa75df..d9556e376e4ecc05fe47d06e8d1e1c70f2351b09 100644 (file)
@@ -12,6 +12,8 @@
 #include <string.h>
 
 #include <plp/log.h>
+
+#include "DeviceTypes.hh"
 #include "Factory.hh"
 #include "W1TemperatureSensor.hh"
 #include "W1CounterDevice.hh"
@@ -38,33 +40,64 @@ Factory::~Factory() {
        // TODO Auto-generated destructor stub
 }
 
-W1Device *Factory::get_device(int family_code,
-                       string device_id,
+int Factory::get_family_code_by_device_type(string device_type_param) {
+       int     ret_val = -1;
+
+       if (device_type_param.compare(DEVICE_TYPE_TEMPERATURESENSOR) == 0) {
+               ret_val = 0x10;
+       }
+       else if (device_type_param.compare(DEVICE_TYPE_COUNTER_DEVICE) == 0) {
+               ret_val = 0x1d;
+       }
+       return ret_val;
+}
+
+string Factory::get_device_type_by_family_code(int family_code_param) {
+       string  ret_val;
+
+       switch(family_code_param) {
+               case    0x10:
+               case    0x28:
+                       ret_val = DEVICE_TYPE_TEMPERATURESENSOR;
+                       break;
+               case    0x1d:
+                       ret_val = DEVICE_TYPE_COUNTER_DEVICE;
+                       break;
+               default:
+                       ret_val = DEVICE_TYPE_UNKNOWN;
+                       log_error("Unknown w1 device type: %d\n", family_code_param);
+                       break;
+       }
+       return ret_val;
+}
+
+Device *Factory::create_w1_device(int family_code_param,
+                       string device_id_param,
                        dirent *direntry_param) {
-       W1Device        *ret_val;
+       Device          *ret_val;
        DeviceConfig    *config;
        string          type;
 
        ret_val = NULL;
-       switch(family_code) {
+       type    = get_device_type_by_family_code(family_code_param);
+       switch(family_code_param) {
                case    0x10:
                case    0x28:
-                       ret_val = new W1TemperatureSensor(family_code, device_id, direntry_param);
+                       ret_val = new W1TemperatureSensor(type, device_id_param, direntry_param);
                        break;
                case    0x1d:
-                       ret_val = new W1CounterDevice(family_code, device_id, direntry_param);
+                       ret_val = new W1CounterDevice(type, device_id_param, direntry_param);
                        break;
                case    0x81:
                        // 0x81 is the 1-wire USB dongle... No need to create device for it.
                        break;
                default:
-                       log_debug("Unsupported 1-wire-family code: %#x, device not created: %s\n", family_code, device_id.c_str());
+                       log_debug("Unsupported 1-wire-family code: %#x, device not created: %s\n", family_code_param, device_id_param.c_str());
                        break;
        }
        if (ret_val != NULL) {
-               log_debug("%s: %#x\n", ret_val->get_device_type().c_str(), ret_val->get_w1_family_code());
                // check that device config exist
-               config  = get_device_config(device_id);
+               config  = get_device_config(device_id_param);
                if (config != NULL) {
                        // if not, create default device config
                        type    = config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
@@ -79,14 +112,24 @@ W1Device *Factory::get_device(int family_code,
        return ret_val;
 }
 
-W1Device *Factory::create_device(dirent *direntry_param, int *err_code_param) {
-       string          folder_name;
-       string          tmp_str;
-       string          device_name;
-       int             pos;
-       int             family_code;
-       bool            suc_flg;
-       W1Device        *ret_val;
+Device *Factory::create_w1_device(string device_type_param,
+                       string device_id_param) {
+       int     family_code;
+       Device  *ret_val;
+
+       family_code     = get_family_code_by_device_type(device_type_param);
+       ret_val         = create_w1_device(family_code, device_id_param, NULL);
+       return ret_val;
+}
+
+Device *Factory::create_w1_device(dirent *direntry_param, int *err_code_param) {
+       string  folder_name;
+       string  tmp_str;
+       string  device_name;
+       int     pos;
+       int     family_code;
+       bool    suc_flg;
+       Device  *ret_val;
 
        ret_val         = NULL;
        *err_code_param = 0;
@@ -101,7 +144,7 @@ W1Device *Factory::create_device(dirent *direntry_param, int *err_code_param) {
                        if (suc_flg == true) {
                                log_debug("1-wire device family code: %#x\n", family_code);
                                device_name     = folder_name.substr(pos + 1, folder_name.length() - pos);
-                               ret_val         = Factory::get_device(family_code,
+                               ret_val         = Factory::create_w1_device(family_code,
                                                                device_name,
                                                                direntry_param);
                                if ((ret_val == NULL) &&
@@ -114,14 +157,24 @@ W1Device *Factory::create_device(dirent *direntry_param, int *err_code_param) {
        return ret_val;
 }
 
-list<W1Device *> Factory::get_device_list() {
-       DIR                     *dir;
-       int                     err_flg;
-       struct dirent           *direntry;
-       W1Device                *device;
-       bool                    is_subdir;
-       list<W1Device *>        ret_val;
+list<Device *> Factory::get_device_list() {
+       DIR                             *dir;
+       int                             err_flg;
+       struct dirent                   *direntry;
+       Device                          *device;
+       bool                            is_subdir;
+       list<DataReader *>              rdr_list;
+       list<Device *>                  ret_val;
+       list<Device *>::iterator        dev_iter;
+       list<DataReader *>::iterator    rdr_iter;
+       DataReader                      *reader;
+       string                          id1;
+       string                          id2;
+       string                          type;
+       string                          dev_type;
+       bool                            found;
 
+       // scan through the list of devices detected from the 1-wire network
        dir     = opendir(W1_SCAN_ROOTDIR);
        if (dir != NULL) {
                direntry        = readdir(dir);
@@ -129,7 +182,7 @@ list<W1Device *> Factory::get_device_list() {
                        is_subdir       = W1Util::is_subdirectory(W1_SCAN_ROOTDIR, direntry);
                        if (is_subdir == true) {
                                err_flg = 0;
-                               device  = create_device(direntry, &err_flg);
+                               device  = create_w1_device(direntry, &err_flg);
                                if (device != NULL) {
                                        ret_val.push_back(device);
                                }
@@ -146,17 +199,38 @@ list<W1Device *> Factory::get_device_list() {
                        log_error("Failed to close 1-wire device directory: %s\n", W1_SCAN_ROOTDIR);
                }
        }
+       // scan through the list of devices which have saved data
+       rdr_list        = get_data_reader_list();
+       for (rdr_iter = rdr_list.begin(); rdr_iter != rdr_list.end(); rdr_iter++) {
+               reader  = (DataReader *)*rdr_iter;
+               id1     = reader->get_device_id();
+               found   = false;
+               for (dev_iter = ret_val.begin(); dev_iter != ret_val.end(); dev_iter++) {
+                       device  = (Device *)*dev_iter;
+                       id2     = device->get_id();
+                       if (id1.compare(id2) == 0) {
+                               found   = true;
+                               break;
+                       }
+               }
+               if (found == false) {
+                       // reader device is not in the list of active devices. create and add it to list as in-active one...
+                       type    = reader->get_device_type();
+                       device  = create_w1_device(type, id1);
+                       ret_val.push_back(device);
+               }
+       }
        return ret_val;
 }
 
-list<DeviceData *> Factory::get_device_data_list() {
+list<DataReader *> Factory::get_data_reader_list() {
        DIR                     *dir;
        string                  dr_name;
        int                     err_flg;
        struct dirent           *direntry;
-       DeviceData              *dev_dta;
+       DataReader              *reader;
        bool                    is_subdir;
-       list<DeviceData *>      ret_val;
+       list<DataReader *>      ret_val;
 
        dr_name = DeviceConfig::get_base_dir_name();
        dir     = opendir(dr_name.c_str());
@@ -166,8 +240,8 @@ list<DeviceData *> Factory::get_device_data_list() {
                        if (strcmp(direntry->d_name, "cache") != 0) {
                                is_subdir       = W1Util::is_subdirectory(dr_name.c_str(), direntry);
                                if (is_subdir == true) {
-                                       dev_dta = new DeviceData(direntry->d_name);
-                                       ret_val.push_back(dev_dta);
+                                       reader  = new DataReader(direntry->d_name);
+                                       ret_val.push_back(reader);
                                }
                        }
                        direntry        = readdir(dir);
index 6e2beb2b337617b21fbf80c1a1608df8468e2933..7c16d6f7b702cedd8243ce2b94c165daf4e2ccdd 100644 (file)
@@ -9,12 +9,13 @@
 #define FACTORY_HH_
 
 #include <string>
+#include <list>
 
 #include <dirent.h>
 
 #include "DeviceConfig.hh"
-#include "DeviceData.hh"
-#include "W1Device.hh"
+#include "DataReader.hh"
+#include "Device.hh"
 
 #ifndef W1_SCAN_ROOTDIR
        #define W1_SCAN_ROOTDIR         "/sys/bus/w1/devices"
@@ -25,13 +26,16 @@ namespace w1 {
                public:
                        Factory();
                        virtual ~Factory();
-                       static W1Device *get_device(int family_code, std::string device_id, dirent *direntry_param);
-                       static std::list<W1Device *> get_device_list();
-                       static std::list<plp::DeviceData *> get_device_data_list();
+                       static std::list<plp::Device *> get_device_list();
                        static DeviceConfig *get_device_config(std::string device_id);
                private:
                        //int parse_family_code(std::string folder_name);
-                       static W1Device *create_device(dirent *direntry_param, int *err_code_param);
+                       static plp::Device *create_w1_device(int family_code, std::string device_id, dirent *direntry_param);
+                       static plp::Device *create_w1_device(std::string device_type_param, std::string device_id_param);
+                       static plp::Device *create_w1_device(dirent *direntry_param, int *err_code_param);
+                       static std::list<plp::DataReader *> get_data_reader_list();
+                       static int get_family_code_by_device_type(std::string device_type_param);
+                       static std::string get_device_type_by_family_code(int family_code_param);
        };
 }
 
index 4ec0e883ffe3d4a6869ceaaa296aa089acaa7887..3ec5e9775ecf23650e30ba10f420a98ea407d2fa 100644 (file)
@@ -1,11 +1,12 @@
 lib_LTLIBRARIES = lib1wire.la
 lib1wire_la_SOURCES = \
        Data.cc Data.hh \
+       DataReader.cc DataReader.hh \
        Date.cc Date.hh \
+       Device.hh \
        DeviceConfig.cc DeviceConfig.hh \
-       DeviceData.cc DeviceData.hh \
-       DeviceTypeGeneric.hh \
-       DeviceTypeSensor.hh \
+       DeviceTypes.hh \
+       SensorDevice.hh \
        Factory.cc Factory.hh \
        Store.cc Store.hh \
        StoreDay.cc StoreDay.hh \
@@ -23,11 +24,12 @@ DISTCLEANFILES = Makefile.in
 lib1wireincludedir=$(includedir)/w1
 lib1wireinclude_HEADERS = \
        Data.hh \
+       DataReader.hh \
        Date.hh \
+       Device.hh \
        DeviceConfig.hh \
-       DeviceData.hh \
-       DeviceTypeGeneric.hh \
-       DeviceTypeSensor.hh \
+       DeviceTypes.hh \
+       SensorDevice.hh \
        Factory.hh \
        Store.hh \
        StoreDay.hh \
similarity index 54%
rename from src/DeviceTypeSensor.hh
rename to src/SensorDevice.hh
index 99403eff0dc6689385e3e89fb59cadca52e905c9..43e59f5500b1a60da3de1a12fcb33ffc0ebfb316 100644 (file)
 
 #include <string>
 
-#include "DeviceTypeGeneric.hh"
+#include "Device.hh"
 
 namespace plp {
-       class DeviceTypeSensor : public DeviceTypeGeneric {
+       class SensorDevice : public Device {
                public:
-               virtual std::string get_unit() = 0;
-               virtual plp::Data *get_data() = 0;
-               virtual void save_data() = 0;
+                       virtual std::string get_unit() = 0;
+                       virtual plp::Data *get_data() = 0;
        };
 }
 
index baea07833af87b1576567b703a63bcfcbbaba40b..f3f2ac29bc553ea1f50a2f30e80adbbb97226da0 100644 (file)
@@ -10,7 +10,6 @@
 #include "StoreCache.hh"
 #include "StoreDay.hh"
 #include "DeviceConfig.hh"
-#include "DeviceData.hh"
 #include "W1Util.hh"
 
 using namespace std;
index 58e9a55ef742e5a9d9ef8e98142dc6f98b04f809..37a0821903d893074f90f4a0911d48d9d5910d40 100644 (file)
@@ -28,9 +28,9 @@ bool string_to_number(NumberDataType& result,
        return !(iss >> format >> result).fail();
 }
 
-W1CounterDevice::W1CounterDevice(int family_code_param,
+W1CounterDevice::W1CounterDevice(string device_type_param,
                                string device_id_param,
-                               dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
+                               dirent *direntry_param): W1Device(device_type_param, device_id_param, direntry_param) {
        string text;
 
        ifstream ifs(slave_file.c_str());
@@ -45,18 +45,6 @@ W1CounterDevice::~W1CounterDevice() {
        // TODO Auto-generated destructor stub
 }
 
-bool W1CounterDevice::is_supported_w1_family_code(int family_code) {
-       bool    ret_val;
-
-       ret_val = false;
-       switch(family_code) {
-               case 0x1d:
-                       ret_val = true;
-                       break;
-       }
-       return ret_val;
-}
-
 vector<double> *W1CounterDevice::get_raw_data() {
        int             pos;
        int             b_cnt;
@@ -91,10 +79,6 @@ string W1CounterDevice::get_unit() {
        return "";
 }
 
-string W1CounterDevice::get_device_type() {
-       return "Counter Device";
-}
-
 unsigned int W1CounterDevice::get_data_decimal_precision() {
        return 0;
 }
index 17c07286866241767103bfd503533dd268fa4016..770bed09b4edb4cae958dc90ce3a4c5d1114c060 100644 (file)
 namespace w1 {
        class W1CounterDevice: public W1Device {
                public:
-                       W1CounterDevice(int family_code_param,
+                       W1CounterDevice(std::string device_type_param,
                                        std::string device_id_param,
                                        dirent *direntry_param);
                        virtual ~W1CounterDevice();
                        std::string get_unit();
-                       std::string get_device_type();
                protected:
                        std::vector<double> *get_raw_data();
                        unsigned int get_data_decimal_precision();
-                       bool is_supported_w1_family_code(int family_code);
        };
 }
 
index 4c108e3857fdb500c5a296c3f3b2f8809212cc72..37a02bafaa9c7647118bdef68237590b46936ce2 100644 (file)
@@ -21,36 +21,31 @@ using namespace std;
 using namespace w1;
 using namespace plp;
 
-W1Device::W1Device(int family_code_param,
+W1Device::W1Device(string device_type_param,
                string device_id_param,
                dirent *direntry_param) {
        string rootdir;
        string device_dir;
        string temp_str;
 
-       rootdir         = W1_SCAN_ROOTDIR;
-       temp_str        = W1_SLAVE_FILE;
-       dir_path        = rootdir + "/" + direntry_param->d_name;
-       slave_file      = dir_path + "/" + temp_str;
-       log_debug("1-wire device's data file: %s\n", slave_file.c_str());
-       family_code     = family_code_param;
-       id              = device_id_param;
+       type    = device_type_param;
+       id      = device_id_param;
+       if (direntry_param != NULL) {
+               rootdir                 = W1_SCAN_ROOTDIR;
+               temp_str                = W1_SLAVE_FILE;
+               dir_path                = rootdir + "/" + direntry_param->d_name;
+               slave_file              = dir_path + "/" + temp_str;
+               lifecycle_status        = LIFECYCLE_STATUS__AVAILABLE;
+               log_debug("1-wire device's data file: %s\n", slave_file.c_str());
+       }
+       else {
+               lifecycle_status        = LIFECYCLE_STATUS__UNAVAILABLE;
+       }
        name            = "";
 }
 
 W1Device::~W1Device() {
-       list<Data *>::iterator  iter;
-       Data                    *data;
-
-       for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
-               data    = (Data *)*iter;
-               delete(data);
-       }
-       memory_cache.clear();
-}
-
-int W1Device::get_w1_family_code() {
-       return family_code;
+       save_and_clean_cache();
 }
 
 string W1Device::get_id() {
@@ -63,7 +58,7 @@ string W1Device::get_name() {
        if (name.empty() == true) {
                cfg     = Factory::get_device_config(id);
                if (cfg != NULL) {
-                       name    = cfg->get_cfg_value("name");
+                       name    = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
                        delete(cfg);
                }
        }
@@ -76,7 +71,7 @@ void W1Device::set_name(string name_param) {
        name    = name_param;
        cfg     = Factory::get_device_config(id);
        if (cfg != NULL) {
-               cfg->set_cfg_value("name", name_param);
+               cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
                delete(cfg);
        }
 }
@@ -84,6 +79,8 @@ void W1Device::set_name(string name_param) {
 void W1Device::printout() {
        Data    *data;
        string  text;
+       string  type;
+       string  name;
 
        data    = get_data();
        if (data != NULL) {
@@ -91,7 +88,9 @@ void W1Device::printout() {
                cout << text << endl;
        }
        else {
-               log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_name().c_str());
+               type    = get_device_type();
+               name    = get_name();
+               log_error("Could not data for %s device: %s\n", type.c_str(), name.c_str());
        }
 }
 
@@ -112,18 +111,21 @@ Data *W1Device::get_data() {
        vect    = get_raw_data();
        if (vect != NULL) {
                ret_val = new Data(vect, get_unit());
-               collect_data(ret_val);
+               cache(ret_val);
                delete(vect);
        }
        return ret_val;
 }
 
-void W1Device::collect_data(Data *data) {
+void W1Device::cache(Data *data) {
        // TODO: add mutex for memory_cache
        memory_cache.push_back(data);
+       if (memory_cache.size() > 5) {
+               save_and_clean_cache();
+       }
 }
 
-void W1Device::save_data() {
+void W1Device::save_and_clean_cache() {
        list<Data *>::iterator  iter;
        Data                    *data;
        int                     dec_precision;
@@ -136,3 +138,18 @@ void W1Device::save_data() {
        }
        memory_cache.clear();
 }
+
+DataReader *W1Device::get_device_data() {
+       string id;
+
+       id      = get_id();
+       return new DataReader(id);
+}
+
+EnumDeviceLifeCycleStatus W1Device::get_lifecycle_state() {
+       return lifecycle_status;
+}
+
+string W1Device::get_device_type() {
+       return type;
+}
index d1371e9e5214a598bd267872bfef9a076789c03f..4dab3a657de10e960d58e6ebdf8657ad156a01f4 100644 (file)
@@ -15,7 +15,8 @@
 #include <stdbool.h>
 
 #include "Data.hh"
-#include "DeviceTypeSensor.hh"
+#include "SensorDevice.hh"
+#include "Device.hh"
 
 #ifndef W1_SCAN_ROOTDIR
 #define W1_SCAN_ROOTDIR                "/sys/bus/w1/devices"
 #endif
 
 namespace w1 {
-       class W1Device : public plp::DeviceTypeSensor {
+       class W1Device : public plp::SensorDevice {
                public:
-                       W1Device(int family_code_param,
+                       W1Device(std::string device_type_param,
                                std::string device_id_param,
                                dirent *direntry_param);
                        virtual ~W1Device();
-                       int get_w1_family_code();
                        std::string get_id();
                        std::string get_name();
                        void set_name(std::string name_param);
                        void printout();
                        plp::Data *get_data();
-                       void save_data();
+                       plp::DataReader *get_device_data();
+                       std::string get_device_type();
+                       plp::EnumDeviceLifeCycleStatus get_lifecycle_state();
                protected:
+                       void save_and_clean_cache();
                        virtual std::vector<double> *get_raw_data() = 0;
                        virtual unsigned int get_data_decimal_precision() = 0;
-                       void collect_data(plp::Data *data);
+                       void cache(plp::Data *data);
                        std::string to_string(double val, int digit_count);
                        //Data *get_formatted_data(Data *data);
-                       virtual bool is_supported_w1_family_code(int family_code) = 0;
-                       int family_code;
                        std::string id;
+                       std::string type;
                        std::string name;
                        std::string dir_path;
                        std::string slave_file;
                        std::list<plp::Data *> memory_cache;
+                       plp::EnumDeviceLifeCycleStatus lifecycle_status;
        };
 }
 
index e01e1cc0677e80f2ba8cb899ee2bece040a40340..b6d1dac08f60070d163c28daface2ffe5f8ca0b9 100644 (file)
@@ -50,9 +50,9 @@ double convert_w1_temperature_to_celcius(string raw_value, int *err_flg) {
        return dbl_val;
 }
 
-W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
+W1TemperatureSensor::W1TemperatureSensor(string device_type_param,
                                string device_id_param,
-                               dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
+                               dirent *direntry_param): W1Device(device_type_param, device_id_param, direntry_param) {
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == false) {
                log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_device_type().c_str(), slave_file.c_str());
@@ -64,19 +64,6 @@ W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
 W1TemperatureSensor::~W1TemperatureSensor() {
 }
 
-bool W1TemperatureSensor::is_supported_w1_family_code(int family_code) {
-       bool    ret_val;
-
-       ret_val = false;
-       switch(family_code) {
-               case 0x10:
-               case 0x28:
-                       ret_val = true;
-                       break;
-       }
-       return ret_val;
-}
-
 vector<double> *W1TemperatureSensor::get_raw_data() {
        vector<double>  *ret_val;
        string          tmp_str;
@@ -117,10 +104,6 @@ string W1TemperatureSensor::get_unit() {
        return CONST_UNIT_CELCIUS;
 }
 
-string W1TemperatureSensor::get_device_type() {
-       return "Temperature Sensor";
-}
-
 unsigned int W1TemperatureSensor::get_data_decimal_precision() {
        return 3;
 }
index 8c06af1acfb4b9bc492eeb1f1718c1b157392599..3616f8de1880af5b1b4f1bd68ccc3bafca4cc0c9 100644 (file)
 namespace w1 {
        class W1TemperatureSensor : public W1Device {
                public:
-                       W1TemperatureSensor(int family_code_param,
+                       W1TemperatureSensor(std::string device_type_param,
                                        std::string device_id_param,
                                        dirent *direntry_param);
                        virtual ~W1TemperatureSensor();
                        std::string get_unit();
-                       std::string get_device_type();
                protected:
                        std::vector<double> *get_raw_data();
                        unsigned int get_data_decimal_precision();
-                       bool is_supported_w1_family_code(int family_code);
        };
 }
 
index b9b8b7adbfde55100718137f39dc84e4e8c04dff..2658c46ebffb26f2fb7a2681b2faa986847f167d 100644 (file)
@@ -14,7 +14,7 @@
 #include <plp/log.h>
 
 #include "Date.hh"
-#include "DeviceData.hh"
+#include "DataReader.hh"
 #include "DeviceConfig.hh"
 #include "Factory.hh"
 #include "W1Util.hh"
@@ -47,13 +47,15 @@ bool try_parse_long(const char *str, long *result) {
 }
 
 int main(int argc, char** argv) {
-       string                  loc;
-       Data                    *fdata;
-       Data                    *ldata;
-       DeviceData              *dta;
-       DataRange               *dr;
-       DataRange               *dr2;
-       list<DeviceData *>      dta_list;
+       string                          loc;
+       Data                            *fdata;
+       Data                            *ldata;
+       DataReader                      *reader;
+       Device                          *device;
+       DataRange                       *dr;
+       DataRange                       *dr2;
+       list<Device *>                  dev_lst;
+       list<Device *>::iterator        list_iter;
 
        // default values than can be overwritten with parameters
        loc     = "/tmp/w1data";
@@ -65,35 +67,38 @@ int main(int argc, char** argv) {
                log_warning("No storage location parameter given, using default location: %s\n", loc.c_str());
        }
        DeviceConfig::set_base_dir_name(loc);
-       dta_list        = Factory::get_device_data_list();
-       for(list<DeviceData *>::iterator list_iter = dta_list.begin(); list_iter != dta_list.end(); list_iter++) {
-               dta     = (DeviceData *)*list_iter;
-               if (dta != NULL) {
-                       dr      = dta->get_data_range();
-                       if (dr != NULL) {
-                               fdata   = dr->get_first();
-                               if (fdata != NULL) {
-                                       fdata->printout();
-                                       ldata   = dr->get_last();
-                                       if (ldata != NULL) {
-                                               ldata->printout();
-                                               plp::Date d1;
-                                               plp::Date d2;
+       dev_lst = Factory::get_device_list();
+       for(list_iter = dev_lst.begin(); list_iter != dev_lst.end(); list_iter++) {
+               device  = (Device *)*list_iter;
+               if (device != NULL) {
+                       reader  = device->get_device_data();
+                       if (reader != NULL) {
+                               dr      = reader->get_data_range();
+                               if (dr != NULL) {
+                                       fdata   = dr->get_first();
+                                       if (fdata != NULL) {
+                                               fdata->printout();
+                                               ldata   = dr->get_last();
+                                               if (ldata != NULL) {
+                                                       ldata->printout();
+                                                       plp::Date d1;
+                                                       plp::Date d2;
 
-                                               d1      = fdata->get_date();
-                                               d2      = ldata->get_date();
-                                               dr2     = dta->get_data(&d1, &d2);
-                                               if (dr2 != NULL) {
-                                                       dr2->printout();
-                                                       delete(dr2);
+                                                       d1      = fdata->get_date();
+                                                       d2      = ldata->get_date();
+                                                       dr2     = reader->get_data(&d1, &d2);
+                                                       if (dr2 != NULL) {
+                                                               dr2->printout();
+                                                               delete(dr2);
+                                                       }
+                                                       delete(ldata);
                                                }
-                                               delete(ldata);
+                                               delete(fdata);
                                        }
-                                       delete(fdata);
+                                       delete(dr);
                                }
-                               delete(dr);
+                               delete(reader);
                        }
-                       delete(dta);
                }
        }
        return 0;
index f397525c95b7004cee6dbe80e4a0222da6fc8f01..08bd2392209e368b2b0c2495676bdb3bf16d9413 100644 (file)
 
 #include "DeviceConfig.hh"
 #include "Factory.hh"
+#include "Device.hh"
 
 using namespace w1;
 using namespace std;
+using namespace plp;
 
 #define DEFAULT_READ_INTERVAL_SECONDS  600
-#define DEFAULT_SAVE_INTERVAL_COUNT    5
 #define DEFAULT_MAX_READ_COUNT         -1
 
 bool try_parse_long(const char *str, long *result) {
@@ -47,39 +48,32 @@ bool try_parse_long(const char *str, long *result) {
 }
 
 int main(int argc, char** argv) {
-       list<W1Device *>                device_list;
-       int                             read_count_afer_save;
+       list<Device *>                  device_list;
        int                             read_count_total;
        long                            cnt_max_scans;
        long                            read_interval_seconds;
-       long                            save_interval_count;
        string                          loc;
-       W1Device                        *device;
-       list<W1Device *>::iterator      iter;
+       Device                          *device;
+       list<Device *>::iterator        iter;
 
        // default values than can be overwritten with parameters
        loc     = "/tmp/w1data";
        read_interval_seconds   = DEFAULT_READ_INTERVAL_SECONDS;
-       save_interval_count     = DEFAULT_SAVE_INTERVAL_COUNT;
        cnt_max_scans           = DEFAULT_MAX_READ_COUNT;
        if (argc > 1) {
                loc     = argv[1];
        }
        else {
                log_info("no parameter given using default values:\n");
-               log_info("\ttest_w1_datalog_write %s %ld %ld %ld\n\n", loc.c_str(), read_interval_seconds, save_interval_count, cnt_max_scans);
-               log_info("usage:\n\ttest_w1_datalog_write <result_save_path> <read_interval_seconds> <save_interval_count> <max_read_count>\n");
-               log_info("\tsave_interval_count == -1: do not save data that is read\n");
+               log_info("\ttest_w1_datalog_write %s %ld %ld\n\n", loc.c_str(), read_interval_seconds, cnt_max_scans);
+               log_info("usage:\n\ttest_w1_datalog_write <result_save_path> <read_interval_seconds> <max_read_count>\n");
                log_info("\tmax_read_count == -1: read devices until application is terminated\n\n");
        }
        if (argc > 2) {
                try_parse_long(argv[2], &read_interval_seconds);
        }
        if (argc > 3) {
-               try_parse_long(argv[3], &save_interval_count);
-       }
-       if (argc > 4) {
-               try_parse_long(argv[4], &cnt_max_scans);
+               try_parse_long(argv[3], &cnt_max_scans);
        }
        log_info("searching 1-wire devices\n");
        if (read_interval_seconds == DEFAULT_READ_INTERVAL_SECONDS) {
@@ -88,18 +82,7 @@ int main(int argc, char** argv) {
        else {
                log_info("\tdevice read interval: %ld seconds\n", read_interval_seconds);
        }
-       if (save_interval_count != -1) {
-               if (save_interval_count == DEFAULT_SAVE_INTERVAL_COUNT) {
-                       log_info("\tsave interval: %ld (default value)\n", save_interval_count);
-               }
-               else {
-                       log_info("\tsave interval: %ld\n", save_interval_count);
-               }
-               log_info("\tdata save dir: %s\n", loc.c_str());
-       }
-       else {
-               log_info("\tresults are not saved\n");
-       }
+       log_info("\tdata save dir: %s\n", loc.c_str());
        if (cnt_max_scans == DEFAULT_MAX_READ_COUNT) {
                log_info("\tmax read count: %ld (default value, devices will be read until application is terminated)\n\n", cnt_max_scans);
        }
@@ -108,11 +91,9 @@ int main(int argc, char** argv) {
        }
        DeviceConfig::set_base_dir_name(loc);
        device_list             = Factory::get_device_list();
-       read_count_afer_save    = 0;
        read_count_total        = 0;
        if (device_list.size() > 0) {
                while(1) {
-                       read_count_afer_save++;
                        read_count_total++;
                        if ((cnt_max_scans != -1) &&
                            (read_count_total > cnt_max_scans)) {
@@ -120,19 +101,16 @@ int main(int argc, char** argv) {
                                break;
                        }
                        for(iter = device_list.begin(); iter != device_list.end(); iter++) {
-                               device = (W1Device *)*iter;
-                               device->printout();
-                               sleep(1);
-                               if ((save_interval_count != -1) &&
-                                   (read_count_afer_save >= save_interval_count)) {
-                                       device->save_data();
+                               device = (Device *)*iter;
+                               if (device->get_lifecycle_state() == LIFECYCLE_STATUS__AVAILABLE) {
+                                       device->printout();
+                                       sleep(1);
+                               }
+                               else {
+                                       log_debug("device not available, %s.\n", device->get_id().c_str());
                                }
                        }
                        sleep(read_interval_seconds);
-                       if ((save_interval_count != -1) &&
-                           (read_count_afer_save >= save_interval_count)) {
-                               read_count_afer_save = 0;
-                       }
                }
        }
        else {