4 * Created on: Nov 7, 2010
13 #include "DeviceData.hh"
15 #include "DeviceConfig.hh"
24 DeviceData::DeviceData(string device_id_param) {
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);
36 DeviceData::~DeviceData() {
37 delete(device_config);
40 Data *DeviceData::find_oldest_data(vector<string> year_vector) {
44 vector<string> month_vector;
45 vector<string> data_vector;
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();
74 Data *DeviceData::find_newest_data(vector<string> year_vector) {
78 vector<string> month_vector;
79 vector<string> data_vector;
86 size = year_vector.size();
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();
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();
111 DataRange *DeviceData::get_data_range() {
113 vector<string> year_list;
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);
132 long int get_interval_type(Date *start_date,
138 diff = end_date->year - start_date->year;
143 diff = end_date->month - start_date->month;
148 diff = end_date->day - start_date->day;
153 diff = end_date->hour - start_date->hour;
158 diff = end_date->min - start_date->min;
172 Data *DeviceData::get_day_summary(Date *date) {
178 store = new W1Store(device_id, date);
180 suc_flg = store->load();
181 if (suc_flg == true) {
182 switch(summary_calc_type) {
184 ret_val = store->get_sum();
187 ret_val = store->get_delta();
191 ret_val = store->get_mean();
194 ret_val = store->get_max();
197 ret_val = store->get_min();
200 if (ret_val != NULL) {
204 log_error("Could not read data log for device: %s\n", device_id.c_str());
208 log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
212 log_error("Could not read data log for device: %s\n", device_id.c_str());
218 DataRange *DeviceData::get_daily_summary(Date *start_date,
225 date = start_date->clone();
226 while(date->before(*end_date)) {
227 data = get_day_summary(date);
229 if (ret_val == NULL) {
230 ret_val = new DataRange(*data);
233 ret_val->add_data(*data);
243 vector<Data *> DeviceData::get_hourly_summary(Date *date) {
244 vector<Data *> ret_val;
247 store = new W1Store(device_id, date);
249 switch(summary_calc_type) {
252 ret_val = store->get_sum();
256 ret_val = store->get_delta(3600);
260 ret_val = store->get_mean(3600);
264 ret_val = store->get_max();
267 ret_val = store->get_min();
275 DataRange *DeviceData::get_hourly_summary(Date *start_date,
278 vector<Data *> dta_list;
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;
289 if (ret_val == NULL) {
290 ret_val = new DataRange(*data);
293 ret_val->add_data(*data);
303 DataRange *DeviceData::get_data(Date *start_date,
309 start_date->printout();
310 end_date->printout();
311 int_type = get_interval_type(start_date, end_date);
314 log_debug("get yearly summary\n");
317 log_debug("get monthly summary\n");
318 ret_val = get_daily_summary(start_date, end_date);
321 log_debug("get daily summary\n");
322 ret_val = get_daily_summary(start_date, end_date);
325 log_debug("get hourly summary\n");
326 ret_val = get_hourly_summary(start_date, end_date);
329 log_debug("get minute summary data\n");
332 log_debug("get second summary data\n");