/* * test_w1.cc * * Created on: Oct 20, 2010 * Author: lamikr */ #include #include #include #include #include #include #include "DeviceConfig.hh" #include "Factory.hh" using namespace w1; using namespace std; bool try_parse_long(const char *str, long *result) { int new_result; char *endptr; bool ret_val; ret_val = false; errno = 0; new_result = strtol(str, &endptr, 10); if (errno != 0) { log_error("invalid input %s, could not convert to integer.\n", str); } else { if (endptr == str) { log_error("invalid input %s, could not convert to integer.\n", str); } else { *result = new_result; ret_val = true; } } return ret_val; } int main(int argc, char** argv) { list device_list; int round; long scan_interval; long store_interval; string loc; W1Device *device; list::iterator iter; // default values than can be overwritten with parameters loc = "/tmp/w1data"; scan_interval = 600; //600; store_interval = 2; if (argc > 1) { loc = argv[1]; log_info("storage location: %s\n", loc.c_str()); } else { log_warning("No storage location parameter given, using default location: %s\n", loc.c_str()); } if (argc > 2) { try_parse_long(argv[2], &scan_interval); } if (argc > 3) { try_parse_long(argv[3], &store_interval); } log_info("start scanning, data saved to location: %s, scan interval: %ld, store interval: %ld\n", loc.c_str(), scan_interval, store_interval); DeviceConfig::set_base_dir_name(loc); device_list = Factory::get_device_list(); round = 0; if (device_list.size() > 0) { while(1) { round++; for(iter = device_list.begin(); iter != device_list.end(); iter++) { device = (W1Device *)*iter; device->printout(); sleep(1); if (round >= store_interval) { device->store(); } } sleep(scan_interval); if (round >= store_interval) { round = 0; } } } else { log_debug("could not find 1-wire devices.\n"); } while (device_list.empty() == false) { device = device_list.back(); device_list.pop_back(); delete(device); } return 0; }