]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
Bug fix for counter device data saving.
[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 "W1Store.hh"
16 #include "W1Device.hh"
17
18 using namespace w1;
19 using namespace std;
20
21 W1Device::W1Device(int family_code_param,
22                 string device_id_param,
23                 dirent *direntry_param) {
24         string rootdir;
25         string device_dir;
26         string temp_str;
27
28         rootdir         = W1_SCAN_ROOTDIR;
29         temp_str        = W1_SLAVE_FILE;
30         dir_path        = rootdir + "/" + direntry_param->d_name;
31         slave_file      = dir_path + "/" + temp_str;
32         log_debug("w1 data file: %s\n", slave_file.c_str());
33         family_code     = family_code_param;
34         id              = device_id_param;
35         name            = device_id_param;
36 }
37
38 W1Device::~W1Device() {
39         list<Data *>::iterator  iter;
40         Data                    *data;
41
42         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
43                 data    = (Data *)*iter;
44                 delete(data);
45         }
46         memory_cache.clear();
47 }
48
49 int W1Device::get_w1_family_code() {
50         return family_code;
51 }
52
53 string W1Device::get_id() {
54         return id;
55 }
56
57 string W1Device::get_name() {
58         return name;
59 }
60
61 void W1Device::set_name(string name_param) {
62         name    = name_param;
63 }
64
65 void W1Device::printout() {
66         Data    *data;
67         string  text;
68
69         data    = get_and_collect_data();
70         if (data != NULL) {
71                 text    = data->to_string();
72                 cout << text << endl;
73         }
74         else {
75                 log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_name().c_str());
76         }
77 }
78
79 string W1Device::to_string(double dbl_val, int digit_count) {
80         string          ret_val;
81         ostringstream   out;
82
83         out << fixed << setprecision(digit_count) << dbl_val;
84         ret_val = out.str();
85         return ret_val;
86 }
87
88
89 Data *W1Device::get_and_collect_data() {
90         Data            *ret_val;
91         vector<double>  *vect;
92
93         ret_val = NULL;
94         vect    = get_raw_data();
95         if (vect != NULL) {
96                 ret_val = new Data(vect, get_unit());
97                 collect_data(ret_val);
98                 delete(vect);
99         }
100         return ret_val;
101 }
102
103 void W1Device::collect_data(Data *data) {
104         // TODO: add mutex for memory_cache
105         memory_cache.push_back(data);
106 }
107
108 void W1Device::save_data() {
109         list<Data *>::iterator  iter;
110         Data                    *data;
111         int                     dec_precision;
112
113         dec_precision   = get_data_decimal_precision();
114         W1Store::save(id, &memory_cache, dec_precision);
115         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
116                 data    = (Data *)*iter;
117                 delete(data);
118         }
119         memory_cache.clear();
120 }