]> pilppa.org Git - lib1wire.git/blob - src_test/test_w1_datalog_write.cc
Api cleanups for device querying
[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 #include "Device.hh"
19
20 using namespace w1;
21 using namespace std;
22 using namespace plp;
23
24 #define DEFAULT_READ_INTERVAL_SECONDS   600
25 #define DEFAULT_MAX_READ_COUNT          -1
26
27 bool try_parse_long(const char *str, long *result) {
28         int     new_result;
29         char    *endptr;
30         bool    ret_val;
31
32         ret_val         = false;
33         errno           = 0;
34         new_result      = strtol(str, &endptr, 10);
35         if (errno != 0) {
36                 log_error("invalid input %s, could not convert to integer.\n", str);
37         }
38         else {
39                 if (endptr == str) {
40                         log_error("invalid input %s, could not convert to integer.\n", str);
41                 }
42                 else {
43                         *result = new_result;
44                         ret_val = true;
45                 }
46         }
47         return ret_val;
48 }
49
50 int main(int argc, char** argv) {
51         list<Device *>                  device_list;
52         int                             read_count_total;
53         long                            cnt_max_scans;
54         long                            read_interval_seconds;
55         string                          loc;
56         Device                          *device;
57         list<Device *>::iterator        iter;
58
59         // default values than can be overwritten with parameters
60         loc     = "/tmp/w1data";
61         read_interval_seconds   = DEFAULT_READ_INTERVAL_SECONDS;
62         cnt_max_scans           = DEFAULT_MAX_READ_COUNT;
63         if (argc > 1) {
64                 loc     = argv[1];
65         }
66         else {
67                 log_info("no parameter given using default values:\n");
68                 log_info("\ttest_w1_datalog_write %s %ld %ld\n\n", loc.c_str(), read_interval_seconds, cnt_max_scans);
69                 log_info("usage:\n\ttest_w1_datalog_write <result_save_path> <read_interval_seconds> <max_read_count>\n");
70                 log_info("\tmax_read_count == -1: read devices until application is terminated\n\n");
71         }
72         if (argc > 2) {
73                 try_parse_long(argv[2], &read_interval_seconds);
74         }
75         if (argc > 3) {
76                 try_parse_long(argv[3], &cnt_max_scans);
77         }
78         log_info("searching 1-wire devices\n");
79         if (read_interval_seconds == DEFAULT_READ_INTERVAL_SECONDS) {
80                 log_info("\tdevice read interval: %ld seconds (default value)\n", read_interval_seconds);
81         }
82         else {
83                 log_info("\tdevice read interval: %ld seconds\n", read_interval_seconds);
84         }
85         log_info("\tdata save dir: %s\n", loc.c_str());
86         if (cnt_max_scans == DEFAULT_MAX_READ_COUNT) {
87                 log_info("\tmax read count: %ld (default value, devices will be read until application is terminated)\n\n", cnt_max_scans);
88         }
89         else {
90                 log_info("\tmax read count: %ld\n\n", cnt_max_scans);
91         }
92         DeviceConfig::set_base_dir_name(loc);
93         device_list             = Factory::get_device_list();
94         read_count_total        = 0;
95         if (device_list.size() > 0) {
96                 while(1) {
97                         read_count_total++;
98                         if ((cnt_max_scans != -1) &&
99                             (read_count_total > cnt_max_scans)) {
100                                 log_info("closing the application, max read count reached. (%ld)\n", cnt_max_scans);
101                                 break;
102                         }
103                         for(iter = device_list.begin(); iter != device_list.end(); iter++) {
104                                 device = (Device *)*iter;
105                                 if (device->get_lifecycle_state() == LIFECYCLE_STATUS__AVAILABLE) {
106                                         device->printout();
107                                         sleep(1);
108                                 }
109                                 else {
110                                         log_debug("device not available, %s.\n", device->get_id().c_str());
111                                 }
112                         }
113                         sleep(read_interval_seconds);
114                 }
115         }
116         else {
117                 log_debug("could not find 1-wire devices.\n");
118         }
119         while (device_list.empty() == false) {
120                 device  = device_list.back();
121                 device_list.pop_back();
122                 delete(device);
123         }
124         return 0;
125 }