]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
Started defining generic device type base classes that could be used
[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(int family_code_param,
25                 string device_id_param,
26                 dirent *direntry_param) {
27         string rootdir;
28         string device_dir;
29         string temp_str;
30
31         rootdir         = W1_SCAN_ROOTDIR;
32         temp_str        = W1_SLAVE_FILE;
33         dir_path        = rootdir + "/" + direntry_param->d_name;
34         slave_file      = dir_path + "/" + temp_str;
35         log_debug("1-wire device's data file: %s\n", slave_file.c_str());
36         family_code     = family_code_param;
37         id              = device_id_param;
38         name            = "";
39 }
40
41 W1Device::~W1Device() {
42         list<Data *>::iterator  iter;
43         Data                    *data;
44
45         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
46                 data    = (Data *)*iter;
47                 delete(data);
48         }
49         memory_cache.clear();
50 }
51
52 int W1Device::get_w1_family_code() {
53         return family_code;
54 }
55
56 string W1Device::get_id() {
57         return id;
58 }
59
60 string W1Device::get_name() {
61         DeviceConfig    *cfg;
62
63         if (name.empty() == true) {
64                 cfg     = Factory::get_device_config(id);
65                 if (cfg != NULL) {
66                         name    = cfg->get_cfg_value("name");
67                         delete(cfg);
68                 }
69         }
70         return name;
71 }
72
73 void W1Device::set_name(string name_param) {
74         DeviceConfig    *cfg;
75
76         name    = name_param;
77         cfg     = Factory::get_device_config(id);
78         if (cfg != NULL) {
79                 cfg->set_cfg_value("name", name_param);
80                 delete(cfg);
81         }
82 }
83
84 void W1Device::printout() {
85         Data    *data;
86         string  text;
87
88         data    = get_data();
89         if (data != NULL) {
90                 text    = data->to_string();
91                 cout << text << endl;
92         }
93         else {
94                 log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_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                 collect_data(ret_val);
116                 delete(vect);
117         }
118         return ret_val;
119 }
120
121 void W1Device::collect_data(Data *data) {
122         // TODO: add mutex for memory_cache
123         memory_cache.push_back(data);
124 }
125
126 void W1Device::save_data() {
127         list<Data *>::iterator  iter;
128         Data                    *data;
129         int                     dec_precision;
130
131         dec_precision   = get_data_decimal_precision();
132         StoreDay::save(id, &memory_cache, dec_precision);
133         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
134                 data    = (Data *)*iter;
135                 delete(data);
136         }
137         memory_cache.clear();
138 }