]> pilppa.org Git - lib1wire.git/blob - src/W1CounterDevice.cc
Refactoring and fixes.
[lib1wire.git] / src / W1CounterDevice.cc
1 /*
2  * W1CounterDevice.cc
3  *
4  *  Created on: Oct 30, 2010
5  *      Author: lamikr
6  */
7
8 #include <iostream>
9 #include <fstream>
10 #include <sstream>
11 #include <iomanip>
12
13 #include <vector>
14
15 #include "W1CounterDevice.hh"
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 W1CounterDevice::W1CounterDevice(int family_code_param,
30                                 string device_id_param,
31                                 dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
32         string text;
33
34         ifstream ifs(slave_file.c_str());
35         if (ifs.is_open() == true) {
36                 text    = get_time() + ": device type = " + get_device_type() + ", id = " + id + ", could not read file: " + slave_file;
37                 cout << text << endl;
38                 cout << "verify that you have w1_ds2423 kernel module loaded." << endl;
39                 ifs.close();
40         }
41 }
42
43 W1CounterDevice::~W1CounterDevice() {
44         // TODO Auto-generated destructor stub
45 }
46
47 bool W1CounterDevice::is_supported_w1_family_code(int family_code) {
48         bool    ret_val;
49
50         ret_val = false;
51         switch(family_code) {
52                 case 0x1d:
53                         ret_val = true;
54                         break;
55         }
56         return ret_val;
57 }
58
59 vector<double> *W1CounterDevice::get_raw_data() {
60         int             pos;
61         int             b_cnt;
62         string          val_str;
63         int             val_dbl;
64         vector<double>  *ret_val;
65
66         ret_val = NULL;
67         ifstream ifs(slave_file.c_str());
68         if (ifs.is_open() == true) {
69                 while(getline(ifs, val_str)) {
70                         b_cnt   = val_str.length();
71                         if (b_cnt > 0) {
72                                 pos     = val_str.find("crc=YES c=");
73                                 if ((pos >= 0) &&
74                                     ((pos + 10) < b_cnt)) {
75                                         if (ret_val == NULL) {
76                                                 ret_val = new vector<double>();
77                                         }
78                                         val_str = val_str.substr(pos + 10);
79                                         string_to_number<int>(val_dbl, val_str, dec);
80                                         ret_val->push_back(val_dbl);
81                                 }
82                         }
83                 }
84                 ifs.close();
85         }
86         return ret_val;
87 }
88
89 string W1CounterDevice::get_unit() {
90         return "";
91 }
92
93 string W1CounterDevice::get_device_type() {
94         return "Counter Device";
95 }