]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
storage data cleanup. allow specifying storage dir location
[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 "W1TemperatureSensor.hh"
13
14 using namespace std;
15 using namespace w1;
16
17 template <class T>
18 bool string_to_number(T& result,
19                  const std::string& string_param,
20                  std::ios_base& (*format)(std::ios_base&))
21 {
22         std::istringstream iss(string_param);
23         return !(iss >> format >> result).fail();
24 }
25
26 string convert_for_3_digits_value(string raw_value) {
27         string  ret_val;
28         int             int_val;
29         bool    sucFlg;
30         double  dbl_val;
31
32         sucFlg          = string_to_number<double>(dbl_val, raw_value, dec);
33         if (sucFlg == true) {
34                 dbl_val = dbl_val / 1000;
35                 std::ostringstream out;
36                 out << fixed << setprecision(3) << dbl_val;
37                 ret_val = out.str();
38         }
39         else {
40                 ret_val = raw_value;
41         }
42         return ret_val;
43 }
44
45 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
46         ifstream ifs(slave_file.c_str());
47         if (ifs.is_open() == false) {
48                 string text;
49
50                 text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file;
51                 cout << text << endl;
52                 cout << "verify that you have w1_therm kernel module loaded." << endl;
53         }
54         else {
55                 ifs.close();
56         }
57 }
58
59 W1TemperatureSensor::~W1TemperatureSensor() {
60 }
61
62 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
63         bool    ret_val;
64
65         ret_val = false;
66         switch(family_code) {
67                 case 0x10:
68                 case 0x28:
69                         ret_val = true;
70                         break;
71         }
72         return ret_val;
73 }
74
75 string W1TemperatureSensor::get_value() {
76         string          temp;
77         string          ret_val;
78         string          last_line;
79         int             pos;
80         int             length;
81         string          formatted_data;
82         int             int_value;
83
84         ret_val = "<could not read>";
85         ifstream ifs(slave_file.c_str());
86         if (ifs.is_open() == true) {
87                 while(getline(ifs, temp)) {
88                         if (temp.length() > 0) {
89                                 last_line       = temp;
90                                 //cout << ret_val << endl;
91                         }
92                 }
93                 ifs.close();
94                 length  = last_line.length();
95                 if (length > 0) {
96                         pos             = last_line.find("t=");
97                         if ((pos >= 0) &&
98                                 (pos + 2 < length)) {
99                                 ret_val = last_line.substr(pos + 2);
100                         }
101                 }
102         }
103         ret_val         = convert_for_3_digits_value(ret_val);
104         return ret_val;
105 }
106
107 string W1TemperatureSensor::get_unit() {
108         return "C";
109 }
110
111 string W1TemperatureSensor::get_devicetype_name() {
112         return "Temperature Sensor";
113 }
114 /*
115 void W1TemperatureSensor::printout() {
116         string text;
117
118         text    = get_formatted_data();
119         cout << text << endl;
120 }
121
122 string W1TemperatureSensor::get_formatted_data() {
123         string ret_val;
124
125         ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit();
126         return ret_val;
127 }
128 */