]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
Support for setting and reading device name from config file.
[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 "W1Store.hh"
18 #include "W1Device.hh"
19
20 using namespace w1;
21 using namespace std;
22
23 W1Device::W1Device(int family_code_param,
24                 string device_id_param,
25                 dirent *direntry_param) {
26         string rootdir;
27         string device_dir;
28         string temp_str;
29
30         rootdir         = W1_SCAN_ROOTDIR;
31         temp_str        = W1_SLAVE_FILE;
32         dir_path        = rootdir + "/" + direntry_param->d_name;
33         slave_file      = dir_path + "/" + temp_str;
34         log_debug("w1 data file: %s\n", slave_file.c_str());
35         family_code     = family_code_param;
36         id              = device_id_param;
37         name            = "";
38 }
39
40 W1Device::~W1Device() {
41         list<Data *>::iterator  iter;
42         Data                    *data;
43
44         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
45                 data    = (Data *)*iter;
46                 delete(data);
47         }
48         memory_cache.clear();
49 }
50
51 int W1Device::get_w1_family_code() {
52         return family_code;
53 }
54
55 string W1Device::get_id() {
56         return id;
57 }
58
59 string W1Device::get_name() {
60         DeviceConfig    *cfg;
61
62         if (name.empty() == true) {
63                 cfg     = Factory::get_device_config(id);
64                 if (cfg != NULL) {
65                         name    = cfg->get_cfg_value("name");
66                         delete(cfg);
67                 }
68         }
69         return name;
70 }
71
72 void W1Device::set_name(string name_param) {
73         DeviceConfig    *cfg;
74
75         name    = name_param;
76         cfg     = Factory::get_device_config(id);
77         if (cfg != NULL) {
78                 cfg->set_cfg_value("name", name_param);
79                 delete(cfg);
80         }
81 }
82
83 void W1Device::printout() {
84         Data    *data;
85         string  text;
86
87         data    = get_and_collect_data();
88         if (data != NULL) {
89                 text    = data->to_string();
90                 cout << text << endl;
91         }
92         else {
93                 log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_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
107 Data *W1Device::get_and_collect_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         W1Store::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 }