}
list<Device *> Factory::get_device_list() {
+ list<Device *> ret_val;
DIR *dir;
int err_flg;
struct dirent *direntry;
Device *device;
bool is_subdir;
list<DataReader *> rdr_list;
- list<Device *> ret_val;
list<Device *>::iterator dev_iter;
list<DataReader *>::iterator rdr_iter;
DataReader *reader;
ret_val.push_back(device);
}
}
+ while(rdr_list.empty() == false) {
+ reader = rdr_list.back();
+ rdr_list.pop_back();
+ delete(reader);
+ }
return ret_val;
}
string device_dir;
string temp_str;
+ pthread_mutex_init(&plock, NULL);
type = device_type_param;
id = device_id_param;
if (direntry_param != NULL) {
}
W1Device::~W1Device() {
+ log_debug("W1Device destructor\n");
save_and_clean_cache();
- delete(reader);
+ if (reader != NULL) {
+ delete(reader);
+ reader = NULL;
+ }
}
string W1Device::get_name() {
if (data != NULL) {
text = data->to_string();
cout << text << endl;
+ delete(data);
}
else {
type = get_type();
return ret_val;
}
-void W1Device::cache(Data *data) {
- // TODO: add mutex for memory_cache
+void W1Device::cache(Data *new_data) {
+ Data *data;
+ int dec_precision;
+
+ data = new_data->clone();
+ pthread_mutex_lock(&plock);
memory_cache.push_back(data);
if (memory_cache.size() > 5) {
- save_and_clean_cache();
+ dec_precision = get_data_decimal_precision();
+ StoreDay::save(id, &memory_cache, dec_precision);
+ while(memory_cache.empty() == false) {
+ data = memory_cache.back();
+ memory_cache.pop_back();
+ delete(data);
+ }
}
+ pthread_mutex_unlock(&plock);
}
void W1Device::save_and_clean_cache() {
- list<Data *>::iterator iter;
- Data *data;
- int dec_precision;
+ Data *data;
+ int dec_precision;
dec_precision = get_data_decimal_precision();
+ pthread_mutex_lock(&plock);
+ log_debug("save_and_clean_cache(): memory cache size: %d\n", memory_cache.size());
StoreDay::save(id, &memory_cache, dec_precision);
- for(iter = memory_cache.begin(); iter != memory_cache.end(); iter++) {
- data = (Data *)*iter;
+ while(memory_cache.empty() == false) {
+ data = memory_cache.back();
+ memory_cache.pop_back();
delete(data);
}
- memory_cache.clear();
+ pthread_mutex_unlock(&plock);
}
-DataReader *W1Device::get_device_data() {
+const DataReader *W1Device::get_device_data() {
return reader;
}
#include <dirent.h>
#include <stdbool.h>
+#include <pthread.h>
#include <plp/Data.hh>
#include <plp/DataReader.hh>
W1Device(std::string device_id_param,
std::string device_type_param,
dirent *direntry_param);
- virtual ~W1Device();
+ ~W1Device();
std::string get_name();
void set_name(std::string name_param);
void printout();
plp::Data *get_data();
- plp::DataReader *get_device_data();
+ const plp::DataReader *get_device_data();
protected:
std::string dir_path;
std::string slave_file;
plp::DataReader *reader;
void save_and_clean_cache();
virtual std::vector<double> *get_raw_data() = 0;
- virtual unsigned int get_data_decimal_precision() = 0;
- void cache(plp::Data *data);
+ virtual unsigned int get_data_decimal_precision() { return 1; }
+ void cache(plp::Data *new_data);
std::string to_string(double val, int digit_count);
+ pthread_mutex_t plock;
};
}
}
W1TemperatureSensor::~W1TemperatureSensor() {
+ log_debug("W1TemperatureSensor destructor\n");
}
vector<double> *W1TemperatureSensor::get_raw_data() {
string loc;
Data *fdata;
Data *ldata;
- DataReader *reader;
+ const DataReader *reader;
Device *dev;
SensorDevice *device;
DataRange *dr;
if (device != NULL) {
reader = device->get_device_data();
if (reader != NULL) {
- dr = reader->get_data_range();
+ dr = ((DataReader *)reader)->get_data_range();
if (dr != NULL) {
fdata = dr->get_first();
if (fdata != NULL) {
d1 = fdata->get_date();
d2 = ldata->get_date();
- dr2 = reader->get_data(&d1, &d2);
+ dr2 = ((DataReader *)reader)->get_data(&d1, &d2);
if (dr2 != NULL) {
dr2->printout();
delete(dr2);
}
delete(dr);
}
- delete(reader);
}
}
}
}
+ while (dev_lst.empty() == false) {
+ dev = dev_lst.back();
+ dev_lst.pop_back();
+ delete(dev);
+ }
return 0;
}
else {
log_debug("could not find 1-wire devices.\n");
}
+ log_debug("trying to start emptying list\n");
while (device_list.empty() == false) {
device = device_list.back();
device_list.pop_back();
+ log_debug("calling delete\n");
delete(device);
}
return 0;