]> pilppa.org Git - lib1wire.git/blob - src/W1CounterDevice.cc
82a0beda2a00e5212ee5c1a59cdc416e49b4b0cc
[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 <plp/log.h>
16
17 #include "W1CounterDevice.hh"
18
19 using namespace std;
20 using namespace w1;
21
22 template <class NumberDataType>
23 bool string_to_number(NumberDataType& result,
24                  const std::string& string_param,
25                  std::ios_base& (*format)(std::ios_base&))
26 {
27         std::istringstream iss(string_param);
28         return !(iss >> format >> result).fail();
29 }
30
31 W1CounterDevice::W1CounterDevice(int family_code_param,
32                                 string device_id_param,
33                                 dirent *direntry_param): W1Device(family_code_param, device_id_param, direntry_param) {
34         string text;
35
36         ifstream ifs(slave_file.c_str());
37         if (ifs.is_open() == false) {
38                 log_error("%s: %s failed to read data from file: %s\n", id.c_str(), get_device_type().c_str(), slave_file.c_str());
39                 log_error("Verify that you have w1_ds2423 kernel module loaded.\n");
40                 ifs.close();
41         }
42 }
43
44 W1CounterDevice::~W1CounterDevice() {
45         // TODO Auto-generated destructor stub
46 }
47
48 bool W1CounterDevice::is_supported_w1_family_code(int family_code) {
49         bool    ret_val;
50
51         ret_val = false;
52         switch(family_code) {
53                 case 0x1d:
54                         ret_val = true;
55                         break;
56         }
57         return ret_val;
58 }
59
60 vector<double> *W1CounterDevice::get_raw_data() {
61         int             pos;
62         int             b_cnt;
63         string          val_str;
64         int             val_dbl;
65         vector<double>  *ret_val;
66
67         ret_val = NULL;
68         ifstream ifs(slave_file.c_str());
69         if (ifs.is_open() == true) {
70                 while(getline(ifs, val_str)) {
71                         b_cnt   = val_str.length();
72                         if (b_cnt > 0) {
73                                 pos     = val_str.find("crc=YES c=");
74                                 if ((pos >= 0) &&
75                                     ((pos + 10) < b_cnt)) {
76                                         if (ret_val == NULL) {
77                                                 ret_val = new vector<double>();
78                                         }
79                                         val_str = val_str.substr(pos + 10);
80                                         string_to_number<int>(val_dbl, val_str, dec);
81                                         ret_val->push_back(val_dbl);
82                                 }
83                         }
84                 }
85                 ifs.close();
86         }
87         return ret_val;
88 }
89
90 string W1CounterDevice::get_unit() {
91         return "";
92 }
93
94 string W1CounterDevice::get_device_type() {
95         return "Counter Device";
96 }
97
98 unsigned int W1CounterDevice::get_data_decimal_precision() {
99         return 0;
100 }