/* * W1DataList.cc * * Created on: Nov 7, 2010 * Author: lamikr */ #include #include #include #include #include "W1Util.hh" #include "W1DataList.hh" #include "W1Store.hh" #include "plp/log.h" using namespace w1; using namespace std; using namespace plp; W1DataList::W1DataList(string device_id_param) { string base_dir; device_id = device_id_param; base_dir = W1Store::get_store_base_dir(); 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); } W1DataList::~W1DataList() { //delete(device_dir); } Data *W1DataList::find_first_data(vector year_vector) { int ii; string year_dir; string month_dir; vector month_vector; vector data_vector; string f_name; W1Store *store; Data *ret_val; ret_val = NULL; if (year_vector.size() > 0) { // dirs are alphabetically sorted year_dir = year_vector.at(0); year_dir = W1Util::concat_paths(device_dir, year_dir); month_vector = W1Util::get_subdirectories(year_dir); for (ii = 0; ii < month_vector.size(); ii++) { month_dir = month_vector.at(ii); month_dir = W1Util::concat_paths(year_dir, month_dir); // scan data files from month dir data_vector = W1Util::get_data_files(month_dir); if (data_vector.size() > 0) { f_name = data_vector.at(0); f_name = W1Util::concat_paths(month_dir, f_name); store = new W1Store(f_name); ret_val = store->load_first_data_row(); delete(store); break; } } } return ret_val; } Data *W1DataList::find_last_data(vector year_vector) { int ii; string year_dir; string month_dir; vector month_vector; vector data_vector; string f_name; Data *ret_val; int size; W1Store *store; ret_val = NULL; size = year_vector.size(); if (size > 0) { // dirs are alphabetically sorted year_dir = year_vector.at(size - 1); year_dir = W1Util::concat_paths(device_dir, year_dir); month_vector = W1Util::get_subdirectories(year_dir); for (ii = month_vector.size() - 1; ii >= 0; ii--) { month_dir = month_vector.at(ii); month_dir = W1Util::concat_paths(year_dir, month_dir); // scan data files from month dir data_vector = W1Util::get_data_files(month_dir); size = data_vector.size(); if (size > 0) { f_name = data_vector.at(size - 1); f_name = W1Util::concat_paths(month_dir, f_name); store = new W1Store(f_name); ret_val = store->load_last_data_row(); delete(store); break; } } } return ret_val; } DataRange *W1DataList::get_data_range() { DataRange *ret_val; DIR *data_dir; struct dirent *year_dirent; vector year_list; Data *first_data; Data *last_data; ret_val = NULL; year_list = W1Util::get_subdirectories(device_dir); first_data = find_first_data(year_list); if (first_data != NULL) { last_data = find_last_data(year_list); if (last_data != NULL) { ret_val = new DataRange(*first_data); ret_val->add_data(*last_data); delete(last_data); } delete(first_data); } return ret_val; } /* long int get_date_as_seconds(struct tm *date) { long int ret_val; ret_val = date->tm date->tm_hour * 3600 + date->tm_min * 60 + date->tm_sec; } */ long int get_interval_type(Date *start_date, Date *end_date) { int diff; int ret_val; ret_val = 0; diff = end_date->year - start_date->year; if (diff != 0) { ret_val = 0; } else { diff = end_date->month - start_date->month; if (diff != 0) { ret_val = 1; } else { diff = end_date->day - start_date->day; if (diff != 0) { ret_val = 2; } else { diff = end_date->hour - start_date->hour; if (diff != 0) { ret_val = 3; } else { diff = end_date->min - start_date->min; if (diff != 0) { ret_val = 4; } else { ret_val = 5; } } } } } return ret_val; } string W1DataList::get_day_data(Date *date) { string ret_val; ret_val = W1Store::get_store_file_name(device_id, date); return ret_val; } Data *W1DataList::get_avg_day_data(Date *date) { return NULL; } /* time_t to_seconds2(const char *date) { struct tm storage={0,0,0,0,0,0,0,0,0}; char *p=NULL; time_t retval=0; p=(char *)strptime(date,"%d-%b-%Y",&storage); if(p==NULL) { retval=0; } else { errno = 0; retval =mktime(&storage); if (retval == ((time_t)-1)) { log_error("Error2 converting struct tm to seconds: %s\n", strerror(errno)); } else { log_debug("succ2: %ld\n", retval); } } return retval; } */ DataRange *W1DataList::get_avg_day_data(Date *start_date, Date *end_date) { DataRange *ret_val; Data *data; W1Store *store; Date *date; ret_val = NULL; date = start_date->clone(); while(date->before(*end_date)) { store = new W1Store(device_id, date); store->load(); data = store->get_mean(); data->printout(); if (ret_val == NULL) { ret_val = new DataRange(*data); } else { ret_val->add_data(*data); } delete(store); delete(data); date->tomorrow(); } delete(date); return ret_val; } DataRange *W1DataList::get_data(Date *start_date, Date *end_date) { DataRange *ret_val; int int_type; 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 avg year data\n"); break; case 1: log_debug("get avg month data\n"); break; case 2: log_debug("get avg day data\n"); ret_val = get_avg_day_data(start_date, end_date); break; case 3: log_debug("get avg hour data\n"); break; case 4: log_debug("get avg min data\n"); break; case 5: log_debug("get avg sec data\n"); break; } return ret_val; }