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