4 * Created on: Oct 20, 2010
13 #include "W1Device.hh"
18 W1Device::W1Device(dirent *direntry,
19 int family_code_param,
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;
34 W1Device::~W1Device() {
37 int W1Device::get_family_code() {
41 string W1Device::get_id() {
45 string W1Device::get_name() {
49 void W1Device::set_name(string name_param) {
53 string W1Device::get_time() {
60 ltime = localtime(&wtime);
61 strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
66 void W1Device::printout() {
69 text = get_formatted_data();
73 string W1Device::get_formatted_data() {
78 ret_val = get_formatted_data(val);
82 string W1Device::get_formatted_data(string value) {
85 ret_val = get_time() + "|" + get_devicetype_name() + "|" + id + "|" + value + " " + get_unit();
86 add_to_memory_cache(ret_val);
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);
95 void W1Device::store() {
96 W1Store::store(id, memory_cache);
98 string file_path = "/tmp/" + id + ".txt";
100 ofstream data_file(file_path.c_str(), ios::app);
102 cout << "storing to " << file_path << "data size " << memory_cache.size() << endl;
103 // TODO: add mutex to protect memory_cache while it's read and emptied
104 if (data_file.is_open()) {
105 while(memory_cache.size() > 0) {
106 text_line = memory_cache.front();
107 memory_cache.pop_front();
108 if (text_line.length() > 0) {
109 cout << "storing line: " << text_line << endl;
110 data_file << text_line << endl;
116 cout << "could not open file " << file_path << " for writing data." << endl;