#include "W1Util.hh"
#include "DeviceData.hh"
-#include "Store.hh"
+#include "StoreDay.hh"
+#include "StoreCache.hh"
#include "DeviceConfig.hh"
#include "Factory.hh"
DeviceData::DeviceData(string device_id_param) {
string base_dir;
- device_config = Factory::get_device_config(device_id_param);
- summary_calc_type = device_config->get_summary_calculation_type();
- device_id = device_id_param;
- base_dir = DeviceConfig::get_base_dir_name();
- device_dir = W1Util::concat_paths(base_dir, device_id);
- device_ch_dir = W1Util::concat_paths(base_dir, "cache");
- device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id);
+ device_config = NULL;
+ device_id = device_id_param;
+ base_dir = DeviceConfig::get_base_dir_name();
+ device_dir = W1Util::concat_paths(base_dir, device_id);
+ device_ch_dir = W1Util::concat_paths(base_dir, "cache");
+ device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id);
}
DeviceData::~DeviceData() {
- delete(device_config);
+ if (device_config != NULL) {
+ delete(device_config);
+ device_config = NULL;
+ }
}
-Data *DeviceData::find_oldest_data(vector<string> year_vector) {
- unsigned int ii;
- string year_dr;
+Data *DeviceData::find_newest_data(string year_name_param) {
+ int ii;
string mon_dr;
vector<string> mon_vcr;
- vector<string> dta_vcr;
+ vector<string> d_vcr;
string f_name;
- Store *store;
+ StoreDay *store;
Data *ret_val;
+ string year_dr;
+ int size;
ret_val = NULL;
- if (year_vector.size() > 0) {
- // dirs are alphabetically sorted
- year_dr = year_vector.at(0);
- year_dr = W1Util::concat_paths(device_dir, year_dr);
- mon_vcr = W1Util::get_subdirectories(year_dr);
- for (ii = 0; ii < mon_vcr.size(); ii++) {
- mon_dr = mon_vcr.at(ii);
- mon_dr = W1Util::concat_paths(year_dr, mon_dr);
- // scan data files from month dir
- dta_vcr = W1Util::get_data_files(mon_dr);
- if (dta_vcr.size() > 0) {
- f_name = dta_vcr.at(0);
- f_name = W1Util::concat_paths(mon_dr, f_name);
- store = new Store(f_name);
- ret_val = store->get_oldest_data();
- delete(store);
- break;
- }
+ year_dr = W1Util::concat_paths(device_dir, year_name_param);
+ mon_vcr = W1Util::get_subdirectories(year_dr);
+ for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
+ mon_dr = mon_vcr.at(ii);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ d_vcr = W1Util::get_data_files(mon_dr);
+ size = d_vcr.size();
+ if (size > 0) {
+ f_name = d_vcr.at(size - 1);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_newest_data();
+ delete(store);
+ break;
}
}
return ret_val;
}
-Data *DeviceData::find_newest_data(vector<string> year_vector) {
- int ii;
+Data *DeviceData::find_newest_data(vector<string> year_name_vector_param) {
+ string year_name;
+ int size;
+ Data *ret_val;
+
+ ret_val = NULL;
+ size = year_name_vector_param.size();
+ if (size > 0) {
+ // dirs are alphabetically sorted
+ year_name = year_name_vector_param.at(size - 1);
+ ret_val = find_newest_data(year_name);
+ }
+ return ret_val;
+}
+
+Data *DeviceData::find_oldest_data(string year_name_param) {
+ int size;
+ unsigned int ii;
string year_dr;
string mon_dr;
vector<string> mon_vcr;
- vector<string> d_vcr;
+ vector<string> dta_vcr;
string f_name;
+ StoreDay *store;
Data *ret_val;
- int size;
- Store *store;
ret_val = NULL;
- size = year_vector.size();
+ year_dr = W1Util::concat_paths(device_dir, year_name_param);
+ mon_vcr = W1Util::get_subdirectories(year_dr);
+ for (ii = 0; ii < mon_vcr.size(); ii++) {
+ mon_dr = mon_vcr.at(ii);
+ mon_dr = W1Util::concat_paths(year_dr, mon_dr);
+ // scan data files from month dir
+ dta_vcr = W1Util::get_data_files(mon_dr);
+ size = dta_vcr.size();
+ if (size > 0) {
+ f_name = dta_vcr.at(0);
+ f_name = W1Util::concat_paths(mon_dr, f_name);
+ store = new StoreDay(f_name);
+ ret_val = store->get_oldest_data();
+ delete(store);
+ break;
+ }
+ }
+ return ret_val;
+}
+
+Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
+ int size;
+ string year_name;
+ Data *ret_val;
+
+ ret_val = NULL;
+ size = year_name_vector_param.size();
if (size > 0) {
// dirs are alphabetically sorted
- year_dr = year_vector.at(size - 1);
- year_dr = W1Util::concat_paths(device_dir, year_dr);
- mon_vcr = W1Util::get_subdirectories(year_dr);
- for (ii = mon_vcr.size() - 1; ii >= 0; ii--) {
- mon_dr = mon_vcr.at(ii);
- mon_dr = W1Util::concat_paths(year_dr, mon_dr);
- // scan data files from month dir
- d_vcr = W1Util::get_data_files(mon_dr);
- size = d_vcr.size();
- if (size > 0) {
- f_name = d_vcr.at(size - 1);
- f_name = W1Util::concat_paths(mon_dr, f_name);
- store = new Store(f_name);
- ret_val = store->get_newest_data();
- delete(store);
- break;
- }
- }
+ year_name = year_name_vector_param.at(0);
+ ret_val = find_oldest_data(year_name);
}
return ret_val;
}
n_data = find_newest_data(y_list);
if (n_data != NULL) {
ret_val = new DataRange(o_data);
- ret_val->add_data(n_data);
+ ret_val->add(n_data);
delete(n_data);
}
delete(o_data);
return ret_val;
}
-long int get_interval_type(Date *start_date,
+EnumSummaryPeriod get_period_type(Date *start_date,
Date *end_date) {
- int diff;
- int ret_val;
+ int diff;
+ EnumSummaryPeriod ret_val;
- ret_val = 0;
+ ret_val = PERIOD_YEARLY;
diff = end_date->year - start_date->year;
if (diff != 0) {
- ret_val = 0;
+ ret_val = PERIOD_YEARLY;
}
else {
diff = end_date->month - start_date->month;
if (diff != 0) {
- ret_val = 1;
+ ret_val = PERIOD_MONTHLY;
}
else {
diff = end_date->day - start_date->day;
if (diff != 0) {
- ret_val = 2;
+ ret_val = PERIOD_DAILY;
}
else {
diff = end_date->hour - start_date->hour;
if (diff != 0) {
- ret_val = 3;
+ ret_val = PERIOD_HOURLY;
}
else {
diff = end_date->min - start_date->min;
if (diff != 0) {
- ret_val = 4;
+ ret_val = PERIOD_MINUTELY;
}
else {
- ret_val = 5;
+ ret_val = PERIOD_SECONDLY;
}
}
}
return ret_val;
}
-Data *DeviceData::get_daily_summary(Date *date,
- int calc_type) {
- Data *ret_val;
- Store *store;
- bool suc_flg;
- //Store *cache_store;
+DataRange *DeviceData::get_summary(Date *date_param,
+ EnumSummaryCalculationType calc_type_param,
+ EnumSummaryPeriod period_type_param) {
+ DataRange *ret_val;
+ StoreCache *store;
ret_val = NULL;
- store = new Store(device_id, date);
+ //store = new StoreDay(device_id, date_param);
+ store = new StoreCache(device_id, date_param);
if (store != NULL) {
- suc_flg = store->load();
- if (suc_flg == true) {
- switch(calc_type) {
- case SUM:
- ret_val = store->get_sum();
- break;
- case DELTA:
- ret_val = store->get_delta();
- break;
- case MAX:
- ret_val = store->get_max();
- break;
- case MIN:
- ret_val = store->get_min();
- break;
- case MEAN:
- default:
- ret_val = store->get_mean();
- break;
- }
- if (ret_val != NULL) {
- ret_val->printout();
- }
- else {
- log_error("Could not read data log for device: %s\n", device_id.c_str());
- }
+ switch(calc_type_param) {
+ case SUM:
+ ret_val = store->get_sum(period_type_param);
+ break;
+ case DELTA:
+ ret_val = store->get_delta(period_type_param);
+ break;
+ case MAX:
+ ret_val = store->get_max(period_type_param);
+ break;
+ case MIN:
+ ret_val = store->get_min(period_type_param);
+ break;
+ case MEAN:
+ default:
+ ret_val = store->get_mean(period_type_param);
+ break;
+ }
+ if (ret_val != NULL) {
+ ret_val->printout();
}
else {
- log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
+ log_error("Could not read data log for device: %s\n", device_id.c_str());
}
}
else {
return ret_val;
}
-Data *DeviceData::get_daily_summary(Date *date) {
- Data *ret_val;
+DataRange *DeviceData::get_yearly_summary(Date *date,
+ EnumSummaryCalculationType calc_type_param) {
+ return get_summary(date, calc_type_param, PERIOD_YEARLY);
+}
- ret_val = get_daily_summary(date, summary_calc_type);
+DataRange *DeviceData::get_yearly_summary(Date *date) {
+ DataRange *ret_val;
+
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
+ ret_val = get_yearly_summary(date, summary_calc_type);
return ret_val;
}
-DataRange *DeviceData::get_daily_summary(Date *start_date,
+DataRange *DeviceData::get_yearly_summary(Date *start_date,
Date *end_date) {
DataRange *ret_val;
- Data *data;
+ DataRange *data;
Date *date;
ret_val = NULL;
date = start_date->clone();
- while(date->before(end_date)) {
- data = get_daily_summary(date);
+ while(date->before_or_equal_year(end_date)) {
+ data = get_yearly_summary(date);
if (data != NULL) {
if (ret_val == NULL) {
ret_val = new DataRange(data);
}
else {
- ret_val->add_data(data);
+ ret_val->add(data);
}
delete(data);
}
- date->tomorrow();
+ date->next_year();
}
delete(date);
return ret_val;
}
-vector<Data *> *DeviceData::get_hourly_summary(Date *date,
- int calc_type) {
- vector<Data *> *ret_val;
- Store *store;
+DataRange *DeviceData::get_monthly_summary(Date *date,
+ EnumSummaryCalculationType calc_type_param) {
+ return get_summary(date, calc_type_param, PERIOD_MONTHLY);
+}
+
+DataRange *DeviceData::get_monthly_summary(Date *date) {
+ DataRange *ret_val;
+
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
+ ret_val = get_monthly_summary(date, summary_calc_type);
+ return ret_val;
+}
+
+DataRange *DeviceData::get_monthly_summary(Date *start_date,
+ Date *end_date) {
+ DataRange *ret_val;
+ DataRange *data;
+ Date *date;
ret_val = NULL;
- store = new Store(device_id, date);
- store->load();
- switch(calc_type) {
- case SUM:
- ret_val = store->get_sum(3600);
- break;
- case DELTA:
- ret_val = store->get_delta(3600);
- break;
- case MEAN:
- default:
- ret_val = store->get_mean(3600);
- break;
- case MAX:
- ret_val = store->get_max(3600);
- break;
- case MIN:
- ret_val = store->get_min(3600);
- break;
+ date = start_date->clone();
+ while(date->before_or_equal_month(end_date)) {
+ data = get_monthly_summary(date);
+ if (data != NULL) {
+ if (ret_val == NULL) {
+ ret_val = new DataRange(data);
+ }
+ else {
+ ret_val->add(data);
+ }
+ delete(data);
+ }
+ date->next_month();
}
- delete(store);
+ delete(date);
+ return ret_val;
+}
+
+DataRange *DeviceData::get_daily_summary(Date *date,
+ EnumSummaryCalculationType calc_type_param) {
+ return get_summary(date, calc_type_param, PERIOD_DAILY);
+}
+
+DataRange *DeviceData::get_daily_summary(Date *date) {
+ DataRange *ret_val;
+
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
+ ret_val = get_daily_summary(date, summary_calc_type);
return ret_val;
}
-vector<Data *> *DeviceData::get_hourly_summary(Date *date) {
- vector<Data *> *ret_val;
+DataRange *DeviceData::get_daily_summary(Date *start_date,
+ Date *end_date) {
+ DataRange *ret_val;
+ DataRange *data;
+ Date *date;
+
+ ret_val = NULL;
+ date = start_date->clone();
+ while(date->before_or_equal_day(end_date)) {
+ data = get_daily_summary(date);
+ if (data != NULL) {
+ if (ret_val == NULL) {
+ ret_val = new DataRange(data);
+ }
+ else {
+ ret_val->add(data);
+ }
+ delete(data);
+ }
+ date->next_day();
+ }
+ delete(date);
+ return ret_val;
+}
+
+DataRange *DeviceData::get_hourly_summary(Date *date,
+ EnumSummaryCalculationType calc_type_param) {
+ return get_summary(date, calc_type_param, PERIOD_HOURLY);
+}
+
+DataRange *DeviceData::get_hourly_summary(Date *date) {
+ DataRange *ret_val;
+ if (device_config == NULL) {
+ device_config = Factory::get_device_config(device_id);
+ summary_calc_type = device_config->get_summary_calculation_type();
+ }
ret_val = get_hourly_summary(date, summary_calc_type);
return ret_val;
}
DataRange *DeviceData::get_hourly_summary(Date *start_date,
Date *end_date) {
- DataRange *ret_val;
- vector<Data *> *dta_lst;
- Data *data;
- Date *date;
- vector<Data *>::iterator iter;
+ DataRange *ret_val;
+ DataRange *dta_lst;
+ Data *data;
+ Date *date;
+ int cnt;
+ int ii;
ret_val = NULL;
date = start_date->clone();
- while(date->before(end_date)) {
+ while(date->before_or_equal_hour(end_date)) {
dta_lst = get_hourly_summary(date);
- for(iter = dta_lst->begin(); iter != dta_lst->end(); iter++) {
- data = (Data *)*iter;
+ cnt = dta_lst->get_count();
+ for(ii = 0; ii < cnt; ii++) {
+ data = dta_lst->get(ii);
if (data != NULL) {
if (ret_val == NULL) {
ret_val = new DataRange(data);
}
else {
- ret_val->add_data(data);
+ ret_val->add(data);
}
delete(data);
}
}
- date->tomorrow();
+ date->next_day();
}
delete(date);
return ret_val;
DataRange *DeviceData::get_data(Date *start_date,
Date *end_date) {
- DataRange *ret_val;
- int int_type;
+ DataRange *ret_val;
+ EnumSummaryPeriod period;
- ret_val = NULL;
+ ret_val = NULL;
start_date->printout();
end_date->printout();
- int_type = get_interval_type(start_date, end_date);
- switch(int_type) {
- case 0:
- log_debug("get yearly summary\n");
+ period = get_period_type(start_date, end_date);
+ switch(period) {
+ case PERIOD_YEARLY:
+ log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
+ ret_val = get_yearly_summary(start_date, end_date);
break;
- case 1:
+ case PERIOD_MONTHLY:
log_debug("get monthly summary\n");
- ret_val = get_daily_summary(start_date, end_date);
+ ret_val = get_monthly_summary(start_date, end_date);
break;
- case 2:
+ case PERIOD_DAILY:
log_debug("get daily summary\n");
ret_val = get_daily_summary(start_date, end_date);
break;
- case 3:
+ case PERIOD_HOURLY:
log_debug("get hourly summary\n");
ret_val = get_hourly_summary(start_date, end_date);
break;
- case 4:
+ case PERIOD_MINUTELY:
log_debug("get minute summary data\n");
break;
- case 5:
+ case PERIOD_SECONDLY:
+ default:
log_debug("get second summary data\n");
break;
}