Fix for "month" number in month folder and file-names when storing data.
Fixes for reading data, when the file containing data can not be opened.
Memory leak fixes.
Hourly data read fixes.
Signed-off-by: Mika Laitio <lamikr@pilppa.org>
unsigned int ii;
value_arr.resize(value_arr_param.size());
-
for (ii = 0; ii < value_arr_param.size(); ii++) {
value_arr[ii] = value_arr_param[ii];
}
Data::~Data() {
}
+Data *Data::clone() {
+ Data *ret_val;
+
+ ret_val = new Data(value_arr, &date_time);
+ return ret_val;
+}
+
plp::Date Data::get_date() {
return date_time;
}
Data *DataRange::get_last_data() {
return get_data(row_count - 1);
}
+
+void DataRange::printout() {
+ int ii;
+ Data *data;
+
+ log_debug("---- DataRange, number of data items: %d\n ----", row_count);
+ for (ii = 0; ii < row_count; ii++) {
+ data = get_data(ii);
+ if (data != NULL) {
+ data->printout();
+ delete(data);
+ }
+ }
+ log_debug("---- DataRange printout done ----\n");
+}
Data(std::vector<double> vector_param, plp::Date *date_param);
Data(std::valarray<double> value_arr_param, plp::Date *date_param);
virtual ~Data();
+ Data *clone();
plp::Date get_date();
void set_date(plp::Date date);
void printout();
Data *get_last_data();
int get_data_row_count();
int get_data_column_count();
+ void printout();
protected:
double *val_matrix;
std::vector<plp::Date *> date_list;
time(&wtime);
ltime = localtime(&wtime);
year = 1900 + ltime->tm_year;
- month = ltime->tm_mon;
+ month = ltime->tm_mon + 1; // ltime-month: values 0 - 11...
day = ltime->tm_mday;
hour = ltime->tm_hour;
min = ltime->tm_min;
}
}
+void Date::next_hour() {
+ if ((hour >= 0) &&
+ (hour <= 24)) {
+ hour++;
+ if (hour > 24) {
+ hour = 0;
+ tomorrow();
+ }
+ }
+}
+
+void Date::inc_minutes(int minutes) {
+ int day_c;
+ int hour_c;
+ int ii;
+
+ day_c = minutes / 1440;
+ minutes = minutes - day_c * 1440;
+ hour_c = minutes / 60;
+ minutes = minutes - hour_c * 60;
+ for (ii = 0; ii < day_c; ii++) {
+ tomorrow();
+ }
+ for (ii = 0; ii < hour_c; ii++) {
+ hour++;
+ if (hour > 24) {
+ hour = 0;
+ tomorrow();
+ }
+ }
+ min = min + minutes;
+ if (min >= 60) {
+ min = min % 60;
+ hour++;
+ if (hour > 24) {
+ hour = 0;
+ tomorrow();
+ }
+ }
+}
+
+void Date::inc_seconds(int seconds) {
+ int min_c;
+
+ min_c = seconds / 60;
+ seconds = seconds - min_c * 60;
+ inc_minutes(min_c);
+ sec = sec + seconds;
+ if (sec >= 60) {
+ sec = sec % 60;
+ inc_minutes(1);
+ }
+}
+
string Date::to_string() {
char buffer[30];
string ret_val;
void printout();
bool is_leap_year();
void tomorrow();
+ void next_hour();
+ void inc_minutes(int minutes);
+ void inc_seconds(int seconds);
Date *clone();
void copy(Date *date);
bool before(Date date2);
ret_val = MEAN;
if (device_type.empty() == false) {
- if (device_type.compare("counter") == 0) {
+ if (device_type.compare("Counter Device") == 0) {
ret_val = DELTA;
}
}
return ret_val;
}
-Data *DeviceData::get_daily_summary(Date *date) {
+Data *DeviceData::get_day_summary(Date *date) {
Data *ret_val;
W1Store *store;
+ bool suc_flg;
+
+ ret_val = NULL;
+ store = new W1Store(device_id, date);
+ if (store != NULL) {
+ suc_flg = store->load();
+ if (suc_flg == true) {
+ switch(summary_calc_type) {
+ case SUM:
+ ret_val = store->get_sum();
+ break;
+ case DELTA:
+ ret_val = store->get_delta();
+ break;
+ case MEAN:
+ default:
+ ret_val = store->get_mean();
+ break;
+ case MAX:
+ ret_val = store->get_max();
+ break;
+ case MIN:
+ ret_val = store->get_min();
+ break;
+ }
+ if (ret_val != NULL) {
+ ret_val->printout();
+ }
+ else {
+ log_error("Could not read data log for device: %s\n", device_id.c_str());
+ }
+ }
+ else {
+ log_error("Could not read data log for device: %s. Data file open load failed.\n", device_id.c_str());
+ }
+ }
+ else {
+ log_error("Could not read data log for device: %s\n", device_id.c_str());
+ }
+ delete(store);
+ return ret_val;
+}
+
+DataRange *DeviceData::get_daily_summary(Date *start_date,
+ Date *end_date) {
+ DataRange *ret_val;
+ Data *data;
+ Date *date;
+
+ ret_val = NULL;
+ date = start_date->clone();
+ while(date->before(*end_date)) {
+ data = get_day_summary(date);
+ if (data != NULL) {
+ if (ret_val == NULL) {
+ ret_val = new DataRange(*data);
+ }
+ else {
+ ret_val->add_data(*data);
+ }
+ delete(data);
+ }
+ date->tomorrow();
+ }
+ delete(date);
+ return ret_val;
+}
+
+vector<Data *> DeviceData::get_hourly_summary(Date *date) {
+ vector<Data *> ret_val;
+ W1Store * store;
store = new W1Store(device_id, date);
store->load();
switch(summary_calc_type) {
+/*
case SUM:
ret_val = store->get_sum();
break;
+*/
case DELTA:
- ret_val = store->get_delta();
+ ret_val = store->get_delta(3600);
break;
case MEAN:
default:
- ret_val = store->get_mean();
+ ret_val = store->get_mean(3600);
break;
+/*
case MAX:
ret_val = store->get_max();
break;
case MIN:
ret_val = store->get_min();
break;
+*/
}
- ret_val->printout();
delete(store);
-
return ret_val;
}
-DataRange *DeviceData::get_daily_summary(Date *start_date,
+DataRange *DeviceData::get_hourly_summary(Date *start_date,
Date *end_date) {
DataRange *ret_val;
+ vector<Data *> dta_list;
Data *data;
Date *date;
ret_val = NULL;
date = start_date->clone();
while(date->before(*end_date)) {
- data = get_daily_summary(date);
- if (ret_val == NULL) {
- ret_val = new DataRange(*data);
- }
- else {
- ret_val->add_data(*data);
+ dta_list = get_hourly_summary(date);
+ for(vector<Data *>::iterator list_iter = dta_list.begin(); list_iter != dta_list.end(); list_iter++) {
+ data = (Data *)*list_iter;
+ if (data != NULL) {
+ if (ret_val == NULL) {
+ ret_val = new DataRange(*data);
+ }
+ else {
+ ret_val->add_data(*data);
+ }
+ delete(data);
+ }
}
- delete(data);
date->tomorrow();
}
delete(date);
return ret_val;
}
-
DataRange *DeviceData::get_data(Date *start_date,
Date *end_date) {
DataRange *ret_val;
break;
case 1:
log_debug("get monthly summary\n");
+ ret_val = get_daily_summary(start_date, end_date);
break;
case 2:
log_debug("get daily summary\n");
break;
case 3:
log_debug("get hourly summary\n");
+ ret_val = get_hourly_summary(start_date, end_date);
break;
case 4:
log_debug("get minute summary data\n");
* Get summary data calculated from the daily data items that is meaning full.
* Depending from the device type, it may be daily mean value, daily delta, highest value, etc...
*/
- Data *get_daily_summary(plp::Date *date);
+ Data *get_day_summary(plp::Date *date);
DataRange *get_daily_summary(plp::Date *start_date, plp::Date *end_date);
+ std::vector<Data *> get_hourly_summary(plp::Date *date);
+ DataRange *get_hourly_summary(plp::Date *start_date, plp::Date *end_date);
DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
protected:
std::string device_id;
delete(date);
}
-void W1Store::load() {
+bool W1Store::load() {
Data *data;
ifstream in;
string line;
+ bool ret_val;
+ ret_val = false;
if (store_data != NULL) {
delete(store_data);
store_data = NULL;
}
delete(data);
}
+ ret_val = true;
}
+ else {
+ log_error("Could not load data from file: %s\n", store_file_name.c_str());
+ }
+ return ret_val;
}
Data *W1Store::get_sum() {
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;
#include <string>
#include <list>
+#include <vector>
+
+#include <stdbool.h>
#include "Data.hh"
#include "Date.hh"
static std::string get_dir_name(std::string device_id, plp::Date *ltime);
static std::string get_file_name(std::string device_id, plp::Date *ltime);
static void store(std::string device_id, std::list<std::string> *string_list);
- void load();
+ bool load();
Data *get_sum();
Data *get_delta();
Data *get_mean();
Data *get_max();
Data *get_min();
+ std::vector<w1::Data *> get_mean(int freq_sec);
+ std::vector<w1::Data *> get_delta(int freq_sec);
w1::Data *get_oldest_data();
w1::Data *get_newest_data();
w1::DataRange *get_oldest_and_newest_data();
d1 = fdata->get_date();
d2 = ldata->get_date();
dr2 = dta->get_data(&d1, &d2);
- delete(ldata);
if (dr2 != NULL) {
+ dr2->printout();
delete(dr2);
}
+ delete(ldata);
}
delete(fdata);
}