]> pilppa.org Git - lib1wire.git/blob - src/DeviceData.cc
5f95966edae83aa8b5156ab58289bd49bd8ded33
[lib1wire.git] / src / DeviceData.cc
1 /*
2  * DeviceData.cc
3  *
4  *  Created on: Nov 7, 2010
5  *      Author: lamikr
6  */
7 #include <dirent.h>
8 #include <malloc.h>
9 #include <errno.h>
10 #include <string.h>
11
12 #include "W1Util.hh"
13 #include "DeviceData.hh"
14 #include "W1Store.hh"
15 #include "DeviceConfig.hh"
16 #include "Factory.hh"
17
18 #include "plp/log.h"
19
20 using namespace w1;
21 using namespace std;
22 using namespace plp;
23
24 DeviceData::DeviceData(string device_id_param) {
25         string          base_dir;
26
27         device_config           = Factory::get_device_config(device_id_param);
28         summary_calc_type       = device_config->get_summary_calculation_type();
29         device_id               = device_id_param;
30         base_dir                = DeviceConfig::get_base_dir_name();
31         device_dir              = W1Util::concat_paths(base_dir, device_id);
32         device_ch_dir           = W1Util::concat_paths(base_dir, "cache");
33         device_ch_dir           = W1Util::concat_paths(device_ch_dir, device_id);
34 }
35
36 DeviceData::~DeviceData() {
37         delete(device_config);
38 }
39
40 Data *DeviceData::find_oldest_data(vector<string> year_vector) {
41         unsigned int    ii;
42         string          year_dr;
43         string          mon_dr;
44         vector<string>  mon_vcr;
45         vector<string>  d_vcr;
46         string          f_name;
47         W1Store         *store;
48         Data            *ret_val;
49
50         ret_val = NULL;
51         if (year_vector.size() > 0) {
52                 // dirs are alphabetically sorted
53                 year_dr = year_vector.at(0);
54                 year_dr = W1Util::concat_paths(device_dir, year_dr);
55                 mon_vcr = W1Util::get_subdirectories(year_dr);
56                 for (ii = 0; ii < mon_vcr.size(); ii++) {
57                         mon_dr  = mon_vcr.at(ii);
58                         mon_dr  = W1Util::concat_paths(year_dr, mon_dr);
59                         // scan data files from month dir
60                         d_vcr   = W1Util::get_data_files(mon_dr);
61                         if (d_vcr.size() > 0) {
62                                 f_name  = d_vcr.at(0);
63                                 f_name  = W1Util::concat_paths(mon_dr, f_name);
64                                 store   = new W1Store(f_name);
65                                 ret_val = store->get_oldest_data();
66                                 delete(store);
67                                 break;
68                         }
69                 }
70         }
71         return ret_val;
72 }
73
74 Data *DeviceData::find_newest_data(vector<string> year_vector) {
75         int             ii;
76         string          year_dr;
77         string          mon_dr;
78         vector<string>  mon_vcr;
79         vector<string>  d_vcr;
80         string          f_name;
81         Data            *ret_val;
82         int             size;
83         W1Store         *store;
84
85         ret_val = NULL;
86         size    = year_vector.size();
87         if (size > 0) {
88                 // dirs are alphabetically sorted
89                 year_dr = year_vector.at(size - 1);
90                 year_dr = W1Util::concat_paths(device_dir, year_dr);
91                 mon_vcr = W1Util::get_subdirectories(year_dr);
92                 for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
93                         mon_dr  = mon_vcr.at(ii);
94                         mon_dr  = W1Util::concat_paths(year_dr, mon_dr);
95                         // scan data files from month dir
96                         d_vcr   = W1Util::get_data_files(mon_dr);
97                         size    = d_vcr.size();
98                         if (size > 0) {
99                                 f_name  = d_vcr.at(size - 1);
100                                 f_name  = W1Util::concat_paths(mon_dr, f_name);
101                                 store   = new W1Store(f_name);
102                                 ret_val = store->get_newest_data();
103                                 delete(store);
104                                 break;
105                         }
106                 }
107         }
108         return ret_val;
109 }
110
111 DataRange *DeviceData::get_data_range() {
112         DataRange       *ret_val;
113         vector<string>  y_list;
114         Data            *o_data;
115         Data            *n_data;
116
117         ret_val = NULL;
118         y_list  = W1Util::get_subdirectories(device_dir);
119         o_data  = find_oldest_data(y_list);
120         if (o_data != NULL) {
121                 n_data  = find_newest_data(y_list);
122                 if (n_data != NULL) {
123                         ret_val = new DataRange(o_data);
124                         ret_val->add_data(n_data);
125                         delete(n_data);
126                 }
127                 delete(o_data);
128         }
129         return ret_val;
130 }
131
132 long int get_interval_type(Date *start_date,
133                         Date *end_date) {
134         int     diff;
135         int     ret_val;
136
137         ret_val = 0;
138         diff    = end_date->year - start_date->year;
139         if (diff != 0) {
140                 ret_val = 0;
141         }
142         else {
143                 diff    = end_date->month - start_date->month;
144                 if (diff != 0) {
145                         ret_val = 1;
146                 }
147                 else {
148                         diff    = end_date->day - start_date->day;
149                         if (diff != 0) {
150                                 ret_val = 2;
151                         }
152                         else {
153                                 diff    = end_date->hour - start_date->hour;
154                                 if (diff != 0) {
155                                         ret_val = 3;
156                                 }
157                                 else {
158                                         diff    = end_date->min - start_date->min;
159                                         if (diff != 0) {
160                                                 ret_val = 4;
161                                         }
162                                         else {
163                                                 ret_val = 5;
164                                         }
165                                 }
166                         }
167                 }
168         }
169         return ret_val;
170 }
171
172 Data *DeviceData::get_day_summary(Date *date) {
173         Data    *ret_val;
174         W1Store *store;
175         bool    suc_flg;
176
177         ret_val = NULL;
178         store   = new W1Store(device_id, date);
179         if (store != NULL) {
180                 suc_flg = store->load();
181                 if (suc_flg == true) {
182                         switch(summary_calc_type) {
183                                 case SUM:
184                                         ret_val = store->get_sum();
185                                         break;
186                                 case DELTA:
187                                         ret_val = store->get_delta();
188                                         break;
189                                 case MEAN:
190                                 default:
191                                         ret_val = store->get_mean();
192                                         break;
193                                 case MAX:
194                                         ret_val = store->get_max();
195                                         break;
196                                 case MIN:
197                                         ret_val = store->get_min();
198                                         break;
199                         }
200                         if (ret_val != NULL) {
201                                 ret_val->printout();
202                         }
203                         else {
204                                 log_error("Could not read data log for device: %s\n", device_id.c_str());
205                         }
206                 }
207                 else {
208                         log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
209                 }
210         }
211         else {
212                 log_error("Could not read data log for device: %s\n", device_id.c_str());
213         }
214         delete(store);
215         return ret_val;
216 }
217
218 DataRange *DeviceData::get_daily_summary(Date *start_date,
219                                         Date *end_date) {
220         DataRange       *ret_val;
221         Data            *data;
222         Date            *date;
223
224         ret_val = NULL;
225         date    = start_date->clone();
226         while(date->before(*end_date)) {
227                 data    = get_day_summary(date);
228                 if (data != NULL) {
229                         if (ret_val == NULL) {
230                                 ret_val = new DataRange(data);
231                         }
232                         else {
233                                 ret_val->add_data(data);
234                         }
235                         delete(data);
236                 }
237                 date->tomorrow();
238         }
239         delete(date);
240         return ret_val;
241 }
242
243 vector<Data *> DeviceData::get_hourly_summary(Date *date) {
244         vector<Data *>  ret_val;
245         W1Store *       store;
246
247         store   = new W1Store(device_id, date);
248         store->load();
249         switch(summary_calc_type) {
250 /*
251                 case SUM:
252                         ret_val = store->get_sum();
253                         break;
254 */
255                 case DELTA:
256                         ret_val = store->get_delta(3600);
257                         break;
258                 case MEAN:
259                 default:
260                         ret_val = store->get_mean(3600);
261                         break;
262 /*
263                 case MAX:
264                         ret_val = store->get_max();
265                         break;
266                 case MIN:
267                         ret_val = store->get_min();
268                         break;
269 */
270         }
271         delete(store);
272         return ret_val;
273 }
274
275 DataRange *DeviceData::get_hourly_summary(Date *start_date,
276                                         Date *end_date) {
277         DataRange                       *ret_val;
278         vector<Data *>                  dta_list;
279         Data                            *data;
280         Date                            *date;
281         vector<Data *>::iterator        iter;
282
283         ret_val = NULL;
284         date    = start_date->clone();
285         while(date->before(*end_date)) {
286                 dta_list        = get_hourly_summary(date);
287                 for(iter = dta_list.begin(); iter != dta_list.end(); iter++) {
288                         data    = (Data *)*iter;
289                         if (data != NULL) {
290                                 if (ret_val == NULL) {
291                                         ret_val = new DataRange(data);
292                                 }
293                                 else {
294                                         ret_val->add_data(data);
295                                 }
296                                 delete(data);
297                         }
298                 }
299                 date->tomorrow();
300         }
301         delete(date);
302         return ret_val;
303 }
304 DataRange *DeviceData::get_data(Date *start_date,
305                                 Date *end_date) {
306         DataRange       *ret_val;
307         int             int_type;
308
309         ret_val         = NULL;
310         start_date->printout();
311         end_date->printout();
312         int_type        = get_interval_type(start_date, end_date);
313         switch(int_type) {
314                 case 0:
315                         log_debug("get yearly summary\n");
316                         break;
317                 case 1:
318                         log_debug("get monthly summary\n");
319                         ret_val = get_daily_summary(start_date, end_date);
320                         break;
321                 case 2:
322                         log_debug("get daily summary\n");
323                         ret_val = get_daily_summary(start_date, end_date);
324                         break;
325                 case 3:
326                         log_debug("get hourly summary\n");
327                         ret_val = get_hourly_summary(start_date, end_date);
328                         break;
329                 case 4:
330                         log_debug("get minute summary data\n");
331                         break;
332                 case 5:
333                         log_debug("get second summary data\n");
334                         break;
335         }
336         return ret_val;
337 }