]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Device.cc
cleanups
[lib1wire.git] / src / W1Device.cc
index 467a3359685eb7a432ee606259684fe4d79e687b..eb94b24b3039ed611d17f5ba6d2692441b3835bb 100644 (file)
  */
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <iomanip>
 
 #include <time.h>
 #include <plp/log.h>
 
-#include "W1Store.hh"
+#include <plp/DeviceConfig.hh>
+#include <plp/StoreDay.hh>
+
+#include "Factory.hh"
 #include "W1Device.hh"
 
-using namespace w1;
 using namespace std;
+using namespace w1;
+using namespace plp;
 
-W1Device::W1Device(int family_code_param,
-               string device_id_param,
-               dirent *direntry_param) {
+W1Device::W1Device(string device_id_param,
+               string device_type_param,
+               dirent *direntry_param) : SensorDevice(device_id_param, device_type_param) {
        string rootdir;
        string device_dir;
        string temp_str;
 
-       rootdir         = W1_SCAN_ROOTDIR;
-       temp_str        = W1_SLAVE_FILE;
-       dir_path        = rootdir + "/" + direntry_param->d_name;
-       slave_file      = dir_path + "/" + temp_str;
-       log_debug("w1 data file: %s\n", slave_file.c_str());
-       family_code     = family_code_param;
-       id              = device_id_param;
-       name            = device_id_param;
+       pthread_mutex_init(&plock, NULL);
+       type    = device_type_param;
+       id      = device_id_param;
+       if (direntry_param != NULL) {
+               rootdir                 = W1_SCAN_ROOTDIR;
+               temp_str                = W1_SLAVE_FILE;
+               dir_path                = rootdir + "/" + direntry_param->d_name;
+               slave_file              = dir_path + "/" + temp_str;
+               lifecycle_status        = LIFECYCLE_STATUS__AVAILABLE;
+               log_debug("1-wire device's data file: %s\n", slave_file.c_str());
+       }
+       else {
+               lifecycle_status        = LIFECYCLE_STATUS__UNAVAILABLE;
+       }
+       reader  = new DataReader(id);
+       name    = "";
 }
 
 W1Device::~W1Device() {
-}
-
-int W1Device::get_family_code() {
-       return family_code;
-}
-
-string W1Device::get_id() {
-       return id;
+       log_debug("started\n");
+       save_and_clean_cache();
+       if (reader != NULL) {
+               delete(reader);
+               reader  = NULL;
+       }
 }
 
 string W1Device::get_name() {
+       DeviceConfig    *cfg;
+
+       if (name.empty() == true) {
+               cfg     = DeviceConfig::get_device_config(id);
+               if (cfg != NULL) {
+                       name    = cfg->get_config_value(DEVICE_CONFIG_VALUE_KEY__NAME);
+                       delete(cfg);
+               }
+       }
        return name;
 }
 
 void W1Device::set_name(string name_param) {
-       name    = name_param;
-}
-
-string W1Device::get_time() {
-       time_t          wtime;
-       struct tm       *ltime;
-       char            buffer[80];
-       string          ret_val;
-
-       time(&wtime);
-       ltime   = localtime(&wtime);
-       strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltime);
-       ret_val = buffer;
-       return ret_val;
+       DeviceConfig    *cfg;
+
+       if (name.compare(name_param) != 0) {
+               name    = name_param;
+               cfg     = DeviceConfig::get_device_config(id);
+               if (cfg != NULL) {
+                       cfg->set_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
+                       delete(cfg);
+               }
+       }
 }
 
 void W1Device::printout() {
-       string text;
-
-       text    = get_formatted_value();
-       cout << text << endl;
+       Data    *data;
+       string  text;
+       string  type;
+       string  name;
+
+       data    = get_data();
+       if (data != NULL) {
+               text    = data->to_string();
+               cout << text << endl;
+               delete(data);
+       }
+       else {
+               type    = get_type();
+               name    = get_name();
+               log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
+       }
 }
 
-string W1Device::get_formatted_value() {
-       string ret_val;
-       string val;
+string W1Device::to_string(double dbl_val, int digit_count) {
+       string          ret_val;
+       ostringstream   out;
 
-       val     = get_raw_value();
-       ret_val = get_formatted_value(val);
+       out << fixed << setprecision(digit_count) << dbl_val;
+       ret_val = out.str();
        return ret_val;
 }
 
-string W1Device::get_formatted_value(string value) {
-       string ret_val;
-
-       ret_val = get_time() + "|" + get_device_type() + "|" + id + "|" + value + " " + get_unit();
-       add_to_memory_cache(ret_val);
+Data *W1Device::get_data() {
+       Data            *ret_val;
+       vector<double>  *vect;
+
+       ret_val = NULL;
+       vect    = get_raw_data();
+       if (vect != NULL) {
+               ret_val = new Data(vect, get_unit());
+               cache(ret_val);
+               delete(vect);
+               log_debug("%s returning new data\n", id.c_str());
+       }
+       else {
+               // read old already saved data
+               log_debug("%s returning old saved data\n", id.c_str());
+               ret_val = reader->get_latest_data();
+       }
        return ret_val;
 }
 
-void W1Device::add_to_memory_cache(std::string formatted_data) {
-       // TODO: add mutex for memory_cache
-       memory_cache.push_back(formatted_data);
+void W1Device::cache(Data *new_data) {
+       Data    *data;
+       int     dec_precision;
+
+       data    = new_data->clone();
+       pthread_mutex_lock(&plock);
+       memory_cache.push_back(data);
+       if (memory_cache.size() > 5) {
+               dec_precision   = get_data_decimal_precision();
+               StoreDay::save(id, &memory_cache, dec_precision);
+               while(memory_cache.empty() == false) {
+                       data    = memory_cache.back();
+                       memory_cache.pop_back();
+                       delete(data);
+               }
+       }
+       pthread_mutex_unlock(&plock);
+}
+
+void W1Device::save_and_clean_cache() {
+       Data    *data;
+       int     dec_precision;
+
+       dec_precision   = get_data_decimal_precision();
+       pthread_mutex_lock(&plock);
+       log_debug("memory cache size: %d\n", memory_cache.size());
+       StoreDay::save(id, &memory_cache, dec_precision);
+       while(memory_cache.empty() == false) {
+               data    = memory_cache.back();
+               memory_cache.pop_back();
+               delete(data);
+       }
+       pthread_mutex_unlock(&plock);
 }
 
-void W1Device::store() {
-       W1Store::store(id, &memory_cache);
+const DataReader *W1Device::get_device_data() {
+       return reader;
 }