]> pilppa.org Git - lib1wire.git/blob - src/DeviceData.cc
Several data read and store fixes.
[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_dir;
43         string          month_dir;
44         vector<string>  month_vector;
45         vector<string>  data_vector;
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_dir        = year_vector.at(0);
54                 year_dir        = W1Util::concat_paths(device_dir, year_dir);
55                 month_vector    = W1Util::get_subdirectories(year_dir);
56                 for (ii = 0; ii < month_vector.size(); ii++) {
57                         month_dir       = month_vector.at(ii);
58                         month_dir       = W1Util::concat_paths(year_dir, month_dir);
59                         // scan data files from month dir
60                         data_vector     = W1Util::get_data_files(month_dir);
61                         if (data_vector.size() > 0) {
62                                 f_name  = data_vector.at(0);
63                                 f_name  = W1Util::concat_paths(month_dir, 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_dir;
77         string          month_dir;
78         vector<string>  month_vector;
79         vector<string>  data_vector;
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_dir        = year_vector.at(size - 1);
90                 year_dir        = W1Util::concat_paths(device_dir, year_dir);
91                 month_vector    = W1Util::get_subdirectories(year_dir);
92                 for (ii = month_vector.size() - 1; ii >= 0; ii--) {
93                         month_dir       = month_vector.at(ii);
94                         month_dir       = W1Util::concat_paths(year_dir, month_dir);
95                         // scan data files from month dir
96                         data_vector     = W1Util::get_data_files(month_dir);
97                         size    = data_vector.size();
98                         if (size > 0) {
99                                 f_name  = data_vector.at(size - 1);
100                                 f_name  = W1Util::concat_paths(month_dir, 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>  year_list;
114         Data            *first_data;
115         Data            *newest_data;
116
117         ret_val         = NULL;
118         year_list       = W1Util::get_subdirectories(device_dir);
119         first_data      = find_oldest_data(year_list);
120         if (first_data != NULL) {
121                 newest_data     = find_newest_data(year_list);
122                 if (newest_data != NULL) {
123                         ret_val = new DataRange(*first_data);
124                         ret_val->add_data(*newest_data);
125                         delete(newest_data);
126                 }
127                 delete(first_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
282         ret_val = NULL;
283         date    = start_date->clone();
284         while(date->before(*end_date)) {
285                 dta_list        = get_hourly_summary(date);
286                 for(vector<Data *>::iterator list_iter = dta_list.begin(); list_iter != dta_list.end(); list_iter++) {
287                         data    = (Data *)*list_iter;
288                         if (data != NULL) {
289                                 if (ret_val == NULL) {
290                                         ret_val = new DataRange(*data);
291                                 }
292                                 else {
293                                         ret_val->add_data(*data);
294                                 }
295                                 delete(data);
296                         }
297                 }
298                 date->tomorrow();
299         }
300         delete(date);
301         return ret_val;
302 }
303 DataRange *DeviceData::get_data(Date *start_date,
304                                 Date *end_date) {
305         DataRange       *ret_val;
306         int             int_type;
307
308         ret_val         = NULL;
309         start_date->printout();
310         end_date->printout();
311         int_type        = get_interval_type(start_date, end_date);
312         switch(int_type) {
313                 case 0:
314                         log_debug("get yearly summary\n");
315                         break;
316                 case 1:
317                         log_debug("get monthly summary\n");
318                         ret_val = get_daily_summary(start_date, end_date);
319                         break;
320                 case 2:
321                         log_debug("get daily summary\n");
322                         ret_val = get_daily_summary(start_date, end_date);
323                         break;
324                 case 3:
325                         log_debug("get hourly summary\n");
326                         ret_val = get_hourly_summary(start_date, end_date);
327                         break;
328                 case 4:
329                         log_debug("get minute summary data\n");
330                         break;
331                 case 5:
332                         log_debug("get second summary data\n");
333                         break;
334         }
335         return ret_val;
336 }