4 * Created on: Oct 20, 2010
12 #include "W1Device.hh"
17 W1Device::W1Device(dirent *direntry, int family_code_param, string id_param) {
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;
31 W1Device::~W1Device() {
34 int W1Device::get_family_code() {
38 string W1Device::get_id() {
42 string W1Device::get_name() {
46 void W1Device::set_name(string name_param) {
50 string W1Device::get_time() {
57 ltime = localtime(&wtime);
58 strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
63 void W1Device::printout() {
66 text = get_formatted_data();
70 string W1Device::get_formatted_data() {
73 ret_val = get_formatted_data(get_value());
77 string W1Device::get_formatted_data(string value) {
80 ret_val = get_time() + ": device type = " + get_devicetype_name() + ", id = " + id + ", value = " + value + " " + get_unit();
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);
89 void W1Device::store() {
90 string file_path = "/tmp/" + id + ".txt";
92 ofstream data_file(file_path.c_str(), ios::app);
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;
108 cout << "could not open file " << file_path << " for writing data." << endl;