From: Mika Laitio Date: Sat, 30 Oct 2010 23:15:50 +0000 (+0300) Subject: storage data cleanup. allow specifying storage dir location X-Git-Url: http://pilppa.com/gitweb/?a=commitdiff_plain;h=c7979a0225a21182f68d40374723a649d032908d;p=lib1wire.git storage data cleanup. allow specifying storage dir location --- diff --git a/src/Makefile.am b/src/Makefile.am index a243760..f1fb5ca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,7 @@ lib_LTLIBRARIES = lib1wire.la lib1wire_la_SOURCES = \ W1Device.cc W1Device.hh \ W1Scanner.cc W1Scanner.hh \ + W1Store.cc W1Store.hh \ W1TemperatureSensor.cc W1TemperatureSensor.hh \ W1CounterDevice.cc W1CounterDevice.hh lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined @@ -13,5 +14,6 @@ lib1wireincludedir=$(includedir)/w1 lib1wireinclude_HEADERS = \ W1Device.hh \ W1Scanner.hh \ + W1Store.hh \ W1TemperatureSensor.hh \ W1CounterDevice.hh \ No newline at end of file diff --git a/src/W1CounterDevice.cc b/src/W1CounterDevice.cc index 5e343bf..2566e1d 100644 --- a/src/W1CounterDevice.cc +++ b/src/W1CounterDevice.cc @@ -80,7 +80,6 @@ string W1CounterDevice::get_value() { } ifs.close(); } - add_to_memory_cache(ret_val); return ret_val; } diff --git a/src/W1Device.cc b/src/W1Device.cc index 7f52c43..fef721c 100644 --- a/src/W1Device.cc +++ b/src/W1Device.cc @@ -9,6 +9,7 @@ #include +#include "W1Store.hh" #include "W1Device.hh" using namespace w1; @@ -71,15 +72,18 @@ void W1Device::printout() { string W1Device::get_formatted_data() { string ret_val; + string val; - ret_val = get_formatted_data(get_value()); + val = get_value(); + ret_val = get_formatted_data(val); 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(); + ret_val = get_time() + "|" + get_devicetype_name() + "|" + id + "|" + value + " " + get_unit(); + add_to_memory_cache(ret_val); return ret_val; } @@ -89,6 +93,8 @@ void W1Device::add_to_memory_cache(std::string formatted_data) { } void W1Device::store() { + W1Store::store(id, memory_cache); +/* string file_path = "/tmp/" + id + ".txt"; string text_line; ofstream data_file(file_path.c_str(), ios::app); @@ -109,4 +115,5 @@ void W1Device::store() { else { cout << "could not open file " << file_path << " for writing data." << endl; } +*/ } diff --git a/src/W1Device.hh b/src/W1Device.hh index 97d0da9..15deba2 100644 --- a/src/W1Device.hh +++ b/src/W1Device.hh @@ -42,7 +42,7 @@ namespace w1 { 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; + int family_code; std::string id; std::string name; std::string dir_path; diff --git a/src/W1Store.cc b/src/W1Store.cc new file mode 100644 index 0000000..9801583 --- /dev/null +++ b/src/W1Store.cc @@ -0,0 +1,56 @@ +/* + * W1Store.cc + * + * Created on: Oct 31, 2010 + * Author: lamikr + */ + +#include +#include +#include +#include + +#include + +#include "W1Store.hh" + +using namespace std; +using namespace w1; + +std::string W1Store::location = "/tmp/"; + +W1Store::W1Store() { + // TODO Auto-generated constructor stub +} + +W1Store::~W1Store() { + // TODO Auto-generated destructor stub +} + +void W1Store::set_location(string location_param) { + location = location_param; +} + +void W1Store::store(std::string device_id, std::list string_list) { + + string file_path = location + device_id + ".txt"; + string text_line; + ofstream data_file(file_path.c_str(), ios::app); + + cout << "storing to " << file_path << ", data size " << string_list.size() << endl; + // TODO: add mutex to protect string_list while it's read and emptied + if (data_file.is_open()) { + while(string_list.size() > 0) { + text_line = string_list.front(); + string_list.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/W1Store.hh b/src/W1Store.hh new file mode 100644 index 0000000..abdc0b6 --- /dev/null +++ b/src/W1Store.hh @@ -0,0 +1,25 @@ +/* + * W1Store.hh + * + * Created on: Oct 31, 2010 + * Author: lamikr + */ + +#ifndef W1STORE_HH_ +#define W1STORE_HH_ + +#include +#include + +namespace w1 { + class W1Store { + public: + W1Store(); + virtual ~W1Store(); + static std::string location; + static void set_location(std::string location_param); + static void store(std::string device_id, std::list string_list); + }; +} + +#endif /* W1STORE_HH_ */ diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index 84db272..b5445c3 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -100,9 +100,7 @@ string W1TemperatureSensor::get_value() { } } } - ret_val = convert_for_3_digits_value(ret_val); - formatted_data = get_formatted_data(ret_val); - add_to_memory_cache(formatted_data); + ret_val = convert_for_3_digits_value(ret_val); return ret_val; } diff --git a/src_test/test_w1.cc b/src_test/test_w1.cc index 3a07a0a..e0ceb1d 100644 --- a/src_test/test_w1.cc +++ b/src_test/test_w1.cc @@ -10,21 +10,28 @@ #include +#include "W1Store.hh" #include "W1Scanner.hh" using namespace w1; using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char** argv) { W1Scanner *scanner; list device_list; int round; int interval_seconds; int store_interval; + string location; - interval_seconds = 60; - store_interval = 10; + location = "/tmp/"; + if (argc > 1) { + location = argv[1]; + log_info("storage location: %s\n", location.c_str()); + } + W1Store::set_location(location.c_str()); + interval_seconds = 3; + store_interval = 3; scanner = new W1Scanner(); device_list = scanner->get_device_list(); round = 0; @@ -34,12 +41,7 @@ int main(int argc, char** argv) 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); if (round >= store_interval) {