]> pilppa.org Git - lib1wire.git/commitdiff
Initial support for reading and writing device specific config data.
authorMika Laitio <lamikr@pilppa.org>
Wed, 22 Dec 2010 00:03:14 +0000 (02:03 +0200)
committerMika Laitio <lamikr@pilppa.org>
Wed, 22 Dec 2010 00:03:14 +0000 (02:03 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
24 files changed:
configure.ac
src/Data.cc
src/Data.hh
src/Date.cc
src/DeviceConfig.cc
src/DeviceConfig.hh
src/Factory.cc [new file with mode: 0644]
src/Factory.hh [new file with mode: 0644]
src/Makefile.am
src/W1CounterDevice.cc
src/W1CounterDevice.hh
src/W1DataList.cc
src/W1Device.cc
src/W1Device.hh
src/W1Scanner.cc
src/W1Scanner.hh
src/W1Store.cc
src/W1Store.hh
src/W1TemperatureSensor.cc
src/W1TemperatureSensor.hh
src/W1Util.cc
src_test/Makefile.am
src_test/test_w1_datalog_read.cc
src_test/test_w1_datalog_write.cc

index 010f01f7a0a14de1fe118a37659a059098d75e9e..a8fc4c7bdebec27ea49e88c49b6e29fba6be99f0 100644 (file)
@@ -9,7 +9,9 @@ CFLAGS="$CFLAGS -ggdb -Wall -Werror"
 LDFLAGS="$LDFLAGS"
 AC_SUBST(CFLAGS)
 AC_SUBST(LDFLAGS)
-AC_MSG_NOTICE([objective c Makefile])
+CXXFLAGS="$CXXFLAGS -ggdb -Wall -Werror"
+AC_SUBST(CXXFLAGS)
+AC_MSG_NOTICE([lib1wire Makefile])
 
 AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
 
index 98425c63d64d63b0506f47b4d2602765c1129cb0..388dbefa71471c3910b680972abba3ac2361b6e6 100644 (file)
@@ -45,13 +45,13 @@ Data::Data(int size, double default_value) {
 }
 
 Data::Data(vector<double> vector_param, Date *date_param) {
-       int     ii;
-       int     size;
+       unsigned int    ii;
+       unsigned int    size;
 
        size    = vector_param.size();
        //log_debug("Data(), value count: %d\n", size);
        value_arr.resize(size);
-       for (int ii = 0; ii < vector_param.size(); ii++) {
+       for (ii = 0; ii < vector_param.size(); ii++) {
                value_arr[ii]   = vector_param.at(ii);
                //log_debug("Data(), value[%d]: %f\n", ii, value_arr[ii]);
        }
@@ -59,8 +59,11 @@ Data::Data(vector<double> vector_param, Date *date_param) {
 }
 
 Data::Data(std::valarray<double> value_arr_param, Date *date_param) {
+       unsigned int ii;
+
        value_arr.resize(value_arr_param.size());
-       for (int ii = 0; ii < value_arr_param.size(); ii++) {
+
+       for (ii = 0; ii < value_arr_param.size(); ii++) {
                value_arr[ii]   = value_arr_param[ii];
        }
        date_time.copy(date_param);
@@ -78,7 +81,7 @@ void Data::set_date(plp::Date date) {
 }
 
 void Data::printout() {
-       int     ii;
+       unsigned int    ii;
 
        date_time.printout();
        for (ii = 0; ii < value_arr.size(); ii++) {
@@ -131,8 +134,8 @@ DataRange::DataRange(Data data) {
 }
 
 DataRange::~DataRange() {
-       int     ii;
-       Date    *date;
+       unsigned int    ii;
+       Date            *date;
 
        if (val_matrix != NULL) {
                free(val_matrix);
@@ -145,10 +148,9 @@ DataRange::~DataRange() {
 }
 
 void DataRange::add_data(Data data) {
-       int                     ii;
-       int                     r_count;
-       int                     indx;
-       Date                    date;
+       unsigned int    ii;
+       int             indx;
+       Date            date;
 
        //log_debug("old row_count: %d, column_count: %d, value_arr_size: %d\n", row_count, column_count, data.value_arr.size());
        val_matrix      = (double *)realloc(val_matrix, ((row_count + 1) * column_count) * sizeof(double));
index 3907a9b384a6631f963bef88dc60ed3112b8c404..35af7c615f64ed8df1415fe76c92656cc0d416e1 100644 (file)
@@ -11,7 +11,6 @@
 #include <string>
 #include <valarray>
 #include <vector>
-#include <hash_map>
 
 #include "Date.hh"
 
index b8b57140d2a4ea3dbd9d2f4c086d55d6a47b449c..8c03c5bd661e6ed7a5c371037ef4e0923b268af2 100644 (file)
@@ -57,7 +57,7 @@ bool Date::is_leap_year() {
 
        ret_val = false;
        if ((year % 4 == 0) &&
-           (year % 400 == 0) || (year % 100 != 0)) {
+           ((year % 400 == 0) || (year % 100 != 0))) {
                ret_val = true;
        }
        return ret_val;
@@ -141,7 +141,6 @@ string Date::to_string() {
        char    buffer[30];
        string  ret_val;
 
-       int n, a=5, b=3;
        sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
        ret_val = buffer;
        return ret_val;
index aa191cc009cebd7f1b89d43a86d35349e3485955..ee06135a4270cc4594c0a2d6770a5ae18775c87b 100644 (file)
 
 #include "DeviceConfig.hh"
 
+#include <string.h>
+#include <malloc.h>
+
+#include <plp/log.h>
+#include "W1Util.hh"
+#include "W1Configure.hh"
+
+#include "Factory.hh"
+
 using namespace std;
 using namespace w1;
 
+string DeviceConfig::store_base_dir    = DEFAULT_STORAGE_BASE_DIR;
+
+ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) {
+       ctx     = ctx_param;
+       pkg     = pkg_param;
+}
+
+ConfigHandle::~ConfigHandle() {
+       uci_unload(ctx, pkg);
+       uci_free_context(ctx);
+}
+
 DeviceConfig::DeviceConfig(string device_id_param) {
        device_id       = device_id_param;
+       uci_handle      = load_device_config(device_id_param);
+       if (uci_handle != NULL) {
+               device_type     = get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
+       }
+       else {
+               log_error("Could not read device config\n");
+       }
 }
 
 DeviceConfig::~DeviceConfig() {
+       if (uci_handle != NULL) {
+               delete(uci_handle);
+               uci_handle      = NULL;
+       }
+}
+
+void DeviceConfig::set_base_dir_name(string store_param) {
+       int     pos;
+       int     b_count;
+
+       pos     = store_param.find_last_of("/");
+       b_count = store_param.length();
+       if (pos == (b_count - 1)) {
+               store_base_dir  = store_param;
+       }
+       else {
+               store_base_dir  = store_param + "/";
+       }
+}
+
+string DeviceConfig::get_base_dir_name() {
+       return store_base_dir;
+}
+
+string DeviceConfig::get_dir_name(string device_id_param) {
+       string  ret_val;
+       string  d_name;
+
+       d_name  = DeviceConfig::get_base_dir_name();
+       ret_val = W1Util::concat_paths(d_name, device_id_param);
+       return ret_val;
+}
+
+string DeviceConfig::get_file_name(string device_id_param) {
+       string  ret_val;
+       string  fname;
+
+       fname   = DEVICE_CONFIG__FILE_NAME;
+       ret_val = get_dir_name(device_id);
+       ret_val = W1Util::concat_paths(ret_val, fname);
+       return ret_val;
+}
+
+string DeviceConfig::get_cfg_value(string key) {
+       struct uci_section      *section;
+       struct uci_option       *option;
+       string                  ret_val;
+
+       if (uci_handle != NULL) {
+               section = uci_lookup_section(uci_handle->ctx, uci_handle->pkg, DEVICE_CONFIG__SECTION_NAME);
+               if (section != NULL) {
+                       option  = uci_lookup_option(uci_handle->ctx, section, key.c_str());
+                       switch (option->type) {
+                               case UCI_TYPE_STRING:
+                                       log_info("config file: key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
+                                       ret_val = option->v.string;
+                                       break;
+                               default:
+                                       log_error("config file: key: %s can not parse parameter value\n", key.c_str());
+                                       break;
+                       }
+               }
+       }
+       return ret_val;
 }
 
-string DeviceConfig::get_config_value(string key) {
-       return NULL;
+void DeviceConfig::set_cfg_value(string key, string value) {
+       string cfg_dir;
+       string cfg_fl;
+
+       cfg_dir = get_dir_name(device_id);
+       cfg_fl  = DEVICE_CONFIG__FILE_NAME;
+
+       set_config_value(cfg_dir.c_str(),
+                       cfg_fl.c_str(),
+                       DEVICE_CONFIG__SECTION_TYPE,
+                       DEVICE_CONFIG__SECTION_NAME,
+                       key.c_str(),
+                       value.c_str());
 }
 
 enum_summary_calculation DeviceConfig::get_summary_calculation_type() {
-       return MEAN;
+       enum_summary_calculation        ret_val;
+
+       ret_val = MEAN;
+       if (device_type.empty() == false) {
+               if (device_type.compare("counter") == 0) {
+                       ret_val = DELTA;
+               }
+       }
+       return ret_val;;
+}
+
+ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
+       int                     err_flg;
+       struct uci_context      *ctx;
+       struct uci_package      *pkg;
+       string                  cfg_fl;
+       string                  cfg_dir;
+       ConfigHandle            *ret_val;
+
+       ret_val = NULL;
+       cfg_dir = get_dir_name(device_id_param);
+       if (cfg_dir.empty() == false) {
+               cfg_fl  = get_file_name(device_id_param);
+               ctx     = uci_alloc_context();
+               if (ctx != NULL) {
+                       log_debug("uci_set_confdir: %s\n", cfg_dir.c_str());
+                       uci_set_confdir(ctx, cfg_dir.c_str());
+                       if (access(cfg_fl.c_str(), R_OK) != 0) {
+                               log_debug("loading file: %s\n", cfg_fl.c_str());
+                               err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
+                               if (err_flg == UCI_OK) {
+                                       log_debug("Loaded device configuration: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg);
+                                       ret_val = new ConfigHandle(ctx, pkg);
+                               }
+                               else {
+                                       log_debug("Failed to load file: %s, UCI_OK: %d, err flg: %d\n.", cfg_fl.c_str(), UCI_OK, err_flg);
+                                       set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "unknowntype");
+                               }
+                       }
+               }
+               else {
+                       log_error("Failed to load device device configurations, invalid device id: %s.\n", cfg_dir.c_str());
+               }
+       }
+       return ret_val;
 }
index ced5c81ae5ccfddc9af7bd041b1365f72b6494e5..c2cfb31512fbce5f2619a3b405bf4339f2e59b8c 100644 (file)
 
 #include <string>
 
+extern "C" {
+#include <uci.h>
+#include <plp/config.h>
+}
+
+#define DEVICE_CONFIG__FILE_NAME               "dev_cfg.txt"
+#define DEVICE_CONFIG__SECTION_TYPE            "device"
+#define DEVICE_CONFIG__SECTION_NAME            "base_data"
+#define DEVICE_CONFIG_VALUE_KEY__TYPE          "type"
+#define DEVICE_CONFIG_VALUE_KEY__NAME          "name"
+
 namespace w1 {
        enum enum_summary_calculation {SUM, DELTA, MEAN, MAX, MIN};
 
+       struct ConfigHandle {
+               public:
+                       ConfigHandle(uci_context *ctx_param, uci_package *pkg_param);
+                       ~ConfigHandle();
+                       struct uci_context      *ctx;
+                       struct uci_package      *pkg;
+       };
+
        class DeviceConfig {
                public:
                        DeviceConfig(std::string device_id_param);
                        virtual ~DeviceConfig();
-                       std::string get_config_value(std::string key);
+                       static std::string get_base_dir_name();
+                       static void set_base_dir_name(std::string store_param);
+                       std::string get_cfg_value(std::string key);
+                       void set_cfg_value(std::string key, std::string value);
                        enum_summary_calculation get_summary_calculation_type();
                private:
-                       std::string     device_id;
+                       static std::string      store_base_dir;
+                       std::string             device_id;
+                       std::string             device_type;
+                       ConfigHandle            *uci_handle;
+                       ConfigHandle *load_device_config(std::string device_id_param);
+                       std::string get_dir_name(std::string device_id);
+                       std::string get_file_name(std::string device_id_param);
        };
 }
 
diff --git a/src/Factory.cc b/src/Factory.cc
new file mode 100644 (file)
index 0000000..e9637a4
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Factory.cc
+ *
+ *  Created on: Dec 11, 2010
+ *      Author: lamikr
+ */
+
+#include <plp/log.h>
+#include "Factory.hh"
+#include "W1TemperatureSensor.hh"
+#include "W1CounterDevice.hh"
+
+using namespace w1;
+using namespace std;
+
+Factory::Factory() {
+       // TODO Auto-generated constructor stub
+}
+
+Factory::~Factory() {
+       // TODO Auto-generated destructor stub
+}
+
+W1Device *Factory::get_device(int family_code,
+                       string device_id,
+                       dirent *direntry_param) {
+       W1Device        *ret_val;
+       DeviceConfig    *config;
+
+       ret_val = NULL;
+       log_debug("family_code: %d\n", family_code);
+       switch(family_code) {
+               case    0x10:
+               case    0x28:
+                       ret_val = new W1TemperatureSensor(family_code, device_id, direntry_param);
+                       log_debug("temperature sensor: %d\n", ret_val->get_family_code());
+                       break;
+               case    0x1d:
+                       ret_val = new W1CounterDevice(family_code, device_id, direntry_param);
+                       log_debug("counter device: %d\n", family_code);
+                       break;
+               default:
+                       log_debug("device not created, unsupported device type: %d\n", family_code);
+                       break;
+       }
+       if (ret_val != NULL) {
+               // check that device config exist
+               // if not, create default...
+               config  = get_device_config(device_id);
+               if (config != NULL) {
+                       string  type;
+                       type    = config->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE);
+                       if (type.empty() == true) {
+                               type    = ret_val->get_device_type();
+                               config->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type);
+                       }
+               }
+       }
+       return ret_val;
+}
+
+DeviceConfig *Factory::get_device_config(string device_id) {
+       DeviceConfig    *ret_val;
+
+       ret_val = new DeviceConfig(device_id);
+       return ret_val;
+}
diff --git a/src/Factory.hh b/src/Factory.hh
new file mode 100644 (file)
index 0000000..595e271
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Factory.hh
+ *
+ *  Created on: Dec 11, 2010
+ *      Author: lamikr
+ */
+
+#ifndef FACTORY_HH_
+#define FACTORY_HH_
+
+#include <string>
+
+#include <dirent.h>
+
+#include "DeviceConfig.hh"
+#include "W1Device.hh"
+
+namespace w1 {
+       class Factory {
+               public:
+                       Factory();
+                       virtual ~Factory();
+                       static W1Device *get_device(int family_code, std::string device_id, dirent *direntry_param);
+                       static DeviceConfig *get_device_config(std::string device_id);
+       };
+}
+
+#endif /* FACTORY_HH_ */
index 5b77fc448e4c6083d5693762e72313e8c193be10..67a7a4b5df7eb6c6769785fc5fac7638c85b6a32 100644 (file)
@@ -1,5 +1,6 @@
 lib_LTLIBRARIES = lib1wire.la
 lib1wire_la_SOURCES = \
+       Factory.cc Factory.hh \
        W1Device.cc W1Device.hh \
        W1Scanner.cc W1Scanner.hh \
        W1Store.cc W1Store.hh \
@@ -12,7 +13,7 @@ lib1wire_la_SOURCES = \
        Date.cc Date.hh \
        W1Configure.hh
 lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined
-AM_CPPFLAGS = $(SRC_CFLAGS) -Iidl
+AM_CPPFLAGS = $(SRC_CFLAGS)
 
 DISTCLEANFILES = Makefile.in
 
index 4f500e9007f4bcf4a9e2bbbb116dda8d88589e17..53c95f93a0751cd97ed55087b0b24ed77ba10c1e 100644 (file)
 using namespace std;
 using namespace w1;
 
-W1CounterDevice::W1CounterDevice(dirent *direntry,
-                               int family_code_param,
-                               string id_param): W1Device(direntry, family_code_param, id_param) {
+W1CounterDevice::W1CounterDevice(int family_code_param,
+                               string device_id_param,
+                               dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
+       string text;
+
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == true) {
-               string text;
-
-               text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
+               text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file;
                cout << text << endl;
                cout << "verify that you have w1_ds2423 kernel module loaded." << endl;
                ifs.close();
@@ -44,11 +44,10 @@ bool W1CounterDevice::is_supported_family_code(int family_code) {
 }
 
 string W1CounterDevice::get_raw_value() {
-       string          ret_val;
-       string          value_line;
-       int             pos;
-       int             length;
-       int             ii;
+       string  ret_val;
+       string  value_line;
+       int     pos;
+       int     length;
 
        ret_val = "<could not read>";
        ifstream ifs(slave_file.c_str());
@@ -85,6 +84,6 @@ string W1CounterDevice::get_unit() {
        return "";
 }
 
-string W1CounterDevice::get_devicetype_name() {
+string W1CounterDevice::get_device_type() {
        return "Counter Device";
 }
index b2080e044a680039bc9cdd18ed2aea62c8b433ff..837488c248cc6e0112c70e9b028865d304cbc557 100644 (file)
 namespace w1 {
        class W1CounterDevice: public w1::W1Device {
                public:
-                       W1CounterDevice(dirent *direntry, int family_code_param, std::string id_param);
+                       W1CounterDevice(int family_code_param,
+                                       std::string device_id_param,
+                                       dirent *direntry_param);
                        virtual ~W1CounterDevice();
                        std::string get_raw_value();
                        std::string get_unit();
-                       std::string get_devicetype_name();
+                       std::string get_device_type();
                protected:
                        bool is_supported_family_code(int family_code);
        };
index 844db8e34a435359b76e230e0f9a9f62befcf93b..9f2674f0971b35b328fcb56f74272212fd0427f8 100644 (file)
@@ -12,6 +12,7 @@
 #include "W1Util.hh"
 #include "W1DataList.hh"
 #include "W1Store.hh"
+#include "DeviceConfig.hh"
 
 #include "plp/log.h"
 
@@ -25,7 +26,7 @@ W1DataList::W1DataList(string device_id_param) {
        device_config           = new DeviceConfig(device_id_param);
        summary_calc_type       = device_config->get_summary_calculation_type();
        device_id               = device_id_param;
-       base_dir                = W1Store::get_base_dir_name();
+       base_dir                = DeviceConfig::get_base_dir_name();
        device_dir              = W1Util::concat_paths(base_dir, device_id);
        device_ch_dir           = W1Util::concat_paths(base_dir, "cache");
        device_ch_dir           = W1Util::concat_paths(device_ch_dir, device_id);
@@ -36,7 +37,7 @@ W1DataList::~W1DataList() {
 }
 
 Data *W1DataList::find_oldest_data(vector<string> year_vector) {
-       int             ii;
+       unsigned int    ii;
        string          year_dir;
        string          month_dir;
        vector<string>  month_vector;
@@ -108,8 +109,6 @@ Data *W1DataList::find_newest_data(vector<string> year_vector) {
 
 DataRange *W1DataList::get_data_range() {
        DataRange       *ret_val;
-       DIR             *data_dir;
-       struct dirent   *year_dirent;
        vector<string>  year_list;
        Data            *first_data;
        Data            *newest_data;
index 843910ac9764bca9a59257ce25eebbd0411399c7..467a3359685eb7a432ee606259684fe4d79e687b 100644 (file)
@@ -8,6 +8,7 @@
 #include <fstream>
 
 #include <time.h>
+#include <plp/log.h>
 
 #include "W1Store.hh"
 #include "W1Device.hh"
 using namespace w1;
 using namespace std;
 
-W1Device::W1Device(dirent *direntry,
-               int family_code_param,
-               string id_param) {
+W1Device::W1Device(int family_code_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->d_name;
+       dir_path        = rootdir + "/" + direntry_param->d_name;
        slave_file      = dir_path + "/" + temp_str;
+       log_debug("w1 data file: %s\n", slave_file.c_str());
        family_code     = family_code_param;
-       id              = id_param;
-       name            = id_param;
+       id              = device_id_param;
+       name            = device_id_param;
 }
 
 W1Device::~W1Device() {
@@ -82,7 +84,7 @@ string W1Device::get_formatted_value() {
 string W1Device::get_formatted_value(string value) {
        string ret_val;
 
-       ret_val = get_time() + "|" + get_devicetype_name() + "|" + id + "|" + value + " " + get_unit();
+       ret_val = get_time() + "|" + get_device_type() + "|" + id + "|" + value + " " + get_unit();
        add_to_memory_cache(ret_val);
        return ret_val;
 }
index 9eab016288075cb646072d383654465b900728a3..a6902f72526379d20325fd1473934a8ff8b1f3c7 100644 (file)
@@ -25,7 +25,9 @@
 namespace w1 {
        class W1Device {
                public:
-                       W1Device(dirent *direntry, int family_code_param, std::string id_param);
+                       W1Device(int family_code_param,
+                               std::string device_id_param,
+                               dirent *direntry_param);
                        virtual ~W1Device();
                        int get_family_code();
                        std::string get_id();
@@ -34,7 +36,7 @@ namespace w1 {
                        virtual std::string get_raw_value() = 0;
                        std::string get_formatted_value();
                        virtual std::string get_unit() = 0;
-                       virtual std::string get_devicetype_name() = 0;
+                       virtual std::string get_device_type() = 0;
                        std::string get_time();
                        virtual void printout();
                        virtual void store();
index 390c2510576f4267747b6ef5064d76a1b4bd93ae..d40108e2bc32d1b89bd21d813ff6c7175cd004f2 100644 (file)
@@ -18,6 +18,7 @@
 #include "W1Scanner.hh"
 #include "W1TemperatureSensor.hh"
 #include "W1CounterDevice.hh"
+#include "Factory.hh"
 
 using namespace w1;
 using namespace std;
@@ -38,7 +39,7 @@ W1Scanner::W1Scanner() {
 W1Scanner::~W1Scanner() {
 }
 
-W1Device *W1Scanner::create_device(dirent *direntry) {
+W1Device *W1Scanner::create_device(dirent *direntry_param) {
        string          folder_name;
        string          tmp_str;
        string          device_name;
@@ -48,7 +49,7 @@ W1Device *W1Scanner::create_device(dirent *direntry) {
        W1Device        *ret_val;
 
        ret_val         = NULL;
-       folder_name     = direntry->d_name;
+       folder_name     = direntry_param->d_name;
        pos             = folder_name.find("-");
        if (pos > 0) {
                tmp_str = folder_name.substr(0, pos);
@@ -57,20 +58,9 @@ W1Device *W1Scanner::create_device(dirent *direntry) {
                if (suc_flg == true) {
                        log_debug("family_code: %d\n", family_code);
                        device_name     = folder_name.substr(pos + 1, folder_name.length() - pos);
-                       switch(family_code) {
-                               case    0x10:
-                               case    0x28:
-                                       ret_val = new W1TemperatureSensor(direntry, family_code, device_name);
-                                       log_debug("temperature sensor: %d\n", ret_val->get_family_code());
-                                       break;
-                               case    0x1d:
-                                       log_debug("counter device: %d\n", family_code);
-                                       ret_val = new W1CounterDevice(direntry, family_code, device_name);
-                                       break;
-                               default:
-                                       log_debug("device not created the device, unsupported device type: %d\n", family_code);
-                                       break;
-                       }
+                       ret_val         = Factory::get_device(family_code,
+                                                       device_name,
+                                                       direntry_param);
                }
        }
        return ret_val;
index c5e55294f451867423846d69b6ab10e4caa001ec..21a7c7b27195d2aab62e7cac08ec19ccdcc66928 100644 (file)
@@ -26,7 +26,7 @@ namespace w1 {
                        std::list<W1Device *> get_device_list();
                private:
                        //int parse_family_code(std::string folder_name);
-                       W1Device *create_device(dirent *direntry);
+                       W1Device *create_device(dirent *direntry_param);
        };
 }
 
index 80dde72388e325635b26abfda9455bf19d96ebea..fda285e540c0e6b13dcf3ca56f185410af86578d 100644 (file)
@@ -27,8 +27,6 @@ using namespace std;
 using namespace w1;
 using namespace plp;
 
-std::string W1Store::store_base_dir    = DEFAULT_STORAGE_BASE_DIR;
-
 W1Store::W1Store(string device_id,
                Date *date_time) {
        store_data      = NULL;
@@ -50,30 +48,14 @@ W1Store::~W1Store() {
        }
 }
 
-void W1Store::set_base_dir_name(string store_param) {
-       int     pos;
-       int     b_count;
-
-       pos     = store_param.find_last_of("/");
-       b_count = store_param.length();
-       if (pos == (b_count - 1)) {
-               store_base_dir  = store_param;
-       }
-       else {
-               store_base_dir  = store_param + "/";
-       }
-}
-
-string W1Store::get_base_dir_name() {
-       return store_base_dir;
-}
-
 string W1Store::get_dir_name(string device_id, Date *date_time) {
        string  ret_val;
        char    buffer[30];
+       string  d_name;
 
+       d_name  = DeviceConfig::get_base_dir_name();
        snprintf(buffer, 30, "%d/%02d", date_time->year, date_time->month);
-       ret_val = W1Util::concat_paths(store_base_dir, device_id);
+       ret_val = W1Util::concat_paths(d_name, device_id);
        ret_val = ret_val + "/" + buffer;
        return ret_val;
 }
@@ -157,7 +139,6 @@ Data *W1Store::get_sum() {
        double  new_val;
        int     ii;
        int     jj;
-       Date    *date;
        Data    *data;
        Data    *ret_val;
 
@@ -260,7 +241,6 @@ Data *W1Store::get_max() {
        double  new_val;
        int     ii;
        int     jj;
-       Date    *date;
        Data    *data;
        Data    *ret_val;
        double  min_val;
@@ -308,7 +288,6 @@ Data *W1Store::get_min() {
        double  new_val;
        int     ii;
        int     jj;
-       Date    *date;
        Data    *data;
        Data    *ret_val;
        double  max_val;
@@ -414,7 +393,6 @@ DataRange *W1Store::get_oldest_and_newest_data() {
 
 Data *W1Store::get_oldest_data() {
        int             row_count;
-       int             col_count;
        Data            *ret_val;
        DataRange       *dr;
 
@@ -432,7 +410,6 @@ Data *W1Store::get_oldest_data() {
 
 Data *W1Store::get_newest_data() {
        int             row_count;
-       int             col_count;
        Data            *ret_val;
        DataRange       *dr;
 
index d27768a04af9fc62881a90396563f17e124b66bf..53b124aa0654a7dcefa18ee4a8f10b3559dc160f 100644 (file)
@@ -21,8 +21,6 @@ namespace w1 {
                                plp::Date *date_time);
                        W1Store(std::string file_name_param);
                        virtual ~W1Store();
-                       static std::string get_base_dir_name();
-                       static void set_base_dir_name(std::string store_param);
                        static std::string get_dir_name(std::string device_id, plp::Date *ltime);
                        static std::string get_file_name(std::string device_id, plp::Date *ltime);
                        static void store(std::string device_id, std::list<std::string> *string_list);
@@ -36,7 +34,6 @@ namespace w1 {
                        w1::Data *get_newest_data();
                        w1::DataRange *get_oldest_and_newest_data();
                protected:
-                       static std::string      store_base_dir;
                        std::string             store_file_name;
                        DataRange               *store_data;
                        DataRange               *range_data;
index bfa72912943e0b9daf942c6d68f55b3b3a6eb8ad..b02bb29dd01518738b2fd59136d3b94f8a7d37d4 100644 (file)
@@ -28,7 +28,6 @@ bool string_to_number(NumberDataType& result,
 
 string convert_celcius_value_to_three_digits(string raw_value) {
        string  ret_val;
-       int     int_val;
        bool    suc_flg;
        double  dbl_val;
 
@@ -45,15 +44,15 @@ string convert_celcius_value_to_three_digits(string raw_value) {
        return ret_val;
 }
 
-W1TemperatureSensor::W1TemperatureSensor(dirent *direntry,
-                               int family_code_param,
-                               string id_param): W1Device(direntry, family_code_param, id_param) {
+W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
+                               string device_id_param,
+                               dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
+       string text;
+
        log_debug("trying to open file: %s\n", slave_file.c_str());
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == false) {
-               string text;
-
-               text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file + "\n";
+               text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file + "\n";
                log_debug(text.c_str());
                log_debug("verify that you have w1_therm kernel module loaded.\n");
                ifs.close();
@@ -82,8 +81,7 @@ string W1TemperatureSensor::get_raw_value() {
        string          last_line;
        int             pos;
        int             length;
-       string          formatted_data;
-       int             int_value;
+       string          format;
 
        ret_val = "<could not read>";
        ifstream ifs(slave_file.c_str());
@@ -111,6 +109,6 @@ string W1TemperatureSensor::get_unit() {
        return "C";
 }
 
-string W1TemperatureSensor::get_devicetype_name() {
+string W1TemperatureSensor::get_device_type() {
        return "Temperature Sensor";
 }
index f5235d63c88399116ada28e4ad508c0d0d4421ec..6946c5cc10c57e1f84e3ba6ffa4d7997a72eeaa1 100644 (file)
 namespace w1 {
        class W1TemperatureSensor : public W1Device {
                public:
-                       W1TemperatureSensor(dirent *direntry, int family_code_param, std::string id_param);
+                       W1TemperatureSensor(int family_code_param,
+                                       std::string device_id_param,
+                                       dirent *direntry_param);
                        virtual ~W1TemperatureSensor();
                        std::string get_raw_value();
                        std::string get_unit();
-                       std::string get_devicetype_name();
+                       std::string get_device_type();
                protected:
                        bool is_supported_family_code(int family_code);
        };
index ba6b944a986ab58326a6d4ae23da4b236d7d5551..08dc7e0e90419341c1202cccd245bf556b3f6173 100644 (file)
@@ -108,9 +108,7 @@ bool W1Util::mkdirs(char *path) {
 
 std::ofstream *W1Util::open_for_writing(const char *f_path) {
        char            *d_path;
-       char            *p;
        size_t          b_count;
-       int             ii;
        ofstream        *ret_val;
        bool            b_flg;
 
index be3a2dbf8997c7113f3355f461f9d21783140cbd..0812159dea4d183de097b7ae24c859011bf71c08 100644 (file)
@@ -8,6 +8,6 @@ test_w1_datalog_read_LDADD = $(SRC_LIBS) ../src/.libs/lib1wire.a
 test_w1_datalog_write_SOURCES = test_w1_datalog_write.cc
 test_w1_datalog_write_LDADD = $(SRC_LIBS) ../src/.libs/lib1wire.a
 
-AM_CPPFLAGS = $(SRC_CFLAGS) -I../src
+AM_CPPFLAGS = -I../src
 
 DISTCLEANFILES = Makefile.in
\ No newline at end of file
index 50003edf754dd580ec075aabda55f02f1f63bbb3..0c4a5b9b1859f65b8833dc3c09589d130fc3d4b3 100644 (file)
 #include <plp/log.h>
 
 #include "W1DataList.hh"
-#include "W1Store.hh"
+#include "DeviceConfig.hh"
 #include "W1Scanner.hh"
 
+#include "Date.hh"
 #include "W1Util.hh"
 
 using namespace w1;
@@ -46,9 +47,7 @@ bool try_parse_long(const char *str, long *result) {
 }
 
 int main(int argc, char** argv) {
-       int             round;
        string          loc;
-       bool            err_flg;
        Data            *fdata;
        Data            *ldata;
        W1DataList      *dlist;
@@ -65,7 +64,7 @@ int main(int argc, char** argv) {
        else {
                log_warning("No storage location parameter given, using default location: %s\n", loc.c_str());
        }
-       W1Store::set_base_dir_name(loc);
+       DeviceConfig::set_base_dir_name(loc);
        dlist   = new W1DataList("0008014e9e09");
        if (dlist != NULL) {
                dr      = dlist->get_data_range();
@@ -76,7 +75,12 @@ int main(int argc, char** argv) {
                                ldata   = dr->get_last_data();
                                if (ldata != NULL) {
                                        ldata->printout();
-                                       dr2     = dlist->get_data(&fdata->get_date(), &ldata->get_date());
+                                       plp::Date d1;
+                                       plp::Date d2;
+
+                                       d1      = fdata->get_date();
+                                       d2      = ldata->get_date();
+                                       dr2     = dlist->get_data(&d1, &d2);
                                        delete(ldata);
                                        if (dr2 != NULL) {
                                                delete(dr2);
index 5931f5fc5090f0de928bfe0f7093fb75687c98f2..f5a86d8820c69f41cb13648606c93383bbd8f1b3 100644 (file)
@@ -13,7 +13,7 @@
 
 #include <plp/log.h>
 
-#include "W1Store.hh"
+#include "DeviceConfig.hh"
 #include "W1Scanner.hh"
 
 using namespace w1;
@@ -49,7 +49,6 @@ int main(int argc, char** argv) {
        long                    scan_interval;
        long                    store_interval;
        string                  location;
-       bool                    err_flg;
        W1Device                *device;
 
        // default values than can be overwritten with parameters
@@ -69,8 +68,8 @@ int main(int argc, char** argv) {
        if (argc > 3) {
                try_parse_long(argv[3], &store_interval);
        }
-       log_info("start scanning, data saved to location: %s, scan interval: %d, store interval: %d\n", location.c_str(), scan_interval, store_interval);
-       W1Store::set_base_dir_name(location);
+       log_info("start scanning, data saved to location: %s, scan interval: %ld, store interval: %ld\n", location.c_str(), scan_interval, store_interval);
+       DeviceConfig::set_base_dir_name(location);
        scanner         = new W1Scanner();
        device_list     = scanner->get_device_list();
        round           = 0;
@@ -81,7 +80,6 @@ int main(int argc, char** argv) {
                        round++;
                        for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
                                device = (W1Device *)*list_iter;
-
                                device->printout();
                                sleep(1);
                                if (round >= store_interval) {