From a610179331592e69c045b41ef44b5e7d389e12cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20=C3=96hrstr=C3=B6m?= Date: Wed, 11 Nov 2020 22:49:20 +0100 Subject: [PATCH] Enable stdin/file/simulation before event loop started, enable other devices after event loop started. --- src/cmdline.cc | 13 ++++++++++++- src/config.cc | 11 ++++++++++- src/main.cc | 27 ++++++++++++++++++++------- src/util.cc | 2 +- tests/test_alarm.sh | 4 ++-- tests/test_rtlwmbus.sh | 2 +- tests/test_serial_bads.sh | 4 ++-- tests/test_stdin_and_file.sh | 12 ++++++------ 8 files changed, 54 insertions(+), 21 deletions(-) diff --git a/src/cmdline.cc b/src/cmdline.cc index 04f049c..e896fab 100644 --- a/src/cmdline.cc +++ b/src/cmdline.cc @@ -481,10 +481,21 @@ shared_ptr parseCommandLine(int argc, char **argv) { while (argv[i]) { bool ok = handleDevice(c, argv[i]); - if (!ok) break; + if (!ok) + { + if (!argv[i+1]) + { + // This was the last argument on the commandline. + // It should have been a device or a file. + error("Not a valid device \"%s\"\n", argv[i]); + } + // There are more arguments... + break; + } i++; } + if (c->supplied_wmbus_devices.size() == 0 && c->use_auto_device_detect == false && !c->list_shell_envs && diff --git a/src/config.cc b/src/config.cc index 9ff5428..4ce7ceb 100644 --- a/src/config.cc +++ b/src/config.cc @@ -67,7 +67,16 @@ void parseMeterConfig(Configuration *c, vector &buf, string file) // If the key starts with # then the line is a comment. Ignore it. if (p.first.length() > 0 && p.first[0] == '#') continue; - if (p.first == "name") name = p.second; + if (p.first == "name") + { + if (name.find(":") != string::npos) + { + // Oups, names are not allowed to contain the : + warning("Found invalid meter name \"%s\" in meter config file, must not contain a ':', skipping meter.\n", name.c_str()); + return; + } + name = p.second; + } else if (p.first == "type") type = p.second; else diff --git a/src/main.cc b/src/main.cc index 65cae2e..da9418a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -49,7 +49,8 @@ void check_for_dead_wmbus_devices(Configuration *config); shared_ptr create_meter(Configuration *config, MeterType type, MeterInfo *mi, const char *keymsg); shared_ptr create_printer(Configuration *config); shared_ptr create_wmbus_object(Detected *detected, Configuration *config, shared_ptr manager); -void detect_and_configure_wmbus_devices(Configuration *config); +enum class DetectionType { STDIN_FILE_SIMULATION, ALL }; +void detect_and_configure_wmbus_devices(Configuration *config, DetectionType dt); SpecifiedDevice *find_specified_device_from_detected(Configuration *c, Detected *d); bool find_specified_device_and_update_detected(Configuration *c, Detected *d); void find_specified_device_and_mark_as_handled(Configuration *c, Detected *d); @@ -310,7 +311,8 @@ LIST_OF_METERS return newm; } -shared_ptr create_wmbus_object(Detected *detected, Configuration *config, shared_ptr manager) +shared_ptr create_wmbus_object(Detected *detected, Configuration *config, + shared_ptr manager) { shared_ptr wmbus; @@ -486,7 +488,7 @@ shared_ptr create_printer(Configuration *config) config->meterfiles_timestamp)); } -void detect_and_configure_wmbus_devices(Configuration *config) +void detect_and_configure_wmbus_devices(Configuration *config, DetectionType dt) { check_for_dead_wmbus_devices(config); @@ -494,7 +496,7 @@ void detect_and_configure_wmbus_devices(Configuration *config) bool must_auto_find_rtlsdrs = false; // The device=auto has been specified.... - if (config->use_auto_device_detect) + if (config->use_auto_device_detect && dt == DetectionType::ALL) { must_auto_find_ttys = true; must_auto_find_rtlsdrs = true; @@ -503,6 +505,15 @@ void detect_and_configure_wmbus_devices(Configuration *config) for (SpecifiedDevice &specified_device : config->supplied_wmbus_devices) { specified_device.handled = false; + if (dt != DetectionType::ALL) + { + if (!specified_device.is_stdin && !specified_device.is_file && !specified_device.is_simulation) + { + // The event loop has not yet started and this is not stdin nor a file, nor a simulation file. + // Therefore, do not try to detect it yet! + continue; + } + } if (specified_device.file == "" && specified_device.command == "") { // File/tty/command not specified, use auto scan later to find actual device file/tty. @@ -583,7 +594,7 @@ void detect_and_configure_wmbus_devices(Configuration *config) for (SpecifiedDevice &specified_device : config->supplied_wmbus_devices) { - if (!specified_device.handled) + if (dt == DetectionType::ALL && !specified_device.handled) { time_t last_alarm = specified_device.last_alarm; time_t now = time(NULL); @@ -973,7 +984,7 @@ void regular_checkup(Configuration *config) if (serial_manager_ && config) { - detect_and_configure_wmbus_devices(config); + detect_and_configure_wmbus_devices(config, DetectionType::ALL); } { @@ -1124,10 +1135,12 @@ bool start(Configuration *config) // Future changes are triggered through this callback. printed_warning_ = true; - detect_and_configure_wmbus_devices(config); + detect_and_configure_wmbus_devices(config, DetectionType::STDIN_FILE_SIMULATION); serial_manager_->startEventLoop(); + detect_and_configure_wmbus_devices(config, DetectionType::ALL); + if (wmbus_devices_.size() == 0) { notice("No wmbus device detected, waiting for a device to be plugged in.\n"); diff --git a/src/util.cc b/src/util.cc index f753b6a..fc9f8df 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1464,7 +1464,7 @@ void logAlarm(Alarm type, string info) string ts = toString(type); envs.push_back("ALARM_TYPE="+ts); - string msg = tostrprintf("(alarm %s) %s", ts.c_str(), info.c_str()); + string msg = tostrprintf("[ALARM %s] %s", ts.c_str(), info.c_str()); envs.push_back("ALARM_MESSAGE="+msg); warning("%s\n", msg.c_str()); diff --git a/tests/test_alarm.sh b/tests/test_alarm.sh index 0e10c99..86e5723 100755 --- a/tests/test_alarm.sh +++ b/tests/test_alarm.sh @@ -25,7 +25,7 @@ cat /tmp/wmbusmeters_alarm_test echo "---------------------------------------" cat > $TEST/test_expected.txt < /tmp/wmbusmeters_alarm_expected < $TEST/test_responses.txt diff --git a/tests/test_rtlwmbus.sh b/tests/test_rtlwmbus.sh index da2c42b..d4e5516 100755 --- a/tests/test_rtlwmbus.sh +++ b/tests/test_rtlwmbus.sh @@ -11,7 +11,7 @@ TESTNAME="Test rtlwmbus starting background script to produce telegrams" TESTRESULT="ERROR" cat tests/rtlwmbus_water.sh | grep '^#{' | tr -d '#' > $TEST/test_expected.txt -$PROG --format=json "rtlwmbus:CMD(tests/rtlwmbus_water.sh)" \ +$PROG --silent --format=json "rtlwmbus:CMD(tests/rtlwmbus_water.sh)" \ ApWater apator162 88888888 00000000000000000000000000000000 \ | grep -v "(rtlwmbus) child process exited! Command was:" \ > $TEST/test_output.txt diff --git a/tests/test_serial_bads.sh b/tests/test_serial_bads.sh index 69de9d0..2be0439 100755 --- a/tests/test_serial_bads.sh +++ b/tests/test_serial_bads.sh @@ -16,7 +16,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -43,7 +43,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt +xxd -r -p simulations/serial_rawtty_bad.hex | $PROG --silent --format=json --listento=any stdin Rummet1 lansenth 00010203 "" Rummet2 rfmamb 11772288 "" | grep Rummet > $TEST/test_output.txt if [ "$?" = "0" ] then diff --git a/tests/test_stdin_and_file.sh b/tests/test_stdin_and_file.sh index 4fde5f3..147ce66 100755 --- a/tests/test_stdin_and_file.sh +++ b/tests/test_stdin_and_file.sh @@ -16,7 +16,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -46,7 +46,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -74,7 +74,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -101,7 +101,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -127,7 +127,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt @@ -154,7 +154,7 @@ cat > $TEST/test_expected.txt < $TEST/test_output.txt