]> pilppa.org Git - lib1wire.git/commitdiff
add support for storing data to text file periodically
authorMika Laitio <lamikr@pilppa.org>
Sat, 23 Oct 2010 08:51:26 +0000 (11:51 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sat, 23 Oct 2010 08:51:26 +0000 (11:51 +0300)
src/W1Device.cc
src/W1Device.hh
src/W1TemperatureSensor.cc
src/W1TemperatureSensor.hh
src_test/test_w1.cc

index 48f366db655a1d74bdf92d717b629a03e7e8c168..3160c81034e6e502b8526a5890c00226d5ebf2e7 100644 (file)
@@ -5,6 +5,8 @@
  *      Author: lamikr
  */
 #include <iostream>
+#include <fstream>
+
 #include <time.h>
 
 #include "W1Device.hh"
@@ -61,6 +63,48 @@ string W1Device::get_time() {
 void W1Device::printout() {
        string text;
 
-       text    = get_time() + ": device type = <unknown>, 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;
+       }
+}
index 190e36ea81b6f5e8842c728843a0ed10cd262e52..97d0da9582ff1e55d5e5b0c53895885c565eea1a 100644 (file)
@@ -8,9 +8,11 @@
 #ifndef W1DEVICE_HH_
 #define W1DEVICE_HH_
 
+#include <string>
+#include <list>
+
 #include <dirent.h>
 #include <stdbool.h>
-#include <string>
 
 #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<std::string> memory_cache;
                private:
        };
 }
index a2b53375a954a4ed7e542675180cdbe5db1e6330..38baecbdb51be505a9af2fa479dcfe68c65e27df 100644 (file)
@@ -50,6 +50,7 @@ string W1TemperatureSensor::get_value() {
        string                  last_line;
        int                             pos;
        int                             length;
+       string                  formatted_data;
 
        ret_val = "<could not read>";
        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;
+}
+*/
index f08ef4b5fe064f66376f5cf30d73006c11c1ee3c..bef91c9907a68dc0d5611453ac097dc44c4e73da 100644 (file)
@@ -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);
        };
 }
index e8f6e2feea85028bf234476857bb56901f737b6d..4b94a612784c7582fc3b5cabaeaccd42d5658d38 100644 (file)
@@ -19,10 +19,17 @@ int main(int argc, char** argv)
 {
        W1Scanner                       *scanner;
        list<W1Device *>        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<W1Device *>::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;
 }