4 * Created on: Oct 20, 2010
15 #include "DeviceConfig.hh"
17 #include "StoreDay.hh"
18 #include "W1Device.hh"
24 W1Device::W1Device(string device_type_param,
25 string device_id_param,
26 dirent *direntry_param) {
31 type = device_type_param;
33 if (direntry_param != NULL) {
34 rootdir = W1_SCAN_ROOTDIR;
35 temp_str = W1_SLAVE_FILE;
36 dir_path = rootdir + "/" + direntry_param->d_name;
37 slave_file = dir_path + "/" + temp_str;
38 lifecycle_status = LIFECYCLE_STATUS__AVAILABLE;
39 log_debug("1-wire device's data file: %s\n", slave_file.c_str());
42 lifecycle_status = LIFECYCLE_STATUS__UNAVAILABLE;
47 W1Device::~W1Device() {
48 save_and_clean_cache();
51 string W1Device::get_id() {
55 string W1Device::get_name() {
58 if (name.empty() == true) {
59 cfg = Factory::get_device_config(id);
61 name = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
68 void W1Device::set_name(string name_param) {
72 cfg = Factory::get_device_config(id);
74 cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
79 void W1Device::printout() {
87 text = data->to_string();
91 type = get_device_type();
93 log_error("Could not data for %s device: %s\n", type.c_str(), name.c_str());
97 string W1Device::to_string(double dbl_val, int digit_count) {
101 out << fixed << setprecision(digit_count) << dbl_val;
106 Data *W1Device::get_data() {
108 vector<double> *vect;
111 vect = get_raw_data();
113 ret_val = new Data(vect, get_unit());
120 void W1Device::cache(Data *data) {
121 // TODO: add mutex for memory_cache
122 memory_cache.push_back(data);
123 if (memory_cache.size() > 5) {
124 save_and_clean_cache();
128 void W1Device::save_and_clean_cache() {
129 list<Data *>::iterator iter;
133 dec_precision = get_data_decimal_precision();
134 StoreDay::save(id, &memory_cache, dec_precision);
135 for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
136 data = (Data *)*iter;
139 memory_cache.clear();
142 DataReader *W1Device::get_device_data() {
146 return new DataReader(id);
149 EnumDeviceLifeCycleStatus W1Device::get_lifecycle_state() {
150 return lifecycle_status;
153 string W1Device::get_device_type() {