]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
Initial support for reading log-data.
[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 template <class NumberDataType>
21 bool string_to_number(NumberDataType& result,
22                  const std::string& string_param,
23                  std::ios_base& (*format)(std::ios_base&))
24 {
25         std::istringstream iss(string_param);
26         return !(iss >> format >> result).fail();
27 }
28
29 string convert_celcius_value_to_three_digits(string raw_value) {
30         string  ret_val;
31         int     int_val;
32         bool    suc_flg;
33         double  dbl_val;
34
35         suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
36         if (suc_flg == true) {
37                 dbl_val = dbl_val / 1000;
38                 std::ostringstream out;
39                 out << fixed << setprecision(3) << dbl_val;
40                 ret_val = out.str();
41         }
42         else {
43                 ret_val = raw_value;
44         }
45         return ret_val;
46 }
47
48 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry,
49                                 int family_code_param,
50                                 string id_param): W1Device(direntry, family_code_param, id_param) {
51         log_debug("trying to open file: %s\n", slave_file.c_str());
52         ifstream ifs(slave_file.c_str());
53         if (ifs.is_open() == false) {
54                 string text;
55
56                 text    = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", could not read file: " + slave_file + "\n";
57                 log_debug(text.c_str());
58                 log_debug("verify that you have w1_therm kernel module loaded.\n");
59                 ifs.close();
60         }
61 }
62
63 W1TemperatureSensor::~W1TemperatureSensor() {
64 }
65
66 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
67         bool    ret_val;
68
69         ret_val = false;
70         switch(family_code) {
71                 case 0x10:
72                 case 0x28:
73                         ret_val = true;
74                         break;
75         }
76         return ret_val;
77 }
78
79 string W1TemperatureSensor::get_raw_value() {
80         string          temp;
81         string          ret_val;
82         string          last_line;
83         int             pos;
84         int             length;
85         string          formatted_data;
86         int             int_value;
87
88         ret_val = "<could not read>";
89         ifstream ifs(slave_file.c_str());
90         if (ifs.is_open() == true) {
91                 while(getline(ifs, temp)) {
92                         if (temp.length() > 0) {
93                                 last_line       = temp;
94                         }
95                 }
96                 ifs.close();
97                 length  = last_line.length();
98                 if (length > 0) {
99                         pos     = last_line.find("t=");
100                         if ((pos >= 0) &&
101                                 (pos + 2 < length)) {
102                                 ret_val = last_line.substr(pos + 2);
103                         }
104                 }
105         }
106         ret_val = convert_celcius_value_to_three_digits(ret_val);
107         return ret_val;
108 }
109
110 string W1TemperatureSensor::get_unit() {
111         return "C";
112 }
113
114 string W1TemperatureSensor::get_devicetype_name() {
115         return "Temperature Sensor";
116 }