]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
cleanups for log messages.
[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         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_device_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 }
66
67 bool W1TemperatureSensor::is_supported_w1_family_code(int family_code) {
68         bool    ret_val;
69
70         ret_val = false;
71         switch(family_code) {
72                 case 0x10:
73                 case 0x28:
74                         ret_val = true;
75                         break;
76         }
77         return ret_val;
78 }
79
80 vector<double> *W1TemperatureSensor::get_raw_data() {
81         vector<double>  *ret_val;
82         string          tmp_str;
83         string          val_str;
84         double          val_dbl;
85         int             pos;
86         int             b_cnt;
87         int             err_flg;
88
89         ret_val = NULL;
90         err_flg = 0;
91         ifstream ifs(slave_file.c_str());
92         if (ifs.is_open() == true) {
93                 while(getline(ifs, tmp_str)) {
94                         if (tmp_str.empty() == false) {
95                                 val_str = tmp_str;
96                         }
97                 }
98                 ifs.close();
99                 b_cnt   = val_str.length();
100                 if (b_cnt > 0) {
101                         pos     = val_str.find("t=");
102                         if ((pos >= 0) &&
103                             ((pos + 2) < b_cnt)) {
104                                 val_str = val_str.substr(pos + 2);
105                                 val_dbl = convert_w1_temperature_to_celcius(val_str, &err_flg);
106                                 if (err_flg == 0) {
107                                         ret_val = new vector<double>();
108                                         ret_val->push_back(val_dbl);
109                                 }
110                         }
111                 }
112         }
113         return ret_val;
114 }
115
116 string W1TemperatureSensor::get_unit() {
117         return CONST_UNIT_CELCIUS;
118 }
119
120 string W1TemperatureSensor::get_device_type() {
121         return "Temperature Sensor";
122 }
123
124 unsigned int W1TemperatureSensor::get_data_decimal_precision() {
125         return 3;
126 }