4 * Created on: Oct 20, 2010
15 #include <plp/DeviceConfig.hh>
16 #include <plp/StoreDay.hh>
19 #include "W1Device.hh"
25 W1Device::W1Device(string device_id_param,
26 string device_type_param,
27 dirent *direntry_param) : SensorDevice(device_id_param, device_type_param) {
32 type = device_type_param;
34 if (direntry_param != NULL) {
35 rootdir = W1_SCAN_ROOTDIR;
36 temp_str = W1_SLAVE_FILE;
37 dir_path = rootdir + "/" + direntry_param->d_name;
38 slave_file = dir_path + "/" + temp_str;
39 lifecycle_status = LIFECYCLE_STATUS__AVAILABLE;
40 log_debug("1-wire device's data file: %s\n", slave_file.c_str());
43 lifecycle_status = LIFECYCLE_STATUS__UNAVAILABLE;
45 reader = new DataReader(id);
49 W1Device::~W1Device() {
50 save_and_clean_cache();
54 string W1Device::get_name() {
57 if (name.empty() == true) {
58 cfg = DeviceConfig::get_device_config(id);
60 name = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
67 void W1Device::set_name(string name_param) {
70 if (name.compare(name_param) != 0) {
72 cfg = DeviceConfig::get_device_config(id);
74 cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
80 void W1Device::printout() {
88 text = data->to_string();
94 log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
98 string W1Device::to_string(double dbl_val, int digit_count) {
102 out << fixed << setprecision(digit_count) << dbl_val;
107 Data *W1Device::get_data() {
109 vector<double> *vect;
111 log_debug("get_data() started\n");
113 vect = get_raw_data();
115 log_debug("get_data() got raw data\n");
116 ret_val = new Data(vect, get_unit());
119 log_debug("get_data() got raw data done\n");
122 log_debug("get_data() try to read old already saved data\n");
123 // read old data already saved
124 ret_val = reader->get_latest_data();
125 log_debug("get_data() try to read old already saved data done\n");
130 void W1Device::cache(Data *data) {
131 // TODO: add mutex for memory_cache
132 memory_cache.push_back(data);
133 if (memory_cache.size() > 5) {
134 save_and_clean_cache();
138 void W1Device::save_and_clean_cache() {
139 list<Data *>::iterator iter;
143 dec_precision = get_data_decimal_precision();
144 StoreDay::save(id, &memory_cache, dec_precision);
145 for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
146 data = (Data *)*iter;
149 memory_cache.clear();
152 DataReader *W1Device::get_device_data() {