#include <plp/log.h>
-#include "DeviceConfig.hh"
+#include <plp/DeviceConfig.hh>
+#include <plp/Device.hh>
+
+#include <plp/str_util.h>
+
#include "Factory.hh"
using namespace w1;
using namespace std;
+using namespace plp;
#define DEFAULT_READ_INTERVAL_SECONDS 600
-#define DEFAULT_SAVE_INTERVAL_COUNT 5
#define DEFAULT_MAX_READ_COUNT -1
-bool try_parse_long(const char *str, long *result) {
- int new_result;
- char *endptr;
- bool ret_val;
-
- ret_val = false;
- errno = 0;
- new_result = strtol(str, &endptr, 10);
- if (errno != 0) {
- log_error("invalid input %s, could not convert to integer.\n", str);
- }
- else {
- if (endptr == str) {
- log_error("invalid input %s, could not convert to integer.\n", str);
- }
- else {
- *result = new_result;
- ret_val = true;
- }
- }
- return ret_val;
-}
-
int main(int argc, char** argv) {
- list<W1Device *> device_list;
- int read_count_afer_save;
int read_count_total;
long cnt_max_scans;
long read_interval_seconds;
- long save_interval_count;
string loc;
- W1Device *device;
- list<W1Device *>::iterator iter;
+ Device *device;
+ list<Device *> device_list;
+ list<Device *>::iterator iter;
// default values than can be overwritten with parameters
loc = "/tmp/w1data";
read_interval_seconds = DEFAULT_READ_INTERVAL_SECONDS;
- save_interval_count = DEFAULT_SAVE_INTERVAL_COUNT;
cnt_max_scans = DEFAULT_MAX_READ_COUNT;
if (argc > 1) {
loc = argv[1];
}
else {
log_info("no parameter given using default values:\n");
- log_info("\ttest_w1_datalog_write %s %ld %ld %ld\n\n", loc.c_str(), read_interval_seconds, save_interval_count, cnt_max_scans);
- log_info("usage:\n\ttest_w1_datalog_write <result_save_path> <read_interval_seconds> <save_interval_count> <max_read_count>\n");
- log_info("\tsave_interval_count == -1: do not save data that is read\n");
+ log_info("\ttest_w1_datalog_write %s %ld %ld\n\n", loc.c_str(), read_interval_seconds, cnt_max_scans);
+ log_info("usage:\n\ttest_w1_datalog_write <result_save_path> <read_interval_seconds> <max_read_count>\n");
log_info("\tmax_read_count == -1: read devices until application is terminated\n\n");
}
if (argc > 2) {
- try_parse_long(argv[2], &read_interval_seconds);
+ parse_long(argv[2], &read_interval_seconds);
}
if (argc > 3) {
- try_parse_long(argv[3], &save_interval_count);
- }
- if (argc > 4) {
- try_parse_long(argv[4], &cnt_max_scans);
+ parse_long(argv[3], &cnt_max_scans);
}
log_info("searching 1-wire devices\n");
if (read_interval_seconds == DEFAULT_READ_INTERVAL_SECONDS) {
else {
log_info("\tdevice read interval: %ld seconds\n", read_interval_seconds);
}
- if (save_interval_count != -1) {
- if (save_interval_count == DEFAULT_SAVE_INTERVAL_COUNT) {
- log_info("\tsave interval: %ld (default value)\n", save_interval_count);
- }
- else {
- log_info("\tsave interval: %ld\n", save_interval_count);
- }
- log_info("\tdata save dir: %s\n", loc.c_str());
- }
- else {
- log_info("\tresults are not saved\n");
- }
+ log_info("\tdata save dir: %s\n", loc.c_str());
if (cnt_max_scans == DEFAULT_MAX_READ_COUNT) {
log_info("\tmax read count: %ld (default value, devices will be read until application is terminated)\n\n", cnt_max_scans);
}
}
DeviceConfig::set_base_dir_name(loc);
device_list = Factory::get_device_list();
- read_count_afer_save = 0;
read_count_total = 0;
if (device_list.size() > 0) {
while(1) {
- read_count_afer_save++;
read_count_total++;
if ((cnt_max_scans != -1) &&
(read_count_total > cnt_max_scans)) {
log_info("closing the application, max read count reached. (%ld)\n", cnt_max_scans);
break;
}
- for(iter = device_list.begin(); iter != device_list.end(); iter++) {
- device = (W1Device *)*iter;
- device->printout();
- sleep(1);
- if ((save_interval_count != -1) &&
- (read_count_afer_save >= save_interval_count)) {
- device->save_data();
+ for (iter = device_list.begin(); iter != device_list.end(); iter++) {
+ device = (Device *)*iter;
+ if (device->get_lifecycle_state() == LIFECYCLE_STATUS__AVAILABLE) {
+ device->printout();
+ sleep(1);
+ }
+ else {
+ log_debug("device not available, %s.\n", device->get_id().c_str());
}
}
sleep(read_interval_seconds);
- if ((save_interval_count != -1) &&
- (read_count_afer_save >= save_interval_count)) {
- read_count_afer_save = 0;
- }
}
}
else {
- log_debug("could not find 1-wire devices.\n");
+ log_debug("could not find 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;