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("%s: Could not read data log for device %s. Data not found.\n",
198 date_param->to_string().c_str(),
203 log_error("%s: Could not read data log for device: %s. Memory allocation error..\n",
204 date_param->to_string().c_str(),
211 DataRange *DataReader::get_yearly_summary(Date *date,
212 EnumSummaryCalculationType calc_type_param) {
213 return get_summary(date, calc_type_param, PERIOD_YEARLY);
216 DataRange *DataReader::get_yearly_summary(Date *date) {
219 if (device_config == NULL) {
220 device_config = DeviceConfig::get_device_config(device_id);
221 summary_calc_type = device_config->get_summary_calculation_type();
223 ret_val = get_yearly_summary(date, summary_calc_type);
227 DataRange *DataReader::get_yearly_summary(Date *start_date,
234 date = start_date->clone();
235 while(date->before_or_equal_year(end_date)) {
236 if (StoreDay::exist_in_year(device_id, date, false)) {
237 data = get_yearly_summary(date);
239 if (ret_val == NULL) {
240 ret_val = new DataRange(data);
254 DataRange *DataReader::get_monthly_summary(Date *date,
255 EnumSummaryCalculationType calc_type_param) {
256 return get_summary(date, calc_type_param, PERIOD_MONTHLY);
259 DataRange *DataReader::get_monthly_summary(Date *date) {
262 if (device_config == NULL) {
263 device_config = DeviceConfig::get_device_config(device_id);
264 summary_calc_type = device_config->get_summary_calculation_type();
266 ret_val = get_monthly_summary(date, summary_calc_type);
270 DataRange *DataReader::get_monthly_summary(Date *start_date,
277 date = start_date->clone();
278 while(date->before_or_equal_month(end_date)) {
279 if (StoreDay::exist_in_month(device_id, date, false)) {
280 data = get_monthly_summary(date);
282 if (ret_val == NULL) {
283 ret_val = new DataRange(data);
298 * Get Daily summary from the latest date
300 DataRange *DataReader::get_daily_summary() {
301 if (device_config == NULL) {
302 device_config = DeviceConfig::get_device_config(device_id);
303 summary_calc_type = device_config->get_summary_calculation_type();
305 return get_daily_summary(summary_calc_type);
309 * Get Daily summary specified by the calc_type from the latest date.
311 DataRange *DataReader::get_daily_summary(EnumSummaryCalculationType calc_type_param) {
317 data = get_latest_data();
319 date = data->get_date();
320 ret_val = get_daily_summary(&date, calc_type_param);
326 DataRange *DataReader::get_daily_summary(Date *date,
327 EnumSummaryCalculationType calc_type_param) {
328 return get_summary(date, calc_type_param, PERIOD_DAILY);
331 DataRange *DataReader::get_daily_summary(Date *date) {
334 if (device_config == NULL) {
335 device_config = DeviceConfig::get_device_config(device_id);
336 summary_calc_type = device_config->get_summary_calculation_type();
338 ret_val = get_daily_summary(date, summary_calc_type);
342 DataRange *DataReader::get_daily_summary(Date *start_date,
349 date = start_date->clone();
350 while(date->before_or_equal_day(end_date)) {
351 if (StoreDay::exist(device_id, date, false)) {
352 data = get_daily_summary(date);
354 if (ret_val == NULL) {
355 ret_val = new DataRange(data);
369 DataRange *DataReader::get_hourly_summary(Date *date,
370 EnumSummaryCalculationType calc_type_param) {
371 return get_summary(date, calc_type_param, PERIOD_HOURLY);
374 DataRange *DataReader::get_hourly_summary(Date *date) {
377 if (device_config == NULL) {
378 device_config = DeviceConfig::get_device_config(device_id);
379 summary_calc_type = device_config->get_summary_calculation_type();
381 ret_val = get_hourly_summary(date, summary_calc_type);
385 DataRange *DataReader::get_hourly_summary(Date *start_date,
395 date = start_date->clone();
396 while(date->before_or_equal_hour(end_date)) {
397 dta_lst = get_hourly_summary(date);
398 cnt = dta_lst->get_count();
399 for(ii = 0; ii < cnt; ii++) {
400 data = dta_lst->get(ii);
402 if (ret_val == NULL) {
403 ret_val = new DataRange(data);
417 DataRange *DataReader::get_data(Date *start_date,
420 EnumSummaryPeriod period;
423 period = get_period_type(start_date, end_date);
426 log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
427 ret_val = get_yearly_summary(start_date, end_date);
430 log_debug("get monthly summary\n");
431 ret_val = get_monthly_summary(start_date, end_date);
434 log_debug("get daily summary\n");
435 ret_val = get_daily_summary(start_date, end_date);
438 log_debug("get hourly summary\n");
439 ret_val = get_hourly_summary(start_date, end_date);
441 case PERIOD_MINUTELY:
442 log_debug("get minute summary data\n");
444 case PERIOD_SECONDLY:
446 log_debug("get second summary data\n");
452 string DataReader::get_device_id() {
457 * Read device type from the device specific config file.
459 * @rerurn string representing device type.
460 * In the case of error, an empty string is returned.
462 bool DataReader::get_device_type(string& type_param) {
466 if (device_config == NULL) {
467 device_config = DeviceConfig::get_device_config(device_id);
469 if (device_config != NULL) {
470 ret_val = device_config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, type_param);