]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Device.cc
Refactoring and fixes.
[lib1wire.git] / src / W1Device.cc
index 6719be76c32ea428a363f4c85d17986ebebb8e44..7bb44183c489287c846d95487dbafa44abe25773 100644 (file)
@@ -6,6 +6,8 @@
  */
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <iomanip>
 
 #include <time.h>
 #include <plp/log.h>
@@ -36,7 +38,7 @@ W1Device::W1Device(int family_code_param,
 W1Device::~W1Device() {
 }
 
-int W1Device::get_family_code() {
+int W1Device::get_w1_family_code() {
        return family_code;
 }
 
@@ -66,34 +68,66 @@ string W1Device::get_time() {
 }
 
 void W1Device::printout() {
-       string text;
+       Data    *data;
+       string  text;
+
+       data    = get_and_collect_data();
+       if (data != NULL) {
+               text    = data->to_string();
+               cout << text << endl;
+       }
+       else {
+               log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_name().c_str());
+       }
+}
+
+string W1Device::to_string(double dbl_val, int digit_count) {
+       string          ret_val;
+       ostringstream   out;
 
-       text    = get_formatted_data();
-       cout << text << endl;
+       out << fixed << setprecision(digit_count) << dbl_val;
+       ret_val = out.str();
+       return ret_val;
 }
 
-string W1Device::get_formatted_data() {
-       string ret_val;
-       string val;
 
-       val     = get_raw_data();
-       ret_val = get_formatted_data(val);
+Data *W1Device::get_and_collect_data() {
+       Data            *ret_val;
+       vector<double>  *vect;
+
+       ret_val = NULL;
+       vect    = get_raw_data();
+       if (vect != NULL) {
+               ret_val = new Data(vect, get_unit());
+               collect_data(ret_val);
+               delete(vect);
+       }
        return ret_val;
 }
 
-string W1Device::get_formatted_data(string raw_data) {
-       string ret_val;
+/*
+Data *W1Device::get_formatted_data(Data *data) {
+       Data    *ret_val;
 
        ret_val = get_time() + "|" + raw_data + " " + get_unit();
-       add_to_memory_cache(ret_val);
+       add_to_save_fifo(ret_val);
        return ret_val;
 }
+*/
 
-void W1Device::add_to_memory_cache(std::string formatted_data) {
+void W1Device::collect_data(Data *data) {
        // TODO: add mutex for memory_cache
-       memory_cache.push_back(formatted_data);
+       memory_cache.push_back(data);
 }
 
-void W1Device::store() {
-       W1Store::store(id, &memory_cache);
+void W1Device::save_data() {
+       list<Data *>::iterator  iter;
+       Data                    *data;
+
+       W1Store::save(id, &memory_cache);
+       for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
+               data    = (Data *)*iter;
+               delete(data);
+       }
+       memory_cache.clear();
 }