]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
add support for storing data to text file periodically
[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
11 #include "W1TemperatureSensor.hh"
12
13 using namespace std;
14 using namespace w1;
15
16 W1TemperatureSensor::W1TemperatureSensor(dirent *direntry, int family_code_param, string id_param): W1Device(direntry, family_code_param, id_param) {
17         ifstream ifs(slave_file.c_str());
18         if (ifs.is_open() == false) {
19                 string text;
20
21                 text    = get_time() + ": device type = temperature sensor, id = " + id + ", could not read file: " + slave_file;
22                 cout << text << endl;
23                 cout << "verify that you have w1_therm kernel module loaded" << endl;
24         }
25         else {
26                 ifs.close();
27         }
28 }
29
30 W1TemperatureSensor::~W1TemperatureSensor() {
31 }
32
33 bool W1TemperatureSensor::is_supported_family_code(int family_code) {
34         bool    ret_val;
35
36         ret_val = false;
37         switch(family_code) {
38                 case 0x10:
39                 case 0x28:
40                         ret_val = true;
41                         break;
42         }
43         return ret_val;
44 }
45
46 string W1TemperatureSensor::get_value() {
47         vector<string>  text_file;
48         string                  temp;
49         string                  ret_val;
50         string                  last_line;
51         int                             pos;
52         int                             length;
53         string                  formatted_data;
54
55         ret_val = "<could not read>";
56         ifstream ifs(slave_file.c_str());
57         if (ifs.is_open() == true) {
58                 while(getline(ifs, temp)) {
59                         if (temp.length() > 0) {
60                                 last_line       = temp;
61                                 //cout << ret_val << endl;
62                         }
63                 }
64                 ifs.close();
65                 length  = last_line.length();
66                 if (length > 0) {
67                         pos             = last_line.find("t=");
68                         if ((pos >= 0) &&
69                                 (pos + 2 < length)) {
70                                 ret_val = last_line.substr(pos + 2);
71                         }
72                 }
73         }
74         formatted_data  = get_formatted_data(ret_val);
75         add_to_memory_cache(formatted_data);
76         return ret_val;
77 }
78
79 string W1TemperatureSensor::get_unit() {
80         return "C";
81 }
82
83 string W1TemperatureSensor::get_devicetype_name() {
84         return "Temperature Sensor";
85 }
86 /*
87 void W1TemperatureSensor::printout() {
88         string text;
89
90         text    = get_formatted_data();
91         cout << text << endl;
92 }
93
94 string W1TemperatureSensor::get_formatted_data() {
95         string ret_val;
96
97         ret_val = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value() + " " + get_unit();
98         return ret_val;
99 }
100 */