]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
Initial support for reading and writing device specific config 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         bool    suc_flg;
32         double  dbl_val;
33
34         suc_flg = string_to_number<double>(dbl_val, raw_value, dec);
35         if (suc_flg == true) {
36                 dbl_val = dbl_val / 1000;
37                 std::ostringstream out;
38                 out << fixed << setprecision(3) << dbl_val;
39                 ret_val = out.str();
40         }
41         else {
42                 ret_val = raw_value;
43         }
44         return ret_val;
45 }
46
47 W1TemperatureSensor::W1TemperatureSensor(int family_code_param,
48                                 string device_id_param,
49                                 dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
50         string text;
51
52         log_debug("trying to open file: %s\n", slave_file.c_str());
53         ifstream ifs(slave_file.c_str());
54         if (ifs.is_open() == false) {
55                 text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file + "\n";
56                 log_debug(text.c_str());
57                 log_debug("verify that you have w1_therm kernel module loaded.\n");
58                 ifs.close();
59         }
60 }
61
62 W1TemperatureSensor::~W1TemperatureSensor() {
63 }
64
65 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
66         bool    ret_val;
67
68         ret_val = false;
69         switch(family_code) {
70                 case 0x10:
71                 case 0x28:
72                         ret_val = true;
73                         break;
74         }
75         return ret_val;
76 }
77
78 string W1TemperatureSensor::get_raw_value() {
79         string          temp;
80         string          ret_val;
81         string          last_line;
82         int             pos;
83         int             length;
84         string          format;
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                         }
93                 }
94                 ifs.close();
95                 length  = last_line.length();
96                 if (length > 0) {
97                         pos     = last_line.find("t=");
98                         if ((pos >= 0) &&
99                                 (pos + 2 < length)) {
100                                 ret_val = last_line.substr(pos + 2);
101                         }
102                 }
103         }
104         ret_val = convert_celcius_value_to_three_digits(ret_val);
105         return ret_val;
106 }
107
108 string W1TemperatureSensor::get_unit() {
109         return "C";
110 }
111
112 string W1TemperatureSensor::get_device_type() {
113         return "Temperature Sensor";
114 }