From: Mika Laitio Date: Sun, 2 Jan 2011 22:20:41 +0000 (+0200) Subject: Bug fix for counter device data saving. X-Git-Url: http://pilppa.org/gitweb/?p=lib1wire.git;a=commitdiff_plain;h=d58713adbc518552c11fd19bd21ae8bd11ab434f Bug fix for counter device data saving. Signed-off-by: Mika Laitio --- diff --git a/src/Data.cc b/src/Data.cc index e81039b..cc29d51 100644 --- a/src/Data.cc +++ b/src/Data.cc @@ -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(); diff --git a/src/Data.hh b/src/Data.hh index 196cf3b..90b0e47 100644 --- a/src/Data.hh +++ b/src/Data.hh @@ -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(); diff --git a/src/W1CounterDevice.cc b/src/W1CounterDevice.cc index 3ebdbd4..82a0bed 100644 --- a/src/W1CounterDevice.cc +++ b/src/W1CounterDevice.cc @@ -12,6 +12,8 @@ #include +#include + #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; +} diff --git a/src/W1CounterDevice.hh b/src/W1CounterDevice.hh index c642eca..17c0728 100644 --- a/src/W1CounterDevice.hh +++ b/src/W1CounterDevice.hh @@ -23,6 +23,7 @@ namespace w1 { std::string get_device_type(); protected: std::vector *get_raw_data(); + unsigned int get_data_decimal_precision(); bool is_supported_w1_family_code(int family_code); }; } diff --git a/src/W1Device.cc b/src/W1Device.cc index 7bb4418..a765cd3 100644 --- a/src/W1Device.cc +++ b/src/W1Device.cc @@ -36,6 +36,14 @@ W1Device::W1Device(int family_code_param, } W1Device::~W1Device() { + list::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::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); diff --git a/src/W1Device.hh b/src/W1Device.hh index a702e0f..321159a 100644 --- a/src/W1Device.hh +++ b/src/W1Device.hh @@ -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 *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); diff --git a/src/W1Store.cc b/src/W1Store.cc index 4f35376..83e1615 100644 --- a/src/W1Store.cc +++ b/src/W1Store.cc @@ -74,7 +74,8 @@ string W1Store::get_file_name(string device_id, Date *date_time) { } void W1Store::save(string device_id, - std::list *data_list) { + std::list *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; diff --git a/src/W1Store.hh b/src/W1Store.hh index b0726a4..5f3dff9 100644 --- a/src/W1Store.hh +++ b/src/W1Store.hh @@ -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_list); + static void save(std::string device_id, std::list *data_list, int dec_precision); bool load(); Data *get_sum(); Data *get_delta(); diff --git a/src/W1TemperatureSensor.cc b/src/W1TemperatureSensor.cc index e7930ff..e738bfc 100644 --- a/src/W1TemperatureSensor.cc +++ b/src/W1TemperatureSensor.cc @@ -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; +} diff --git a/src/W1TemperatureSensor.hh b/src/W1TemperatureSensor.hh index 15989d5..8c06af1 100644 --- a/src/W1TemperatureSensor.hh +++ b/src/W1TemperatureSensor.hh @@ -23,6 +23,7 @@ namespace w1 { std::string get_device_type(); protected: std::vector *get_raw_data(); + unsigned int get_data_decimal_precision(); bool is_supported_w1_family_code(int family_code); }; }