]> pilppa.org Git - lib1wire.git/blobdiff - src_test/test_w1.cc
add support for specifying scan interval and store interval in test app
[lib1wire.git] / src_test / test_w1.cc
index 4b94a612784c7582fc3b5cabaeaccd42d5658d38..6aa372fb3e05e2f9e2bfe7303d22ac3428408ba7 100644 (file)
@@ -8,46 +8,91 @@
 #include <string>
 #include <iostream>
 
-#include <unistd.h>
+#include <errno.h>
+#include <stdlib.h>
 
+#include <plp/log.h>
+
+#include "W1Store.hh"
 #include "W1Scanner.hh"
 
 using namespace w1;
 using namespace std;
 
-int main(int argc, char** argv)
-{
-       W1Scanner                       *scanner;
+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) {
+       W1Scanner               *scanner;
        list<W1Device *>        device_list;
-       int                                     round;
-       int                                     interval_seconds;
-       int                                     store_interval;
-
-       interval_seconds        = 60;
-       store_interval          = 10;
-       scanner                         = new W1Scanner();
-       device_list                     = scanner->get_device_list();
-       round                           = 0;
-       while(1) {
-               round++;
-               for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++)
-               {
-                       W1Device *device = (W1Device *)*list_iter;
-/*
-                       string name     = device->get_name();
-                       string value    = device->get_value();
-                       string unit             = device->get_unit();
-                       cout << name << ": " << value << " " << unit << endl;
-*/
-                       device->printout();
+       int                     round;
+       long                    scan_interval;
+       long                    store_interval;
+       string                  location;
+       bool                    err_flg;
+
+       // default values than can be overwritten with parameters
+       location                = "/tmp/";
+       scan_interval   = 600;
+       store_interval          = 6;
+       if (argc > 1) {
+               location        = argv[1];
+               log_info("storage location: %s\n", location.c_str());
+       }
+       else {
+               log_warning("storage location was not given as a parameter, using default location: %s\n", location.c_str());
+       }
+       if (argc > 2) {
+               try_parse_long(argv[2], &scan_interval);
+       }
+       if (argc > 3) {
+                       try_parse_long(argv[3], &store_interval);
+       }
+       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_location(location.c_str());
+       scanner                 = new W1Scanner();
+       device_list             = scanner->get_device_list();
+       round                   = 0;
+       if (device_list.size() > 0) {
+               while(1) {
+                       round++;
+                       for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
+                               W1Device *device = (W1Device *)*list_iter;
+
+                               device->printout();
+                               sleep(1);
+                               if (round >= store_interval) {
+                                       device->store();
+                               }
+                       }
+                       sleep(scan_interval);
                        if (round >= store_interval) {
-                               device->store();
+                               round = 0;
                        }
                }
-               sleep(interval_seconds);
-               if (round >= store_interval) {
-                       round = 0;
-               }
+       }
+       else {
+               log_debug("could not find 1-wire devices.\n");
        }
        return 0;
 }