]> pilppa.com Git - lib1wire.git/commitdiff
Bug fix for counter device data saving.
authorMika Laitio <lamikr@pilppa.org>
Sun, 2 Jan 2011 22:20:41 +0000 (00:20 +0200)
committerMika Laitio <lamikr@pilppa.org>
Sun, 2 Jan 2011 22:20:41 +0000 (00:20 +0200)
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/Data.cc
src/Data.hh
src/W1CounterDevice.cc
src/W1CounterDevice.hh
src/W1Device.cc
src/W1Device.hh
src/W1Store.cc
src/W1Store.hh
src/W1TemperatureSensor.cc
src/W1TemperatureSensor.hh

index e81039b13bc039f040d0f0f6f9d21a29aff43a5f..cc29d51cd733aeebbfa2f356661a9fd6ab9add4b 100644 (file)
@@ -172,7 +172,7 @@ Data *Data::parse_string(const string& dataline) {
        return ret_val;
 }
 
-string Data::to_string() {
+string Data::to_string(int dec_precision) {
        unsigned int    ii;
        ostringstream   out;
        string          ret_val;
@@ -180,10 +180,9 @@ string Data::to_string() {
        ret_val = date_time.to_string();
        if (value_arr.size() > 0) {
                for (ii = 0; ii < value_arr.size(); ii++) {
-                       out << fixed << setprecision(3) << value_arr[ii];
-                       ret_val.append("|");
-                       ret_val.append(out.str());
+                       out << "|" << fixed << setprecision(dec_precision) << value_arr[ii];
                }
+               ret_val.append(out.str());
                if (unit.empty() == false) {
                        ret_val.append(" ");
                        ret_val.append(unit.c_str());
@@ -192,6 +191,10 @@ string Data::to_string() {
        return ret_val;
 }
 
+string Data::to_string() {
+       return to_string(3);
+}
+
 DataRange::DataRange(Data *data) {
        val_matrix      = NULL;
        column_count    = data->value_arr.size();
index 196cf3bbd3f096eaaba0133193de084cb9457e7c..90b0e47ad8b046688f0a020577e7de8df5818911 100644 (file)
@@ -26,6 +26,7 @@ namespace w1 {
                        virtual ~Data();
                        Data *clone();
                        void printout();
+                       std::string to_string(int dec_precision);
                        std::string to_string();
                        static Data *parse_string(const std::string& data_str);
                        plp::Date get_date();
index 3ebdbd47c50ae61404d5f5e15b74e5809d79321c..82a0beda2a00e5212ee5c1a59cdc416e49b4b0cc 100644 (file)
@@ -12,6 +12,8 @@
 
 #include <vector>
 
+#include <plp/log.h>
+
 #include "W1CounterDevice.hh"
 
 using namespace std;
@@ -32,10 +34,9 @@ W1CounterDevice::W1CounterDevice(int family_code_param,
        string text;
 
        ifstream ifs(slave_file.c_str());
-       if (ifs.is_open() == true) {
-               text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file;
-               cout << text << endl;
-               cout << "verify that you have w1_ds2423 kernel module loaded." << endl;
+       if (ifs.is_open() == false) {
+               log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_device_type().c_str(), slave_file.c_str());
+               log_error("Verify that you have w1_ds2423 kernel module loaded.\n");
                ifs.close();
        }
 }
@@ -93,3 +94,7 @@ string W1CounterDevice::get_unit() {
 string W1CounterDevice::get_device_type() {
        return "Counter Device";
 }
+
+unsigned int W1CounterDevice::get_data_decimal_precision() {
+       return 0;
+}
index c642ecac5748d7f3814e7e37e4cffe5fb9f53a05..17c07286866241767103bfd503533dd268fa4016 100644 (file)
@@ -23,6 +23,7 @@ namespace w1 {
                        std::string get_device_type();
                protected:
                        std::vector<double> *get_raw_data();
+                       unsigned int get_data_decimal_precision();
                        bool is_supported_w1_family_code(int family_code);
        };
 }
index 7bb44183c489287c846d95487dbafa44abe25773..a765cd36a6091432b4d2577ae6828efc3f36694b 100644 (file)
@@ -36,6 +36,14 @@ W1Device::W1Device(int family_code_param,
 }
 
 W1Device::~W1Device() {
+       list<Data *>::iterator  iter;
+       Data                    *data;
+
+       for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
+               data    = (Data *)*iter;
+               delete(data);
+       }
+       memory_cache.clear();
 }
 
 int W1Device::get_w1_family_code() {
@@ -54,19 +62,6 @@ void W1Device::set_name(string name_param) {
        name    = name_param;
 }
 
-string W1Device::get_time() {
-       time_t          wtime;
-       struct tm       *ltime;
-       char            buffer[80];
-       string          ret_val;
-
-       time(&wtime);
-       ltime   = localtime(&wtime);
-       strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
-       ret_val = buffer;
-       return ret_val;
-}
-
 void W1Device::printout() {
        Data    *data;
        string  text;
@@ -105,16 +100,6 @@ Data *W1Device::get_and_collect_data() {
        return ret_val;
 }
 
-/*
-Data *W1Device::get_formatted_data(Data *data) {
-       Data    *ret_val;
-
-       ret_val = get_time() + "|" + raw_data + " " + get_unit();
-       add_to_save_fifo(ret_val);
-       return ret_val;
-}
-*/
-
 void W1Device::collect_data(Data *data) {
        // TODO: add mutex for memory_cache
        memory_cache.push_back(data);
@@ -123,8 +108,10 @@ void W1Device::collect_data(Data *data) {
 void W1Device::save_data() {
        list<Data *>::iterator  iter;
        Data                    *data;
+       int                     dec_precision;
 
-       W1Store::save(id, &memory_cache);
+       dec_precision   = get_data_decimal_precision();
+       W1Store::save(id, &memory_cache, dec_precision);
        for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
                data    = (Data *)*iter;
                delete(data);
index a702e0f728473166fa5550dc200fa4b41dad03fd..321159abecd682afd1a0501ff99921dd98df98a0 100644 (file)
@@ -37,12 +37,12 @@ namespace w1 {
                        void set_name(std::string name_param);
                        virtual std::string get_unit() = 0;
                        virtual std::string get_device_type() = 0;
-                       std::string get_time();
                        Data *get_and_collect_data();
                        virtual void save_data();
                        virtual void printout();
                protected:
                        virtual std::vector<double> *get_raw_data() = 0;
+                       virtual unsigned int get_data_decimal_precision() = 0;
                        void collect_data(Data *data);
                        std::string to_string(double val, int digit_count);
                        //Data *get_formatted_data(Data *data);
index 4f35376762bd72758a4f2a3d285b6d28ae859a16..83e16150fdb388565f8c5c77a3b53647210eeb88 100644 (file)
@@ -74,7 +74,8 @@ string W1Store::get_file_name(string device_id, Date *date_time) {
 }
 
 void W1Store::save(string device_id,
-               std::list<Data *> *data_list) {
+               std::list<Data *> *data_list,
+               int dec_precision) {
        string                  n_path;
        string                  f_path;
        string                  line;
@@ -102,7 +103,7 @@ void W1Store::save(string device_id,
                }
                if ((ostream != NULL) &&
                    (ostream->is_open() == true)) {
-                       line    = data->to_string();
+                       line    = data->to_string(dec_precision);
                        if (line.length() > 0) {
                                log_debug("storing line: %s\n", line.c_str());
                                *ostream << line << endl;
index b0726a4573dddb118c4e5a84c1568aae2ccf5152..5f3dff94888e8d279d66e1e95de62ede509fa935 100644 (file)
@@ -26,7 +26,7 @@ namespace w1 {
                        virtual ~W1Store();
                        static std::string get_dir_name(std::string device_id, plp::Date *ltime);
                        static std::string get_file_name(std::string device_id, plp::Date *ltime);
-                       static void save(std::string device_id, std::list<Data *> *data_list);
+                       static void save(std::string device_id, std::list<Data *> *data_list, int dec_precision);
                        bool load();
                        Data *get_sum();
                        Data *get_delta();
index e7930ff6333a8668740373d9757748286f8c336f..e738bfc34cb905f652a7d28e96c4dea90ae308bf 100644 (file)
@@ -53,14 +53,12 @@ double convert_w1_temperature_to_celcius(string raw_value, int *err_flg) {
 W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
                                string device_id_param,
                                dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
-       string  text;
-
        log_debug("trying to open file: %s\n", slave_file.c_str());
+
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == false) {
-               text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file + "\n";
-               log_debug(text.c_str());
-               log_debug("verify that you have w1_therm kernel module loaded.\n");
+               log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_device_type().c_str(), slave_file.c_str());
+               log_error("Verify that you have w1_therm kernel module loaded.\n");
                ifs.close();
        }
 }
@@ -124,3 +122,7 @@ string W1TemperatureSensor::get_unit() {
 string W1TemperatureSensor::get_device_type() {
        return "Temperature Sensor";
 }
+
+unsigned int W1TemperatureSensor::get_data_decimal_precision() {
+       return 3;
+}
index 15989d5b46d8754572ed7cdf75d5f5bdd896e745..8c06af1acfb4b9bc492eeb1f1718c1b157392599 100644 (file)
@@ -23,6 +23,7 @@ namespace w1 {
                        std::string get_device_type();
                protected:
                        std::vector<double> *get_raw_data();
+                       unsigned int get_data_decimal_precision();
                        bool is_supported_w1_family_code(int family_code);
        };
 }