#include <list>
#include <string>
#include <fstream>
+#include <limits>
#include <time.h>
#include <dirent.h>
#include <plp/log.h>
+#include "DeviceConfig.hh"
#include "W1Configure.hh"
#include "W1Store.hh"
#include "W1Util.hh"
using namespace std;
using namespace w1;
+using namespace plp;
-#define DIR_BUFFER_SIZE 20
+W1Store::W1Store(string device_id,
+ Date *date_time) {
+ store_data = NULL;
+ range_data = NULL;
+ store_file_name = get_file_name(device_id, date_time);
+ log_debug("data file name: %s\n", store_file_name.c_str());
+}
+
+W1Store::W1Store(string file_name_param) {
+ store_data = NULL;
+ range_data = NULL;
+ store_file_name = file_name_param;
+}
-std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
+W1Store::~W1Store() {
+ if (store_data != NULL) {
+ delete(store_data);
+ store_data = NULL;
+ }
+}
+string W1Store::get_dir_name(string device_id, Date *date_time) {
+ string ret_val;
+ char buffer[30];
+ string d_name;
-W1Store::W1Store() {
- // TODO Auto-generated constructor stub
+ d_name = DeviceConfig::get_base_dir_name();
+ snprintf(buffer, 30, "%d/%02d", date_time->year, date_time->month);
+ ret_val = W1Util::concat_paths(d_name, device_id);
+ ret_val = ret_val + "/" + buffer;
+ return ret_val;
}
-W1Store::~W1Store() {
- // TODO Auto-generated destructor stub
+string W1Store::get_file_name(string device_id, Date *date_time) {
+ string ret_val;
+ 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_dir_name(device_id, date_time);
+ ret_val = W1Util::concat_paths(ret_val, fname);
+ return ret_val;
}
-void W1Store::set_location_base_dir(string location_param) {
- int pos;
- int b_count;
+void W1Store::save(string device_id,
+ std::list<Data *> *data_list,
+ int dec_precision) {
+ string n_path;
+ string f_path;
+ string line;
+ Data *data;
+ ofstream *ostream;
+ Date date;
+ list<Data *>::iterator iter;
- pos = location_param.find_last_of("/");
- b_count = location_param.length();
- if (pos == (b_count - 1)) {
- location_base_dir = location_param;
+ ostream = NULL;
+ f_path = "";
+ log_info("[%s] writing %d data values to save.\n", device_id.c_str(), data_list->size());
+ // TODO: add mutex to protect string_list while it's read and emptied
+ for(iter = data_list->begin(); iter != data_list->end(); iter++) {
+ data = (Data *)*iter;
+ date = data->get_date();
+ n_path = get_file_name(device_id, &date);
+ if (n_path.compare(f_path) != 0) {
+ if (ostream != NULL) {
+ ostream->close();
+ delete(ostream);
+ }
+ f_path = n_path;
+ log_info("[%s] Opening file for save: %s\n", device_id.c_str(), f_path.c_str());
+ ostream = W1Util::open_for_writing(f_path.c_str());
+ }
+ if ((ostream != NULL) &&
+ (ostream->is_open() == true)) {
+ line = data->to_string(dec_precision);
+ if (line.length() > 0) {
+ log_debug("storing line: %s\n", line.c_str());
+ *ostream << line << endl;
+ }
+ }
+ else {
+ log_error("[%s] File open for data save failed: %s\n", device_id.c_str(), f_path.c_str());
+ }
+ }
+ if (ostream != NULL) {
+ ostream->close();
+ delete(ostream);
+ }
+}
+
+bool W1Store::load() {
+ Data *data;
+ ifstream in;
+ string data_str;
+ bool ret_val;
+
+ ret_val = false;
+ if (store_data != NULL) {
+ delete(store_data);
+ store_data = NULL;
+ }
+ log_debug("opening file: %s\n", store_file_name.c_str());
+ in.open(store_file_name.c_str());
+ if (in.is_open() == true) {
+ while (in.eof() == false) {
+ getline(in, data_str);
+ if (data_str.empty() == false) {
+ data = Data::parse_string(data_str);
+ if (data != NULL) {
+ if (store_data == NULL) {
+ store_data = new DataRange(data);
+ }
+ else {
+ store_data->add_data(data);
+ }
+ delete(data);
+ }
+ }
+ }
+ ret_val = true;
}
else {
- location_base_dir = location_param + "/";
+ log_error("Could not load data from file: %s\n", store_file_name.c_str());
}
+ return ret_val;
}
-string W1Store::get_location_base_dir() {
- return location_base_dir;
+Data *W1Store::get_sum() {
+ int row_count;
+ int col_count;
+ double new_val;
+ int ii;
+ int jj;
+ Data *data;
+ Data *ret_val;
+ Date date;
+
+ ret_val = NULL;
+ data = NULL;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ log_debug("data row count: %d\n", row_count);
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ log_debug("data item count per row: %d\n", col_count);
+ ret_val = new Data(col_count);
+ if (col_count > 0) {
+ for (ii = 0; ii < row_count - 1; ii++) {
+ data = store_data->get_data(ii);
+ for (jj = 0; jj < col_count; jj++) {
+ new_val = data->value_arr[jj];
+ ret_val->value_arr[jj] = ret_val->value_arr[jj] + new_val;
+ }
+ if (ii < (row_count - 2)) {
+ delete(data);
+ data = NULL;
+ }
+ //log_debug("new val: %f, sum: %f\n", new_val, sum);
+ }
+ }
+ date = data->get_date();
+ ret_val->set_date(&date);
+ if (data != NULL) {
+ delete(data);
+ }
+ }
+ }
+ return ret_val;
}
-string W1Store::get_location_dir(string device_id, struct tm *ltime) {
- char buffer[DIR_BUFFER_SIZE];
- string year;
- string month;
- string ret_val;
+Data *W1Store::get_delta() {
+ int row_count;
+ int col_count;
+ Data *o_data;
+ Data *n_data;
+ Data *ret_val;
+ int ii;
+ DataRange *dr;
+ Date date;
- 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;
+ ret_val = NULL;
+ dr = get_oldest_and_newest_data();
+ if (dr != NULL) {
+ row_count = dr->get_data_row_count();
+ if (row_count == 2) {
+ o_data = dr->get_data(0);
+ n_data = dr->get_data(1);
+ col_count = dr->get_data_column_count();
+ ret_val = new Data(col_count);
+ if (col_count > 0) {
+ for (ii = 0; ii < col_count; ii++) {
+ log_debug("old_data[%d]: %f new data: %f\n", ii, o_data->value_arr[ii], n_data->value_arr[ii]);
+ ret_val->value_arr[ii] = n_data->value_arr[ii] - o_data->value_arr[ii];
+ }
+ }
+ date = n_data->get_date();
+ ret_val->set_date(&date);
+ delete(o_data);
+ delete(n_data);
+ }
+ delete(dr);
+ }
return ret_val;
}
-string W1Store::get_location_file(string device_id, struct tm *ltime) {
- char buffer[DIR_BUFFER_SIZE];
- string ret_val;
+Data *W1Store::get_mean() {
+ int row_count;
+ int col_count;
+ int ii;
+ Data *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;
+ ret_val = NULL;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ ret_val = get_sum();
+ if (col_count > 0) {
+ for (ii = 0; ii < col_count; ii++) {
+ ret_val->value_arr[ii] = ret_val->value_arr[ii] / row_count;
+ log_debug("avg: %f\n", ret_val->value_arr[ii]);
+ }
+ }
+ }
+ }
return ret_val;
}
-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;
-
- time(&wtime);
- ltime = localtime(&wtime);
- f_path = get_location_file(device_id, ltime);
- 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) {
- if (ostream->is_open()) {
- log_info("[%s] writing %d data values to file: %s\n", device_id.c_str(), string_list->size(), f_path.c_str());
- while(string_list->size() > 0) {
- line = string_list->front();
- string_list->pop_front();
- if (line.length() > 0) {
- log_debug("storing line: %s\n", line.c_str());
- *ostream << line << endl;
+Data *W1Store::get_max() {
+ int row_count;
+ int col_count;
+ double new_val;
+ int ii;
+ int jj;
+ Data *data;
+ Data *ret_val;
+ double min_val;
+ Date date;
+
+ ret_val = NULL;
+ data = NULL;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ log_debug("data row count: %d\n", row_count);
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ log_debug("data item count per row: %d\n", col_count);
+ min_val = numeric_limits<double>::min();
+ data = store_data->get_data(0);
+ ret_val = new Data(col_count,
+ min_val,
+ data->get_unit());
+ if (col_count > 0) {
+ for (ii = 0; ii < row_count - 1; ii++) {
+ data = store_data->get_data(ii);
+ for (jj = 0; jj < col_count; jj++) {
+ new_val = data->value_arr[jj];
+ if (new_val > ret_val->value_arr[jj]) {
+ ret_val->value_arr[jj] = new_val;
+ }
+ }
+ if (ii < (row_count - 2)) {
+ delete(data);
+ data = NULL;
+ }
}
}
- ostream->close();
+ date = data->get_date();
+ ret_val->set_date(&date);
+ if (data != NULL) {
+ delete(data);
+ }
}
- else {
- log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
+ }
+ return ret_val;
+}
+
+Data *W1Store::get_min() {
+ int row_count;
+ int col_count;
+ double new_val;
+ int ii;
+ int jj;
+ Data *data;
+ Data *ret_val;
+ double max_val;
+ Date date;
+
+ ret_val = NULL;
+ data = NULL;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ log_debug("data row count: %d\n", row_count);
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ log_debug("data item count per row: %d\n", col_count);
+ max_val = numeric_limits<double>::max();
+ data = store_data->get_data(0);
+ ret_val = new Data(col_count,
+ max_val,
+ data->get_unit());
+ if (col_count > 0) {
+ for (ii = 0; ii < row_count - 1; ii++) {
+ data = store_data->get_data(ii);
+ for (jj = 0; jj < col_count; jj++) {
+ new_val = data->value_arr[jj];
+ if (new_val < ret_val->value_arr[jj]) {
+ ret_val->value_arr[jj] = new_val;
+ }
+ }
+ if (ii < (row_count - 2)) {
+ delete(data);
+ data = NULL;
+ }
+ }
+ }
+ date = data->get_date();
+ ret_val->set_date(&date);
+ if (data != NULL) {
+ delete(data);
+ }
+ }
+ }
+ return ret_val;
+}
+
+vector<Data *> W1Store::get_mean(int freq_sec) {
+ int row_count;
+ int col_count;
+ int d_count;
+ int jj;
+ int ii;
+ Data *data;
+ Data *calc;
+ Date *limit_d;
+ Date date;
+ vector<Data *> ret_val;
+
+ calc = NULL;
+ limit_d = NULL;
+ d_count = 1;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ if (col_count > 0) {
+ for (ii = 0; ii < row_count; ii++) {
+ data = store_data->get_data(ii);
+ if (data != NULL) {
+ if (calc == NULL) {
+ d_count = 1;
+ calc = data->clone();
+ limit_d = data->get_date().clone();
+ limit_d->min = 0;
+ limit_d->sec = 0;
+ limit_d->inc_seconds(freq_sec);
+ }
+ else {
+ date = data->get_date();
+ if (date.before(*limit_d)) {
+ for (jj = 0; jj < col_count; jj++) {
+ calc->value_arr[jj] = calc->value_arr[jj] + data->value_arr[jj];
+ }
+ d_count++;
+ }
+ else {
+ for (jj = 0; jj < col_count; jj++) {
+ calc->value_arr[jj] = calc->value_arr[jj] / d_count;
+ }
+ ret_val.push_back(calc);
+ d_count = 1;
+ calc = data->clone();
+ if (limit_d != NULL) {
+ delete(limit_d);
+ }
+ limit_d = data->get_date().clone();
+ limit_d->min = 0;
+ limit_d->sec = 0;
+ limit_d->inc_seconds(freq_sec);
+ }
+ }
+ delete(data);
+ }
+ }
+ if (calc != NULL) {
+ delete(calc);
+ calc = NULL;
+ }
+ if (limit_d != NULL) {
+ delete(limit_d);
+ }
+ }
+ }
+ }
+ return ret_val;
+}
+
+vector<Data *> W1Store::get_delta(int freq_sec) {
+ int row_count;
+ int col_count;
+ int jj;
+ int ii;
+ Data *data;
+ Data *calc1;
+ Data *calc2;
+ Date *limit_d;
+ Date date;
+ vector<Data *> ret_val;
+
+ calc1 = NULL;
+ calc2 = NULL;
+ limit_d = NULL;
+ if (store_data == NULL) {
+ load();
+ }
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ if (row_count > 0) {
+ col_count = store_data->get_data_column_count();
+ if (col_count > 0) {
+ for (ii = 0; ii < row_count; ii++) {
+ data = store_data->get_data(ii);
+ if (data != NULL) {
+ if (calc1 == NULL) {
+ calc1 = data->clone();
+ limit_d = data->get_date().clone();
+ limit_d->min = 0;
+ limit_d->sec = 0;
+ limit_d->inc_seconds(freq_sec);
+ if (calc2 != NULL) {
+ delete(calc2);
+ }
+ calc2 = NULL;
+ }
+ else {
+ date = data->get_date();
+ if (date.before(*limit_d)) {
+ if (calc2 != NULL) {
+ delete(calc2);
+ }
+ calc2 = data->clone();
+ }
+ else {
+ if (calc2 == NULL) {
+ calc2 = calc1->clone();
+ }
+ for (jj = 0; jj < col_count; jj++) {
+ calc2->value_arr[jj] = calc2->value_arr[jj] - calc1->value_arr[jj];
+ }
+ ret_val.push_back(calc2);
+ delete(calc1);
+ calc1 = data->clone();
+ calc2 = NULL; // do not delete calc2 as it's stored to array
+ if (limit_d != NULL) {
+ delete(limit_d);
+ }
+ limit_d = data->get_date().clone();
+ limit_d->min = 0;
+ limit_d->sec = 0;
+ limit_d->inc_seconds(freq_sec);
+ }
+ }
+ delete(data);
+ }
+ }
+ if (calc1 != NULL) {
+ delete(calc1);
+ calc1 = NULL;
+ }
+ if (calc2 != NULL) {
+ delete(calc2);
+ calc2 = NULL;
+ }
+ if (limit_d != NULL) {
+ delete(limit_d);
+ }
+ }
+ }
+ }
+ return ret_val;
+}
+
+DataRange *W1Store::get_oldest_and_newest_data() {
+ DataRange *ret_val;
+ ifstream in;
+ Data *o_data;
+ Data *n_data;
+ string latest;
+ int row_count;
+ string line;
+ string prev_line;
+
+ ret_val = NULL;
+ o_data = NULL;
+ n_data = NULL;
+ if (store_data != NULL) {
+ row_count = store_data->get_data_row_count();
+ if (row_count > 0) {
+ o_data = store_data->get_data(0);
+ n_data = store_data->get_data(row_count - 1);
}
- delete(ostream);
}
else {
- log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
+ if (range_data != NULL) {
+ row_count = range_data->get_data_row_count();
+ if (row_count > 0) {
+ o_data = range_data->get_data(0);
+ n_data = range_data->get_data(row_count - 1);
+ }
+ }
+ else {
+ in.open(store_file_name.c_str());
+ while (in.eof() == false) {
+ getline(in, line);
+ if (line.empty() == false) {
+ if (o_data == NULL) {
+ o_data = Data::parse_string(line);
+ }
+ prev_line = line;
+ }
+ }
+ if (prev_line.empty() == false) {
+ n_data = Data::parse_string(prev_line);
+ }
+ }
+ }
+ if ((o_data != NULL) &&
+ (n_data != NULL)) {
+ ret_val = new DataRange(o_data);
+ ret_val->add_data(n_data);
+ if (range_data != NULL) {
+ range_data = new DataRange(o_data);
+ range_data->add_data(n_data);
+ }
}
+ if (o_data != NULL) {
+ delete(o_data);
+ }
+ if (n_data != NULL) {
+ delete(n_data);
+ }
+ return ret_val;
+}
+
+Data *W1Store::get_oldest_data() {
+ int row_count;
+ Data *ret_val;
+ DataRange *dr;
+
+ ret_val = NULL;
+ dr = get_oldest_and_newest_data();
+ if (dr != NULL) {
+ row_count = dr->get_data_row_count();
+ if (row_count >= 1) {
+ ret_val = dr->get_data(0);
+ }
+ delete(dr);
+ }
+ return ret_val;
+}
+
+Data *W1Store::get_newest_data() {
+ int row_count;
+ Data *ret_val;
+ DataRange *dr;
+
+ ret_val = NULL;
+ dr = get_oldest_and_newest_data();
+ if (dr != NULL) {
+ row_count = dr->get_data_row_count();
+ if (row_count == 2) {
+ ret_val = dr->get_data(1);
+ }
+ delete(dr);
+ }
+ return ret_val;
}