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());
39 device_id = device_id_param;
40 base_dir = DeviceConfig::get_base_dir_name();
41 device_dir = FileUtil::concat_paths(base_dir, device_id);
42 device_ch_dir = FileUtil::concat_paths(base_dir, "cache");
43 device_ch_dir = FileUtil::concat_paths(device_ch_dir, device_id);
46 DataReader::~DataReader() {
47 if (device_config != NULL) {
48 delete(device_config);
53 Data *DataReader::find_latest_data(vector<string> year_name_vector_param) {
61 size = year_name_vector_param.size();
63 // dirs are alphabetically sorted
64 year_name = year_name_vector_param.at(size - 1);
65 string_to_number<int>(val_int, year_name, dec);
67 ret_val = StoreCache::get_latest_data(&date, device_id, PERIOD_YEARLY);
72 Data *DataReader::get_latest_data() {
73 vector<string> y_list;
77 y_list = FileUtil::get_subdirectories(device_dir);
78 if (y_list.size() > 0) {
79 ret_val = find_latest_data(y_list);
84 Data *DataReader::find_oldest_data(vector<string> year_name_vector_param) {
92 size = year_name_vector_param.size();
94 // dirs are alphabetically sorted
95 year_name = year_name_vector_param.at(0);
96 string_to_number<int>(val_int, year_name, dec);
98 ret_val = StoreCache::get_oldest_data(&date, device_id, PERIOD_YEARLY);
103 DataRange *DataReader::get_data_range() {
105 vector<string> y_list;
110 y_list = FileUtil::get_subdirectories(device_dir);
111 o_data = find_oldest_data(y_list);
112 if (o_data != NULL) {
113 n_data = find_latest_data(y_list);
114 if (n_data != NULL) {
115 ret_val = new DataRange(o_data);
116 ret_val->add(n_data);
124 EnumSummaryPeriod get_period_type(Date *start_date,
127 EnumSummaryPeriod ret_val;
129 ret_val = PERIOD_YEARLY;
130 diff = end_date->year - start_date->year;
132 ret_val = PERIOD_YEARLY;
135 diff = end_date->month - start_date->month;
137 ret_val = PERIOD_MONTHLY;
140 diff = end_date->day - start_date->day;
142 ret_val = PERIOD_DAILY;
145 diff = end_date->hour - start_date->hour;
147 ret_val = PERIOD_HOURLY;
150 diff = end_date->min - start_date->min;
152 ret_val = PERIOD_MINUTELY;
155 ret_val = PERIOD_SECONDLY;
164 DataRange *DataReader::get_summary(Date *date_param,
165 EnumSummaryCalculationType calc_type_param,
166 EnumSummaryPeriod period_type_param) {
171 //store = new StoreDay(device_id, date_param);
172 store = new StoreCache(device_id, date_param);
174 switch(calc_type_param) {
176 ret_val = store->get_sum(period_type_param);
179 ret_val = store->get_delta(period_type_param);
182 ret_val = store->get_max(period_type_param);
185 ret_val = store->get_min(period_type_param);
189 ret_val = store->get_mean(period_type_param);
192 if (ret_val != NULL) {
196 log_error("Could not read data log for device: %s\n", device_id.c_str());
200 log_error("Could not read data log for device: %s\n", device_id.c_str());
206 DataRange *DataReader::get_yearly_summary(Date *date,
207 EnumSummaryCalculationType calc_type_param) {
208 return get_summary(date, calc_type_param, PERIOD_YEARLY);
211 DataRange *DataReader::get_yearly_summary(Date *date) {
214 if (device_config == NULL) {
215 device_config = DeviceConfig::get_device_config(device_id);
216 summary_calc_type = device_config->get_summary_calculation_type();
218 ret_val = get_yearly_summary(date, summary_calc_type);
222 DataRange *DataReader::get_yearly_summary(Date *start_date,
229 date = start_date->clone();
230 while(date->before_or_equal_year(end_date)) {
231 data = get_yearly_summary(date);
233 if (ret_val == NULL) {
234 ret_val = new DataRange(data);
247 DataRange *DataReader::get_monthly_summary(Date *date,
248 EnumSummaryCalculationType calc_type_param) {
249 return get_summary(date, calc_type_param, PERIOD_MONTHLY);
252 DataRange *DataReader::get_monthly_summary(Date *date) {
255 if (device_config == NULL) {
256 device_config = DeviceConfig::get_device_config(device_id);
257 summary_calc_type = device_config->get_summary_calculation_type();
259 ret_val = get_monthly_summary(date, summary_calc_type);
263 DataRange *DataReader::get_monthly_summary(Date *start_date,
270 date = start_date->clone();
271 while(date->before_or_equal_month(end_date)) {
272 data = get_monthly_summary(date);
274 if (ret_val == NULL) {
275 ret_val = new DataRange(data);
289 * Get Daily summary from the latest date
291 DataRange *DataReader::get_daily_summary() {
292 if (device_config == NULL) {
293 device_config = DeviceConfig::get_device_config(device_id);
294 summary_calc_type = device_config->get_summary_calculation_type();
296 return get_daily_summary(summary_calc_type);
300 * Get Daily summary specified by the calc_type from the latest date.
302 DataRange *DataReader::get_daily_summary(EnumSummaryCalculationType calc_type_param) {
308 data = get_latest_data();
310 date = data->get_date();
311 ret_val = get_daily_summary(&date, calc_type_param);
316 DataRange *DataReader::get_daily_summary(Date *date,
317 EnumSummaryCalculationType calc_type_param) {
318 return get_summary(date, calc_type_param, PERIOD_DAILY);
321 DataRange *DataReader::get_daily_summary(Date *date) {
324 if (device_config == NULL) {
325 device_config = DeviceConfig::get_device_config(device_id);
326 summary_calc_type = device_config->get_summary_calculation_type();
328 ret_val = get_daily_summary(date, summary_calc_type);
332 DataRange *DataReader::get_daily_summary(Date *start_date,
339 date = start_date->clone();
340 while(date->before_or_equal_day(end_date)) {
341 data = get_daily_summary(date);
343 if (ret_val == NULL) {
344 ret_val = new DataRange(data);
357 DataRange *DataReader::get_hourly_summary(Date *date,
358 EnumSummaryCalculationType calc_type_param) {
359 return get_summary(date, calc_type_param, PERIOD_HOURLY);
362 DataRange *DataReader::get_hourly_summary(Date *date) {
365 if (device_config == NULL) {
366 device_config = DeviceConfig::get_device_config(device_id);
367 summary_calc_type = device_config->get_summary_calculation_type();
369 ret_val = get_hourly_summary(date, summary_calc_type);
373 DataRange *DataReader::get_hourly_summary(Date *start_date,
383 date = start_date->clone();
384 while(date->before_or_equal_hour(end_date)) {
385 dta_lst = get_hourly_summary(date);
386 cnt = dta_lst->get_count();
387 for(ii = 0; ii < cnt; ii++) {
388 data = dta_lst->get(ii);
390 if (ret_val == NULL) {
391 ret_val = new DataRange(data);
405 DataRange *DataReader::get_data(Date *start_date,
408 EnumSummaryPeriod period;
411 period = get_period_type(start_date, end_date);
414 log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
415 ret_val = get_yearly_summary(start_date, end_date);
418 log_debug("get monthly summary\n");
419 ret_val = get_monthly_summary(start_date, end_date);
422 log_debug("get daily summary\n");
423 ret_val = get_daily_summary(start_date, end_date);
426 log_debug("get hourly summary\n");
427 ret_val = get_hourly_summary(start_date, end_date);
429 case PERIOD_MINUTELY:
430 log_debug("get minute summary data\n");
432 case PERIOD_SECONDLY:
434 log_debug("get second summary data\n");
440 string DataReader::get_device_id() {
445 * Read device type from the device specific config file.
447 * @rerurn string representing device type.
448 * In the case of error, an empty string is returned.
450 string DataReader::get_device_type() {
453 if (device_config == NULL) {
454 device_config = DeviceConfig::get_device_config(device_id);
456 if (device_config != NULL) {
457 ret_val = device_config->get_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE);