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