4 * Created on: Nov 7, 2010
15 #include "DeviceData.hh"
16 #include "StoreDay.hh"
17 #include "StoreCache.hh"
18 #include "DeviceConfig.hh"
27 template <class NumberDataType>
28 bool string_to_number(NumberDataType& result,
29 const std::string& string_param,
30 std::ios_base& (*format)(std::ios_base&))
32 istringstream iss(string_param);
33 return !(iss >> format >> result).fail();
36 DeviceData::DeviceData(string device_id_param) {
40 device_id = device_id_param;
41 base_dir = DeviceConfig::get_base_dir_name();
42 device_dir = W1Util::concat_paths(base_dir, device_id);
43 device_ch_dir = W1Util::concat_paths(base_dir, "cache");
44 device_ch_dir = W1Util::concat_paths(device_ch_dir, device_id);
47 DeviceData::~DeviceData() {
48 if (device_config != NULL) {
49 delete(device_config);
54 Data *DeviceData::find_newest_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_newest_data(&date, device_id, PERIOD_YEARLY);
73 Data *DeviceData::find_oldest_data(vector<string> year_name_vector_param) {
81 size = year_name_vector_param.size();
83 // dirs are alphabetically sorted
84 year_name = year_name_vector_param.at(0);
85 string_to_number<int>(val_int, year_name, dec);
87 ret_val = StoreCache::get_oldest_data(&date, device_id, PERIOD_YEARLY);
92 DataRange *DeviceData::get_data_range() {
94 vector<string> y_list;
99 y_list = W1Util::get_subdirectories(device_dir);
100 o_data = find_oldest_data(y_list);
101 if (o_data != NULL) {
102 n_data = find_newest_data(y_list);
103 if (n_data != NULL) {
104 ret_val = new DataRange(o_data);
105 ret_val->add(n_data);
113 EnumSummaryPeriod get_period_type(Date *start_date,
116 EnumSummaryPeriod ret_val;
118 ret_val = PERIOD_YEARLY;
119 diff = end_date->year - start_date->year;
121 ret_val = PERIOD_YEARLY;
124 diff = end_date->month - start_date->month;
126 ret_val = PERIOD_MONTHLY;
129 diff = end_date->day - start_date->day;
131 ret_val = PERIOD_DAILY;
134 diff = end_date->hour - start_date->hour;
136 ret_val = PERIOD_HOURLY;
139 diff = end_date->min - start_date->min;
141 ret_val = PERIOD_MINUTELY;
144 ret_val = PERIOD_SECONDLY;
153 DataRange *DeviceData::get_summary(Date *date_param,
154 EnumSummaryCalculationType calc_type_param,
155 EnumSummaryPeriod period_type_param) {
160 //store = new StoreDay(device_id, date_param);
161 store = new StoreCache(device_id, date_param);
163 switch(calc_type_param) {
165 ret_val = store->get_sum(period_type_param);
168 ret_val = store->get_delta(period_type_param);
171 ret_val = store->get_max(period_type_param);
174 ret_val = store->get_min(period_type_param);
178 ret_val = store->get_mean(period_type_param);
181 if (ret_val != NULL) {
185 log_error("Could not read data log for device: %s\n", device_id.c_str());
189 log_error("Could not read data log for device: %s\n", device_id.c_str());
195 DataRange *DeviceData::get_yearly_summary(Date *date,
196 EnumSummaryCalculationType calc_type_param) {
197 return get_summary(date, calc_type_param, PERIOD_YEARLY);
200 DataRange *DeviceData::get_yearly_summary(Date *date) {
203 if (device_config == NULL) {
204 device_config = Factory::get_device_config(device_id);
205 summary_calc_type = device_config->get_summary_calculation_type();
207 ret_val = get_yearly_summary(date, summary_calc_type);
211 DataRange *DeviceData::get_yearly_summary(Date *start_date,
218 date = start_date->clone();
219 while(date->before_or_equal_year(end_date)) {
220 data = get_yearly_summary(date);
222 if (ret_val == NULL) {
223 ret_val = new DataRange(data);
236 DataRange *DeviceData::get_monthly_summary(Date *date,
237 EnumSummaryCalculationType calc_type_param) {
238 return get_summary(date, calc_type_param, PERIOD_MONTHLY);
241 DataRange *DeviceData::get_monthly_summary(Date *date) {
244 if (device_config == NULL) {
245 device_config = Factory::get_device_config(device_id);
246 summary_calc_type = device_config->get_summary_calculation_type();
248 ret_val = get_monthly_summary(date, summary_calc_type);
252 DataRange *DeviceData::get_monthly_summary(Date *start_date,
259 date = start_date->clone();
260 while(date->before_or_equal_month(end_date)) {
261 data = get_monthly_summary(date);
263 if (ret_val == NULL) {
264 ret_val = new DataRange(data);
277 DataRange *DeviceData::get_daily_summary(Date *date,
278 EnumSummaryCalculationType calc_type_param) {
279 return get_summary(date, calc_type_param, PERIOD_DAILY);
282 DataRange *DeviceData::get_daily_summary(Date *date) {
285 if (device_config == NULL) {
286 device_config = Factory::get_device_config(device_id);
287 summary_calc_type = device_config->get_summary_calculation_type();
289 ret_val = get_daily_summary(date, summary_calc_type);
293 DataRange *DeviceData::get_daily_summary(Date *start_date,
300 date = start_date->clone();
301 while(date->before_or_equal_day(end_date)) {
302 data = get_daily_summary(date);
304 if (ret_val == NULL) {
305 ret_val = new DataRange(data);
318 DataRange *DeviceData::get_hourly_summary(Date *date,
319 EnumSummaryCalculationType calc_type_param) {
320 return get_summary(date, calc_type_param, PERIOD_HOURLY);
323 DataRange *DeviceData::get_hourly_summary(Date *date) {
326 if (device_config == NULL) {
327 device_config = Factory::get_device_config(device_id);
328 summary_calc_type = device_config->get_summary_calculation_type();
330 ret_val = get_hourly_summary(date, summary_calc_type);
334 DataRange *DeviceData::get_hourly_summary(Date *start_date,
344 date = start_date->clone();
345 while(date->before_or_equal_hour(end_date)) {
346 dta_lst = get_hourly_summary(date);
347 cnt = dta_lst->get_count();
348 for(ii = 0; ii < cnt; ii++) {
349 data = dta_lst->get(ii);
351 if (ret_val == NULL) {
352 ret_val = new DataRange(data);
366 DataRange *DeviceData::get_data(Date *start_date,
369 EnumSummaryPeriod period;
372 start_date->printout();
373 end_date->printout();
374 period = get_period_type(start_date, end_date);
377 log_debug("get yearly summary: %s - %s\n", start_date->to_string().c_str(), end_date->to_string().c_str());
378 ret_val = get_yearly_summary(start_date, end_date);
381 log_debug("get monthly summary\n");
382 ret_val = get_monthly_summary(start_date, end_date);
385 log_debug("get daily summary\n");
386 ret_val = get_daily_summary(start_date, end_date);
389 log_debug("get hourly summary\n");
390 ret_val = get_hourly_summary(start_date, end_date);
392 case PERIOD_MINUTELY:
393 log_debug("get minute summary data\n");
395 case PERIOD_SECONDLY:
397 log_debug("get second summary data\n");