]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Device.cc
Api cleanups for device querying
[lib1wire.git] / src / W1Device.cc
index 4c108e3857fdb500c5a296c3f3b2f8809212cc72..37a02bafaa9c7647118bdef68237590b46936ce2 100644 (file)
@@ -21,36 +21,31 @@ using namespace std;
 using namespace w1;
 using namespace plp;
 
-W1Device::W1Device(int family_code_param,
+W1Device::W1Device(string device_type_param,
                string device_id_param,
                dirent *direntry_param) {
        string rootdir;
        string device_dir;
        string temp_str;
 
-       rootdir         = W1_SCAN_ROOTDIR;
-       temp_str        = W1_SLAVE_FILE;
-       dir_path        = rootdir + "/" + direntry_param->d_name;
-       slave_file      = dir_path + "/" + temp_str;
-       log_debug("1-wire device's data file: %s\n", slave_file.c_str());
-       family_code     = family_code_param;
-       id              = device_id_param;
+       type    = device_type_param;
+       id      = device_id_param;
+       if (direntry_param != NULL) {
+               rootdir                 = W1_SCAN_ROOTDIR;
+               temp_str                = W1_SLAVE_FILE;
+               dir_path                = rootdir + "/" + direntry_param->d_name;
+               slave_file              = dir_path + "/" + temp_str;
+               lifecycle_status        = LIFECYCLE_STATUS__AVAILABLE;
+               log_debug("1-wire device's data file: %s\n", slave_file.c_str());
+       }
+       else {
+               lifecycle_status        = LIFECYCLE_STATUS__UNAVAILABLE;
+       }
        name            = "";
 }
 
 W1Device::~W1Device() {
-       list<Data *>::iterator  iter;
-       Data                    *data;
-
-       for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
-               data    = (Data *)*iter;
-               delete(data);
-       }
-       memory_cache.clear();
-}
-
-int W1Device::get_w1_family_code() {
-       return family_code;
+       save_and_clean_cache();
 }
 
 string W1Device::get_id() {
@@ -63,7 +58,7 @@ string W1Device::get_name() {
        if (name.empty() == true) {
                cfg     = Factory::get_device_config(id);
                if (cfg != NULL) {
-                       name    = cfg->get_cfg_value("name");
+                       name    = cfg->get_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME);
                        delete(cfg);
                }
        }
@@ -76,7 +71,7 @@ void W1Device::set_name(string name_param) {
        name    = name_param;
        cfg     = Factory::get_device_config(id);
        if (cfg != NULL) {
-               cfg->set_cfg_value("name", name_param);
+               cfg->set_cfg_value(DEVICE_CONFIG_VALUE_KEY__NAME, name_param);
                delete(cfg);
        }
 }
@@ -84,6 +79,8 @@ void W1Device::set_name(string name_param) {
 void W1Device::printout() {
        Data    *data;
        string  text;
+       string  type;
+       string  name;
 
        data    = get_data();
        if (data != NULL) {
@@ -91,7 +88,9 @@ void W1Device::printout() {
                cout << text << endl;
        }
        else {
-               log_error("Could not data for %s device: %s\n", get_device_type().c_str(),  get_name().c_str());
+               type    = get_device_type();
+               name    = get_name();
+               log_error("Could not data for %s device: %s\n", type.c_str(), name.c_str());
        }
 }
 
@@ -112,18 +111,21 @@ Data *W1Device::get_data() {
        vect    = get_raw_data();
        if (vect != NULL) {
                ret_val = new Data(vect, get_unit());
-               collect_data(ret_val);
+               cache(ret_val);
                delete(vect);
        }
        return ret_val;
 }
 
-void W1Device::collect_data(Data *data) {
+void W1Device::cache(Data *data) {
        // TODO: add mutex for memory_cache
        memory_cache.push_back(data);
+       if (memory_cache.size() > 5) {
+               save_and_clean_cache();
+       }
 }
 
-void W1Device::save_data() {
+void W1Device::save_and_clean_cache() {
        list<Data *>::iterator  iter;
        Data                    *data;
        int                     dec_precision;
@@ -136,3 +138,18 @@ void W1Device::save_data() {
        }
        memory_cache.clear();
 }
+
+DataReader *W1Device::get_device_data() {
+       string id;
+
+       id      = get_id();
+       return new DataReader(id);
+}
+
+EnumDeviceLifeCycleStatus W1Device::get_lifecycle_state() {
+       return lifecycle_status;
+}
+
+string W1Device::get_device_type() {
+       return type;
+}