X-Git-Url: http://pilppa.org/gitweb/?a=blobdiff_plain;f=src_test%2Ftest_w1_datalog_write.cc;h=a0c0cc238ba707cc8400e4784da957130bd47f80;hb=22f0b4dd0b81bce75ac7a842a72ca86eec02f7bc;hp=5931f5fc5090f0de928bfe0f7093fb75687c98f2;hpb=e3fe75505f606622b3a21cf1c4b71b7144639e06;p=lib1wire.git diff --git a/src_test/test_w1_datalog_write.cc b/src_test/test_w1_datalog_write.cc index 5931f5f..a0c0cc2 100644 --- a/src_test/test_w1_datalog_write.cc +++ b/src_test/test_w1_datalog_write.cc @@ -13,11 +13,17 @@ #include -#include "W1Store.hh" -#include "W1Scanner.hh" +#include +#include + +#include "Factory.hh" using namespace w1; using namespace std; +using namespace plp; + +#define DEFAULT_READ_INTERVAL_SECONDS 600 +#define DEFAULT_MAX_READ_COUNT -1 bool try_parse_long(const char *str, long *result) { int new_result; @@ -43,65 +49,80 @@ bool try_parse_long(const char *str, long *result) { } int main(int argc, char** argv) { - W1Scanner *scanner; - list device_list; - int round; - long scan_interval; - long store_interval; - string location; - bool err_flg; - W1Device *device; + int read_count_total; + long cnt_max_scans; + long read_interval_seconds; + string loc; + Device *device; + list device_list; + list::iterator iter; // default values than can be overwritten with parameters - location = "/tmp/"; - scan_interval = 2; //600; - store_interval = 2; + loc = "/tmp/w1data"; + read_interval_seconds = DEFAULT_READ_INTERVAL_SECONDS; + cnt_max_scans = DEFAULT_MAX_READ_COUNT; if (argc > 1) { - location = argv[1]; - log_info("storage location: %s\n", location.c_str()); + loc = argv[1]; } else { - log_warning("No storage location parameter given, using default location: %s\n", location.c_str()); + log_info("no parameter given using default values:\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 \n"); + log_info("\tmax_read_count == -1: read devices until application is terminated\n\n"); } if (argc > 2) { - try_parse_long(argv[2], &scan_interval); + try_parse_long(argv[2], &read_interval_seconds); } if (argc > 3) { - try_parse_long(argv[3], &store_interval); + try_parse_long(argv[3], &cnt_max_scans); + } + log_info("searching 1-wire devices\n"); + if (read_interval_seconds == DEFAULT_READ_INTERVAL_SECONDS) { + log_info("\tdevice read interval: %ld seconds (default value)\n", read_interval_seconds); + } + else { + log_info("\tdevice read interval: %ld seconds\n", read_interval_seconds); + } + 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); } - log_info("start scanning, data saved to location: %s, scan interval: %d, store interval: %d\n", location.c_str(), scan_interval, store_interval); - W1Store::set_base_dir_name(location); - scanner = new W1Scanner(); - device_list = scanner->get_device_list(); - round = 0; - int t = 0; + else { + log_info("\tmax read count: %ld\n\n", cnt_max_scans); + } + DeviceConfig::set_base_dir_name(loc); + device_list = Factory::get_device_list(); + read_count_total = 0; if (device_list.size() > 0) { - while(t < 3) { - t++; - round++; - for(list::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) { - device = (W1Device *)*list_iter; - - device->printout(); - sleep(1); - if (round >= store_interval) { - device->store(); - } + while(1) { + 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; } - sleep(scan_interval); - if (round >= store_interval) { - round = 0; + 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); } } 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); } - delete(scanner); return 0; }