]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
a5f89dfc9f487c17df8e86adb02ec1c24fb1ac96
[lib1wire.git] / src / W1Device.cc
1 /*
2  * W1Device.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 #include <iomanip>
11
12 #include <time.h>
13 #include <plp/log.h>
14
15 #include <plp/DeviceConfig.hh>
16 #include <plp/StoreDay.hh>
17
18 #include "Factory.hh"
19 #include "W1Device.hh"
20
21 using namespace std;
22 using namespace w1;
23 using namespace plp;
24
25 W1Device::W1Device(string device_id_param,
26                 string device_type_param,
27                 dirent *direntry_param) : SensorDevice(device_id_param, device_type_param) {
28         string rootdir;
29         string device_dir;
30         string temp_str;
31
32         type    = device_type_param;
33         id      = device_id_param;
34         if (direntry_param != NULL) {
35                 rootdir                 = W1_SCAN_ROOTDIR;
36                 temp_str                = W1_SLAVE_FILE;
37                 dir_path                = rootdir + "/" + direntry_param->d_name;
38                 slave_file              = dir_path + "/" + temp_str;
39                 lifecycle_status        = LIFECYCLE_STATUS__AVAILABLE;
40                 log_debug("1-wire device's data file: %s\n", slave_file.c_str());
41         }
42         else {
43                 lifecycle_status        = LIFECYCLE_STATUS__UNAVAILABLE;
44         }
45         reader  = new DataReader(id);
46         name    = "";
47 }
48
49 W1Device::~W1Device() {
50         save_and_clean_cache();
51         delete(reader);
52 }
53
54 string W1Device::get_name() {
55         DeviceConfig    *cfg;
56
57         if (name.empty() == true) {
58                 cfg     = DeviceConfig::get_device_config(id);
59                 if (cfg != NULL) {
60                         name    = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
61                         delete(cfg);
62                 }
63         }
64         return name;
65 }
66
67 void W1Device::set_name(string name_param) {
68         DeviceConfig    *cfg;
69
70         if (name.compare(name_param) != 0) {
71                 name    = name_param;
72                 cfg     = DeviceConfig::get_device_config(id);
73                 if (cfg != NULL) {
74                         cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
75                         delete(cfg);
76                 }
77         }
78 }
79
80 void W1Device::printout() {
81         Data    *data;
82         string  text;
83         string  type;
84         string  name;
85
86         data    = get_data();
87         if (data != NULL) {
88                 text    = data->to_string();
89                 cout << text << endl;
90         }
91         else {
92                 type    = get_type();
93                 name    = get_name();
94                 log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
95         }
96 }
97
98 string W1Device::to_string(double dbl_val, int digit_count) {
99         string          ret_val;
100         ostringstream   out;
101
102         out << fixed << setprecision(digit_count) << dbl_val;
103         ret_val = out.str();
104         return ret_val;
105 }
106
107 Data *W1Device::get_data() {
108         Data            *ret_val;
109         vector<double>  *vect;
110
111         ret_val = NULL;
112         vect    = get_raw_data();
113         if (vect != NULL) {
114                 ret_val = new Data(vect, get_unit());
115                 cache(ret_val);
116                 delete(vect);
117         }
118         else {
119                 // read old data already saved
120                 ret_val = reader->get_latest_data();
121         }
122         return ret_val;
123 }
124
125 void W1Device::cache(Data *data) {
126         // TODO: add mutex for memory_cache
127         memory_cache.push_back(data);
128         if (memory_cache.size() > 5) {
129                 save_and_clean_cache();
130         }
131 }
132
133 void W1Device::save_and_clean_cache() {
134         list<Data *>::iterator  iter;
135         Data                    *data;
136         int                     dec_precision;
137
138         dec_precision   = get_data_decimal_precision();
139         StoreDay::save(id, &memory_cache, dec_precision);
140         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
141                 data    = (Data *)*iter;
142                 delete(data);
143         }
144         memory_cache.clear();
145 }
146
147 DataReader *W1Device::get_device_data() {
148         return reader;
149 }