4 * Created on: Nov 7, 2010
16 #include "DataReader.hh"
17 #include "DeviceConfig.hh"
18 #include "FileUtil.hh"
19 #include "StoreDay.hh"
20 #include "StoreCache.hh"
25 template <class NumberDataType>
26 bool string_to_number(NumberDataType& result,
27 const std::string& string_param,
28 std::ios_base& (*format)(std::ios_base&))
30 istringstream iss(string_param);
31 return !(iss >> format >> result).fail();
34 DataReader::DataReader(string device_id_param) {
37 log_debug("device_id: %s\n", device_id_param.c_str());
38 summary_calc_type = MEAN;
40 device_id = device_id_param;
41 base_dir = DeviceConfig::get_base_dir_name();
42 device_dir = FileUtil::concat_paths(base_dir, device_id);
43 device_ch_dir = FileUtil::concat_paths(base_dir, "cache");
44 device_ch_dir = FileUtil::concat_paths(device_ch_dir, device_id);
47 DataReader::~DataReader() {
48 if (device_config != NULL) {
49 delete(device_config);
54 Data *DataReader::find_latest_data(vector<string> year_name_vector_param) {
62 size = year_name_vector_param.size();
64 // dirs are alphabetically sorted
65 year_name = year_name_vector_param.at(size - 1);
66 string_to_number<int>(val_int, year_name, dec);
68 ret_val = StoreCache::get_latest_data(&date, device_id, PERIOD_YEARLY);
73 Data *DataReader::get_latest_data() {
74 vector<string> y_list;
78 y_list = FileUtil::get_subdirectories(device_dir);
79 if (y_list.size() > 0) {
80 ret_val = find_latest_data(y_list);
85 Data *DataReader::find_oldest_data(vector<string> year_name_vector_param) {
93 size = year_name_vector_param.size();
95 // dirs are alphabetically sorted
96 year_name = year_name_vector_param.at(0);
97 string_to_number<int>(val_int, year_name, dec);
99 ret_val = StoreCache::get_oldest_data(&date, device_id, PERIOD_YEARLY);
104 DataRange *DataReader::get_data_range() {
106 vector<string> y_list;
111 y_list = FileUtil::get_subdirectories(device_dir);
112 o_data = find_oldest_data(y_list);
113 if (o_data != NULL) {
114 n_data = find_latest_data(y_list);
115 if (n_data != NULL) {
116 ret_val = new DataRange(o_data);
117 ret_val->add(n_data);
125 EnumSummaryPeriod get_period_type(Date *start_date,
128 EnumSummaryPeriod ret_val;
130 ret_val = PERIOD_YEARLY;
131 diff = end_date->year - start_date->year;
133 ret_val = PERIOD_YEARLY;
136 diff = end_date->month - start_date->month;
138 ret_val = PERIOD_MONTHLY;
141 diff = end_date->day - start_date->day;
143 ret_val = PERIOD_DAILY;
146 diff = end_date->hour - start_date->hour;
148 ret_val = PERIOD_HOURLY;
151 diff = end_date->min - start_date->min;
153 ret_val = PERIOD_MINUTELY;
156 ret_val = PERIOD_SECONDLY;
165 DataRange *DataReader::get_summary(Date *date_param,
166 EnumSummaryCalculationType calc_type_param,
167 EnumSummaryPeriod period_type_param) {
172 //store = new StoreDay(device_id, date_param);
173 store = new StoreCache(device_id, date_param);
175 switch(calc_type_param) {
177 ret_val = store->get_sum(period_type_param);
180 ret_val = store->get_delta(period_type_param);
183 ret_val = store->get_max(period_type_param);
186 ret_val = store->get_min(period_type_param);
190 ret_val = store->get_mean(period_type_param);
193 if (ret_val != NULL) {
197 log_error("Could not read data log for device: %s\n", device_id.c_str());
201 log_error("Could not read data log for device: %s\n", device_id.c_str());
207 DataRange *DataReader::get_yearly_summary(Date *date,
208 EnumSummaryCalculationType calc_type_param) {
209 return get_summary(date, calc_type_param, PERIOD_YEARLY);
212 DataRange *DataReader::get_yearly_summary(Date *date) {
215 if (device_config == NULL) {
216 device_config = DeviceConfig::get_device_config(device_id);
217 summary_calc_type = device_config->get_summary_calculation_type();
219 ret_val = get_yearly_summary(date, summary_calc_type);
223 DataRange *DataReader::get_yearly_summary(Date *start_date,
230 date = start_date->clone();
231 while(date->before_or_equal_year(end_date)) {
232 data = get_yearly_summary(date);
234 if (ret_val == NULL) {
235 ret_val = new DataRange(data);
248 DataRange *DataReader::get_monthly_summary(Date *date,
249 EnumSummaryCalculationType calc_type_param) {
250 return get_summary(date, calc_type_param, PERIOD_MONTHLY);
253 DataRange *DataReader::get_monthly_summary(Date *date) {
256 if (device_config == NULL) {
257 device_config = DeviceConfig::get_device_config(device_id);
258 summary_calc_type = device_config->get_summary_calculation_type();
260 ret_val = get_monthly_summary(date, summary_calc_type);
264 DataRange *DataReader::get_monthly_summary(Date *start_date,
271 date = start_date->clone();
272 while(date->before_or_equal_month(end_date)) {
273 data = get_monthly_summary(date);
275 if (ret_val == NULL) {
276 ret_val = new DataRange(data);
290 * Get Daily summary from the latest date
292 DataRange *DataReader::get_daily_summary() {
293 if (device_config == NULL) {
294 device_config = DeviceConfig::get_device_config(device_id);
295 summary_calc_type = device_config->get_summary_calculation_type();
297 return get_daily_summary(summary_calc_type);
301 * Get Daily summary specified by the calc_type from the latest date.
303 DataRange *DataReader::get_daily_summary(EnumSummaryCalculationType calc_type_param) {
309 data = get_latest_data();
311 date = data->get_date();
312 ret_val = get_daily_summary(&date, calc_type_param);
317 DataRange *DataReader::get_daily_summary(Date *date,
318 EnumSummaryCalculationType calc_type_param) {
319 return get_summary(date, calc_type_param, PERIOD_DAILY);
322 DataRange *DataReader::get_daily_summary(Date *date) {
325 if (device_config == NULL) {
326 device_config = DeviceConfig::get_device_config(device_id);
327 summary_calc_type = device_config->get_summary_calculation_type();
329 ret_val = get_daily_summary(date, summary_calc_type);
333 DataRange *DataReader::get_daily_summary(Date *start_date,
340 date = start_date->clone();
341 while(date->before_or_equal_day(end_date)) {
342 data = get_daily_summary(date);
344 if (ret_val == NULL) {
345 ret_val = new DataRange(data);
358 DataRange *DataReader::get_hourly_summary(Date *date,
359 EnumSummaryCalculationType calc_type_param) {
360 return get_summary(date, calc_type_param, PERIOD_HOURLY);
363 DataRange *DataReader::get_hourly_summary(Date *date) {
366 if (device_config == NULL) {
367 device_config = DeviceConfig::get_device_config(device_id);
368 summary_calc_type = device_config->get_summary_calculation_type();
370 ret_val = get_hourly_summary(date, summary_calc_type);
374 DataRange *DataReader::get_hourly_summary(Date *start_date,
384 date = start_date->clone();
385 while(date->before_or_equal_hour(end_date)) {
386 dta_lst = get_hourly_summary(date);
387 cnt = dta_lst->get_count();
388 for(ii = 0; ii < cnt; ii++) {
389 data = dta_lst->get(ii);
391 if (ret_val == NULL) {
392 ret_val = new DataRange(data);
406 DataRange *DataReader::get_data(Date *start_date,
409 EnumSummaryPeriod period;
412 period = get_period_type(start_date, end_date);
415 log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
416 ret_val = get_yearly_summary(start_date, end_date);
419 log_debug("get monthly summary\n");
420 ret_val = get_monthly_summary(start_date, end_date);
423 log_debug("get daily summary\n");
424 ret_val = get_daily_summary(start_date, end_date);
427 log_debug("get hourly summary\n");
428 ret_val = get_hourly_summary(start_date, end_date);
430 case PERIOD_MINUTELY:
431 log_debug("get minute summary data\n");
433 case PERIOD_SECONDLY:
435 log_debug("get second summary data\n");
441 string DataReader::get_device_id() {
446 * Read device type from the device specific config file.
448 * @rerurn string representing device type.
449 * In the case of error, an empty string is returned.
451 bool DataReader::get_device_type(string& type_param) {
455 if (device_config == NULL) {
456 device_config = DeviceConfig::get_device_config(device_id);
458 if (device_config != NULL) {
459 ret_val = device_config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type_param);