]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
Refactoring and fixes.
[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 }
40
41 int W1Device::get_w1_family_code() {
42         return family_code;
43 }
44
45 string W1Device::get_id() {
46         return id;
47 }
48
49 string W1Device::get_name() {
50         return name;
51 }
52
53 void W1Device::set_name(string name_param) {
54         name    = name_param;
55 }
56
57 string W1Device::get_time() {
58         time_t          wtime;
59         struct tm       *ltime;
60         char            buffer[80];
61         string          ret_val;
62
63         time(&wtime);
64         ltime   = localtime(&wtime);
65         strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
66         ret_val = buffer;
67         return ret_val;
68 }
69
70 void W1Device::printout() {
71         Data    *data;
72         string  text;
73
74         data    = get_and_collect_data();
75         if (data != NULL) {
76                 text    = data->to_string();
77                 cout << text << endl;
78         }
79         else {
80                 log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_name().c_str());
81         }
82 }
83
84 string W1Device::to_string(double dbl_val, int digit_count) {
85         string          ret_val;
86         ostringstream   out;
87
88         out << fixed << setprecision(digit_count) << dbl_val;
89         ret_val = out.str();
90         return ret_val;
91 }
92
93
94 Data *W1Device::get_and_collect_data() {
95         Data            *ret_val;
96         vector<double>  *vect;
97
98         ret_val = NULL;
99         vect    = get_raw_data();
100         if (vect != NULL) {
101                 ret_val = new Data(vect, get_unit());
102                 collect_data(ret_val);
103                 delete(vect);
104         }
105         return ret_val;
106 }
107
108 /*
109 Data *W1Device::get_formatted_data(Data *data) {
110         Data    *ret_val;
111
112         ret_val = get_time() + "|" + raw_data + " " + get_unit();
113         add_to_save_fifo(ret_val);
114         return ret_val;
115 }
116 */
117
118 void W1Device::collect_data(Data *data) {
119         // TODO: add mutex for memory_cache
120         memory_cache.push_back(data);
121 }
122
123 void W1Device::save_data() {
124         list<Data *>::iterator  iter;
125         Data                    *data;
126
127         W1Store::save(id, &memory_cache);
128         for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
129                 data    = (Data *)*iter;
130                 delete(data);
131         }
132         memory_cache.clear();
133 }