From a38a5f6dd1192dd32d01008d53897443359faf6b Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Sat, 30 Oct 2010 17:19:42 +0300 Subject: [PATCH] added W1CounterDevice --- src/Makefile.am | 7 +-- src/W1CounterDevice.cc | 93 ++++++++++++++++++++++++++++++++++++++ src/W1CounterDevice.hh | 26 +++++++++++ src/W1Scanner.cc | 10 ++-- src/W1TemperatureSensor.cc | 6 +-- src/W1TemperatureSensor.hh | 2 - src_test/test_w1.cc | 45 ++++++++++-------- 7 files changed, 157 insertions(+), 32 deletions(-) create mode 100644 src/W1CounterDevice.cc create mode 100644 src/W1CounterDevice.hh diff --git a/src/Makefile.am b/src/Makefile.am index f34f446..a243760 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,8 @@ lib_LTLIBRARIES = lib1wire.la lib1wire_la_SOURCES = \ W1Device.cc W1Device.hh \ W1Scanner.cc W1Scanner.hh \ - W1TemperatureSensor.cc W1TemperatureSensor.hh + W1TemperatureSensor.cc W1TemperatureSensor.hh \ + W1CounterDevice.cc W1CounterDevice.hh lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined AM_CPPFLAGS = $(SRC_CFLAGS) -Iidl @@ -12,5 +13,5 @@ lib1wireincludedir=$(includedir)/w1 lib1wireinclude_HEADERS = \ W1Device.hh \ W1Scanner.hh \ - W1TemperatureSensor.hh - + W1TemperatureSensor.hh \ + W1CounterDevice.hh \ No newline at end of file diff --git a/src/W1CounterDevice.cc b/src/W1CounterDevice.cc new file mode 100644 index 0000000..d339562 --- /dev/null +++ b/src/W1CounterDevice.cc @@ -0,0 +1,93 @@ +/* + * W1CounterDevice.cc + * + * Created on: Oct 30, 2010 + * Author: lamikr + */ + +#include +#include + +#include "W1CounterDevice.hh" + +using namespace std; +using namespace w1; + +W1CounterDevice::W1CounterDevice(dirent *direntry, + int family_code_param, + string id_param): W1Device(direntry, family_code_param, id_param) { + 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; + cout << text << endl; + cout << "verify that you have w1_ds2423 kernel module loaded." << endl; + } + else { + ifs.close(); + } +} + +W1CounterDevice::~W1CounterDevice() { + // TODO Auto-generated destructor stub +} + +bool W1CounterDevice::is_supported_family_code(int family_code) { + bool ret_val; + + ret_val = false; + switch(family_code) { + case 0x1d: + ret_val = true; + break; + } + return ret_val; +} + +string W1CounterDevice::get_value() { + string ret_val; + string value_line; + int pos; + int length; + int ii; + + ret_val = ""; + ifstream ifs(slave_file.c_str()); + if (ifs.is_open() == true) { + ret_val = ""; + while(getline(ifs, value_line)) { + length = value_line.length(); + if (length > 0) { + pos = value_line.find("c="); + if ((pos >= 0) && + (pos + 2 < length)) { + value_line = value_line.substr(pos + 2); + } + else { + value_line = ""; + } + } + else { + value_line = ""; + } + if (ret_val.length() == 0) { + ret_val = value_line; + } + else { + ret_val = ret_val + "|" + value_line; + } + } + ifs.close(); + } + add_to_memory_cache(ret_val); + return ret_val; +} + +string W1CounterDevice::get_unit() { + return ""; +} + +string W1CounterDevice::get_devicetype_name() { + return "Counter Device"; +} diff --git a/src/W1CounterDevice.hh b/src/W1CounterDevice.hh new file mode 100644 index 0000000..816f3b8 --- /dev/null +++ b/src/W1CounterDevice.hh @@ -0,0 +1,26 @@ +/* + * W1CounterDevice.hh + * + * Created on: Oct 30, 2010 + * Author: lamikr + */ + +#ifndef W1COUNTERDEVICE_HH_ +#define W1COUNTERDEVICE_HH_ + +#include "W1Device.hh" + +namespace w1 { + class W1CounterDevice: public w1::W1Device { + public: + W1CounterDevice(dirent *direntry, int family_code_param, std::string id_param); + virtual ~W1CounterDevice(); + std::string get_value(); + std::string get_unit(); + std::string get_devicetype_name(); + protected: + bool is_supported_family_code(int family_code); + }; +} + +#endif /* W1COUNTERDEVICE_HH_ */ diff --git a/src/W1Scanner.cc b/src/W1Scanner.cc index 6a55eeb..a27b23d 100644 --- a/src/W1Scanner.cc +++ b/src/W1Scanner.cc @@ -15,16 +15,17 @@ #include #include "W1Scanner.hh" #include "W1TemperatureSensor.hh" +#include "W1CounterDevice.hh" using namespace w1; using namespace std; W1Scanner::W1Scanner() { - log_debug("created"); + log_debug("created\n"); } W1Scanner::~W1Scanner() { - log_debug("destroyed"); + log_debug("destroyed\n"); } bool W1Scanner::is_subdirectory(dirent *direntry) { @@ -70,11 +71,14 @@ W1Device *W1Scanner::create_device(dirent *direntry) { cout << "temperature sensor: " << family_code << endl; ret_val = new W1TemperatureSensor(direntry, family_code, device_name); break; + case 0x1d: + cout << "counter device: " << family_code << endl; + ret_val = new W1CounterDevice(direntry, family_code, device_name); + break; } } } return ret_val; - } list W1Scanner::get_device_list() { diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index 39977e5..84db272 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -5,7 +5,6 @@ * Author: lamikr */ #include -#include #include #include #include @@ -48,9 +47,9 @@ W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param if (ifs.is_open() == false) { string text; - text = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file; + text = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file; cout << text << endl; - cout << "verify that you have w1_therm kernel module loaded" << endl; + cout << "verify that you have w1_therm kernel module loaded." << endl; } else { ifs.close(); @@ -74,7 +73,6 @@ bool W1TemperatureSensor::is_supported_family_code(int family_code) { } string W1TemperatureSensor::get_value() { - vector text_file; string temp; string ret_val; string last_line; diff --git a/src/W1TemperatureSensor.hh b/src/W1TemperatureSensor.hh index bef91c9..bf55522 100644 --- a/src/W1TemperatureSensor.hh +++ b/src/W1TemperatureSensor.hh @@ -18,9 +18,7 @@ namespace w1 { std::string get_value(); std::string get_unit(); std::string get_devicetype_name(); - //void printout(); protected: - //std::string get_formatted_data(); bool is_supported_family_code(int family_code); }; } diff --git a/src_test/test_w1.cc b/src_test/test_w1.cc index 6c6c116..3a07a0a 100644 --- a/src_test/test_w1.cc +++ b/src_test/test_w1.cc @@ -8,7 +8,7 @@ #include #include -#include +#include #include "W1Scanner.hh" @@ -17,38 +17,43 @@ using namespace std; int main(int argc, char** argv) { - W1Scanner *scanner; + W1Scanner *scanner; list device_list; - int round; - int interval_seconds; - int store_interval; + int round; + int interval_seconds; + int store_interval; interval_seconds = 60; store_interval = 10; - scanner = new W1Scanner(); - device_list = scanner->get_device_list(); - round = 0; - while(1) { - round++; - for(list::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) - { - W1Device *device = (W1Device *)*list_iter; + scanner = new W1Scanner(); + device_list = scanner->get_device_list(); + round = 0; + if (device_list.size() > 0) { + while(1) { + round++; + for(list::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) + { + W1Device *device = (W1Device *)*list_iter; /* string name = device->get_name(); string value = device->get_value(); string unit = device->get_unit(); cout << name << ": " << value << " " << unit << endl; */ - device->printout(); - sleep(1); + device->printout(); + sleep(1); + if (round >= store_interval) { + device->store(); + } + } + sleep(interval_seconds); if (round >= store_interval) { - device->store(); + round = 0; } } - sleep(interval_seconds); - if (round >= store_interval) { - round = 0; - } + } + else { + log_debug("could not find 1-wire devices.\n"); } return 0; } -- 2.41.1