4 * Created on: Oct 31, 2010
14 #include <sys/types.h>
20 #include "W1Configure.hh"
27 #define DIR_BUFFER_SIZE 20
29 std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
33 // TODO Auto-generated constructor stub
37 // TODO Auto-generated destructor stub
40 void W1Store::set_location_base_dir(string location_param) {
44 pos = location_param.find_last_of("/");
45 b_count = location_param.length();
46 if (pos == (b_count - 1)) {
47 location_base_dir = location_param;
50 location_base_dir = location_param + "/";
54 string W1Store::get_location_base_dir() {
55 return location_base_dir;
58 string W1Store::get_location_dir(string device_id, struct tm *ltime) {
59 char buffer[DIR_BUFFER_SIZE];
64 strftime(buffer, DIR_BUFFER_SIZE, "%Y", ltime);
66 strftime(buffer, DIR_BUFFER_SIZE, "%m", ltime);
68 ret_val = W1Util::concat_paths(location_base_dir, device_id);
69 ret_val = ret_val + "/" + year + "/" + month;
73 string W1Store::get_location_file(string device_id, struct tm *ltime) {
74 char buffer[DIR_BUFFER_SIZE];
77 strftime(buffer, DIR_BUFFER_SIZE, "%Y-%m-%d", ltime);
78 ret_val = get_location_dir(device_id, ltime);
79 ret_val = ret_val + "/" + buffer + DATAFILE_SUFFIX;
83 void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
91 ltime = localtime(&wtime);
92 f_path = get_location_file(device_id, ltime);
93 ostream = W1Util::open_for_writing(f_path.c_str());
94 // TODO: add mutex to protect string_list while it's read and emptied
95 if (ostream != NULL) {
96 if (ostream->is_open()) {
97 log_info("[%s] writing %d data values to file: %s\n", device_id.c_str(), string_list->size(), f_path.c_str());
98 while(string_list->size() > 0) {
99 line = string_list->front();
100 string_list->pop_front();
101 if (line.length() > 0) {
102 log_debug("storing line: %s\n", line.c_str());
103 *ostream << line << endl;
109 log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
114 log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());