From 5c90b2e54752b52c8e5851df68ab9d9063c52ffd Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Sat, 23 Oct 2010 11:51:26 +0300 Subject: [PATCH] add support for storing data to text file periodically --- src/W1Device.cc | 46 +++++++++++++++++++++++++++++++++++++- src/W1Device.hh | 12 ++++++++-- src/W1TemperatureSensor.cc | 17 +++++++++++++- src/W1TemperatureSensor.hh | 4 +++- src_test/test_w1.cc | 19 +++++++++++++--- 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/W1Device.cc b/src/W1Device.cc index 48f366d..3160c81 100644 --- a/src/W1Device.cc +++ b/src/W1Device.cc @@ -5,6 +5,8 @@ * Author: lamikr */ #include +#include + #include #include "W1Device.hh" @@ -61,6 +63,48 @@ string W1Device::get_time() { void W1Device::printout() { string text; - text = get_time() + ": device type = , id = " + id + ", value = " + get_value(); + text = get_formatted_data(); cout << text << endl; } + +string W1Device::get_formatted_data() { + string ret_val; + + ret_val = get_formatted_data(get_value()); + return ret_val; +} + +string W1Device::get_formatted_data(string value) { + string ret_val; + + ret_val = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", value = " + value + " " + get_unit(); + return ret_val; +} + +void W1Device::add_to_memory_cache(std::string formatted_data) { + // TODO: add mutex for memory_cache + memory_cache.push_back(formatted_data); +} + +void W1Device::store() { + string file_path = "/tmp/" + id + ".txt"; + string text_line; + ofstream data_file(file_path.c_str(), ios::app); + + cout << "storing to " << file_path << "data size " << memory_cache.size() << endl; + // TODO: add mutex to protect memory_cache while it's read and emptied + if (data_file.is_open()) { + while(memory_cache.size() > 0) { + text_line = memory_cache.front(); + memory_cache.pop_front(); + if (text_line.length() > 0) { + cout << "storing line: " << text_line << endl; + data_file << text_line << endl; + } + } + data_file.close(); + } + else { + cout << "could not open file " << file_path << " for writing data." << endl; + } +} diff --git a/src/W1Device.hh b/src/W1Device.hh index 190e36e..97d0da9 100644 --- a/src/W1Device.hh +++ b/src/W1Device.hh @@ -8,9 +8,11 @@ #ifndef W1DEVICE_HH_ #define W1DEVICE_HH_ +#include +#include + #include #include -#include #ifndef W1_SCAN_ROOTDIR #define W1_SCAN_ROOTDIR "/sys/bus/w1/devices" @@ -28,18 +30,24 @@ namespace w1 { int get_family_code(); std::string get_id(); std::string get_name(); - virtual void printout(); void set_name(std::string name_param); virtual std::string get_value() = 0; virtual std::string get_unit() = 0; + virtual std::string get_devicetype_name() = 0; std::string get_time(); + virtual void printout(); + virtual void store(); protected: + void add_to_memory_cache(std::string formatted_data); + std::string get_formatted_data(); + std::string get_formatted_data(std::string value); virtual bool is_supported_family_code(int family_code) = 0; int family_code; std::string id; std::string name; std::string dir_path; std::string slave_file; + std::list memory_cache; private: }; } diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index a2b5337..38baecb 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -50,6 +50,7 @@ string W1TemperatureSensor::get_value() { string last_line; int pos; int length; + string formatted_data; ret_val = ""; ifstream ifs(slave_file.c_str()); @@ -70,6 +71,8 @@ string W1TemperatureSensor::get_value() { } } } + formatted_data = get_formatted_data(ret_val); + add_to_memory_cache(formatted_data); return ret_val; } @@ -77,9 +80,21 @@ string W1TemperatureSensor::get_unit() { return "C"; } +string W1TemperatureSensor::get_devicetype_name() { + return "Temperature Sensor"; +} +/* void W1TemperatureSensor::printout() { string text; - text = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value(); + text = get_formatted_data(); cout << text << endl; } + +string W1TemperatureSensor::get_formatted_data() { + string ret_val; + + ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit(); + return ret_val; +} +*/ diff --git a/src/W1TemperatureSensor.hh b/src/W1TemperatureSensor.hh index f08ef4b..bef91c9 100644 --- a/src/W1TemperatureSensor.hh +++ b/src/W1TemperatureSensor.hh @@ -17,8 +17,10 @@ namespace w1 { virtual ~W1TemperatureSensor(); std::string get_value(); std::string get_unit(); - void printout(); + 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 e8f6e2f..4b94a61 100644 --- a/src_test/test_w1.cc +++ b/src_test/test_w1.cc @@ -19,10 +19,17 @@ int main(int argc, char** argv) { W1Scanner *scanner; list device_list; + int round; + int interval_seconds; + int store_interval; - scanner = new W1Scanner(); - device_list = scanner->get_device_list(); + 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; @@ -33,8 +40,14 @@ int main(int argc, char** argv) cout << name << ": " << value << " " << unit << endl; */ device->printout(); + if (round >= store_interval) { + device->store(); + } + } + sleep(interval_seconds); + if (round >= store_interval) { + round = 0; } - sleep(60); } return 0; } -- 2.41.1