]> pilppa.org Git - lib1wire.git/blob - src/W1TemperatureSensor.cc
initial version
[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
54         ret_val = "<could not read>";
55         ifstream ifs(slave_file.c_str());
56         if (ifs.is_open() == true) {
57                 while(getline(ifs, temp)) {
58                         if (temp.length() > 0) {
59                                 last_line       = temp;
60                                 //cout << ret_val << endl;
61                         }
62                 }
63                 ifs.close();
64                 length  = last_line.length();
65                 if (length > 0) {
66                         pos             = last_line.find("t=");
67                         if ((pos >= 0) &&
68                                 (pos + 2 < length)) {
69                                 ret_val = last_line.substr(pos + 2);
70                         }
71                 }
72         }
73         return ret_val;
74 }
75
76 string W1TemperatureSensor::get_unit() {
77         return "C";
78 }
79
80 void W1TemperatureSensor::printout() {
81         string text;
82
83         text    = get_time() + ": device type = temperature sensor, id = " + id + ", value = " + get_value();
84         cout << text << endl;
85 }