4 * Created on: Oct 20, 2010
12 #include "W1Device.hh"
17 W1Device::W1Device(dirent *direntry,
18 int family_code_param,
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;
33 W1Device::~W1Device() {
36 int W1Device::get_family_code() {
40 string W1Device::get_id() {
44 string W1Device::get_name() {
48 void W1Device::set_name(string name_param) {
52 string W1Device::get_time() {
59 ltime = localtime(&wtime);
60 strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
65 void W1Device::printout() {
68 text = get_formatted_data();
72 string W1Device::get_formatted_data() {
75 ret_val = get_formatted_data(get_value());
79 string W1Device::get_formatted_data(string value) {
82 ret_val = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", value = " + value + " " + get_unit();
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);
91 void W1Device::store() {
92 string file_path = "/tmp/" + id + ".txt";
94 ofstream data_file(file_path.c_str(), ios::app);
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;
110 cout << "could not open file " << file_path << " for writing data." << endl;