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 pthread_mutex_init(&plock, NULL);
33 type = device_type_param;
35 if (direntry_param != NULL) {
36 rootdir = W1_SCAN_ROOTDIR;
37 temp_str = W1_SLAVE_FILE;
38 dir_path = rootdir + "/" + direntry_param->d_name;
39 slave_file = dir_path + "/" + temp_str;
40 lifecycle_status = LIFECYCLE_STATUS__AVAILABLE;
41 log_debug("1-wire device's data file: %s\n", slave_file.c_str());
44 lifecycle_status = LIFECYCLE_STATUS__UNAVAILABLE;
46 reader = new DataReader(id);
50 W1Device::~W1Device() {
51 log_debug("W1Device destructor\n");
52 save_and_clean_cache();
59 string W1Device::get_name() {
62 if (name.empty() == true) {
63 cfg = DeviceConfig::get_device_config(id);
65 name = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
72 void W1Device::set_name(string name_param) {
75 if (name.compare(name_param) != 0) {
77 cfg = DeviceConfig::get_device_config(id);
79 cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
85 void W1Device::printout() {
93 text = data->to_string();
100 log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
104 string W1Device::to_string(double dbl_val, int digit_count) {
108 out << fixed << setprecision(digit_count) << dbl_val;
113 Data *W1Device::get_data() {
115 vector<double> *vect;
117 log_debug("get_data() started\n");
119 vect = get_raw_data();
121 log_debug("get_data() got raw data\n");
122 ret_val = new Data(vect, get_unit());
125 log_debug("get_data() got raw data done\n");
128 log_debug("get_data() try to read old already saved data\n");
129 // read old data already saved
130 ret_val = reader->get_latest_data();
131 log_debug("get_data() try to read old already saved data done\n");
136 void W1Device::cache(Data *new_data) {
140 data = new_data->clone();
141 pthread_mutex_lock(&plock);
142 memory_cache.push_back(data);
143 if (memory_cache.size() > 5) {
144 dec_precision = get_data_decimal_precision();
145 StoreDay::save(id, &memory_cache, dec_precision);
146 while(memory_cache.empty() == false) {
147 data = memory_cache.back();
148 memory_cache.pop_back();
152 pthread_mutex_unlock(&plock);
155 void W1Device::save_and_clean_cache() {
159 dec_precision = get_data_decimal_precision();
160 pthread_mutex_lock(&plock);
161 log_debug("save_and_clean_cache(): memory cache size: %d\n", memory_cache.size());
162 StoreDay::save(id, &memory_cache, dec_precision);
163 while(memory_cache.empty() == false) {
164 data = memory_cache.back();
165 memory_cache.pop_back();
168 pthread_mutex_unlock(&plock);
171 const DataReader *W1Device::get_device_data() {