]> pilppa.com Git - lib1wire.git/commitdiff
started adding support for reading average and sum data...
authorMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 00:22:49 +0000 (02:22 +0200)
committerMika Laitio <lamikr@pilppa.org>
Thu, 9 Dec 2010 00:22:49 +0000 (02:22 +0200)
13 files changed:
src/Data.cc [new file with mode: 0644]
src/Data.hh [new file with mode: 0644]
src/Date.cc [new file with mode: 0644]
src/Date.hh [new file with mode: 0644]
src/Makefile.am
src/W1DataList.cc
src/W1DataList.hh
src/W1Store.cc
src/W1Store.hh
src/W1Util.cc
src/W1Util.hh
src_test/test_w1_datalog_read.cc
src_test/test_w1_datalog_write.cc

diff --git a/src/Data.cc b/src/Data.cc
new file mode 100644 (file)
index 0000000..d9c6e1a
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * DataRange.cc
+ *
+ *  Created on: Dec 7, 2010
+ *      Author: lamikr
+ */
+
+#include <iostream>
+
+#include <stdio.h>
+#include <time.h>
+#include <malloc.h>
+
+#include "Data.hh"
+#include <plp/log.h>
+
+using namespace std;
+using namespace plp;
+using namespace w1;
+
+Data::Data(int size) {
+       value_arr.resize(size);
+}
+
+Data::Data(vector<double> vector_param, Date *date_param) {
+       int     ii;
+       int     size;
+
+       size    = vector_param.size();
+       //log_debug("Data(), value count: %d\n", size);
+       value_arr.resize(size);
+       for (int ii = 0; ii < vector_param.size(); ii++) {
+               value_arr[ii]   = vector_param.at(ii);
+               //log_debug("Data(), value[%d]: %f\n", ii, value_arr[ii]);
+       }
+       date_time.copy(date_param);
+}
+
+Data::Data(std::valarray<double> value_arr_param, Date *date_param) {
+       value_arr.resize(value_arr_param.size());
+       for (int ii = 0; ii < value_arr_param.size(); ii++) {
+               value_arr[ii]   = value_arr_param[ii];
+       }
+       date_time.copy(date_param);
+}
+
+Data::~Data() {
+}
+
+plp::Date Data::get_date() {
+       return date_time;
+}
+
+void Data::set_date(plp::Date date) {
+       date_time.copy(&date);
+}
+
+void Data::printout() {
+       int     ii;
+
+       date_time.printout();
+       for (ii = 0; ii < value_arr.size(); ii++) {
+               log_debug("  data[%d] = %f\n", ii, value_arr[ii]);
+       }
+}
+
+DataRange::DataRange(int value_count_per_data_item) {
+       val_matrix      = NULL;
+       column_count    = value_count_per_data_item;
+       row_count       = 0;
+}
+
+DataRange::DataRange(Data data) {
+       val_matrix      = NULL;
+       column_count    = data.value_arr.size();
+       row_count       = 0;
+       add_data(data);
+}
+
+DataRange::~DataRange() {
+       int     ii;
+       Date    *date;
+
+       if (val_matrix != NULL) {
+               free(val_matrix);
+               val_matrix      = NULL;
+       }
+       for (ii = 0; ii < date_list.size(); ii++) {
+               date    = date_list.at(ii);
+               delete(date);
+       }
+}
+
+void DataRange::add_data(Data data) {
+       int                     ii;
+       int                     r_count;
+       int                     indx;
+       Date                    date;
+
+       //log_debug("old row_count: %d, column_count: %d, value_arr_size: %d\n", row_count, column_count, data.value_arr.size());
+       val_matrix      = (double *)realloc(val_matrix, ((row_count + 1) * column_count) * sizeof(double));
+       indx            = row_count * column_count;
+       for (ii = 0; ii < data.value_arr.size(); ii++) {
+               val_matrix[indx + ii]   = data.value_arr[ii];
+       }
+/*
+       for (int ii = 0; ii < ((row_count + 1) * column_count); ii++) {
+               log_debug("data_matrix[%d] = %f\n", ii, val_matrix[ii]);
+       }
+*/
+       date    = data.get_date();
+       date_list.push_back(date.clone());
+       row_count++;
+}
+
+Data *DataRange::get_data(int row_index) {
+       Data            *ret_val;
+       int             start_indx;
+       int             ii;
+       vector<double>  vector;
+       Date            *date;
+
+       ret_val = NULL;
+       if ((row_index >= 0) &&
+           (row_index < row_count)) {
+               start_indx      = row_index * column_count;
+               for (ii = 0; ii < column_count; ii++) {
+                       vector.push_back(val_matrix[start_indx + ii]);
+               }
+               date    = date_list.at(row_index);
+               ret_val = new Data(vector, date);
+       }
+       return ret_val;
+}
+
+int DataRange::get_data_row_count() {
+       return row_count;
+}
+
+int DataRange::get_data_column_count() {
+       return column_count;
+}
+
+Data *DataRange::get_first_data() {
+       Data    *ret_val;
+
+       ret_val = NULL;
+       if (row_count > 0) {
+               ret_val = get_data(0);
+       }
+       return ret_val;
+}
+
+Data *DataRange::get_last_data() {
+       return get_data(row_count - 1);
+}
diff --git a/src/Data.hh b/src/Data.hh
new file mode 100644 (file)
index 0000000..59d42ce
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Data.hh
+ *
+ *  Created on: Dec 8, 2010
+ *      Author: lamikr
+ */
+
+#ifndef DATA_HH_
+#define DATA_HH_
+
+#include <string>
+#include <valarray>
+#include <vector>
+#include <hash_map>
+
+#include "Date.hh"
+
+namespace w1 {
+       class Data {
+               public:
+                       Data(int size);
+                       Data(std::vector<double> vector_param, plp::Date *date_param);
+                       Data(std::valarray<double> value_arr_param, plp::Date *date_param);
+                       virtual ~Data();
+                       plp::Date get_date();
+                       void set_date(plp::Date date);
+                       void printout();
+                       std::valarray<double>   value_arr;
+               private:
+                       plp::Date               date_time;
+       };
+
+       class DataRange {
+               public:
+                       DataRange(Data data);
+                       DataRange(int value_count_per_data_item);
+                       virtual ~DataRange();
+                       void add_data(Data data);
+                       Data *get_data(int row_index);
+                       Data *get_first_data();
+                       Data *get_last_data();
+                       int get_data_row_count();
+                       int get_data_column_count();
+               protected:
+                       double                          *val_matrix;
+                       std::vector<plp::Date *>        date_list;
+                       int                             row_count;
+                       int                             column_count;
+       };
+}
+
+#endif /* DATA_HH_ */
diff --git a/src/Date.cc b/src/Date.cc
new file mode 100644 (file)
index 0000000..8327d15
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+ * Date.cc
+ *
+ *  Created on: Dec 7, 2010
+ *      Author: lamikr
+ */
+
+#include <iostream>
+
+#include <stdio.h>
+#include <time.h>
+#include <malloc.h>
+
+#include "Date.hh"
+
+using namespace std;
+using namespace plp;
+
+static const int CONST__DAYS_PER_MONTH[]       = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+
+Date::Date() {
+       time_t          wtime;
+       struct tm       *ltime;
+
+       time(&wtime);
+       ltime   = localtime(&wtime);
+       year    = ltime->tm_year;
+       month   = ltime->tm_mon;
+       day     = ltime->tm_mday;
+       hour    = ltime->tm_hour;
+       min     = ltime->tm_min;
+       sec     = ltime->tm_sec;
+}
+
+Date::Date(int year_param,
+       int month_param,
+       int day_param,
+       int hour_param,
+       int min_param,
+       int sec_param) {
+       year    = year_param;
+       month   = month_param;
+       day     = day_param;
+       hour    = hour_param;
+       min     = min_param;
+       sec     = sec_param;
+}
+
+Date::~Date() {
+       // TODO Auto-generated destructor stub
+}
+
+bool Date::is_leap_year() {
+       bool    ret_val;
+
+       ret_val = false;
+       if ((year % 4 == 0) &&
+           (year % 400 == 0) || (year % 100 != 0)) {
+               ret_val = true;
+       }
+       return ret_val;
+}
+
+void Date::printout() {
+       cout << "date: " << year << " " << month << " " << day << " " << hour << " " << min << " " << sec << endl;
+}
+
+Date *Date::clone() {
+       Date    *ret_val;
+
+       ret_val = new Date(this->year,
+                       this->month,
+                       this->day,
+                       this->hour,
+                       this->min,
+                       this->sec);
+       return ret_val;
+}
+
+void Date::copy(Date *date) {
+       this->year      = date->year;
+       this->month     = date->month;
+       this->day       = date->day;
+       this->hour      = date->hour;
+       this->min       = date->min;
+       this->sec       = date->sec;
+}
+
+bool Date::before(Date date2) {
+       bool    ret_val;
+       string s1 = this->to_string();
+       string s2 = date2.to_string();
+
+       ret_val = false;
+       if (s1.compare(s2) < 0) {
+               ret_val = true;
+       }
+       return ret_val;
+}
+
+bool Date::equals(Date date2) {
+       bool ret_val;
+
+       ret_val = false;
+       if ((this->sec == date2.sec) &&
+           (this->min == date2.min) &&
+           (this->hour == date2.hour) &&
+           (this->day == date2.day) &&
+           (this->month == date2.month) &&
+           (this->year == date2.year)) {
+               ret_val = true;
+       }
+       return ret_val;
+}
+
+void Date::tomorrow() {
+       if ((month > 0) &&
+           (month <= 12)) {
+               day++;
+               if (day > CONST__DAYS_PER_MONTH[month - 1]) {
+                       if ((month == 2) &&
+                           (is_leap_year() == true) &&
+                           (day == 29)) {
+                               // ok
+                       }
+                       else {
+                               day     = 1;
+                               month++;
+                               if (month == 12) {
+                                       year++;
+                                       month   = 1;
+                               }
+                       }
+               }
+       }
+}
+
+string Date::to_string() {
+       char    buffer[30];
+       string  ret_val;
+
+       int n, a=5, b=3;
+       sprintf(buffer, "%016d%02d%02d%02d%02d%02d", year, month, day, hour, min, sec);
+       ret_val = buffer;
+       return ret_val;
+}
diff --git a/src/Date.hh b/src/Date.hh
new file mode 100644 (file)
index 0000000..b2b908d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Date.hh
+ *
+ *  Created on: Dec 7, 2010
+ *      Author: lamikr
+ */
+
+#ifndef DATE_HH_
+#define DATE_HH_
+
+#include <string>
+
+namespace plp {
+       class Date {
+               public:
+                       Date();
+                       Date(int year_param,
+                            int month_param,
+                            int day_param,
+                            int hour_param,
+                            int min_param,
+                            int sec_param);
+                       virtual ~Date();
+                       void printout();
+                       bool is_leap_year();
+                       void tomorrow();
+                       Date *clone();
+                       void copy(Date *date);
+                       bool before(Date date2);
+                       bool equals(Date date2);
+                       std::string to_string();
+
+                       int     year;
+                       int     month;
+                       int     day;
+                       int     hour;
+                       int     min;
+                       int     sec;
+               private:
+                       //static const int arr_days_per_month[];
+       };
+}
+
+#endif /* DATE_HH_ */
index 8065dbad93b8d6c565805b2db5362af285855123..d99707102595f00a03992fc0fd69744a124ae6f0 100644 (file)
@@ -7,6 +7,8 @@ lib1wire_la_SOURCES = \
        W1CounterDevice.cc W1CounterDevice.hh \
        W1Util.cc W1Util.hh \
        W1DataList.cc W1DataList.hh \
