]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
152125a1d34672817f0910e4b59461c307d09dfd
[lib1wire.git] / src / W1TemperatureSensor.cc
1 /*
2  * W1TemperatureSensor.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <fstream>
8 #include <iostream>
9 #include <sstream>
10 #include <iomanip>
11
12 #include <plp/log.h>
13
14 #include "W1TemperatureSensor.hh"
15
16 using namespace std;
17 using namespace w1;
18
19 #define CONST_UNIT_CELCIUS      "C"
20
21 template <class NumberDataType>
22 bool string_to_number(NumberDataType& result,
23                  const std::string& string_param,
24                  std::ios_base& (*format)(std::ios_base&))
25 {
26         std::istringstream iss(string_param);
27         return !(iss >> format >> result).fail();
28 }
29
30 double convert_w1_temperature_to_celcius(string raw_value,
31                                 int *err_flg) {
32         bool    suc_flg;
33         double  dbl_val;
34
35         dbl_val = 0;
36         suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
37         if (suc_flg == true) {
38                 dbl_val         = dbl_val / 1000;
39                 *err_flg        = 0;
40 /*
41                 std::ostringstream out;
42                 out << fixed << setprecision(3) << dbl_val;
43                 ret_val = out.str();
44 */
45         }
46         else {
47                 log_error("Failed to convert temperature %s to celsius value", raw_value.c_str());
48                 *err_flg        = 1;
49         }
50         return dbl_val;
51 }
52
53 W1TemperatureSensor::W1TemperatureSensor(string device_id_param,
54                 string device_type_param,
55                 dirent *direntry_param): W1Device(device_id_param, device_type_param, direntry_param) {
56         ifstream ifs(slave_file.c_str());
57         if (ifs.is_open() == false) {
58                 log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_type().c_str(), slave_file.c_str());
59                 log_error("Verify that you have w1_therm kernel module loaded.\n");
60                 ifs.close();
61         }
62 }
63
64 W1TemperatureSensor::~W1TemperatureSensor() {
65         log_debug("W1TemperatureSensor destructor\n");
66 }
67
68 vector<double> *W1TemperatureSensor::get_raw_data() {
69         vector<double>  *ret_val;
70         string          tmp_str;
71         string          val_str;
72         double          val_dbl;
73         int             pos;
74         int             b_cnt;
75         int             err_flg;
76
77         ret_val = NULL;
78         err_flg = 0;
79         ifstream ifs(slave_file.c_str());
80         if (ifs.is_open() == true) {
81                 while(getline(ifs, tmp_str)) {
82                         if (tmp_str.empty() == false) {
83                                 val_str = tmp_str;
84                         }
85                 }
86                 ifs.close();
87                 b_cnt   = val_str.length();
88                 if (b_cnt > 0) {
89                         pos     = val_str.find("t=");
90                         if ((pos >= 0) &&
91                             ((pos + 2) < b_cnt)) {
92                                 val_str = val_str.substr(pos + 2);
93                                 val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg);
94                                 if (err_flg == 0) {
95                                         ret_val = new vector<double>();
96                                         ret_val->push_back(val_dbl);
97                                 }
98                         }
99                 }
100         }
101         return ret_val;
102 }
103
104 string W1TemperatureSensor::get_unit() {
105         return CONST_UNIT_CELCIUS;
106 }
107
108 unsigned int W1TemperatureSensor::get_data_decimal_precision() {
109         return 3;
110 }