diff --git a/install.sh b/install.sh index 03af17a..e9883f9 100755 --- a/install.sh +++ b/install.sh @@ -164,6 +164,7 @@ then [Unit] Description=wmbusmeters service After=network.target +StopWhenUnneeded=true [Service] Type=forking @@ -204,8 +205,8 @@ then mkdir -p $ROOT/etc/udev/rules.d # Create service file cat < $ROOT/etc/udev/rules.d/99-wmbus-usb-serial.rules -SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", SYMLINK+="im871a",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters.service" -SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="amb8465",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters.service" +SUBSYSTEM=="tty", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60",SYMLINK+="im871a",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters.service" +SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001",SYMLINK+="amb8465",MODE="0660", GROUP="wmbusmeters",TAG+="systemd",ENV{SYSTEMD_WANTS}="wmbusmeters.service" EOF echo udev: installed $ROOT/etc/udev/rules.d/99-wmbus-usb-serial.rules else diff --git a/src/main.cc b/src/main.cc index 9e0abe9..9dbee60 100644 --- a/src/main.cc +++ b/src/main.cc @@ -97,30 +97,30 @@ int main(int argc, char **argv) } } -void startUsingCommandline(Configuration *cmdline) +void startUsingCommandline(Configuration *config) { - warningSilenced(cmdline->silence); - verboseEnabled(cmdline->verbose); - logTelegramsEnabled(cmdline->logtelegrams); - debugEnabled(cmdline->debug); + warningSilenced(config->silence); + verboseEnabled(config->verbose); + logTelegramsEnabled(config->logtelegrams); + debugEnabled(config->debug); - if (cmdline->exitafter != 0) { - verbose("(cmdline) wmbusmeters will exit after %d seconds\n", cmdline->exitafter); + if (config->exitafter != 0) { + verbose("(config) wmbusmeters will exit after %d seconds\n", config->exitafter); } - if (cmdline->meterfiles) { - verbose("(cmdline) store meter files in: \"%s\"\n", cmdline->meterfiles_dir.c_str()); + if (config->meterfiles) { + verbose("(config) store meter files in: \"%s\"\n", config->meterfiles_dir.c_str()); } - verbose("(cmdline) using usb device: %s\n", cmdline->usb_device.c_str()); - verbose("(cmdline) number of meters: %d\n", cmdline->meters.size()); + verbose("(config) using usb device: %s\n", config->usb_device.c_str()); + verbose("(config) number of meters: %d\n", config->meters.size()); - auto manager = createSerialCommunicationManager(cmdline->exitafter); + auto manager = createSerialCommunicationManager(config->exitafter); onExit(call(manager.get(),stop)); unique_ptr wmbus; - auto type_and_device = detectMBusDevice(cmdline->usb_device, manager.get()); + auto type_and_device = detectMBusDevice(config->usb_device, manager.get()); switch (type_and_device.first) { case DEVICE_IM871A: @@ -137,7 +137,7 @@ void startUsingCommandline(Configuration *cmdline) break; case DEVICE_UNKNOWN: warning("No wmbus device found! Exiting!\n"); - if (cmdline->daemon) { + if (config->daemon) { // If starting as a daemon, wait a bit so that systemd have time to catch up. sleep(1); } @@ -145,35 +145,35 @@ void startUsingCommandline(Configuration *cmdline) break; } - if (!cmdline->link_mode_set) { + if (!config->link_mode_set) { // The link mode is not explicitly set. Examine the meters to see which // link mode to use. - for (auto &m : cmdline->meters) { - if (!cmdline->link_mode_set) { - cmdline->link_mode = toMeterLinkMode(m.type); - cmdline->link_mode_set = true; + for (auto &m : config->meters) { + if (!config->link_mode_set) { + config->link_mode = toMeterLinkMode(m.type); + config->link_mode_set = true; } else { - if (cmdline->link_mode != toMeterLinkMode(m.type)) { + if (config->link_mode != toMeterLinkMode(m.type)) { error("A different link mode has been set already.\n"); } } } } - if (!cmdline->link_mode_set) { + if (!config->link_mode_set) { error("If you specify no meters, you have to specify the link mode: --c1 or --t1\n"); } - wmbus->setLinkMode(cmdline->link_mode); + wmbus->setLinkMode(config->link_mode); string using_link_mode = linkModeName(wmbus->getLinkMode()); - verbose("(cmdline) using link mode: %s\n", using_link_mode.c_str()); + verbose("(config) using link mode: %s\n", using_link_mode.c_str()); - auto output = unique_ptr(new Printer(cmdline->json, cmdline->fields, - cmdline->separator, cmdline->meterfiles, cmdline->meterfiles_dir, - cmdline->shells)); + auto output = unique_ptr(new Printer(config->json, config->fields, + config->separator, config->meterfiles, config->meterfiles_dir, + config->shells)); vector> meters; - if (cmdline->meters.size() > 0) { - for (auto &m : cmdline->meters) { + if (config->meters.size() > 0) { + for (auto &m : config->meters) { const char *keymsg = (m.key[0] == 0) ? "not-encrypted" : "encrypted"; switch (toMeterType(m.type)) { case MULTICAL21_METER: @@ -208,11 +208,11 @@ void startUsingCommandline(Configuration *cmdline) error("No such meter type \"%s\"\n", m.type.c_str()); break; } - if (cmdline->list_shell_envs) { + if (config->list_shell_envs) { string ignore1, ignore2, ignore3; vector envs; meters.back()->printMeter(&ignore1, - &ignore2, cmdline->separator, + &ignore2, config->separator, &ignore3, &envs); printf("Environment variables provided to shell for meter %s:\n", m.type.c_str()); @@ -224,7 +224,7 @@ void startUsingCommandline(Configuration *cmdline) exit(0); } meters.back()->onUpdate(calll(output.get(),print,Meter*)); - meters.back()->onUpdate([&](Meter*meter) { oneshotCheck(cmdline, manager.get(), meter, meters); }); + meters.back()->onUpdate([&](Meter*meter) { oneshotCheck(config, manager.get(), meter, meters); }); } } else { printf("No meters configured. Printing id:s of all telegrams heard!\n\n"); @@ -236,13 +236,19 @@ void startUsingCommandline(Configuration *cmdline) wmbus->simulate(); } - notice("wmbusmeters waiting for telegrams\n"); + if (config->daemon || config->use_logfile) { + notice("(wmbusmeters) waiting for telegrams\n"); + } manager->waitForStop(); + + if (config->daemon || config->use_logfile) { + notice("(wmbusmeters) shutting down\n"); + } } -void oneshotCheck(Configuration *cmdline, SerialCommunicationManager *manager, Meter *meter, vector> &meters) +void oneshotCheck(Configuration *config, SerialCommunicationManager *manager, Meter *meter, vector> &meters) { - if (!cmdline->oneshot) return; + if (!config->oneshot) return; for (auto &m : meters) { if (m->numUpdates() == 0) return; @@ -259,10 +265,10 @@ void writePid(string pid_file, int pid) } if (pid > 0) { int n = fprintf(pidf, "%d\n", pid); - notice("writing pid %s\n", pid_file.c_str()); if (!n) { error("Could not write pid (%d) to file \"%s\"!\n", pid, pid_file.c_str()); } + notice("(wmbusmeters) started %s\n", pid_file.c_str()); } fclose(pidf); return; @@ -275,8 +281,6 @@ void startDaemon(string pid_file) enableSyslog(); - notice("wmbusmeters starting...\n"); - // Pre check that the pid file can be writte to. // Exit before fork, if it fails. writePid(pid_file, 0); diff --git a/tests/config2/etc/wmbusmeters.d/MoreWater b/tests/config2/etc/wmbusmeters.d/MoreWater new file mode 100644 index 0000000..c59621a --- /dev/null +++ b/tests/config2/etc/wmbusmeters.d/MoreWater @@ -0,0 +1,4 @@ +name=MoreWater +type=iperl +id=12345699 +key= diff --git a/tests/config2/etc/wmbusmeters.d/MyColdWater b/tests/config2/etc/wmbusmeters.d/MyColdWater new file mode 100644 index 0000000..e38e600 --- /dev/null +++ b/tests/config2/etc/wmbusmeters.d/MyColdWater @@ -0,0 +1,4 @@ +name=MyColdWater +type=supercom587 +id=11111111 +key= \ No newline at end of file diff --git a/tests/config2/etc/wmbusmeters.d/MyWarmWater b/tests/config2/etc/wmbusmeters.d/MyWarmWater new file mode 100644 index 0000000..44f69b6 --- /dev/null +++ b/tests/config2/etc/wmbusmeters.d/MyWarmWater @@ -0,0 +1,4 @@ +name=MyWarmWater +type=supercom587 +id=12345678 +key= \ No newline at end of file diff --git a/tests/test_c1_meters.sh b/tests/test_c1_meters.sh index dfc56f8..4656467 100755 --- a/tests/test_c1_meters.sh +++ b/tests/test_c1_meters.sh @@ -16,7 +16,7 @@ $PROG --robot=json simulations/simulation_c1.txt \ if [ "$?" == "0" ] then - cat $TEST/test_output.txt | grep -v "wmbusmeters waiting for telegrams" | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt + cat $TEST/test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt diff $TEST/test_expected.txt $TEST/test_responses.txt if [ "$?" == "0" ] then diff --git a/tests/test_config1.sh b/tests/test_config1.sh index bbabc9f..2d29c84 100755 --- a/tests/test_config1.sh +++ b/tests/test_config1.sh @@ -10,7 +10,7 @@ WMBUSMETERS_CONFIG_ROOT=tests/config1 $PROG --useconfig > $TEST/test_output.txt if [ "$?" == "0" ] then - cat $TEST/test_output.txt | grep -v "wmbusmeters waiting for telegrams" | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt + cat $TEST/test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt diff $TEST/test_expected.txt $TEST/test_responses.txt if [ "$?" == "0" ] then diff --git a/tests/test_shell.sh b/tests/test_shell.sh index e932cdf..8d3c916 100755 --- a/tests/test_shell.sh +++ b/tests/test_shell.sh @@ -9,7 +9,7 @@ TEST=testoutput $PROG --shell='echo "$METER_JSON"' simulations/simulation_shell.txt MWW supercom587 12345678 "" > $TEST/test_output.txt if [ "$?" == "0" ] then - cat $TEST/test_output.txt | grep -v "wmbusmeters waiting for telegrams" | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt + cat $TEST/test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt echo '{"media":"warm water","meter":"supercom587","name":"MWW","id":"12345678","total_m3":5.548000,"timestamp":"1111-11-11T11:11:11Z"}' > $TEST/test_expected.txt diff $TEST/test_expected.txt $TEST/test_responses.txt if [ "$?" == "0" ] diff --git a/tests/test_t1_meters.sh b/tests/test_t1_meters.sh index e925be6..dd58f9f 100755 --- a/tests/test_t1_meters.sh +++ b/tests/test_t1_meters.sh @@ -14,7 +14,7 @@ $PROG --robot=json simulations/simulation_t1.txt \ > $TEST/test_output.txt if [ "$?" == "0" ] then - cat $TEST/test_output.txt | grep -v "wmbusmeters waiting for telegrams" | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt + cat $TEST/test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt diff $TEST/test_expected.txt $TEST/test_responses.txt if [ "$?" == "0" ] then