From: Mika Laitio Date: Sun, 26 Dec 2010 13:41:23 +0000 (+0200) Subject: Support for querying the list devices having data stored. X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=8c97cbb9b5f8997fe9ab3917fcbea66d74b45837;p=lib1wire.git Support for querying the list devices having data stored. Also refactored the code, removed W1Scanner and moved that functionality to Factory.hh Signed-off-by: Mika Laitio --- diff --git a/configure.ac b/configure.ac index a8fc4c7..327399b 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT(src/W1Scanner.cc) +AC_INIT(src/Data.cc) AM_CONFIG_HEADER(config.h) AC_CONFIG_MACRO_DIR([m4]) diff --git a/src/DeviceConfig.cc b/src/DeviceConfig.cc index ee06135..f9e8f72 100644 --- a/src/DeviceConfig.cc +++ b/src/DeviceConfig.cc @@ -115,7 +115,6 @@ void DeviceConfig::set_cfg_value(string key, string value) { 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, @@ -147,12 +146,15 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { ret_val = NULL; cfg_dir = get_dir_name(device_id_param); if (cfg_dir.empty() == false) { + if (access(cfg_dir.c_str(), W_OK) != 0) { + W1Util::mkdirs(cfg_dir.c_str()); + } 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) { + 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) { @@ -164,9 +166,12 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) { set_cfg_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "unknowntype"); } } + else { + log_error("Failed to load device device configuration, file does not exit: %s.\n", cfg_fl.c_str()); + } } else { - log_error("Failed to load device device configurations, invalid device id: %s.\n", cfg_dir.c_str()); + log_error("Failed to load device device configuration, invalid device id: %s.\n", cfg_dir.c_str()); } } return ret_val; diff --git a/src/W1DataList.cc b/src/DeviceData.cc similarity index 90% rename from src/W1DataList.cc rename to src/DeviceData.cc index 9f2674f..259d821 100644 --- a/src/W1DataList.cc +++ b/src/DeviceData.cc @@ -1,5 +1,5 @@ /* - * W1DataList.cc + * DeviceData.cc * * Created on: Nov 7, 2010 * Author: lamikr @@ -10,9 +10,10 @@ #include #include "W1Util.hh" -#include "W1DataList.hh" +#include "DeviceData.hh" #include "W1Store.hh" #include "DeviceConfig.hh" +#include "Factory.hh" #include "plp/log.h" @@ -20,10 +21,10 @@ using namespace w1; using namespace std; using namespace plp; -W1DataList::W1DataList(string device_id_param) { +DeviceData::DeviceData(string device_id_param) { string base_dir; - device_config = new DeviceConfig(device_id_param); + device_config = Factory::get_device_config(device_id_param); summary_calc_type = device_config->get_summary_calculation_type(); device_id = device_id_param; base_dir = DeviceConfig::get_base_dir_name(); @@ -32,11 +33,11 @@ W1DataList::W1DataList(string device_id_param) { device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id); } -W1DataList::~W1DataList() { +DeviceData::~DeviceData() { delete(device_config); } -Data *W1DataList::find_oldest_data(vector year_vector) { +Data *DeviceData::find_oldest_data(vector year_vector) { unsigned int ii; string year_dir; string month_dir; @@ -70,7 +71,7 @@ Data *W1DataList::find_oldest_data(vector year_vector) { return ret_val; } -Data *W1DataList::find_newest_data(vector year_vector) { +Data *DeviceData::find_newest_data(vector year_vector) { int ii; string year_dir; string month_dir; @@ -107,7 +108,7 @@ Data *W1DataList::find_newest_data(vector year_vector) { return ret_val; } -DataRange *W1DataList::get_data_range() { +DataRange *DeviceData::get_data_range() { DataRange *ret_val; vector year_list; Data *first_data; @@ -168,7 +169,7 @@ long int get_interval_type(Date *start_date, return ret_val; } -Data *W1DataList::get_daily_summary(Date *date) { +Data *DeviceData::get_daily_summary(Date *date) { Data *ret_val; W1Store *store; @@ -198,7 +199,7 @@ Data *W1DataList::get_daily_summary(Date *date) { return ret_val; } -DataRange *W1DataList::get_daily_summary(Date *start_date, +DataRange *DeviceData::get_daily_summary(Date *start_date, Date *end_date) { DataRange *ret_val; Data *data; @@ -221,7 +222,7 @@ DataRange *W1DataList::get_daily_summary(Date *start_date, return ret_val; } -DataRange *W1DataList::get_data(Date *start_date, +DataRange *DeviceData::get_data(Date *start_date, Date *end_date) { DataRange *ret_val; int int_type; diff --git a/src/W1DataList.hh b/src/DeviceData.hh similarity index 91% rename from src/W1DataList.hh rename to src/DeviceData.hh index 2d6ceda..3362971 100644 --- a/src/W1DataList.hh +++ b/src/DeviceData.hh @@ -1,5 +1,5 @@ /* - * W1DataList.hh + * DeviceData.hh * * Created on: Nov 7, 2010 * Author: lamikr @@ -18,10 +18,10 @@ #include namespace w1 { - class W1DataList { + class DeviceData { public: - W1DataList(std::string device_id); - virtual ~W1DataList(); + DeviceData(std::string device_id); + virtual ~DeviceData(); DataRange *get_data_range(); /** * Get summary data calculated from the daily data items that is meaning full. diff --git a/src/Factory.cc b/src/Factory.cc index c81dbe1..8a09eaa 100644 --- a/src/Factory.cc +++ b/src/Factory.cc @@ -5,14 +5,30 @@ * Author: lamikr */ +#include +#include +#include + +#include + #include #include "Factory.hh" #include "W1TemperatureSensor.hh" #include "W1CounterDevice.hh" +#include "W1Util.hh" using namespace w1; using namespace std; +template +bool string_to_number(NumberDataType& result, + const std::string& string_param, + std::ios_base& (*format)(std::ios_base&)) +{ + std::istringstream iss(string_param); + return !(iss >> format >> result).fail(); +} + Factory::Factory() { // TODO Auto-generated constructor stub } @@ -60,6 +76,101 @@ W1Device *Factory::get_device(int family_code, return ret_val; } +W1Device *Factory::create_device(dirent *direntry_param) { + string folder_name; + string tmp_str; + string device_name; + int pos; + int family_code; + bool suc_flg; + W1Device *ret_val; + + ret_val = NULL; + folder_name = direntry_param->d_name; + pos = folder_name.find("-"); + if (pos > 0) { + tmp_str = folder_name.substr(0, pos); + // number in string is in hex format, convert to int + suc_flg = string_to_number(family_code, tmp_str, hex); + if (suc_flg == true) { + log_debug("family_code: %d\n", family_code); + device_name = folder_name.substr(pos + 1, folder_name.length() - pos); + ret_val = Factory::get_device(family_code, + device_name, + direntry_param); + } + } + return ret_val; +} + +list Factory::get_device_list() { + DIR *dir; + int err_flg; + struct dirent *direntry; + W1Device *device; + bool is_subdir; + list ret_val; + + printf("get_device_list() started\n"); + dir = opendir(W1_SCAN_ROOTDIR); + if (dir != NULL) { + direntry = readdir(dir); + while(direntry != NULL) { + is_subdir = W1Util::is_subdirectory(W1_SCAN_ROOTDIR, direntry); + if (is_subdir == true) { + log_info("dir_name: %s\n", direntry->d_name); + device = create_device(direntry); + if (device != NULL) { + log_info("device: %d\n", device->get_family_code()); + ret_val.push_back(device); + } + else { + log_info("unsupported device-directory: %s\n", direntry->d_name); + } + } + direntry = readdir(dir); + } + err_flg = closedir(dir); + if (err_flg < 0) { + log_error("failed to close 1-wire device directory: %s\n", W1_SCAN_ROOTDIR); + } + } + return ret_val; +} + +list Factory::get_device_data_list() { + DIR *dir; + string dr_name; + int err_flg; + struct dirent *direntry; + DeviceData *dev_dta; + bool is_subdir; + list ret_val; + + printf("get_device_list() started\n"); + dr_name = DeviceConfig::get_base_dir_name(); + dir = opendir(dr_name.c_str()); + if (dir != NULL) { + direntry = readdir(dir); + while(direntry != NULL) { + 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); + log_info("dir_name: %s\n", direntry->d_name); + ret_val.push_back(dev_dta); + } + } + direntry = readdir(dir); + } + err_flg = closedir(dir); + if (err_flg < 0) { + log_error("failed to close 1-wire device data directory: %s\n", dr_name.c_str()); + } + } + return ret_val; +} + DeviceConfig *Factory::get_device_config(string device_id) { DeviceConfig *ret_val; diff --git a/src/Factory.hh b/src/Factory.hh index 595e271..a5cf254 100644 --- a/src/Factory.hh +++ b/src/Factory.hh @@ -13,15 +13,25 @@ #include #include "DeviceConfig.hh" +#include "DeviceData.hh" #include "W1Device.hh" +#ifndef W1_SCAN_ROOTDIR + #define W1_SCAN_ROOTDIR "/sys/bus/w1/devices" +#endif + namespace w1 { class Factory { public: Factory(); virtual ~Factory(); static W1Device *get_device(int family_code, std::string device_id, dirent *direntry_param); + static std::list get_device_list(); + static std::list get_device_data_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); }; } diff --git a/src/Makefile.am b/src/Makefile.am index 67a7a4b..ec3ec8f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,12 +2,11 @@ lib_LTLIBRARIES = lib1wire.la lib1wire_la_SOURCES = \ Factory.cc Factory.hh \ W1Device.cc W1Device.hh \ - W1Scanner.cc W1Scanner.hh \ W1Store.cc W1Store.hh \ W1TemperatureSensor.cc W1TemperatureSensor.hh \ W1CounterDevice.cc W1CounterDevice.hh \ W1Util.cc W1Util.hh \ - W1DataList.cc W1DataList.hh \ + DeviceData.cc DeviceData.hh \ DeviceConfig.cc DeviceConfig.hh \ Data.cc Data.hh \ Date.cc Date.hh \ @@ -19,9 +18,13 @@ DISTCLEANFILES = Makefile.in lib1wireincludedir=$(includedir)/w1 lib1wireinclude_HEADERS = \ + Data.hh \ + Date.hh \ + DeviceConfig.hh \ + DeviceData.hh \ + Factory.hh \ + W1CounterDevice.hh \ W1Device.hh \ - W1Scanner.hh \ W1Store.hh \ W1TemperatureSensor.hh \ - Date.hh \ - W1CounterDevice.hh \ No newline at end of file + W1Util.hh \ No newline at end of file diff --git a/src/W1Scanner.cc b/src/W1Scanner.cc deleted file mode 100644 index d40108e..0000000 --- a/src/W1Scanner.cc +++ /dev/null @@ -1,102 +0,0 @@ -/* - * W1Scanner.cc - * - * Created on: Oct 20, 2010 - * Author: lamikr - */ - -#include -#include - -#include -#include -#include - -#include - -#include "W1Util.hh" -#include "W1Scanner.hh" -#include "W1TemperatureSensor.hh" -#include "W1CounterDevice.hh" -#include "Factory.hh" - -using namespace w1; -using namespace std; - -template -bool string_to_number(NumberDataType& result, - const std::string& string_param, - std::ios_base& (*format)(std::ios_base&)) -{ - std::istringstream iss(string_param); - return !(iss >> format >> result).fail(); -} - -W1Scanner::W1Scanner() { - log_debug("W1Scanner created\n"); -} - -W1Scanner::~W1Scanner() { -} - -W1Device *W1Scanner::create_device(dirent *direntry_param) { - string folder_name; - string tmp_str; - string device_name; - int pos; - int family_code; - bool suc_flg; - W1Device *ret_val; - - ret_val = NULL; - folder_name = direntry_param->d_name; - pos = folder_name.find("-"); - if (pos > 0) { - tmp_str = folder_name.substr(0, pos); - // number in string is in hex format, convert to int - suc_flg = string_to_number(family_code, tmp_str, hex); - if (suc_flg == true) { - log_debug("family_code: %d\n", family_code); - device_name = folder_name.substr(pos + 1, folder_name.length() - pos); - ret_val = Factory::get_device(family_code, - device_name, - direntry_param); - } - } - return ret_val; -} - -list W1Scanner::get_device_list() { - DIR *dir; - int err_flg; - struct dirent *direntry; - W1Device *device; - list ret_val; - bool is_subdir; - - printf("get_device_list() started\n"); - dir = opendir(W1_SCAN_ROOTDIR); - if (dir != NULL) { - direntry = readdir(dir); - while(direntry != NULL) { - is_subdir = W1Util::is_subdirectory(W1_SCAN_ROOTDIR, direntry); - if (is_subdir == true) { - log_info("dir_name: %s\n", direntry->d_name); - device = create_device(direntry); - if (device != NULL) { - log_info("device: %d\n", device->get_family_code()); - ret_val.push_back(device); - } - else { - log_info("unsupported device-directory: %s\n", direntry->d_name); - } - } - direntry = readdir(dir); - } - err_flg = closedir(dir); - if (err_flg < 0) { - log_error("failed to close 1-wire device directory scanned: %s\n", W1_SCAN_ROOTDIR); - } - } - return ret_val; -} diff --git a/src/W1Scanner.hh b/src/W1Scanner.hh deleted file mode 100644 index 21a7c7b..0000000 --- a/src/W1Scanner.hh +++ /dev/null @@ -1,33 +0,0 @@ -/* - * W1Scanner.hh - * - * Created on: Oct 20, 2010 - * Author: lamikr - */ - -#ifndef W1SCANNER_HH_ -#define W1SCANNER_HH_ - -#include -#include -#include - -#include "W1Device.hh" - -#ifndef W1_SCAN_ROOTDIR - #define W1_SCAN_ROOTDIR "/sys/bus/w1/devices" -#endif - -namespace w1 { - class W1Scanner { - public: - W1Scanner(); - virtual ~W1Scanner(); - std::list get_device_list(); - private: - //int parse_family_code(std::string folder_name); - W1Device *create_device(dirent *direntry_param); - }; -} - -#endif /* W1SCANNER_HH_ */ diff --git a/src/W1Store.cc b/src/W1Store.cc index fda285e..8debaa1 100644 --- a/src/W1Store.cc +++ b/src/W1Store.cc @@ -19,6 +19,7 @@ #include +#include "DeviceConfig.hh" #include "W1Configure.hh" #include "W1Store.hh" #include "W1Util.hh" diff --git a/src/W1Util.cc b/src/W1Util.cc index 08dc7e0..452d729 100644 --- a/src/W1Util.cc +++ b/src/W1Util.cc @@ -63,7 +63,7 @@ char *W1Util::parse_directory_path(const char *file_path) { return ret_val; } -bool W1Util::mkdirs(char *path) { +bool W1Util::mkdirs(const char *path) { bool ret_val; char *p; int err_flg; @@ -71,13 +71,13 @@ bool W1Util::mkdirs(char *path) { ret_val = true; if (path != NULL) { // go through each directory one by and and create if not exist - for (p = path; *p; p++) { + for (p = (char *)path; *p; p++) { if ((p != path) && ((*p == '/') || (*p == '\\'))) { *p = '\0'; - // test whether directory exist and is writable - if (access(path, F_OK)) { + // if dir does not exist, create it + if (access(path, F_OK) != 0) { log_debug("trying to create directory: %s\n", path); err_flg = mkdir(path, S_IRWXU); if (err_flg != 0) { @@ -89,8 +89,8 @@ bool W1Util::mkdirs(char *path) { } } if (ret_val == true) { - // test also the existense of whole directory - if (access(path, F_OK)) { + // if dir does not exist, create it + if (access(path, F_OK) != 0) { log_debug("trying to create directory: %s\n", path); err_flg = mkdir(path, S_IRWXU); if (err_flg != 0) { diff --git a/src/W1Util.hh b/src/W1Util.hh index 161d2f0..cbc74ab 100644 --- a/src/W1Util.hh +++ b/src/W1Util.hh @@ -18,8 +18,6 @@ #include #include -#include "W1DataList.hh" - namespace w1 { class W1Util { public: @@ -33,7 +31,7 @@ namespace w1 { static std::vector get_data_files(const std::string& path); static plp::Date parse_date_str(std::string date_str); static char *parse_directory_path(const char *file_path); - static bool mkdirs(char *path); + static bool mkdirs(const char *path); static std::ofstream *open_for_writing(const char *path); }; } diff --git a/src_test/test_w1_datalog_read.cc b/src_test/test_w1_datalog_read.cc index 0c4a5b9..046a475 100644 --- a/src_test/test_w1_datalog_read.cc +++ b/src_test/test_w1_datalog_read.cc @@ -13,11 +13,10 @@ #include -#include "W1DataList.hh" -#include "DeviceConfig.hh" -#include "W1Scanner.hh" - #include "Date.hh" +#include "DeviceData.hh" +#include "DeviceConfig.hh" +#include "Factory.hh" #include "W1Util.hh" using namespace w1; @@ -47,16 +46,16 @@ bool try_parse_long(const char *str, long *result) { } int main(int argc, char** argv) { - string loc; - Data *fdata; - Data *ldata; - W1DataList *dlist; - DataRange *dr; - DataRange *dr2; + string loc; + Data *fdata; + Data *ldata; + DeviceData *dta; + DataRange *dr; + DataRange *dr2; + list dta_list; // default values than can be overwritten with parameters - //location = "/tmp/"; - loc = "/home/lamikr/own/src/plp/w1data2/"; + loc = "/tmp/w1data"; if (argc > 1) { loc = argv[1]; log_info("storage location: %s\n", loc.c_str()); @@ -65,32 +64,35 @@ 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); - dlist = new W1DataList("0008014e9e09"); - if (dlist != NULL) { - dr = dlist->get_data_range(); - if (dr != NULL) { - fdata = dr->get_first_data(); - if (fdata != NULL) { - fdata->printout(); - ldata = dr->get_last_data(); - if (ldata != NULL) { - ldata->printout(); - plp::Date d1; - plp::Date d2; + dta_list = Factory::get_device_data_list(); + for(list::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_data(); + if (fdata != NULL) { + fdata->printout(); + ldata = dr->get_last_data(); + if (ldata != NULL) { + ldata->printout(); + 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); + d1 = fdata->get_date(); + d2 = ldata->get_date(); + dr2 = dta->get_data(&d1, &d2); + delete(ldata); + if (dr2 != NULL) { + delete(dr2); + } } + delete(fdata); } - delete(fdata); + delete(dr); } - delete(dr); + delete(dta); } - delete(dlist); } return 0; } diff --git a/src_test/test_w1_datalog_write.cc b/src_test/test_w1_datalog_write.cc index f5a86d8..d0cd8e9 100644 --- a/src_test/test_w1_datalog_write.cc +++ b/src_test/test_w1_datalog_write.cc @@ -14,7 +14,7 @@ #include #include "DeviceConfig.hh" -#include "W1Scanner.hh" +#include "Factory.hh" using namespace w1; using namespace std; @@ -43,24 +43,23 @@ bool try_parse_long(const char *str, long *result) { } int main(int argc, char** argv) { - W1Scanner *scanner; list device_list; int round; long scan_interval; long store_interval; - string location; + string loc; W1Device *device; // default values than can be overwritten with parameters - location = "/tmp/"; - scan_interval = 2; //600; + loc = "/tmp/w1data"; + scan_interval = 600; //600; store_interval = 2; if (argc > 1) { - location = argv[1]; - log_info("storage location: %s\n", location.c_str()); + loc = argv[1]; + log_info("storage location: %s\n", loc.c_str()); } else { - log_warning("No storage location parameter given, using default location: %s\n", location.c_str()); + log_warning("No storage location parameter given, using default location: %s\n", loc.c_str()); } if (argc > 2) { try_parse_long(argv[2], &scan_interval); @@ -68,15 +67,12 @@ 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: %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(); + log_info("start scanning, data saved to location: %s, scan interval: %ld, store interval: %ld\n", loc.c_str(), scan_interval, store_interval); + DeviceConfig::set_base_dir_name(loc); + device_list = Factory::get_device_list(); round = 0; - int t = 0; if (device_list.size() > 0) { - while(t < 3) { - t++; + while(1) { round++; for(list::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) { device = (W1Device *)*list_iter; @@ -100,6 +96,5 @@ int main(int argc, char** argv) { device_list.pop_back(); delete(device); } - delete(scanner); return 0; }