From: Mika Laitio Date: Thu, 3 Mar 2011 17:42:05 +0000 (+0200) Subject: Api cleanups for device querying X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=da203ad44792ca37320768f4cbe68587c4806910;p=lib1wire.git Api cleanups for device querying 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 --- diff --git a/src/DeviceData.cc b/src/DataReader.cc similarity index 85% rename from src/DeviceData.cc rename to src/DataReader.cc index c564377..799ead1 100644 --- a/src/DeviceData.cc +++ b/src/DataReader.cc @@ -1,5 +1,5 @@ /* - * DeviceData.cc + * DataReader.cc * * Created on: Nov 7, 2010 * Author: lamikr @@ -12,7 +12,7 @@ #include #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 year_name_vector_param) { +Data *DataReader::find_newest_data(vector year_name_vector_param) { string year_name; int size; Data *ret_val; @@ -70,7 +70,7 @@ Data *DeviceData::find_newest_data(vector year_name_vector_param) { return ret_val; } -Data *DeviceData::find_oldest_data(vector year_name_vector_param) { +Data *DataReader::find_oldest_data(vector year_name_vector_param) { int size; string year_name; Data *ret_val; @@ -89,7 +89,7 @@ Data *DeviceData::find_oldest_data(vector year_name_vector_param) { return ret_val; } -DataRange *DeviceData::get_data_range() { +DataRange *DataReader::get_data_range() { DataRange *ret_val; vector 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; +} diff --git a/src/DeviceData.hh b/src/DataReader.hh similarity index 93% rename from src/DeviceData.hh rename to src/DataReader.hh index 1ccbb4f..ac7e13e 100644 --- a/src/DeviceData.hh +++ b/src/DataReader.hh @@ -1,5 +1,5 @@ /* - * DeviceData.hh + * DataReader.hh * * Created on: Nov 7, 2010 * Author: lamikr @@ -18,10 +18,10 @@ #include 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; diff --git a/src/DeviceTypeGeneric.hh b/src/Device.hh similarity index 63% rename from src/DeviceTypeGeneric.hh rename to src/Device.hh index ba944a8..f5ee509 100644 --- a/src/DeviceTypeGeneric.hh +++ b/src/Device.hh @@ -10,13 +10,19 @@ #include +#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 index 0000000..e37a68b --- /dev/null +++ b/src/DeviceTypes.hh @@ -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_ */ diff --git a/src/Factory.cc b/src/Factory.cc index 76287eb..d9556e3 100644 --- a/src/Factory.cc +++ b/src/Factory.cc @@ -12,6 +12,8 @@ #include #include + +#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 Factory::get_device_list() { - DIR *dir; - int err_flg; - struct dirent *direntry; - W1Device *device; - bool is_subdir; - list ret_val; +list Factory::get_device_list() { + DIR *dir; + int err_flg; + struct dirent *direntry; + Device *device; + bool is_subdir; + list rdr_list; + list ret_val; + list::iterator dev_iter; + list::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 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 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 Factory::get_device_data_list() { +list 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 ret_val; + list ret_val; dr_name = DeviceConfig::get_base_dir_name(); dir = opendir(dr_name.c_str()); @@ -166,8 +240,8 @@ list 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); diff --git a/src/Factory.hh b/src/Factory.hh index 6e2beb2..7c16d6f 100644 --- a/src/Factory.hh +++ b/src/Factory.hh @@ -9,12 +9,13 @@ #define FACTORY_HH_ #include +#include #include #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 get_device_list(); - static std::list get_device_data_list(); + static std::list 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 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); }; } diff --git a/src/Makefile.am b/src/Makefile.am index 4ec0e88..3ec5e97 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/DeviceTypeSensor.hh b/src/SensorDevice.hh similarity index 54% rename from src/DeviceTypeSensor.hh rename to src/SensorDevice.hh index 99403ef..43e59f5 100644 --- a/src/DeviceTypeSensor.hh +++ b/src/SensorDevice.hh @@ -10,14 +10,13 @@ #include -#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; }; } diff --git a/src/StoreCache.cc b/src/StoreCache.cc index baea078..f3f2ac2 100644 --- a/src/StoreCache.cc +++ b/src/StoreCache.cc @@ -10,7 +10,6 @@ #include "StoreCache.hh" #include "StoreDay.hh" #include "DeviceConfig.hh" -#include "DeviceData.hh" #include "W1Util.hh" using namespace std; diff --git a/src/W1CounterDevice.cc b/src/W1CounterDevice.cc index 58e9a55..37a0821 100644 --- a/src/W1CounterDevice.cc +++ b/src/W1CounterDevice.cc @@ -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 *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; } diff --git a/src/W1CounterDevice.hh b/src/W1CounterDevice.hh index 17c0728..770bed0 100644 --- a/src/W1CounterDevice.hh +++ b/src/W1CounterDevice.hh @@ -15,16 +15,14 @@ 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 *get_raw_data(); unsigned int get_data_decimal_precision(); - bool is_supported_w1_family_code(int family_code); }; } diff --git a/src/W1Device.cc b/src/W1Device.cc index 4c108e3..37a02ba 100644 --- a/src/W1Device.cc +++ b/src/W1Device.cc @@ -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::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::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; +} diff --git a/src/W1Device.hh b/src/W1Device.hh index d1371e9..4dab3a6 100644 --- a/src/W1Device.hh +++ b/src/W1Device.hh @@ -15,7 +15,8 @@ #include #include "Data.hh" -#include "DeviceTypeSensor.hh" +#include "SensorDevice.hh" +#include "Device.hh" #ifndef W1_SCAN_ROOTDIR #define W1_SCAN_ROOTDIR "/sys/bus/w1/devices" @@ -26,32 +27,34 @@ #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 *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 memory_cache; + plp::EnumDeviceLifeCycleStatus lifecycle_status; }; } diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index e01e1cc..b6d1dac 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -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 *W1TemperatureSensor::get_raw_data() { vector *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; } diff --git a/src/W1TemperatureSensor.hh b/src/W1TemperatureSensor.hh index 8c06af1..3616f8d 100644 --- a/src/W1TemperatureSensor.hh +++ b/src/W1TemperatureSensor.hh @@ -15,16 +15,14 @@ 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 *get_raw_data(); unsigned int get_data_decimal_precision(); - bool is_supported_w1_family_code(int family_code); }; } diff --git a/src_test/test_w1_datalog_read.cc b/src_test/test_w1_datalog_read.cc index b9b8b7a..2658c46 100644 --- a/src_test/test_w1_datalog_read.cc +++ b/src_test/test_w1_datalog_read.cc @@ -14,7 +14,7 @@ #include #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 dta_list; + string loc; + Data *fdata; + Data *ldata; + DataReader *reader; + Device *device; + DataRange *dr; + DataRange *dr2; + list dev_lst; + list::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::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; diff --git a/src_test/test_w1_datalog_write.cc b/src_test/test_w1_datalog_write.cc index f397525..08bd239 100644 --- a/src_test/test_w1_datalog_write.cc +++ b/src_test/test_w1_datalog_write.cc @@ -15,12 +15,13 @@ #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 device_list; - int read_count_afer_save; + list device_list; int read_count_total; long cnt_max_scans; long read_interval_seconds; - long save_interval_count; string loc; - W1Device *device; - list::iterator iter; + Device *device; + list::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 \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 \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 {