From 69cf31ace096505027195abae13839ee5c326714 Mon Sep 17 00:00:00 2001 From: weetmuts Date: Sat, 23 Feb 2019 18:30:16 +0100 Subject: [PATCH] Source cleanup. --- .gitignore | 1 + Makefile | 24 ++++++---- install.sh | 95 ++++++++++++++++++++++++++++++++++++++++ src/cmdline.cc | 9 +++- src/cmdline.h | 2 +- src/config.cc | 7 +++ src/config.h | 1 + src/main.cc | 22 +++++----- test.sh | 77 ++------------------------------ tests/test_c1_meters.sh | 26 +++++++++++ tests/test_meterfiles.sh | 30 +++++++++++++ tests/test_shell.sh | 21 +++++++++ tests/test_t1_meters.sh | 25 +++++++++++ 13 files changed, 246 insertions(+), 94 deletions(-) create mode 100755 install.sh create mode 100755 tests/test_c1_meters.sh create mode 100755 tests/test_meterfiles.sh create mode 100755 tests/test_shell.sh create mode 100755 tests/test_t1_meters.sh diff --git a/.gitignore b/.gitignore index 0a0d26d..56e5052 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ build_debug build_arm/ build_arm_debug/ archive/ +testoutput/ *~ \ No newline at end of file diff --git a/Makefile b/Makefile index 81ea1fd..9be4df7 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,8 @@ # make DEBUG=true # make DEBUG=true HOST=arm +VERSION=0.8.4 + ifeq "$(HOST)" "arm" CXX=arm-linux-gnueabihf-g++ STRIP=arm-linux-gnueabihf-strip @@ -44,7 +46,7 @@ endif $(shell mkdir -p $(BUILD)) -CXXFLAGS := $(DEBUG_FLAGS) -fPIC -fmessage-length=0 -std=c++11 -Wall -Wno-maybe-uninitialized -Wno-unused-function "-DWMBUSMETERS_VERSION=\"0.8.4\"" +CXXFLAGS := $(DEBUG_FLAGS) -fPIC -fmessage-length=0 -std=c++11 -Wall -Wno-maybe-uninitialized -Wno-unused-function "-DWMBUSMETERS_VERSION=\"$(VERSION)\"" $(BUILD)/%.o: src/%.cc $(wildcard src/%.h) $(CXX) $(CXXFLAGS) $< -c -o $@ @@ -73,23 +75,27 @@ METERS_OBJS:=\ all: $(BUILD)/wmbusmeters $(BUILD)/testinternals @$(STRIP_BINARY) - @rm -f $(BUILD)/wmbusmetersd - @cp $(BUILD)/wmbusmeters $(BUILD)/wmbusmetersd -dist: wmbusmeters_0.8.1_$(DEBARCH).deb +dist: wmbusmeters_$(VERSION)_$(DEBARCH).deb -wmbusmeters_0.8.1_$(DEBARCH).deb: +install: $(BUILD)/wmbusmeters + @./install.sh $(BUILD)/wmbusmeters + +wmbusmeters_$(VERSION)_$(DEBARCH).deb: + @rm -rf $(BUILD)/debian/wmbusmeters @mkdir -p $(BUILD)/debian/wmbusmeters/DEBIAN - @mkdir -p $(BUILD)/debian/wmbusmeters/usr/local/bin - @cp $(BUILD)/wmbusmeters $(BUILD)/debian/wmbusmeters/usr/local/bin + @mkdir -p $(BUILD)/debian/wmbusmeters/usr/bin + @mkdir -p $(BUILD)/debian/wmbusmeters/usr/sbin + @cp $(BUILD)/wmbusmeters $(BUILD)/debian/wmbusmeters/usr/bin/wmbusmeters + @ln $(BUILD)/debian/wmbusmeters/usr/bin/wmbusmeters $(BUILD)/debian/wmbusmeters/usr/sbin/wmbusmetersd @rm -f $(BUILD)/debian/wmbusmeters/DEBIAN/control @echo "Package: wmbusmeters" >> $(BUILD)/debian/wmbusmeters/DEBIAN/control - @echo "Version: 0.8.1" >> $(BUILD)/debian/wmbusmeters/DEBIAN/control + @echo "Version: $(VERSION)" >> $(BUILD)/debian/wmbusmeters/DEBIAN/control @echo "Maintainer: Fredrik Öhrström" >> $(BUILD)/debian/wmbusmeters/DEBIAN/control @echo "Architecture: $(DEBARCH)" >> $(BUILD)/debian/wmbusmeters/DEBIAN/control @echo "Description: A tool to read wireless mbus telegrams from utility meters." >> $(BUILD)/debian/wmbusmeters/DEBIAN/control @(cd $(BUILD)/debian; dpkg-deb --build wmbusmeters .) - @mv $(BUILD)/debian/wmbusmeters_0.8.1_$(DEBARCH).deb . + @mv $(BUILD)/debian/wmbusmeters_$(VERSION)_$(DEBARCH).deb . @echo Built package $@ $(BUILD)/wmbusmeters: $(METERS_OBJS) $(BUILD)/main.o diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..1748e7e --- /dev/null +++ b/install.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +if [ "$1" = "" ] +then + echo Usage: install.sh build/wmbusmeters + exit 0 +fi + +if [ ! $(basename "$1") = "wmbusmeters" ] +then + echo Oups, please only try to install wmbusmeters using this script. + exit 1 +fi + +if [ ! -x "$1" ] +then + echo This is not an executable. + exit 1 +fi + +if [ "$EUID" -ne 0 ] +then echo "Please run as root." + exit +fi + +rm -f /usr/bin/wmbusmeters /usr/sbin/wmbusmetersd +cp "$1" /usr/bin/wmbusmeters +ln -s /usr/bin/wmbusmeters /usr/sbin/wmbusmetersd + +echo binaries: installed /usr/bin/wmbusmeters and /usr/sbin/wmbusmetersd + +ID=$(id -u wmbusmeters 2>/dev/null) + +if [ "$ID" == "" ] +then + # Create the wmbusmeters user + adduser --no-create-home --shell /usr/sbin/nologin --disabled-login --gecos "" wmbusmeters + echo user: added wmbusmeters +else + echo user: wmbusmeters unmodified +fi + +if [ ! -f /etc/wmbusmeters.conf ] +then + # Create default configuration + cat < /etc/wmbusmeters.conf +loglevel=normal +device=auto +logtelegrams=false +meterfiles=/tmp/wmbusmeters +EOF + chown root:root /etc/wmbusmeters.conf + chmod 644 /etc/wmbusmeters.d + echo conf file: created /etc/wmbusmeters.conf +else + echo conf file: /etc/wmbusmeters.conf unchanged +fi + +if [ ! -d /etc/wmbusmeters.d ] +then + # Create the configuration directory + mkdir -p /etc/wmbusmeters.d + chown -R root:root /etc/wmbusmeters.d + chmod -R 644 /etc/wmbusmeters.d + echo conf dir: created /etc/wmbusmeters.d +else + echo conf dir: /etc/wmbusmeters.d unchanged +fi + +if [ -d /etc/systemd/system ] +then + if [ ! -f /etc/systemd/system/wmbusmeters.service ] + then + # Create service file + cat < /etc/systemd/system/wmbusmeters.service +[Unit] +Description=wmbusmeters service +After=network.target + +[Service] +Type=simple +#Restart=always +RestartSec=1 +User=wmbusmeters +ExecStart=/usr/sbin/wmbusmetersd + +[Install] +WantedBy=multi-user.target +EOF + + echo systemd: installed /etc/systemd/system/wmbusmeters.service + else + echo systemd: /etc/systemd/system/wmbusmeters.service unchanged + fi +fi diff --git a/src/cmdline.cc b/src/cmdline.cc index e1f9755..36cff72 100644 --- a/src/cmdline.cc +++ b/src/cmdline.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2017-2018 Fredrik Öhrström + Copyright (C) 2017-2019 Fredrik Öhrström This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -90,6 +90,13 @@ unique_ptr parseCommandLine(int argc, char **argv) { } return unique_ptr(c); } + if (!strcmp(argv[i], "--reload")) { + c->reload = true; + if (i > 1 || argc > 2) { + error("Usage error: --reload implies no other arguments on the command line.\n"); + } + return unique_ptr(c); + } if (!strncmp(argv[i], "--robot", 7)) { if (strlen(argv[i]) == 7 || (strlen(argv[i]) == 12 && diff --git a/src/cmdline.h b/src/cmdline.h index 9229eff..34137bd 100644 --- a/src/cmdline.h +++ b/src/cmdline.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2017-2018 Fredrik Öhrström + Copyright (C) 2017-2019 Fredrik Öhrström This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/src/config.cc b/src/config.cc index f35bdfc..a028a8f 100644 --- a/src/config.cc +++ b/src/config.cc @@ -79,6 +79,7 @@ unique_ptr loadConfiguration() auto i = global_conf.begin(); string loglevel; string device; + string meterfiles_dir; for (;;) { auto p = getNextKeyValue(global_conf, i); @@ -87,6 +88,7 @@ unique_ptr loadConfiguration() if (p.first == "loglevel") loglevel = p.second; if (p.first == "device") device = p.second; + if (p.first == "meterfilesdir") meterfiles_dir = p.second; } if (loglevel == "verbose") { @@ -105,6 +107,11 @@ unique_ptr loadConfiguration() } // cmdline->logtelegrams + c->meterfiles_dir = meterfiles_dir; + if (!checkIfDirExists(c->meterfiles_dir.c_str())) { + warning("Cannot write meter files into dir \"%s\"\n", c->meterfiles_dir.c_str()); + } + c->usb_device = device; vector meter_files; diff --git a/src/config.h b/src/config.h index 8317ddc..613781e 100644 --- a/src/config.h +++ b/src/config.h @@ -41,6 +41,7 @@ struct MeterInfo { struct CommandLine { bool daemon {}; bool useconfig {}; + bool reload {}; bool need_help {}; bool silence {}; bool verbose {}; diff --git a/src/main.cc b/src/main.cc index e2ec58f..0fc4bfb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2017-2018 Fredrik Öhrström + Copyright (C) 2017-2019 Fredrik Öhrström This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -63,30 +63,32 @@ int main(int argc, char **argv) printf(" --exitafter=20h program exits after running for twenty hoursh\n" " or 10m for ten minutes or 5s for five seconds.\n"); printf(" --useconfig read from /etc/wmbusmeters.conf and /etc/wmbusmeters.d\n"); - printf(" check the man page for how to write the config files.\n\n"); + printf(" check the man page for how to write the config files.\n"); + printf(" --reload signals a running wmbusmeters daemon to reload the configuration,\n"); + printf(" when you have modified config files and/or usb dongles.\n\n"); + printf("Specifying auto as the device will automatically look for usb\n"); printf("wmbus dongles on /dev/im871a and /dev/amb8465\n\n"); printf("The meter types: multical21,flowiq3100,supercom587,iperl (water meters) are supported.\n" "The meter types: multical302 (heat) and omnipower (electricity) qcaloric (heat cost)\n" "are work in progress.\n\n"); - exit(0); } - + else if (cmdline->daemon) { startDaemon(); exit(0); } - + else if (cmdline->useconfig) { startUsingConfigFiles(); exit(0); } + else { + // We want the data visible in the log file asap! + setbuf(stdout, NULL); - // We want the data visible in the log file asap! - setbuf(stdout, NULL); - - startUsingCommandline(cmdline.get()); - exit(0); + startUsingCommandline(cmdline.get()); + } } void startUsingCommandline(CommandLine *cmdline) diff --git a/test.sh b/test.sh index 1747848..8627bad 100755 --- a/test.sh +++ b/test.sh @@ -8,76 +8,7 @@ if [ "$?" = "0" ]; then echo Internal test OK fi -cat simulations/simulation_c1.txt | grep '^{' > test_expected.txt -$PROG --robot=json simulations/simulation_c1.txt \ - MyHeater multical302 12345678 "" \ - MyTapWater multical21 76348799 "" \ - Vadden multical21 44556677 "" \ - MyElectricity omnipower 15947107 "" \ - > test_output.txt -if [ "$?" == "0" ] -then - cat test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > test_responses.txt - diff test_expected.txt test_responses.txt - if [ "$?" == "0" ] - then - echo C1 OK - fi -else - Failure. -fi - -cat simulations/simulation_t1.txt | grep '^{' > test_expected.txt -$PROG --robot=json simulations/simulation_t1.txt \ - MyWarmWater supercom587 12345678 "" \ - MyColdWater supercom587 11111111 "" \ - MoreWater iperl 12345699 "" \ - > test_output.txt -if [ "$?" == "0" ] -then - cat test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > test_responses.txt - diff test_expected.txt test_responses.txt - if [ "$?" == "0" ] - then - echo T1 OK - fi -else - Failure. -fi - -$PROG --shell='echo "$METER_JSON"' simulations/simulation_shell.txt MWW supercom587 12345678 "" > test_output.txt -if [ "$?" == "0" ] -then - cat test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > test_responses.txt - echo '{"media":"warm water","meter":"supercom587","name":"MWW","id":"12345678","total_m3":5.548000,"timestamp":"1111-11-11T11:11:11Z"}' > test_expected.txt - diff test_expected.txt test_responses.txt - if [ "$?" == "0" ] - then - echo SHELL OK - fi -else - Failure. -fi - -rm -f /tmp/MyTapWater -cat simulations/simulation_c1.txt | grep '^{' | grep 76348799 | tail -n 1 > test_expected.txt -$PROG --meterfiles --robot=json simulations/simulation_c1.txt MyTapWater multical21 76348799 "" -cat /tmp/MyTapWater | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > test_response.txt -diff test_expected.txt test_response.txt -if [ "$?" == "0" ] -then - echo Meterfiles OK - rm /tmp/MyTapWater -fi - -rm -rf /tmp/testmeters -mkdir /tmp/testmeters -cat simulations/simulation_c1.txt | grep '^{' | grep 76348799 | tail -n 1 > test_expected.txt -$PROG --meterfiles=/tmp/testmeters --robot=json simulations/simulation_c1.txt MyTapWater multical21 76348799 "" -cat /tmp/testmeters/MyTapWater | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > test_response.txt -diff test_expected.txt test_response.txt -if [ "$?" == "0" ] -then - echo Meterfiles dir OK - rm -rf /tmp/testmeters -fi +tests/test_c1_meters.sh $PROG +tests/test_t1_meters.sh $PROG +tests/test_shell.sh $PROG +tests/test_meterfiles.sh $PROG diff --git a/tests/test_c1_meters.sh b/tests/test_c1_meters.sh new file mode 100755 index 0000000..1e8121f --- /dev/null +++ b/tests/test_c1_meters.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +PROG="$1" + +mkdir -p testoutput + +TEST=testoutput + +cat simulations/simulation_c1.txt | grep '^{' > $TEST/test_expected.txt +$PROG --robot=json simulations/simulation_c1.txt \ + MyHeater multical302 12345678 "" \ + MyTapWater multical21 76348799 "" \ + Vadden multical21 44556677 "" \ + MyElectricity omnipower 15947107 "" \ + > $TEST/test_output.txt +if [ "$?" == "0" ] +then + 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 + echo C1 OK + fi +else + Failure. +fi diff --git a/tests/test_meterfiles.sh b/tests/test_meterfiles.sh new file mode 100755 index 0000000..06ab2a5 --- /dev/null +++ b/tests/test_meterfiles.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +PROG="$1" + +mkdir -p testoutput + +TEST=testoutput + +rm -f /tmp/MyTapWater +cat simulations/simulation_c1.txt | grep '^{' | grep 76348799 | tail -n 1 > $TEST/test_expected.txt +$PROG --meterfiles --robot=json simulations/simulation_c1.txt MyTapWater multical21 76348799 "" +cat /tmp/MyTapWater | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_response.txt +diff $TEST/test_expected.txt $TEST/test_response.txt +if [ "$?" == "0" ] +then + echo Meterfiles OK + rm /tmp/MyTapWater +fi + +rm -rf /tmp/testmeters +mkdir /tmp/testmeters +cat simulations/simulation_c1.txt | grep '^{' | grep 76348799 | tail -n 1 > $TEST/test_expected.txt +$PROG --meterfiles=/tmp/testmeters --robot=json simulations/simulation_c1.txt MyTapWater multical21 76348799 "" +cat /tmp/testmeters/MyTapWater | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_response.txt +diff $TEST/test_expected.txt $TEST/test_response.txt +if [ "$?" == "0" ] +then + echo Meterfiles dir OK + rm -rf /tmp/testmeters +fi diff --git a/tests/test_shell.sh b/tests/test_shell.sh new file mode 100755 index 0000000..8d3c916 --- /dev/null +++ b/tests/test_shell.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +PROG="$1" + +mkdir -p testoutput + +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 | 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" ] + then + echo SHELL OK + fi +else + Failure. +fi diff --git a/tests/test_t1_meters.sh b/tests/test_t1_meters.sh new file mode 100755 index 0000000..dd58f9f --- /dev/null +++ b/tests/test_t1_meters.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +PROG="$1" + +mkdir -p testoutput + +TEST=testoutput + +cat simulations/simulation_t1.txt | grep '^{' > $TEST/test_expected.txt +$PROG --robot=json simulations/simulation_t1.txt \ + MyWarmWater supercom587 12345678 "" \ + MyColdWater supercom587 11111111 "" \ + MoreWater iperl 12345699 "" \ + > $TEST/test_output.txt +if [ "$?" == "0" ] +then + 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 + echo T1 OK + fi +else + Failure. +fi