4 * Created on: Oct 31, 2010
15 #include <sys/types.h>
21 #include "W1Configure.hh"
29 std::string W1Store::store_base_dir = DEFAULT_STORAGE_BASE_DIR;
30 DataRange *data_range = NULL;
32 W1Store::W1Store(string device_id,
35 store_file_name = get_store_file_name(device_id, date_time);
36 log_debug("data file name: %s\n", store_file_name.c_str());
39 W1Store::W1Store(string file_name_param) {
41 store_file_name = file_name_param;
45 if (data_range != NULL) {
51 void W1Store::set_store_base_dir(string store_param) {
55 pos = store_param.find_last_of("/");
56 b_count = store_param.length();
57 if (pos == (b_count - 1)) {
58 store_base_dir = store_param;
61 store_base_dir = store_param + "/";
65 string W1Store::get_store_base_dir() {
66 return store_base_dir;
69 string W1Store::get_store_dir_name(string device_id, Date *date_time) {
73 snprintf(buffer, 30, "%d/%02d", date_time->year, date_time->month);
74 ret_val = W1Util::concat_paths(store_base_dir, device_id);
75 ret_val = ret_val + "/" + buffer;
79 string W1Store::get_store_file_name(string device_id, Date *date_time) {
84 snprintf(buffer, 30, "%d-%02d-%02d", date_time->year, date_time->month, date_time->day);
86 fname = fname + DATAFILE_SUFFIX;
87 ret_val = get_store_dir_name(device_id, date_time);
88 ret_val = W1Util::concat_paths(ret_val, fname);
92 void W1Store::store(std::string device_id,
93 std::list<std::string> *string_list) {
100 f_path = get_store_file_name(device_id, date);
101 ostream = W1Util::open_for_writing(f_path.c_str());
102 // TODO: add mutex to protect string_list while it's read and emptied
103 if (ostream != NULL) {
104 if (ostream->is_open()) {
105 log_info("[%s] writing %d data values to file: %s\n", device_id.c_str(), string_list->size(), f_path.c_str());
106 while(string_list->size() > 0) {
107 line = string_list->front();
108 string_list->pop_front();
109 if (line.length() > 0) {
110 log_debug("storing line: %s\n", line.c_str());
111 *ostream << line << endl;
117 log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
122 log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
127 void W1Store::load() {
132 if (data_range != NULL) {
136 in.open(store_file_name.c_str());
137 if (in.is_open() == true) {
138 while (in.eof() == false) {
140 data = W1Util::parse_data_line(line);
141 if (data_range == NULL) {
142 data_range = new DataRange(*data);
145 data_range->add_data(*data);
152 Data *W1Store::get_mean() {
164 if (data_range == NULL) {
167 if (data_range != NULL) {
168 d_count = data_range->get_data_row_count();
169 log_debug("data row count: %d\n", d_count);
171 i_count = data_range->get_data_column_count();
172 log_debug("data item count per row: %d\n", i_count);
173 ret_val = new Data(i_count);
175 for (ii = 0; ii < d_count - 1; ii++) {
176 data = data_range->get_data(ii);
177 for (jj = 0; jj < i_count; jj++) {
178 new_val = data->value_arr[jj];
179 ret_val->value_arr[jj] = ret_val->value_arr[jj] + new_val;
181 if (ii < (d_count - 2)) {
184 //log_debug("new val: %f, sum: %f\n", new_val, sum);
186 for (ii = 0; ii < i_count; ii++) {
187 ret_val->value_arr[ii] = ret_val->value_arr[ii] / d_count;
188 log_debug("avg: %f\n", ret_val->value_arr[ii]);
191 ret_val->set_date(data->get_date());
198 Data *W1Store::load_first_data_row() {
204 in.open(store_file_name.c_str());
205 if (in.eof() == false) {
207 ret_val = W1Util::parse_data_line(line);
212 Data *W1Store::load_last_data_row() {
219 in.open(store_file_name.c_str());
220 while (in.eof() == false) {
222 if (line.empty() == false) {
226 if (prev_line.empty() == false) {
227 ret_val = W1Util::parse_data_line(prev_line);