]> pilppa.org Git - lib1wire.git/blob - src/DeviceData.cc
d066bbdc3f96f9f423d0c58935b3709baae9547f
[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 "Store.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>  dta_vcr;
46         string          f_name;
47         Store           *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                         dta_vcr = W1Util::get_data_files(mon_dr);
61                         if (dta_vcr.size() > 0) {
62                                 f_name  = dta_vcr.at(0);
63                                 f_name  = W1Util::concat_paths(mon_dr, f_name);
64                                 store   = new Store(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         Store           *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 Store(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_daily_summary(Date *date,
173                                 int calc_type) {
174         Data    *ret_val;
175         Store   *store;
176         bool    suc_flg;
177         //Store *cache_store;
178
179         ret_val = NULL;
180         store   = new Store(device_id, date);
181         if (store != NULL) {
182                 suc_flg = store->load();
183                 if (suc_flg == true) {
184                         switch(calc_type) {
185                                 case SUM:
186                                         ret_val = store->get_sum();
187                                         break;
188                                 case DELTA:
189                                         ret_val = store->get_delta();
190                                         break;
191                                 case MAX:
192                                         ret_val = store->get_max();
193                                         break;
194                                 case MIN:
195                                         ret_val = store->get_min();
196                                         break;
197                                 case MEAN:
198                                 default:
199                                         ret_val = store->get_mean();
200                                         break;
201                         }
202                         if (ret_val != NULL) {
203                                 ret_val->printout();
204                         }
205                         else {
206                                 log_error("Could not read data log for device: %s\n", device_id.c_str());
207                         }
208                 }
209                 else {
210                         log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
211                 }
212         }
213         else {
214                 log_error("Could not read data log for device: %s\n", device_id.c_str());
215         }
216         delete(store);
217         return ret_val;
218 }
219
220 Data *DeviceData::get_daily_summary(Date *date) {
221         Data    *ret_val;
222
223         ret_val = get_daily_summary(date, summary_calc_type);
224         return ret_val;
225 }
226
227 DataRange *DeviceData::get_daily_summary(Date *start_date,
228                                         Date *end_date) {
229         DataRange       *ret_val;
230         Data            *data;
231         Date            *date;
232
233         ret_val = NULL;
234         date    = start_date->clone();
235         while(date->before(end_date)) {
236                 data    = get_daily_summary(date);
237                 if (data != NULL) {
238                         if (ret_val == NULL) {
239                                 ret_val = new DataRange(data);
240                         }
241                         else {
242                                 ret_val->add_data(data);
243                         }
244                         delete(data);
245                 }
246                 date->tomorrow();
247         }
248         delete(date);
249         return ret_val;
250 }
251
252 vector<Data *> *DeviceData::get_hourly_summary(Date *date,
253                                         int calc_type) {
254         vector<Data *>  *ret_val;
255         Store           *store;
256
257         ret_val = NULL;
258         store   = new Store(device_id, date);
259         store->load();
260         switch(calc_type) {
261                 case SUM:
262                         ret_val = store->get_sum(3600);
263                         break;
264                 case DELTA:
265                         ret_val = store->get_delta(3600);
266                         break;
267                 case MEAN:
268                 default:
269                         ret_val = store->get_mean(3600);
270                         break;
271                 case MAX:
272                         ret_val = store->get_max(3600);
273                         break;
274                 case MIN:
275                         ret_val = store->get_min(3600);
276                         break;
277         }
278         delete(store);
279         return ret_val;
280 }
281
282 vector<Data *> *DeviceData::get_hourly_summary(Date *date) {
283         vector<Data *>  *ret_val;
284
285         ret_val = get_hourly_summary(date, summary_calc_type);
286         return ret_val;
287 }
288
289 DataRange *DeviceData::get_hourly_summary(Date *start_date,
290                                         Date *end_date) {
291         DataRange                       *ret_val;
292         vector<Data *>                  *dta_lst;
293         Data                            *data;
294         Date                            *date;
295         vector<Data *>::iterator        iter;
296
297         ret_val = NULL;
298         date    = start_date->clone();
299         while(date->before(end_date)) {
300                 dta_lst = get_hourly_summary(date);
301                 for(iter = dta_lst->begin(); iter != dta_lst->end(); iter++) {
302                         data    = (Data *)*iter;
303                         if (data != NULL) {
304                                 if (ret_val == NULL) {
305                                         ret_val = new DataRange(data);
306                                 }
307                                 else {
308                                         ret_val->add_data(data);
309                                 }
310                                 delete(data);
311                         }
312                 }
313                 date->tomorrow();
314         }
315         delete(date);
316         return ret_val;
317 }
318
319 DataRange *DeviceData::get_data(Date *start_date,
320                                 Date *end_date) {
321         DataRange       *ret_val;
322         int             int_type;
323
324         ret_val         = NULL;
325         start_date->printout();
326         end_date->printout();
327         int_type        = get_interval_type(start_date, end_date);
328         switch(int_type) {
329                 case 0:
330                         log_debug("get yearly summary\n");
331                         break;
332                 case 1:
333                         log_debug("get monthly summary\n");
334                         ret_val = get_daily_summary(start_date, end_date);
335                         break;
336                 case 2:
337                         log_debug("get daily summary\n");
338                         ret_val = get_daily_summary(start_date, end_date);
339                         break;
340                 case 3:
341                         log_debug("get hourly summary\n");
342                         ret_val = get_hourly_summary(start_date, end_date);
343                         break;
344                 case 4:
345                         log_debug("get minute summary data\n");
346                         break;
347                 case 5:
348                         log_debug("get second summary data\n");
349                         break;
350         }
351         return ret_val;
352 }