]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
Support for querying the list devices having data stored.
[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 "DeviceConfig.hh"
17 #include "Factory.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         list<W1Device *>        device_list;
47         int                     round;
48         long                    scan_interval;
49         long                    store_interval;
50         string                  loc;
51         W1Device                *device;
52
53         // default values than can be overwritten with parameters
54         loc     = "/tmp/w1data";
55         scan_interval   = 600; //600;
56         store_interval  = 2;
57         if (argc > 1) {
58                 loc     = argv[1];
59                 log_info("storage location: %s\n", loc.c_str());
60         }
61         else {
62                 log_warning("No storage location parameter given, using default location: %s\n", loc.c_str());
63         }
64         if (argc > 2) {
65                 try_parse_long(argv[2], &scan_interval);
66         }
67         if (argc > 3) {
68                 try_parse_long(argv[3], &store_interval);
69         }
70         log_info("start scanning, data saved to location: %s, scan interval: %ld, store interval: %ld\n", loc.c_str(), scan_interval, store_interval);
71         DeviceConfig::set_base_dir_name(loc);
72         device_list     = Factory::get_device_list();
73         round           = 0;
74         if (device_list.size() > 0) {
75                 while(1) {
76                         round++;
77                         for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
78                                 device = (W1Device *)*list_iter;
79                                 device->printout();
80                                 sleep(1);
81                                 if (round >= store_interval) {
82                                         device->store();
83                                 }
84                         }
85                         sleep(scan_interval);
86                         if (round >= store_interval) {
87                                 round = 0;
88                         }
89                 }
90         }
91         else {
92                 log_debug("could not find 1-wire devices.\n");
93         }
94         while (device_list.empty() == false) {
95                 device  = device_list.back();
96                 device_list.pop_back();
97                 delete(device);
98         }
99         return 0;
100 }