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 W1Device::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 W1Device::set_name(string name_param) {
75 if (name.compare(name_param) != 0) {
77 cfg = DeviceConfig::get_device_config(id);
79 cfg->set_config_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param, true);
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;
118 vect = get_raw_data();
120 ret_val = new Data(vect, get_unit());
123 log_debug("%s returning new data\n", id.c_str());
126 // read old already saved data
127 log_debug("%s returning old saved data\n", id.c_str());
128 ret_val = reader->get_latest_data();
133 void W1Device::cache(Data *new_data) {
137 data = new_data->clone();
138 pthread_mutex_lock(&plock);
139 memory_cache.push_back(data);
140 if (memory_cache.size() > 5) {
141 dec_precision = get_data_decimal_precision();
142 StoreDay::save(id, &memory_cache, dec_precision);
143 while(memory_cache.empty() == false) {
144 data = memory_cache.back();
145 memory_cache.pop_back();
149 pthread_mutex_unlock(&plock);
152 void W1Device::save_and_clean_cache() {
156 dec_precision = get_data_decimal_precision();
157 pthread_mutex_lock(&plock);
158 /* needs to be casted to long unsigned int value is "unsigned int" in some
159 toolchains and that would otherwise cause an warning/error
161 log_debug("memory cache size: %lu\n", (long unsigned int)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() {