]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
renamed the test_w1 to test_w1_datalog_write
[lib1wire.git] / src_test / test_w1_datalog_write.cc
1 /*
2  * test_w1.cc
3  *
4  *  Created on: Oct 20, 2010
5  *      Author: lamikr
6  */
7 #include <list>
8 #include <string>
9 #include <iostream>
10
11 #include <errno.h>
12 #include <stdlib.h>
13
14 #include <plp/log.h>
15
16 #include "W1Store.hh"
17 #include "W1Scanner.hh"
18
19 using namespace w1;
20 using namespace std;
21
22 bool try_parse_long(const char *str, long *result) {
23         int     new_result;
24         char    *endptr;
25         bool    ret_val;
26
27         ret_val         = false;
28         errno           = 0;
29         new_result      = strtol(str, &endptr, 10);
30         if (errno != 0) {
31                 log_error("invalid input %s, could not convert to integer.\n", str);
32         }
33         else {
34                 if (endptr == str) {
35                         log_error("invalid input %s, could not convert to integer.\n", str);
36                 }
37                 else {
38                         *result = new_result;
39                         ret_val = true;
40                 }
41         }
42         return ret_val;
43 }
44
45 int main(int argc, char** argv) {
46         W1Scanner               *scanner;
47         list<W1Device *>        device_list;
48         int                     round;
49         long                    scan_interval;
50         long                    store_interval;
51         string                  location;
52         bool                    err_flg;
53
54         // default values than can be overwritten with parameters
55         location        = "/tmp/";
56         scan_interval   = 600;
57         store_interval  = 6;
58         if (argc > 1) {
59                 location        = argv[1];
60                 log_info("storage location: %s\n", location.c_str());
61         }
62         else {
63                 log_warning("No storage location parameter given, using default location: %s\n", location.c_str());
64         }
65         if (argc > 2) {
66                 try_parse_long(argv[2], &scan_interval);
67         }
68         if (argc > 3) {
69                 try_parse_long(argv[3], &store_interval);
70         }
71         log_info("start scanning, data saved to location: %s, scan interval: %d, store interval: %d\n", location.c_str(), scan_interval, store_interval);
72         W1Store::set_location_base_dir(location.c_str());
73         scanner         = new W1Scanner();
74         device_list     = scanner->get_device_list();
75         round           = 0;
76         if (device_list.size() > 0) {
77                 while(1) {
78                         round++;
79                         for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
80                                 W1Device *device = (W1Device *)*list_iter;
81
82                                 device->printout();
83                                 sleep(1);
84                                 if (round >= store_interval) {
85                                         device->store();
86                                 }
87                         }
88                         sleep(scan_interval);
89                         if (round >= store_interval) {
90                                 round = 0;
91                         }
92                 }
93         }
94         else {
95                 log_debug("could not find 1-wire devices.\n");
96         }
97         return 0;
98 }