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