#include <list>
#include <string>
-#include <iostream>
#include <fstream>
#include <time.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <plp/log.h>
+
+#include "W1Configure.hh"
#include "W1Store.hh"
+#include "W1Util.hh"
using namespace std;
using namespace w1;
-std::string W1Store::location = "/tmp/";
+#define DIR_BUFFER_SIZE 20
+
+std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
+
W1Store::W1Store() {
// TODO Auto-generated constructor stub
// TODO Auto-generated destructor stub
}
-void W1Store::set_location(string location_param) {
- location = location_param;
+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 + "/";
+ }
}
-void W1Store::store(std::string device_id, std::list<std::string> string_list) {
+string W1Store::get_location_base_dir() {
+ return location_base_dir;
+}
- string file_path = location + device_id + ".txt";
- string text_line;
- ofstream data_file(file_path.c_str(), ios::app);
+string W1Store::get_location_dir(string device_id, struct tm *ltime) {
+ char buffer[DIR_BUFFER_SIZE];
+ string year;
+ string month;
+ string ret_val;
- cout << "storing to " << file_path << ", data size " << string_list.size() << endl;
+ 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<std::string> *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 (data_file.is_open()) {
- while(string_list.size() > 0) {
- text_line = string_list.front();
- string_list.pop_front();
- if (text_line.length() > 0) {
- cout << "storing line: " << text_line << endl;
- data_file << text_line << endl;
+ 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());
}
- data_file.close();
+ delete(ostream);
}
else {
- cout << "could not open file " << file_path << " for writing data." << endl;
+ log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
}
}