]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
Refactoring and fixes.
[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 "W1Util.hh"
13 #include "W1TemperatureSensor.hh"
14
15 #include <plp/log.h>
16
17 using namespace std;
18 using namespace w1;
19
20 #define CONST_UNIT_CELCIUS      "C"
21
22 template <class NumberDataType>
23 bool string_to_number(NumberDataType& result,
24                  const std::string& string_param,
25                  std::ios_base& (*format)(std::ios_base&))
26 {
27         std::istringstream iss(string_param);
28         return !(iss >> format >> result).fail();
29 }
30
31 double convert_w1_temperature_to_celcius(string raw_value, 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(int family_code_param,
54                                 string device_id_param,
55                                 dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
56         string  text;
57
58         log_debug("trying to open file: %s\n", slave_file.c_str());
59         ifstream ifs(slave_file.c_str());
60         if (ifs.is_open() == false) {
61                 text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file + "\n";
62                 log_debug(text.c_str());
63                 log_debug("verify that you have w1_therm kernel module loaded.\n");
64                 ifs.close();
65         }
66 }
67
68 W1TemperatureSensor::~W1TemperatureSensor() {
69 }
70
71 bool W1TemperatureSensor::is_supported_w1_family_code(int family_code) {
72         bool    ret_val;
73
74         ret_val = false;
75         switch(family_code) {
76                 case 0x10:
77                 case 0x28:
78                         ret_val = true;
79                         break;
80         }
81         return ret_val;
82 }
83
84 vector<double> *W1TemperatureSensor::get_raw_data() {
85         vector<double>  *ret_val;
86         string          tmp_str;
87         string          val_str;
88         double          val_dbl;
89         int             pos;
90         int             b_cnt;
91         int             err_flg;
92
93         ret_val = NULL;
94         err_flg = 0;
95         ifstream ifs(slave_file.c_str());
96         if (ifs.is_open() == true) {
97                 while(getline(ifs, tmp_str)) {
98                         if (tmp_str.empty() == false) {
99                                 val_str = tmp_str;
100                         }
101                 }
102                 ifs.close();
103                 b_cnt   = val_str.length();
104                 if (b_cnt > 0) {
105                         pos     = val_str.find("t=");
106                         if ((pos >= 0) &&
107                             ((pos + 2) < b_cnt)) {
108                                 val_str = val_str.substr(pos + 2);
109                                 val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg);
110                                 if (err_flg == 0) {
111                                         ret_val = new vector<double>();
112                                         ret_val->push_back(val_dbl);
113                                 }
114                         }
115                 }
116         }
117         return ret_val;
118 }
119
120 string W1TemperatureSensor::get_unit() {
121         return CONST_UNIT_CELCIUS;
122 }
123
124 string W1TemperatureSensor::get_device_type() {
125         return "Temperature Sensor";
126 }