#include <list>
#include <string>
-#include <iostream>
#include <fstream>
+#include <valarray>
#include <time.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <plp/log.h>
+
+#include "W1Configure.hh"
#include "W1Store.hh"
+#include "W1Util.hh"
using namespace std;
using namespace w1;
+using namespace plp;
+
+std::string W1Store::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
-std::string W1Store::location = "/tmp/";
+W1Store::W1Store(string device_id,
+ Date *date_time) {
+ store_data = NULL;
+ range_data = 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) {
+ store_data = NULL;
+ range_data = NULL;
+ store_file_name = file_name_param;
}
W1Store::~W1Store() {
- // TODO Auto-generated destructor stub
+ if (store_data != NULL) {
+ delete(store_data);
+ store_data = NULL;
+ }
+}
+
+void W1Store::set_store_base_dir(string store_param) {
+ int pos;
+ int b_count;
+
+ pos = store_param.find_last_of("/");
+ b_count = store_param.length();
+ if (pos == (b_count - 1)) {
+ store_base_dir = store_param;
+ }
+ else {
+ store_base_dir = store_param + "/";
+ }
+}
+
+string W1Store::get_store_base_dir() {
+ return store_base_dir;
}
-void W1Store::set_location(string location_param) {
- location = location_param;
+string W1Store::get_store_dir_name(string device_id, Date *date_time) {
+ string ret_val;
+ char buffer[30];
+
+ 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;
}
-void W1Store::store(std::string device_id, std::list<std::string> string_list) {
+string W1Store::get_store_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_store_dir_name(device_id, date_time);
+ ret_val = W1Util::concat_paths(ret_val, fname);
+ return ret_val;
+}
- string file_path = location + device_id + ".txt";
- string text_line;
- ofstream data_file(file_path.c_str(), ios::app);
+void W1Store::store(std::string device_id,
+ std::list<std::string> *string_list) {
+ string f_path;
+ string line;
+ ofstream *ostream;
+ Date *date;
- cout << "storing to " << file_path << ", data size " << string_list.size() << endl;
+ 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 (data_file.is_open()) {
- while(string_list.size() > 0) {
- text_line = string_list.front();
- string_list.pop_front();
- if (text_line.length() > 0) {
- cout << "storing line: " << text_line << endl;
- data_file << text_line << endl;
+ 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;
+ }
+ }
+ ostream->close();
+ }
+ else {
+ log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
+ }
+ delete(ostream);
+ }
+ 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 (store_data != NULL) {
+ delete(store_data);
+ store_data = 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 (store_data == NULL) {
+ store_data = new DataRange(*data);
+ }
+ else {
+ store_data->add_data(*data);
+ }
+ delete(data);
+ }
+ }
+}
+
+Data *W1Store::get_delta() {
+ int row_count;
+ int col_count;
+ Data *o_data;
+ Data *n_data;
+ Data *ret_val;
+ int ii;
+ 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) {
+ 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++) {
+ ret_val->value_arr[ii] = n_data->value_arr[ii] - o_data->value_arr[ii];
+ }
}
+ ret_val->set_date(n_data->get_date());
+ delete(o_data);
+ delete(n_data);
+ }
+ delete(dr);
+ }
+ return ret_val;
+}
+
+Data *W1Store::get_mean() {
+ int row_count;
+ int col_count;
+ double avg;
+ double new_val;
+ int ii;
+ int jj;
+ Date *date;
+ Data *data;
+ Data *ret_val;
+
+ 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);
+ }
+ 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]);
+ }
+ }
+ ret_val->set_date(data->get_date());
+ if (data != NULL) {
+ delete(data);
+ }
+ }
+ }
+ 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);
}
- data_file.close();
}
else {
- cout << "could not open file " << file_path << " for writing data." << endl;
+ 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 = W1Util::parse_data_line(line);
+ }
+ prev_line = line;
+ }
+ }
+ if (prev_line.empty() == false) {
+ n_data = W1Util::parse_data_line(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;
+ int col_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;
+ int col_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;
}