]> pilppa.com Git - lib1wire.git/commitdiff
storage data cleanup. allow specifying storage dir location
authorMika Laitio <lamikr@pilppa.org>
Sat, 30 Oct 2010 23:15:50 +0000 (02:15 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sat, 30 Oct 2010 23:15:50 +0000 (02:15 +0300)
src/Makefile.am
src/W1CounterDevice.cc
src/W1Device.cc
src/W1Device.hh
src/W1Store.cc [new file with mode: 0644]
src/W1Store.hh [new file with mode: 0644]
src/W1TemperatureSensor.cc
src_test/test_w1.cc

index a243760e7a9cdb9332669d9e781e2ab08919d384..f1fb5ca996bec9c5251522956fd870349e2ea609 100644 (file)
@@ -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
index 5e343bf879b8efcfc6c1b14dc5fec253437d6fed..2566e1d4888114c0ccad31f28965a510948f39a0 100644 (file)
@@ -80,7 +80,6 @@ string W1CounterDevice::get_value() {
                }
                ifs.close();
        }
-       add_to_memory_cache(ret_val);
        return ret_val;
 }
 
index 7f52c43f5795341c5943eb8610c5fd71f43d96a4..fef721c55ff7fc757767d9570de0ec66af7567cd 100644 (file)
@@ -9,6 +9,7 @@
 
 #include <time.h>
 
+#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;
        }
+*/
 }
index 97d0da9582ff1e55d5e5b0c53895885c565eea1a..15deba21b7d09bd7210a43792b28cfecf6e8eb69 100644 (file)
@@ -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 (file)
index 0000000..9801583
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * W1Store.cc
+ *
+ *  Created on: Oct 31, 2010
+ *      Author: lamikr
+ */
+
+#include <list>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#include <time.h>
+
+#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<std::string> 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 (file)
index 0000000..abdc0b6
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * W1Store.hh
+ *
+ *  Created on: Oct 31, 2010
+ *      Author: lamikr
+ */
+
+#ifndef W1STORE_HH_
+#define W1STORE_HH_
+
+#include <string>
+#include <list>
+
+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<std::string> string_list);
+       };
+}
+
+#endif /* W1STORE_HH_ */
index 84db272a83d7aa17a1b9f58a1054f70ac75ec955..b5445c3e58ed309a96065e1b3eb0e92574d7a9af 100644 (file)
@@ -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;
 }
 
index 3a07a0a37ab780f8322b69cc26312edabe639738..e0ceb1d4c4f4eec634d07798d87480c7d8d36f1f 100644 (file)
 
 #include <plp/log.h>
 
+#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<W1Device *>        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<W1Device *>::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) {