]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
started adding support for reading average and sum 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 "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         W1Device                *device;
54
55         // default values than can be overwritten with parameters
56         location        = "/tmp/";
57         scan_interval   = 2; //600;
58         store_interval  = 2;
59         if (argc > 1) {
60                 location        = argv[1];
61                 log_info("storage location: %s\n", location.c_str());
62         }
63         else {
64                 log_warning("No storage location parameter given, using default location: %s\n", location.c_str());
65         }
66         if (argc > 2) {
67                 try_parse_long(argv[2], &scan_interval);
68         }
69         if (argc > 3) {
70                 try_parse_long(argv[3], &store_interval);
71         }
72         log_info("start scanning, data saved to location: %s, scan interval: %d, store interval: %d\n", location.c_str(), scan_interval, store_interval);
73         W1Store::set_store_base_dir(location.c_str());
74         scanner         = new W1Scanner();
75         device_list     = scanner->get_device_list();
76         round           = 0;
77         int     t = 0;
78         if (device_list.size() > 0) {
79                 while(t < 3) {
80                         t++;
81                         round++;
82                         for(list<W1Device *>::iterator list_iter = device_list.begin(); list_iter != device_list.end(); list_iter++) {
83                                 device = (W1Device *)*list_iter;
84
85                                 device->printout();
86                                 sleep(1);
87                                 if (round >= store_interval) {
88                                         device->store();
89                                 }
90                         }
91                         sleep(scan_interval);
92                         if (round >= store_interval) {
93                                 round = 0;
94                         }
95                 }
96         }
97         else {
98                 log_debug("could not find 1-wire devices.\n");
99         }
100         while (device_list.empty() == false) {
101                 device  = device_list.back();
102                 device_list.pop_back();
103                 delete(device);
104         }
105
106         delete(scanner);
107         return 0;
108 }