]> pilppa.org Git - lib1wire.git/blobdiff - src/W1TemperatureSensor.cc
Initial support for reading log-data.
[lib1wire.git] / src / W1TemperatureSensor.cc
index 38baecbdb51be505a9af2fa479dcfe68c65e27df..bfa72912943e0b9daf942c6d68f55b3b3a6eb8ad 100644 (file)
@@ -5,24 +5,57 @@
  *      Author: lamikr
  */
 #include <fstream>
-#include <vector>
 #include <iostream>
+#include <sstream>
+#include <iomanip>
 
+#include "W1Util.hh"
 #include "W1TemperatureSensor.hh"
 
+#include <plp/log.h>
+
 using namespace std;
 using namespace w1;
 
-W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
+template <class NumberDataType>
+bool string_to_number(NumberDataType& 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_celcius_value_to_three_digits(string raw_value) {
+       string  ret_val;
+       int     int_val;
+       bool    suc_flg;
+       double  dbl_val;
+
+       suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
+       if (suc_flg == 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) {
+       log_debug("trying to open file: %s\n", slave_file.c_str());
        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;
-               cout << text << endl;
-               cout << "verify that you have w1_therm kernel module loaded" << endl;
-       }
-       else {
+               text    = get_time() + ": device type = " + get_devicetype_name() + ", 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");
                ifs.close();
        }
 }
@@ -43,14 +76,14 @@ bool W1TemperatureSensor::is_supported_family_code(int family_code) {
        return ret_val;
 }
 
-string W1TemperatureSensor::get_value() {
-       vector<string>  text_file;
-       string                  temp;
-       string                  ret_val;
-       string                  last_line;
-       int                             pos;
-       int                             length;
-       string                  formatted_data;
+string W1TemperatureSensor::get_raw_value() {
+       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());
@@ -58,21 +91,19 @@ string W1TemperatureSensor::get_value() {
                while(getline(ifs, temp)) {
                        if (temp.length() > 0) {
                                last_line       = temp;
-                               //cout << ret_val << endl;
                        }
                }
                ifs.close();
                length  = last_line.length();
                if (length > 0) {
-                       pos             = last_line.find("t=");
+                       pos     = last_line.find("t=");
                        if ((pos >= 0) &&
                                (pos + 2 < length)) {
                                ret_val = last_line.substr(pos + 2);
                        }
                }
        }
-       formatted_data  = get_formatted_data(ret_val);
-       add_to_memory_cache(formatted_data);
+       ret_val = convert_celcius_value_to_three_digits(ret_val);
        return ret_val;
 }
 
@@ -83,18 +114,3 @@ string W1TemperatureSensor::get_unit() {
 string W1TemperatureSensor::get_devicetype_name() {
        return "Temperature Sensor";
 }
-/*
-void W1TemperatureSensor::printout() {
-       string text;
-
-       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;
-}
-*/