]> pilppa.org Git - lib1wire.git/blob - src/W1Device.cc
store each days file separately to year/month/id_year-month-day.txt subdir. Fix also...
[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 "W1Store.hh"
13 #include "W1Device.hh"
14
15 using namespace w1;
16 using namespace std;
17
18 W1Device::W1Device(dirent *direntry,
19                 int family_code_param,
20                 string id_param) {
21         string rootdir;
22         string device_dir;
23         string temp_str;
24
25         rootdir         = W1_SCAN_ROOTDIR;
26         temp_str        = W1_SLAVE_FILE;
27         dir_path        = rootdir + "/" + direntry->d_name;
28         slave_file      = dir_path + "/" + temp_str;
29         family_code     = family_code_param;
30         id              = id_param;
31         name            = id_param;
32 }
33
34 W1Device::~W1Device() {
35 }
36
37 int W1Device::get_family_code() {
38         return family_code;
39 }
40
41 string W1Device::get_id() {
42         return id;
43 }
44
45 string W1Device::get_name() {
46         return name;
47 }
48
49 void W1Device::set_name(string name_param) {
50         name    = name_param;
51 }
52
53 string W1Device::get_time() {
54         time_t          wtime;
55         struct tm       *ltime;
56         char            buffer[80];
57         string          ret_val;
58
59         time(&wtime);
60         ltime   = localtime(&wtime);
61         strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
62         ret_val = buffer;
63         return ret_val;
64 }
65
66 void W1Device::printout() {
67         string text;
68
69         text    = get_formatted_data();
70         cout << text << endl;
71 }
72
73 string W1Device::get_formatted_data() {
74         string ret_val;
75         string val;
76
77         val     = get_value();
78         ret_val = get_formatted_data(val);
79         return ret_val;
80 }
81
82 string W1Device::get_formatted_data(string value) {
83         string ret_val;
84
85         ret_val = get_time() + "|" + get_devicetype_name() + "|" + id + "|" + value + " " + get_unit();
86         add_to_memory_cache(ret_val);
87         return ret_val;
88 }
89
90 void W1Device::add_to_memory_cache(std::string formatted_data) {
91         // TODO: add mutex for memory_cache
92         memory_cache.push_back(formatted_data);
93 }
94
95 void W1Device::store() {
96         W1Store::store(id, &memory_cache);
97 }