]> pilppa.org Git - lib1wire.git/blobdiff - src/W1TemperatureSensor.cc
storage data cleanup. allow specifying storage dir location
[lib1wire.git] / src / W1TemperatureSensor.cc
index a2b53375a954a4ed7e542675180cdbe5db1e6330..b5445c3e58ed309a96065e1b3eb0e92574d7a9af 100644 (file)
@@ -5,22 +5,51 @@
  *      Author: lamikr
  */
 #include <fstream>
-#include <vector>
 #include <iostream>
+#include <sstream>
+#include <iomanip>
 
 #include "W1TemperatureSensor.hh"
 
 using namespace std;
 using namespace w1;
 
+template <class T>
+bool string_to_number(T& result,
+                 const std::string& string_param,
+                 std::ios_base& (*format)(std::ios_base&))
+{
+       std::istringstream iss(string_param);
+       return !(iss >> format >> result).fail();
+}
+
+string convert_for_3_digits_value(string raw_value) {
+       string  ret_val;
+       int             int_val;
+       bool    sucFlg;
+       double  dbl_val;
+
+       sucFlg          = string_to_number<double>(dbl_val, raw_value, dec);
+       if (sucFlg == true) {
+               dbl_val = dbl_val / 1000;
+               std::ostringstream out;
+               out << fixed << setprecision(3) << dbl_val;
+               ret_val = out.str();
+       }
+       else {
+               ret_val = raw_value;
+       }
+       return ret_val;
+}
+
 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
        ifstream ifs(slave_file.c_str());
        if (ifs.is_open() == false) {
                string text;
 
-               text    = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file;
+               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;
+               cout << "verify that you have w1_therm kernel module loaded." << endl;
        }
        else {
                ifs.close();
@@ -44,12 +73,13 @@ bool W1TemperatureSensor::is_supported_family_code(int family_code) {
 }
 
 string W1TemperatureSensor::get_value() {
-       vector<string>  text_file;
-       string                  temp;
-       string                  ret_val;
-       string                  last_line;
-       int                             pos;
-       int                             length;
+       string          temp;
+       string          ret_val;
+       string          last_line;
+       int             pos;
+       int             length;
+       string          formatted_data;
+       int             int_value;
 
        ret_val = "<could not read>";
        ifstream ifs(slave_file.c_str());
@@ -70,6 +100,7 @@ string W1TemperatureSensor::get_value() {
                        }
                }
        }
+       ret_val         = convert_for_3_digits_value(ret_val);
        return ret_val;
 }
 
@@ -77,9 +108,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;
+}
+*/