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("started\n");
52 save_and_clean_cache();
59 string Device::get_name() {
62 if (name.empty() == true) {
63 cfg = DeviceConfig::get_device_config(id);
65 cfg->get_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name);
72 void Device::set_name(string name_param) {
75 log_debug("set_name started\n");
76 if (name.compare(name_param) != 0) {
78 cfg = DeviceConfig::get_device_config(id);
80 cfg->set_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param, true);
86 void W1Device::printout() {
94 text = data->to_string();
101 log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
105 string W1Device::to_string(double dbl_val, int digit_count) {
109 out << fixed << setprecision(digit_count) << dbl_val;
114 Data *W1Device::get_data() {
116 vector<double> *vect;
119 vect = get_raw_data();
121 ret_val = new Data(vect, get_unit());
124 log_debug("%s returning new data\n", id.c_str());
127 // read old already saved data
128 log_debug("%s returning old saved data\n", id.c_str());
129 ret_val = reader->get_latest_data();
134 void W1Device::cache(Data *new_data) {
138 data = new_data->clone();
139 pthread_mutex_lock(&plock);
140 memory_cache.push_back(data);
141 if (memory_cache.size() > 5) {
142 dec_precision = get_data_decimal_precision();
143 StoreDay::save(id, &memory_cache, dec_precision);
144 while(memory_cache.empty() == false) {
145 data = memory_cache.back();
146 memory_cache.pop_back();
150 pthread_mutex_unlock(&plock);
153 void W1Device::save_and_clean_cache() {
157 dec_precision = get_data_decimal_precision();
158 pthread_mutex_lock(&plock);
159 /* needs to be casted to long unsigned int value is "unsigned int" in some
160 toolchains and that would otherwise cause an warning/error
162 log_debug("memory cache size: %lu\n", (long unsigned int)memory_cache.size());
163 StoreDay::save(id, &memory_cache, dec_precision);
164 while(memory_cache.empty() == false) {
165 data = memory_cache.back();
166 memory_cache.pop_back();
169 pthread_mutex_unlock(&plock);
172 const DataReader *W1Device::get_datareader() {