]> pilppa.org Git - lib1wire.git/blobdiff - src/W1TemperatureSensor.cc
configuration data related changes
[lib1wire.git] / src / W1TemperatureSensor.cc
index 84db272a83d7aa17a1b9f58a1054f70ac75ec955..5f954cfaa1403923fd58f639128510a2288ee33b 100644 (file)
@@ -9,13 +9,17 @@
 #include <sstream>
 #include <iomanip>
 
+#include <plp/log.h>
+
 #include "W1TemperatureSensor.hh"
 
 using namespace std;
 using namespace w1;
 
-template <class T>
-bool string_to_number(T& result,
+#define CONST_UNIT_CELCIUS     "C"
+
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
                  const std::string& string_param,
                  std::ios_base& (*format)(std::ios_base&))
 {
@@ -23,108 +27,84 @@ bool string_to_number(T& result,
        return !(iss >> format >> result).fail();
 }
 
-string convert_for_3_digits_value(string raw_value) {
-       string  ret_val;
-       int             int_val;
-       bool    sucFlg;
+double convert_w1_temperature_to_celcius(string raw_value,
+                               int *err_flg) {
+       bool    suc_flg;
        double  dbl_val;
 
-       sucFlg          = string_to_number<double>(dbl_val, raw_value, dec);
-       if (sucFlg == true) {
-               dbl_val = dbl_val / 1000;
+       dbl_val = 0;
+       suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
+       if (suc_flg == true) {
+               dbl_val         = dbl_val / 1000;
+               *err_flg        = 0;
+/*
                std::ostringstream out;
                out << fixed << setprecision(3) << dbl_val;
                ret_val = out.str();
+*/
        }
        else {
-               ret_val = raw_value;
+               log_error("Failed to convert temperature %s to celcius value.", raw_value.c_str());
+               *err_flg        = 1;
        }
-       return ret_val;
+       return dbl_val;
 }
 
-W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
+W1TemperatureSensor::W1TemperatureSensor(string device_id_param,
+               string device_type_param,
+               dirent *direntry_param): W1Device(device_id_param, device_type_param, direntry_param) {
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == false) {
-               string text;
-
-               text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
-               cout << text << endl;
-               cout << "verify that you have w1_therm kernel module loaded." << endl;
-       }
-       else {
+               log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_type().c_str(), slave_file.c_str());
+               log_error("Verify that you have w1_therm kernel module loaded.\n");
                ifs.close();
        }
 }
 
 W1TemperatureSensor::~W1TemperatureSensor() {
+       log_debug("destructor\n");
 }
 
-bool W1TemperatureSensor::is_supported_family_code(int family_code) {
-       bool    ret_val;
-
-       ret_val = false;
-       switch(family_code) {
-               case 0x10:
-               case 0x28:
-                       ret_val = true;
-                       break;
-       }
-       return ret_val;
-}
-
-string W1TemperatureSensor::get_value() {
-       string          temp;
-       string          ret_val;
-       string          last_line;
+vector<double> *W1TemperatureSensor::get_raw_data() {
+       vector<double>  *ret_val;
+       string          tmp_str;
+       string          val_str;
+       double          val_dbl;
        int             pos;
-       int             length;
-       string          formatted_data;
-       int             int_value;
+       int             b_cnt;
+       int             err_flg;
 
-       ret_val = "<could not read>";
+       ret_val = NULL;
+       err_flg = 0;
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == true) {
-               while(getline(ifs, temp)) {
-                       if (temp.length() > 0) {
-                               last_line       = temp;
-                               //cout << ret_val << endl;
+               while(getline(ifs, tmp_str)) {
+                       if (tmp_str.empty() == false) {
+                               val_str = tmp_str;
                        }
                }
                ifs.close();
-               length  = last_line.length();
-               if (length > 0) {
-                       pos             = last_line.find("t=");
+               b_cnt   = val_str.length();
+               if (b_cnt > 0) {
+                       pos     = val_str.find("t=");
                        if ((pos >= 0) &&
-                               (pos + 2 < length)) {
-                               ret_val = last_line.substr(pos + 2);
+                           ((pos + 2) < b_cnt)) {
+                               val_str = val_str.substr(pos + 2);
+                               val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg);
+                               if (err_flg == 0) {
+                                       ret_val = new vector<double>();
+                                       ret_val->push_back(val_dbl);
+                               }
                        }
                }
        }
-       ret_val                 = convert_for_3_digits_value(ret_val);
-       formatted_data  = get_formatted_data(ret_val);
-       add_to_memory_cache(formatted_data);
        return ret_val;
 }
 
 string W1TemperatureSensor::get_unit() {
-       return "C";
-}
-
-string W1TemperatureSensor::get_devicetype_name() {
-       return "Temperature Sensor";
-}
-/*
-void W1TemperatureSensor::printout() {
-       string text;
-
-       text    = get_formatted_data();
-       cout << text << endl;
+       return CONST_UNIT_CELCIUS;
 }
 
-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;
+unsigned int W1TemperatureSensor::get_data_decimal_precision() {
+       return 3;
 }
-*/