]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
add support for storing data to text file periodically
[lib1wire.git] / src / W1Device.cc
1 /*
2  * W1Device.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <iostream>
8 #include <fstream>
9
10 #include <time.h>
11
12 #include "W1Device.hh"
13
14 using namespace w1;
15 using namespace std;
16
17 W1Device::W1Device(dirent *direntry, int family_code_param, string id_param) {
18         string rootdir;
19         string device_dir;
20         string temp_str;
21
22         rootdir         = W1_SCAN_ROOTDIR;
23         temp_str        = W1_SLAVE_FILE;
24         dir_path        = rootdir + "/" + direntry->d_name;
25         slave_file      = dir_path + "/" + temp_str;
26         family_code     = family_code_param;
27         id                      = id_param;
28         name            = id_param;
29 }
30
31 W1Device::~W1Device() {
32 }
33
34 int W1Device::get_family_code() {
35         return family_code;
36 }
37
38 string W1Device::get_id() {
39         return id;
40 }
41
42 string W1Device::get_name() {
43         return name;
44 }
45
46 void W1Device::set_name(string name_param) {
47         name    = name_param;
48 }
49
50 string W1Device::get_time() {
51         time_t          wtime;
52         struct tm       *ltime;
53         char            buffer[80];
54         string          ret_val;
55
56         time(&wtime);
57         ltime   = localtime(&wtime);
58         strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
59         ret_val = buffer;
60         return ret_val;
61 }
62
63 void W1Device::printout() {
64         string text;
65
66         text    = get_formatted_data();
67         cout << text << endl;
68 }
69
70 string W1Device::get_formatted_data() {
71         string ret_val;
72
73         ret_val = get_formatted_data(get_value());
74         return ret_val;
75 }
76
77 string W1Device::get_formatted_data(string value) {
78         string ret_val;
79
80         ret_val = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", value = " + value + " " + get_unit();
81         return ret_val;
82 }
83
84 void W1Device::add_to_memory_cache(std::string formatted_data) {
85         // TODO: add mutex for memory_cache
86         memory_cache.push_back(formatted_data);
87 }
88
89 void W1Device::store() {
90         string file_path = "/tmp/" + id + ".txt";
91         string text_line;
92         ofstream data_file(file_path.c_str(), ios::app);
93
94         cout << "storing to " << file_path << "data size " << memory_cache.size() << endl;
95         // TODO: add mutex to protect memory_cache while it's read and emptied
96         if (data_file.is_open()) {
97                 while(memory_cache.size() > 0) {
98                         text_line       = memory_cache.front();
99                         memory_cache.pop_front();
100                         if (text_line.length() > 0) {
101                                 cout << "storing line: " << text_line << endl;
102                                 data_file << text_line << endl;
103                         }
104                 }
105                 data_file.close();
106         }
107         else {
108                 cout << "could not open file " << file_path << " for writing data." << endl;
109         }
110 }