]> pilppa.com Git - lib1wire.git/commitdiff
cleaned up test_w1_datalog_write example applications parameter handling.
authorMika Laitio <lamikr@pilppa.org>
Wed, 26 Jan 2011 20:13:13 +0000 (22:13 +0200)
committerMika Laitio <lamikr@pilppa.org>
Wed, 26 Jan 2011 20:13:13 +0000 (22:13 +0200)
test_w1_datalog_write canscan 1-wire devices, reads 1-wire device data
values and save them.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/DeviceConfig.cc
src/W1Device.cc
src_test/test_w1_datalog_write.cc

index 70027925ebb045eace8e17ad0bed5fc0b4fc75bb..da3d85ffa93f744f16fa80e3a26d6a826eef8986 100644 (file)
@@ -98,7 +98,7 @@ string DeviceConfig::get_cfg_value(string key) {
                        if (option != NULL) {
                                switch (option->type) {
                                        case UCI_TYPE_STRING:
-                                               log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
+                                               //log_info("key: %s option name: %s, value: %s\n", key.c_str(), option->e.name, option->v.string);
                                                ret_val = option->v.string;
                                                break;
                                        default:
index 17164eabba9c68cc08341dbdd5e16284055d099e..781ef6d878e2191a8f2fdc83f271d255a4e81494 100644 (file)
@@ -32,7 +32,7 @@ W1Device::W1Device(int family_code_param,
        temp_str        = W1_SLAVE_FILE;
        dir_path        = rootdir + "/" + direntry_param->d_name;
        slave_file      = dir_path + "/" + temp_str;
-       log_debug("w1 data file: %s\n", slave_file.c_str());
+       log_debug("1-wire device's data file: %s\n", slave_file.c_str());
        family_code     = family_code_param;
        id              = device_id_param;
        name            = "";
index 141031b50c35fe58134937c9085a633e77da3eb6..f397525c95b7004cee6dbe80e4a0222da6fc8f01 100644 (file)
 using namespace w1;
 using namespace std;
 
+#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;
@@ -44,50 +48,90 @@ bool try_parse_long(const char *str, long *result) {
 
 int main(int argc, char** argv) {
        list<W1Device *>                device_list;
-       int                             round;
-       long                            scan_interval;
-       long                            store_interval;
+       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;
 
        // default values than can be overwritten with parameters
        loc     = "/tmp/w1data";
-       scan_interval   = 600; //600;
-       store_interval  = 2;
+       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];
-               log_info("storage location: %s\n", loc.c_str());
        }
        else {
-               log_warning("No storage location parameter given, using default location: %s\n", loc.c_str());
+               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("\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], &save_interval_count);
+       }
+       if (argc > 4) {
+               try_parse_long(argv[4], &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);
+       }
+       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");
+       }
+       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);
+       }
+       else {
+               log_info("\tmax read count: %ld\n\n", cnt_max_scans);
        }
-       log_info("scanning 1-wire devices\n");
-       log_info("data save dir: %s, scan interval: %ld, save interval: %ld\n", loc.c_str(), scan_interval, store_interval);
        DeviceConfig::set_base_dir_name(loc);
-       device_list     = Factory::get_device_list();
-       round           = 0;
+       device_list             = Factory::get_device_list();
+       read_count_afer_save    = 0;
+       read_count_total        = 0;
        if (device_list.size() > 0) {
                while(1) {
-                       round++;
+                       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 (round >= store_interval) {
+                               if ((save_interval_count != -1) &&
+                                   (read_count_afer_save >= save_interval_count)) {
                                        device->save_data();
                                }
                        }
-                       sleep(scan_interval);
-                       if (round >= store_interval) {
-                               round = 0;
-                               break;
+                       sleep(read_interval_seconds);
+                       if ((save_interval_count != -1) &&
+                           (read_count_afer_save >= save_interval_count)) {
+                               read_count_afer_save = 0;
                        }
                }
        }