/* * W1Store.cc * * Created on: Oct 31, 2010 * Author: lamikr */ #include #include #include #include #include #include #include #include #include #include "W1Configure.hh" #include "W1Store.hh" #include "W1Util.hh" using namespace std; using namespace w1; #define DIR_BUFFER_SIZE 20 std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR; W1Store::W1Store() { // TODO Auto-generated constructor stub } W1Store::~W1Store() { // TODO Auto-generated destructor stub } void W1Store::set_location_base_dir(string location_param) { int pos; int b_count; pos = location_param.find_last_of("/"); b_count = location_param.length(); if (pos == (b_count - 1)) { location_base_dir = location_param; } else { location_base_dir = location_param + "/"; } } string W1Store::get_location_base_dir() { return location_base_dir; } string W1Store::get_location_dir(string device_id, struct tm *ltime) { char buffer[DIR_BUFFER_SIZE]; string year; string month; string ret_val; 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; return ret_val; } string W1Store::get_location_file(string device_id, struct tm *ltime) { char buffer[DIR_BUFFER_SIZE]; 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; return ret_val; } void W1Store::store(std::string device_id, std::list *string_list) { string f_path; string line; time_t wtime; struct tm *ltime; ofstream *ostream; time(&wtime); ltime = localtime(&wtime); f_path = get_location_file(device_id, ltime); 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) { if (ostream->is_open()) { log_info("[%s] writing %d data values to file: %s\n", device_id.c_str(), string_list->size(), f_path.c_str()); while(string_list->size() > 0) { line = string_list->front(); string_list->pop_front(); if (line.length() > 0) { log_debug("storing line: %s\n", line.c_str()); *ostream << line << endl; } } ostream->close(); } else { log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str()); } delete(ostream); } else { log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str()); } }