#include <list>
#include <string>
#include <fstream>
+#include <valarray>
#include <time.h>
#include <dirent.h>
using namespace std;
using namespace w1;
+using namespace plp;
-#define DIR_BUFFER_SIZE 20
-
-std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
+std::string W1Store::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
+DataRange *data_range = NULL;
+W1Store::W1Store(string device_id,
+ Date *date_time) {
+ data_range = NULL;
+ store_file_name = get_store_file_name(device_id, date_time);
+ log_debug("data file name: %s\n", store_file_name.c_str());
+}
-W1Store::W1Store() {
- // TODO Auto-generated constructor stub
+W1Store::W1Store(string file_name_param) {
+ data_range = NULL;
+ store_file_name = file_name_param;
}
W1Store::~W1Store() {
- // TODO Auto-generated destructor stub
+ if (data_range != NULL) {
+ delete(data_range);
+ data_range = NULL;
+ }
}
-void W1Store::set_location_base_dir(string location_param) {
+void W1Store::set_store_base_dir(string store_param) {
int pos;
int b_count;
- pos = location_param.find_last_of("/");
- b_count = location_param.length();
+ pos = store_param.find_last_of("/");
+ b_count = store_param.length();
if (pos == (b_count - 1)) {
- location_base_dir = location_param;
+ store_base_dir = store_param;
}
else {
- location_base_dir = location_param + "/";
+ store_base_dir = store_param + "/";
}
}
-string W1Store::get_location_base_dir() {
- return location_base_dir;
+string W1Store::get_store_base_dir() {
+ return store_base_dir;
}
-string W1Store::get_location_dir(string device_id, struct tm *ltime) {
- char buffer[DIR_BUFFER_SIZE];
- string year;
- string month;
+string W1Store::get_store_dir_name(string device_id, Date *date_time) {
string ret_val;
+ char buffer[30];
- strftime(buffer, DIR_BUFFER_SIZE, "%Y", ltime);
- year = buffer;
- strftime(buffer, DIR_BUFFER_SIZE, "%m", ltime);
- month = buffer;
- ret_val = W1Util::concat_paths(location_base_dir, device_id);
- ret_val = ret_val + "/" + year + "/" + month;
+ snprintf(buffer, 30, "%d/%02d", date_time->year, date_time->month);
+ ret_val = W1Util::concat_paths(store_base_dir, device_id);
+ ret_val = ret_val + "/" + buffer;
return ret_val;
}
-string W1Store::get_location_file(string device_id, struct tm *ltime) {
- char buffer[DIR_BUFFER_SIZE];
+string W1Store::get_store_file_name(string device_id, Date *date_time) {
string ret_val;
-
- strftime(buffer, DIR_BUFFER_SIZE, "%Y-%m-%d", ltime);
- ret_val = get_location_dir(device_id, ltime);
- ret_val = ret_val + "/" + buffer + DATAFILE_SUFFIX;
+ string fname;
+ char buffer[30];
+
+ snprintf(buffer, 30, "%d-%02d-%02d", date_time->year, date_time->month, date_time->day);
+ fname = buffer;
+ fname = fname + DATAFILE_SUFFIX;
+ ret_val = get_store_dir_name(device_id, date_time);
+ ret_val = W1Util::concat_paths(ret_val, fname);
return ret_val;
}
-void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
+void W1Store::store(std::string device_id,
+ std::list<std::string> *string_list) {
string f_path;
string line;
- time_t wtime;
- struct tm *ltime;
ofstream *ostream;
+ Date *date;
- time(&wtime);
- ltime = localtime(&wtime);
- f_path = get_location_file(device_id, ltime);
+ date = new Date();
+ f_path = get_store_file_name(device_id, date);
ostream = W1Util::open_for_writing(f_path.c_str());
// TODO: add mutex to protect string_list while it's read and emptied
if (ostream != NULL) {
else {
log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
}
+ delete(date);
+}
+
+void W1Store::load() {
+ Data *data;
+ ifstream in;
+ string line;
+
+ if (data_range != NULL) {
+ delete(data_range);
+ data_range = NULL;
+ }
+ in.open(store_file_name.c_str());
+ if (in.is_open() == true) {
+ while (in.eof() == false) {
+ getline(in, line);
+ data = W1Util::parse_data_line(line);
+ if (data_range == NULL) {
+ data_range = new DataRange(*data);
+ }
+ else {
+ data_range->add_data(*data);
+ }
+ delete(data);
+ }
+ }
+}
+
+Data *W1Store::get_mean() {
+ int d_count;
+ int i_count;
+ double avg;
+ double new_val;
+ int ii;
+ int jj;
+ Date *date;
+ Data *data;
+ Data *ret_val;
+
+ ret_val = NULL;
+ if (data_range == NULL) {
+ load();
+ }
+ if (data_range != NULL) {
+ d_count = data_range->get_data_row_count();
+ log_debug("data row count: %d\n", d_count);
+ if (d_count > 0) {
+ i_count = data_range->get_data_column_count();
+ log_debug("data item count per row: %d\n", i_count);
+ ret_val = new Data(i_count);
+ if (i_count > 0) {
+ for (ii = 0; ii < d_count - 1; ii++) {
+ data = data_range->get_data(ii);
+ for (jj = 0; jj < i_count; jj++) {
+ new_val = data->value_arr[jj];
+ ret_val->value_arr[jj] = ret_val->value_arr[jj] + new_val;
+ }
+ if (ii < (d_count - 2)) {
+ delete(data);
+ }
+ //log_debug("new val: %f, sum: %f\n", new_val, sum);
+ }
+ for (ii = 0; ii < i_count; ii++) {
+ ret_val->value_arr[ii] = ret_val->value_arr[ii] / d_count;
+ log_debug("avg: %f\n", ret_val->value_arr[ii]);
+ }
+ }
+ ret_val->set_date(data->get_date());
+ delete(data);
+ }
+ }
+ return ret_val;
+}
+
+Data *W1Store::load_first_data_row() {
+ Data *ret_val;
+ ifstream in;
+ string line;
+
+ ret_val = NULL;
+ in.open(store_file_name.c_str());
+ if (in.eof() == false) {
+ getline(in, line);
+ ret_val = W1Util::parse_data_line(line);
+ }
+ return ret_val;
+}
+
+Data *W1Store::load_last_data_row() {
+ Data *ret_val;
+ ifstream in;
+ string line;
+ string prev_line;
+
+ ret_val = NULL;
+ in.open(store_file_name.c_str());
+ while (in.eof() == false) {
+ getline(in, line);
+ if (line.empty() == false) {
+ prev_line = line;
+ }
+ }
+ if (prev_line.empty() == false) {
+ ret_val = W1Util::parse_data_line(prev_line);
+ }
+ return ret_val;
}