]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
Initial support for reading and writing device specific config data.
[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 "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         W1Device                *device;
53
54         // default values than can be overwritten with parameters
55         location        = "/tmp/";
56         scan_interval   = 2; //600;
57         store_interval  = 2;
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: %ld, store interval: %ld\n", location.c_str(), scan_interval, store_interval);
72         DeviceConfig::set_base_dir_name(location);
73         scanner         = new W1Scanner();
74         device_list     = scanner->get_device_list();
75         round           = 0;
76         int     t = 0;
77         if (device_list.size() > 0) {
78                 while(t < 3) {
79                         t++;
80                         round++;
81                         for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
82                                 device = (W1Device *)*list_iter;
83                                 device->printout();
84                                 sleep(1);
85                                 if (round >= store_interval) {
86                                         device->store();
87                                 }
88                         }
89                         sleep(scan_interval);
90                         if (round >= store_interval) {
91                                 round = 0;
92                         }
93                 }
94         }
95         else {
96                 log_debug("could not find 1-wire devices.\n");
97         }
98         while (device_list.empty() == false) {
99                 device  = device_list.back();
100                 device_list.pop_back();
101                 delete(device);
102         }
103         delete(scanner);
104         return 0;
105 }