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);
51 W1Device::~W1Device() {
52 log_debug("started\n");
53 save_and_clean_cache();
60 string Device::get_name() {
61 if (name.empty() == true) {
63 _cfg = DeviceConfig::get_device_config(id);
66 _cfg->get_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name);
72 void Device::set_name(string name_param) {
73 if (name.compare(name_param) != 0) {
76 _cfg = DeviceConfig::get_device_config(id);
79 _cfg->set_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param, true);
84 void W1Device::printout() {
92 text = data->to_string();
99 log_error("Could not read data for %s device: %s\n", type.c_str(), name.c_str());
103 string W1Device::to_string(double dbl_val, int digit_count) {
107 out << fixed << setprecision(digit_count) << dbl_val;
112 Data *W1Device::get_data() {
114 vector<double> *vect;
117 vect = get_raw_data();
119 ret_val = new Data(vect, get_unit());
122 log_debug("%s returning new data\n", id.c_str());
125 // read old already saved data
126 log_debug("%s returning old saved data\n", id.c_str());
127 ret_val = reader->get_latest_data();
132 void W1Device::cache(Data *new_data) {
136 data = new_data->clone();
137 pthread_mutex_lock(&plock);
138 memory_cache.push_back(data);
139 if (memory_cache.size() > 5) {
140 dec_precision = get_data_decimal_precision();
141 StoreDay::save(id, &memory_cache, dec_precision);
142 while(memory_cache.empty() == false) {
143 data = memory_cache.back();
144 memory_cache.pop_back();
148 pthread_mutex_unlock(&plock);
151 void W1Device::save_and_clean_cache() {
155 dec_precision = get_data_decimal_precision();
156 pthread_mutex_lock(&plock);
157 /* needs to be casted to long unsigned int value is "unsigned int" in some
158 toolchains and that would otherwise cause an warning/error
160 log_debug("memory cache size: %lu\n", (long unsigned int)memory_cache.size());
161 StoreDay::save(id, &memory_cache, dec_precision);
162 while(memory_cache.empty() == false) {
163 data = memory_cache.back();
164 memory_cache.pop_back();
167 pthread_mutex_unlock(&plock);
170 const DataReader *W1Device::get_datareader() {