]> pilppa.org Git - lib1wire.git/blob - src/W1Store.cc
Initial support for reading log-data.
[lib1wire.git] / src / W1Store.cc
1 /*
2  * W1Store.cc
3  *
4  *  Created on: Oct 31, 2010
5  *      Author: lamikr
6  */
7
8 #include <list>
9 #include <string>
10 #include <fstream>
11
12 #include <time.h>
13 #include <dirent.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17
18 #include <plp/log.h>
19
20 #include "W1Configure.hh"
21 #include "W1Store.hh"
22 #include "W1Util.hh"
23
24 using namespace std;
25 using namespace w1;
26
27 #define DIR_BUFFER_SIZE 20
28
29 std::string W1Store::location_base_dir = DEFAULT_STORAGE_BASE_DIR;
30
31
32 W1Store::W1Store() {
33         // TODO Auto-generated constructor stub
34 }
35
36 W1Store::~W1Store() {
37         // TODO Auto-generated destructor stub
38 }
39
40 void W1Store::set_location_base_dir(string location_param) {
41         int     pos;
42         int     b_count;
43
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;
48         }
49         else {
50                 location_base_dir       = location_param + "/";
51         }
52 }
53
54 string W1Store::get_location_base_dir() {
55         return location_base_dir;
56 }
57
58 string W1Store::get_location_dir(string device_id, struct tm *ltime) {
59         char    buffer[DIR_BUFFER_SIZE];
60         string  year;
61         string  month;
62         string  ret_val;
63
64         strftime(buffer, DIR_BUFFER_SIZE, "%Y", ltime);
65         year    = buffer;
66         strftime(buffer, DIR_BUFFER_SIZE, "%m", ltime);
67         month   = buffer;
68         ret_val = W1Util::concat_paths(location_base_dir, device_id);
69         ret_val = ret_val + "/" + year + "/" + month;
70         return ret_val;
71 }
72
73 string W1Store::get_location_file(string device_id, struct tm *ltime) {
74         char    buffer[DIR_BUFFER_SIZE];
75         string  ret_val;
76
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;
80         return ret_val;
81 }
82
83 void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
84         string          f_path;
85         string          line;
86         time_t          wtime;
87         struct tm       *ltime;
88         ofstream        *ostream;
89
90         time(&wtime);
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;
104                                 }
105                         }
106                         ostream->close();
107                 }
108                 else {
109                         log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
110                 }
111                 delete(ostream);
112         }
113         else {
114                 log_error("[%s] Could not store data to file: %s\n", device_id.c_str(), f_path.c_str());
115         }
116 }