+       Data.cc Data.hh \
+       Date.cc Date.hh \
        W1Configure.hh
 lib1wire_la_LDFLAGS = $(SRC_LIBS) $(all_libraries) -version-info 1:0:0 -no-undefined
 AM_CPPFLAGS = $(SRC_CFLAGS) -Iidl
@@ -19,4 +21,5 @@ lib1wireinclude_HEADERS = \
        W1Scanner.hh \
        W1Store.hh \
        W1TemperatureSensor.hh \
+       Date.hh \
        W1CounterDevice.hh
\ No newline at end of file
index 3d04f955ea708e3fb898649ccd385e7395494c10..949454cf3aa0bb2935bb1f3b2e2878b636be79f9 100644 (file)
@@ -6,35 +6,27 @@
  */
 #include <dirent.h>
 #include <malloc.h>
+#include <errno.h>
+#include <string.h>
 
 #include "W1Util.hh"
 #include "W1DataList.hh"
 #include "W1Store.hh"
 
+#include "plp/log.h"
+
 using namespace w1;
 using namespace std;
-
-Data::~Data() {
-       free(date_time);
-}
-
-DataRange::~DataRange() {
-       if (first_data != NULL) {
-               delete(first_data);
-               first_data      = NULL;
-       }
-       if (last_data != NULL) {
-               delete(last_data);
-               last_data       = NULL;
-       }
-}
+using namespace plp;
 
 W1DataList::W1DataList(string device_id_param) {
        string          base_dir;
 
        device_id       = device_id_param;
-       base_dir        = W1Store::get_location_base_dir();
-       device_dir      = W1Util::concat_paths(base_dir.c_str(), device_id.c_str());
+       base_dir        = W1Store::get_store_base_dir();
+       device_dir      = W1Util::concat_paths(base_dir, device_id);
+       device_ch_dir   = W1Util::concat_paths(base_dir, "cache");
+       device_ch_dir   = W1Util::concat_paths(device_ch_dir, device_id);
 }
 
 W1DataList::~W1DataList() {
@@ -47,24 +39,27 @@ Data *W1DataList::find_first_data(vector<string> year_vector) {
        string          month_dir;
        vector<string>  month_vector;
        vector<string>  data_vector;
-       string          first_file;
+       string          f_name;
+       W1Store         *store;
        Data            *ret_val;
 
        ret_val = NULL;
        if (year_vector.size() > 0) {
                // dirs are alphabetically sorted
                year_dir        = year_vector.at(0);
-               year_dir        = W1Util::concat_paths(device_dir.c_str(), year_dir.c_str());
+               year_dir        = W1Util::concat_paths(device_dir, year_dir);
                month_vector    = W1Util::get_subdirectories(year_dir);
                for (ii = 0; ii < month_vector.size(); ii++) {
                        month_dir       = month_vector.at(ii);
-                       month_dir       = W1Util::concat_paths(year_dir.c_str(), month_dir.c_str());
+                       month_dir       = W1Util::concat_paths(year_dir, month_dir);
                        // scan data files from month dir
                        data_vector     = W1Util::get_data_files(month_dir);
                        if (data_vector.size() > 0) {
-                               first_file      = data_vector.at(0);
-                               first_file      = W1Util::concat_paths(month_dir.c_str(), first_file.c_str());
-                               ret_val         = W1Util::load_first_data_row(first_file.c_str());
+                               f_name  = data_vector.at(0);
+                               f_name  = W1Util::concat_paths(month_dir, f_name);
+                               store   = new W1Store(f_name);
+                               ret_val = store->load_first_data_row();
+                               delete(store);
                                break;
                        }
                }
@@ -78,27 +73,30 @@ Data *W1DataList::find_last_data(vector<string> year_vector) {
        string          month_dir;
        vector<string>  month_vector;
        vector<string>  data_vector;
-       string          last_file;
+       string          f_name;
        Data            *ret_val;
        int             size;
+       W1Store         *store;
 
        ret_val = NULL;
        size    = year_vector.size();
        if (size > 0) {
                // dirs are alphabetically sorted
                year_dir        = year_vector.at(size - 1);
-               year_dir        = W1Util::concat_paths(device_dir.c_str(), year_dir.c_str());
+               year_dir        = W1Util::concat_paths(device_dir, year_dir);
                month_vector    = W1Util::get_subdirectories(year_dir);
                for (ii = month_vector.size() - 1; ii >= 0; ii--) {
                        month_dir       = month_vector.at(ii);
-                       month_dir       = W1Util::concat_paths(year_dir.c_str(), month_dir.c_str());
+                       month_dir       = W1Util::concat_paths(year_dir, month_dir);
                        // scan data files from month dir
                        data_vector     = W1Util::get_data_files(month_dir);
                        size    = data_vector.size();
                        if (size > 0) {
-                               last_file       = data_vector.at(size - 1);
-                               last_file       = W1Util::concat_paths(month_dir.c_str(), last_file.c_str());
-                               ret_val         = W1Util::load_last_data_row(last_file.c_str());
+                               f_name  = data_vector.at(size - 1);
+                               f_name  = W1Util::concat_paths(month_dir, f_name);
+                               store   = new W1Store(f_name);
+                               ret_val = store->load_last_data_row();
+                               delete(store);
                                break;
                        }
                }
@@ -120,10 +118,159 @@ DataRange *W1DataList::get_data_range() {
        if (first_data != NULL) {
                last_data       = find_last_data(year_list);
                if (last_data != NULL) {
-                       ret_val = new DataRange();
-                       ret_val->first_data     = first_data;
-                       ret_val->last_data      = last_data;
+                       ret_val = new DataRange(*first_data);
+                       ret_val->add_data(*last_data);
+                       delete(last_data);
                }
+               delete(first_data);
+       }
+       return ret_val;
+}
+/*
+long int get_date_as_seconds(struct tm *date) {
+       long int ret_val;
+
+       ret_val = date->tm
+               date->tm_hour * 3600 +
+               date->tm_min * 60 +
+               date->tm_sec;
+}
+*/
+
+long int get_interval_type(Date *start_date,
+                       Date *end_date) {
+       int     diff;
+       int     ret_val;
+
+       ret_val = 0;
+       diff    = end_date->year - start_date->year;
+       if (diff != 0) {
+               ret_val = 0;
+       }
+       else {
+               diff    = end_date->month - start_date->month;
+               if (diff != 0) {
+                       ret_val = 1;
+               }
+               else {
+                       diff    = end_date->day - start_date->day;
+                       if (diff != 0) {
+                               ret_val = 2;
+                       }
+                       else {
+                               diff    = end_date->hour - start_date->hour;
+                               if (diff != 0) {
+                                       ret_val = 3;
+                               }
+                               else {
+                                       diff    = end_date->min - start_date->min;
+                                       if (diff != 0) {
+                                               ret_val = 4;
+                                       }
+                                       else {
+                                               ret_val = 5;
+                                       }
+                               }
+                       }
+               }
+       }
+       return ret_val;
+}
+
+string W1DataList::get_day_data(Date *date) {
+       string ret_val;
+
+       ret_val = W1Store::get_store_file_name(device_id, date);
+       return ret_val;
+}
+
+Data *W1DataList::get_avg_day_data(Date *date) {
+       return NULL;
+}
+
+/*
+time_t to_seconds2(const char *date)
+{
+        struct tm storage={0,0,0,0,0,0,0,0,0};
+        char *p=NULL;
+        time_t retval=0;
+
+        p=(char *)strptime(date,"%d-%b-%Y",&storage);
+        if(p==NULL) {
+                retval=0;
+        }
+        else
+        {
+               errno   = 0;
+                retval =mktime(&storage);
+                if (retval == ((time_t)-1)) {
+                       log_error("Error2 converting struct tm to seconds: %s\n", strerror(errno));
+                }
+                else {
+                       log_debug("succ2: %ld\n", retval);
+                }
+        }
+        return retval;
+}
+*/
+
+DataRange *W1DataList::get_avg_day_data(Date *start_date,
+                                       Date *end_date) {
+       DataRange       *ret_val;
+       Data            *data;
+       W1Store         *store;
+       Date            *date;
+
+       ret_val = NULL;
+       date    = start_date->clone();
+       while(date->before(*end_date)) {
+               store   = new W1Store(device_id, date);
+               store->load();
+               data    = store->get_mean();
+               data->printout();
+               if (ret_val == NULL) {
+                       ret_val = new DataRange(*data);
+               }
+               else {
+                       ret_val->add_data(*data);
+               }
+               delete(store);
+               delete(data);
+               date->tomorrow();
+       }
+       delete(date);
+       return ret_val;
+}
+
+DataRange *W1DataList::get_data(Date *start_date,
+                               Date *end_date) {
+       DataRange       *ret_val;
+       int             int_type;
+
+       ret_val         = NULL;
+       start_date->printout();
+       end_date->printout();
+       int_type        = get_interval_type(start_date, end_date);
+       switch(int_type) {
+               case 0:
+                       log_debug("get avg year data\n");
+                       break;
+               case 1:
+                       log_debug("get avg month data\n");
+                       break;
+               case 2:
+                       log_debug("get avg day data\n");
+                       ret_val = get_avg_day_data(start_date, end_date);
+                       break;
+               case 3:
+                       log_debug("get avg hour data\n");
+                       break;
+               case 4:
+                       log_debug("get avg min data\n");
+                       break;
+               case 5:
+                       log_debug("get avg sec data\n");
+                       break;
        }
        return ret_val;
 }
index 7823876083ffc5915a0b14b32e39ac3ef97a40ec..dd2259a0a6f07faf36a436c13a2d164b5afd8768 100644 (file)
 
 #include <string>
 #include <vector>
-#include <time.h>
 
-struct Data {
-       public:
-               virtual ~Data();
-               struct tm               *date_time;
-               std::vector<double>     data_list;
-};
+#include "Data.hh"
+#include "Date.hh"
 
-struct DataRange {
-       public:
-               virtual ~DataRange();
-               Data    *first_data;
-               Data    *last_data;
-};
+#include <time.h>
 
 namespace w1 {
        class W1DataList {
@@ -32,9 +22,14 @@ namespace w1 {
                        W1DataList(std::string device_id);
                        virtual ~W1DataList();
                        DataRange *get_data_range();
+                       Data *get_avg_day_data(plp::Date *date);
+                       DataRange *get_avg_day_data(plp::Date *start_date, plp::Date *end_date);
+                       DataRange *get_data(plp::Date *start_date, plp::Date *end_date);
                protected:
                        std::string     device_id;
                        std::string     device_dir;
+                       std::string     device_ch_dir;
+                       std::string get_day_data(plp::Date *date);
                        Data *find_first_data(std::vector<std::string> year_vector);
                        Data *find_last_data(std::vector<std::string> year_vector);
 
index f40623e6c3a9ff3b2268d984a68d871727dc6be7..d59e12121bf5999cb005cab53725f2e22f7f9296 100644 (file)
@@ -8,6 +8,7 @@
 #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) {
@@ -113,4 +121,110 @@ void W1Store::store(std::string device_id, std::list<std::string> *string_list)
        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;
 }
index 396b419d005d5d6ca89bcb6175ccbc8d3428b76e..528b19a996503677ae4af2486b2c2058c5d374ed 100644 (file)
 #include <string>
 #include <list>
 
+#include "Data.hh"
+#include "Date.hh"
+
 namespace w1 {
        class W1Store {
                public:
-                       W1Store();
+                       W1Store(std::string device_id,
+                               plp::Date *date_time);
+                       W1Store(std::string file_name_param);
                        virtual ~W1Store();
-                       static std::string get_location_base_dir();
-                       static void set_location_base_dir(std::string location_param);
-                       static std::string get_location_dir(std::string device_id, struct tm *ltime);
-                       static std::string get_location_file(std::string device_id, struct tm *ltime);
+                       static std::string get_store_base_dir();
+                       static void set_store_base_dir(std::string store_param);
+                       static std::string get_store_dir_name(std::string device_id, plp::Date *ltime);
+                       static std::string get_store_file_name(std::string device_id, plp::Date *ltime);
                        static void store(std::string device_id, std::list<std::string> *string_list);
+                       void load();
+                       Data *get_sum();
+                       Data *get_mean();
+                       w1::Data *load_first_data_row();
+                       w1::Data *load_last_data_row();
                protected:
-                       static std::string location_base_dir;
+                       static std::string store_base_dir;
+                       std::string store_file_name;
+                       DataRange *data_range;
        };
 }
 
index 001f9f63c293d5677f3aa9a9bfd67ba4e1d18329..c5773bc781f2cdb805c770ace2d1e9389b2a32ce 100644 (file)
@@ -22,6 +22,7 @@
 
 using namespace std;
 using namespace w1;
+using namespace plp;
 
 template <class NumberDataType>
 bool string_to_number(NumberDataType& result,
@@ -38,20 +39,6 @@ W1Util::W1Util() {
 W1Util::~W1Util() {
 }
 
-void W1Util::printout_date(struct tm *date_time) {
-       cout << "date: " << date_time->tm_year << " " << date_time->tm_mon << " " << date_time->tm_mday << " " << date_time->tm_hour << " " << date_time->tm_min << " " << date_time->tm_sec << endl;
-}
-
-void W1Util::printout_data(Data *data) {
-       int     ii;
-       if (data != NULL) {
-               printout_date(data->date_time);
-               for (ii = 0; ii < data->data_list.size(); ii++) {
-                       log_debug("  data[%d] = %f\n", ii, data->data_list.at(ii));
-               }
-       }
-}
-
 char *W1Util::parse_directory_path(const char *file_path) {
        char    *p;
        size_t  b_count;
@@ -301,85 +288,42 @@ vector<string> W1Util::get_data_files(const string& path) {
        return ret_val;
 }
 
-struct tm *W1Util::parse_date_str(string date_str) {
-       stringstream ss(date_str);
-       struct tm *ret_val = NULL;
+Date W1Util::parse_date_str(string date_str) {
+       char            c;
+       stringstream    ss(date_str);
+       Date            ret_val;
 
-       //ss << "2007-07-19 17:18:01";
-       int year;
-       int month;
-       int day;
-       int hour;
-       int min;
-       int sec;
-       char c;
-
-       ret_val = (struct tm*)malloc(sizeof(struct tm));
-       ss >>ret_val->tm_year >>c >>ret_val->tm_mon >>c >>ret_val->tm_mday >>ret_val->tm_hour >>c >>ret_val->tm_min >>c >>ret_val->tm_sec;
-       mktime(ret_val);
-       //printout_date(ret_val);
+       ss >>ret_val.year >>c >>ret_val.month >>c >>ret_val.day >>ret_val.hour >>c >>ret_val.min >>c >>ret_val.sec;
        return ret_val;
 }
 
 Data *W1Util::parse_data_line(const string& dataline) {
        stringstream    ss(dataline);
        string          item;
-       double          dbl;
+       double          val;
        Data            *ret_val;
        int             ii;
        bool            suc_flg;
+       vector<double>  v;
+       Date            date;
 
-       ret_val = new Data();
        ii      = 0;
        while(getline(ss, item, '|')) {
                if (ii == 0) {
                        // parse date
-                       ret_val->date_time      = parse_date_str(item);
-               }
-               else if (ii == 1) {
-                       // skip the device type for now.Maybe better to store only once under the year dir...
+                       date    = parse_date_str(item);
                }
-               else if (ii >= 2) {
-                       suc_flg = string_to_number<double>(dbl, item, dec);
+               // skip the device type and device id fields
+               // TODO: store device type and id to own file
+               else if (ii >= 3) {
+                       suc_flg = string_to_number<double>(val, item, dec);
                        if (suc_flg) {
-                               ret_val->data_list.push_back(dbl);
+                               //log_debug("adding number: %f\n", val);
+                               v.push_back(val);
                        }
                }
                ii++;
        }
-       return ret_val;
-}
-
-Data *W1Util::load_first_data_row(const string& datafile_path) {
-       Data            *ret_val;
-       ifstream        in;
-       string          line;
-
-       ret_val = NULL;
-       in.open(datafile_path.c_str());
-       if (in.eof() == false) {
-               getline(in, line);
-               ret_val = parse_data_line(line);
-       }
-       return ret_val;
-}
-
-Data *W1Util::load_last_data_row(const string& datafile_path) {
-       Data            *ret_val;
-       ifstream        in;
-       string          line;
-       string          prev_line;
-
-       ret_val = NULL;
-       in.open(datafile_path.c_str());
-       while (in.eof() == false) {
-               getline(in, line);
-               if (line.empty() == false) {
-                       prev_line       = line;
-               }
-       }
-       if (prev_line.empty() == false) {
-               ret_val = parse_data_line(prev_line);
-       }
+       ret_val = new Data(v, &date);
        return ret_val;
 }
index 57a7daf8af344376c9c64e37338feef61398286f..2ddf05adbc952dc3adc8e8b7c7000ff367a61744 100644 (file)
@@ -12,6 +12,8 @@
 #include <string>
 #include <vector>
 
+#include "Date.hh"
+
 #include <stdbool.h>
 #include <dirent.h>
 #include <stdbool.h>
@@ -29,12 +31,8 @@ namespace w1 {
                        static bool is_datafile(const char *path, dirent *direntry);
                        static std::vector<std::string> get_subdirectories(const std::string& path);
                        static std::vector<std::string> get_data_files(const std::string& path);
-                       static struct tm *parse_date_str(std::string date_str);
+                       static plp::Date parse_date_str(std::string date_str);
                        static Data *parse_data_line(const std::string& dataline);
-                       static Data *load_first_data_row(const std::string& datafile_path);
-                       static Data *load_last_data_row(const std::string& datafile_path);
-                       static void printout_date(struct tm *date_time);
-                       static void printout_data(Data *data);
                        static char *parse_directory_path(const char *file_path);
                        static bool mkdirs(char *path);
                        static std::ofstream *open_for_writing(const char *path);
index 49c567e746928102281487b34ac9670e106cd037..acddf0d6fd69d7d4c73c2695d19da07ab5a3c6ad 100644 (file)
@@ -47,30 +47,46 @@ bool try_parse_long(const char *str, long *result) {
 
 int main(int argc, char** argv) {
        int             round;
-       string          location;
+       string          loc;
        bool            err_flg;
-       W1DataList      *datalist;
+       Data            *fdata;
+       Data            *ldata;
+       W1DataList      *dlist;
        DataRange       *dr;
+       DataRange       *dr2;
 
        // default values than can be overwritten with parameters
        //location      = "/tmp/";
-       location        = "/home/lamikr/own/src/plp/w1data2/";
+       loc     = "/home/lamikr/own/src/plp/w1data2/";
        if (argc > 1) {
-               location        = argv[1];
-               log_info("storage location: %s\n", location.c_str());
+               loc     = argv[1];
+               log_info("storage location: %s\n", loc.c_str());
        }
        else {
-               log_warning("No storage location parameter given, using default location: %s\n", location.c_str());
+               log_warning("No storage location parameter given, using default location: %s\n", loc.c_str());
        }
-       W1Store::set_location_base_dir(location.c_str());
-       datalist        = new W1DataList("00080160c563");
-       if (datalist != NULL) {
-               dr      = datalist->get_data_range();
+       W1Store::set_store_base_dir(loc.c_str());
+       dlist   = new W1DataList("0008014e9e09");
+       if (dlist != NULL) {
+               dr      = dlist->get_data_range();
                if (dr != NULL) {
-                       W1Util::printout_data(dr->first_data);
+                       fdata   = dr->get_first_data();
+                       if (fdata != NULL) {
+                               fdata->printout();
+                               ldata   = dr->get_last_data();
+                               if (ldata != NULL) {
+                                       ldata->printout();
+                                       dr2     = dlist->get_data(&fdata->get_date(), &ldata->get_date());
+                                       delete(ldata);
+                                       if (dr2 != NULL) {
+                                               delete(dr2);
+                                       }
+                               }
+                               delete(fdata);
+                       }
                        delete(dr);
                }
-               delete(datalist);
+               delete(dlist);
        }
        return 0;
 }
index 5b5e6eb8d80d2dbe135a52eadc11e4758a4c29d0..db0febca0cdaf3fa9da16df0971b98c10f11c75d 100644 (file)
@@ -50,11 +50,12 @@ int main(int argc, char** argv) {
        long                    store_interval;
        string                  location;
        bool                    err_flg;
+       W1Device                *device;
 
        // default values than can be overwritten with parameters
        location        = "/tmp/";
-       scan_interval   = 600;
-       store_interval  = 6;
+       scan_interval   = 2; //600;
+       store_interval  = 2;
        if (argc > 1) {
                location        = argv[1];
                log_info("storage location: %s\n", location.c_str());
@@ -69,15 +70,17 @@ int main(int argc, char** argv) {
                try_parse_long(argv[3], &store_interval);
        }
        log_info("start scanning, data saved to location: %s, scan interval: %d, store interval: %d\n", location.c_str(), scan_interval, store_interval);
-       W1Store::set_location_base_dir(location.c_str());
+       W1Store::set_store_base_dir(location.c_str());
        scanner         = new W1Scanner();
        device_list     = scanner->get_device_list();
        round           = 0;
+       int     t = 0;
        if (device_list.size() > 0) {
-               while(1) {
+               while(t < 3) {
+                       t++;
                        round++;
                        for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
-                               W1Device *device = (W1Device *)*list_iter;
+                               device = (W1Device *)*list_iter;
 
                                device->printout();
                                sleep(1);
@@ -94,5 +97,12 @@ int main(int argc, char** argv) {
        else {
                log_debug("could not find 1-wire devices.\n");
        }
+       while (device_list.empty() == false) {
+               device  = device_list.back();
+               device_list.pop_back();
+               delete(device);
+       }
+
+       delete(scanner);
        return 0;
 }