]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Store.cc
support for sum, min and max queryes from the collected data.
[lib1wire.git] / src / W1Store.cc
index 4f35376762bd72758a4f2a3d285b6d28ae859a16..effea2e27e47df37adcb3ecd8da623444542ca59 100644 (file)
@@ -74,7 +74,8 @@ string W1Store::get_file_name(string device_id, Date *date_time) {
 }
 
 void W1Store::save(string device_id,
-               std::list<Data *> *data_list) {
+               std::list<Data *> *data_list,
+               int dec_precision) {
        string                  n_path;
        string                  f_path;
        string                  line;
@@ -102,7 +103,7 @@ void W1Store::save(string device_id,
                }
                if ((ostream != NULL) &&
                    (ostream->is_open() == true)) {
-                       line    = data->to_string();
+                       line    = data->to_string(dec_precision);
                        if (line.length() > 0) {
                                log_debug("storing line: %s\n", line.c_str());
                                *ostream << line << endl;
@@ -366,7 +367,74 @@ Data *W1Store::get_min() {
        return ret_val;
 }
 
-vector<Data *> W1Store::get_mean(int freq_sec) {
+vector<Data *> *W1Store::get_sum(int freq_sec) {
+       int             row_count;
+       int             col_count;
+       int             jj;
+       int             ii;
+       Data            *data;
+       Data            *calc;
+       Date            *limit_d;
+       Date            date;
+       vector<Data *>  *ret_val;
+
+       ret_val = new vector<Data *>();
+       calc    = 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 (calc == NULL) {
+                                                       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];
+                                                               }
+                                                       }
+                                                       else {
+                                                               ret_val->push_back(calc);
+                                                               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_mean(int freq_sec) {
        int             row_count;
        int             col_count;
        int             d_count;
@@ -376,8 +444,9 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
        Data            *calc;
        Date            *limit_d;
        Date            date;
-       vector<Data *>  ret_val;
+       vector<Data *>  *ret_val;
 
+       ret_val = new vector<Data *>();
        calc    = NULL;
        limit_d = NULL;
        d_count = 1;
@@ -412,7 +481,7 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
                                                                for (jj = 0; jj < col_count; jj++) {
                                                                        calc->value_arr[jj]     = calc->value_arr[jj] / d_count;
                                                                }
-                                                               ret_val.push_back(calc);
+                                                               ret_val->push_back(calc);
                                                                d_count         = 1;
                                                                calc            = data->clone();
                                                                if (limit_d != NULL) {
@@ -440,7 +509,7 @@ vector<Data *> W1Store::get_mean(int freq_sec) {
        return ret_val;
 }
 
-vector<Data *> W1Store::get_delta(int freq_sec) {
+vector<Data *> *W1Store::get_delta(int freq_sec) {
        int             row_count;
        int             col_count;
        int             jj;
@@ -450,8 +519,9 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
        Data            *calc2;
        Date            *limit_d;
        Date            date;
-       vector<Data *>  ret_val;
+       vector<Data *>  *ret_val;
 
+       ret_val = new vector<Data *>();
        calc1   = NULL;
        calc2   = NULL;
        limit_d = NULL;
@@ -492,7 +562,7 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
                                                                for (jj = 0; jj < col_count; jj++) {
                                                                        calc2->value_arr[jj]    = calc2->value_arr[jj] - calc1->value_arr[jj];
                                                                }
-                                                               ret_val.push_back(calc2);
+                                                               ret_val->push_back(calc2);
                                                                delete(calc1);
                                                                calc1   = data->clone();
                                                                calc2   = NULL; // do not delete calc2 as it's stored to array
@@ -525,6 +595,98 @@ vector<Data *> W1Store::get_delta(int freq_sec) {
        return ret_val;
 }
 
+vector<Data *> *W1Store::get_max_or_min(int freq_sec, bool max) {
+       int             row_count;
+       int             col_count;
+       int             jj;
+       int             ii;
+       Data            *data;
+       Data            *calc;
+       Date            *limit_d;
+       Date            date;
+       vector<Data *>  *ret_val;
+
+       ret_val = new vector<Data *>();
+       calc    = 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 (calc == NULL) {
+                                                       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)) {
+                                                               if (max == true) {
+                                                                       for (jj = 0; jj < col_count; jj++) {
+                                                                               if (calc->value_arr[jj] < data->value_arr[jj]) {
+                                                                                       calc->value_arr[jj]     = data->value_arr[jj];
+                                                                               }
+                                                                       }
+                                                               }
+                                                               else {
+                                                                       for (jj = 0; jj < col_count; jj++) {
+                                                                               if (data->value_arr[jj] < calc->value_arr[jj]) {
+                                                                                       calc->value_arr[jj]     = data->value_arr[jj];
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                                       else {
+                                                               ret_val->push_back(calc);
+                                                               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_max(int freq_sec) {
+       vector<Data *>  *ret_val;
+
+       ret_val = get_max_or_min(freq_sec, true);
+       return ret_val;
+}
+
+vector<Data *> *W1Store::get_min(int freq_sec) {
+       vector<Data *>  *ret_val;
+
+       ret_val = get_max_or_min(freq_sec, false);
+       return ret_val;
+}
+
 DataRange *W1Store::get_oldest_and_newest_data() {
        DataRange       *ret_val;
        ifstream        in;