]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
Support for setting and reading device name from config file.
[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         list<W1Device *>::iterator      iter;
53
54         // default values than can be overwritten with parameters
55         loc     = "/tmp/w1data";
56         scan_interval   = 600; //600;
57         store_interval  = 2;
58         if (argc > 1) {
59                 loc     = argv[1];
60                 log_info("storage location: %s\n", loc.c_str());
61         }
62         else {
63                 log_warning("No storage location parameter given, using default location: %s\n", loc.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("scanning 1-wire devices\n");
72         log_info("data save dir: %s, scan interval: %ld, save interval: %ld\n", loc.c_str(), scan_interval, store_interval);
73         DeviceConfig::set_base_dir_name(loc);
74         device_list     = Factory::get_device_list();
75         round           = 0;
76         if (device_list.size() > 0) {
77                 while(1) {
78                         round++;
79                         for(iter = device_list.begin(); iter != device_list.end(); iter++) {
80                                 device = (W1Device *)*iter;
81                                 device->printout();
82                                 sleep(1);
83                                 if (round >= store_interval) {
84                                         device->save_data();
85                                 }
86                         }
87                         sleep(scan_interval);
88                         if (round >= store_interval) {
89                                 round = 0;
90                         }
91                 }
92         }
93         else {
94                 log_debug("could not find 1-wire devices.\n");
95         }
96         while (device_list.empty() == false) {
97                 device  = device_list.back();
98                 device_list.pop_back();
99                 delete(device);
100         }
101         return 0;
102 }