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