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