From 6829d05453658a9f9672c847299c652b15126db1 Mon Sep 17 00:00:00 2001 From: xengineering Date: Fri, 30 Oct 2020 10:49:55 +0100 Subject: Introduce iot-barcode.target --- Makefile | 18 ++++++++---- README.md | 3 +- iot-barcode-device-handler.service | 12 -------- iot-barcode.service | 14 --------- iot_barcode_daemon | 46 ------------------------------ iot_barcode_transmitter | 46 ++++++++++++++++++++++++++++++ systemd/iot-barcode-device-handler.service | 12 ++++++++ systemd/iot-barcode-transmitter.service | 14 +++++++++ systemd/iot-barcode.target | 10 +++++++ 9 files changed, 96 insertions(+), 79 deletions(-) delete mode 100644 iot-barcode-device-handler.service delete mode 100644 iot-barcode.service delete mode 100755 iot_barcode_daemon create mode 100755 iot_barcode_transmitter create mode 100644 systemd/iot-barcode-device-handler.service create mode 100644 systemd/iot-barcode-transmitter.service create mode 100644 systemd/iot-barcode.target diff --git a/Makefile b/Makefile index 895cb38..7c9252f 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ install: # install executables mkdir -p $(bindir) $(INSTALL_PROGRAM) iot_barcode_device_handler $(bindir)/ - $(INSTALL_PROGRAM) iot_barcode_daemon $(bindir)/ + $(INSTALL_PROGRAM) iot_barcode_transmitter $(bindir)/ # install library / package mkdir -p $(libdir)/ @@ -35,8 +35,12 @@ install: $(INSTALL_DATA) config.json $(confdir)/config.json # install systemd unit files - $(INSTALL_DATA) iot-barcode.service $(systemddir)/ - $(INSTALL_DATA) iot-barcode-device-handler.service $(systemddir)/ + $(INSTALL_DATA) systemd/iot-barcode-transmitter.service $(systemddir)/ + systemctl enable iot-barcode-transmitter.service + $(INSTALL_DATA) systemd/iot-barcode-device-handler.service $(systemddir)/ + systemctl enable iot-barcode-device-handler.service + $(INSTALL_DATA) systemd/iot-barcode.target $(systemddir)/ + systemctl daemon-reload # install license mkdir -p $(sharedir)/licenses/ @@ -46,11 +50,15 @@ install: uninstall: rm -f $(bindir)/iot_barcode_device_handler - rm -f $(bindir)/iot_barcode_daemon + rm -f $(bindir)/iot_barcode_transmitter rm -f $(libdir)/__init__.py rm -f $(libdir)/config.py rm -f $(libdir)/mqtt.py rm -f $(libdir)/static.py - rm -f $(systemddir)/iot-barcode.service + systemctl disable iot-barcode-transmitter.service + rm -f $(systemddir)/iot-barcode-transmitter.service + systemctl disable iot-barcode-device-handler.service rm -f $(systemddir)/iot-barcode-device-handler.service + systemctl disable iot-barcode.target + rm -f $(systemddir)/iot-barcode.target rm -f $(sharedir)/licenses/$(PKGNAME) diff --git a/README.md b/README.md index 68b1235..12bea3b 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ A service that makes barcode scanners available on the network for IoT use. sudo useradd -Urs /usr/bin/nologin iotbarcode ``` 2. Configure ```/etc/iot_barcode/config.json``` -3. Run ```sudo systemctl enable --now iot-barcode-device-handler``` -4. Run ```sudo systemctl enable --now iot-barcode``` +3. Run ```sudo systemctl enable --now iot-barcode.target``` 5. Listen to MQTT output (e.g. with ```mosquitto_sub -h localhost -t "xengineering.eu/iot-barcode"```) diff --git a/iot-barcode-device-handler.service b/iot-barcode-device-handler.service deleted file mode 100644 index 8933cfd..0000000 --- a/iot-barcode-device-handler.service +++ /dev/null @@ -1,12 +0,0 @@ - -[Unit] -Description=A Daemon to read Barcodes from a specified Barcode Reader - -[Service] -Type=simple -ExecStart=/usr/bin/iot_barcode_device_handler -User=root -Group=root - -[Install] -WantedBy=default.target diff --git a/iot-barcode.service b/iot-barcode.service deleted file mode 100644 index fbd9940..0000000 --- a/iot-barcode.service +++ /dev/null @@ -1,14 +0,0 @@ - -[Unit] -Description=A Daemon to transmit Barcodes to an MQTT Broker -Requires=iot-barcode-device-handler.service -After=iot-barcode-device-handler.service - -[Service] -Type=simple -ExecStart=/usr/bin/iot_barcode_daemon -User=iotbarcode -Group=iotbarcode - -[Install] -WantedBy=default.target diff --git a/iot_barcode_daemon b/iot_barcode_daemon deleted file mode 100755 index 5b08111..0000000 --- a/iot_barcode_daemon +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/python3 - - -"""Executable to transmit the Barcode Data to MQTT without root Priviledges""" - - -from iot_barcode.static import KEYMAP -import iot_barcode.config as config -from iot_barcode.mqtt import MqttService - - -def main(): - try: - - # read config - cfg = config.get_config() - fifo_path = cfg["scanner"]["fifo_path"] - topic = cfg["mqtt"]["topic"] - - # setup mqtt - mqtt_service = MqttService(cfg) - mqtt_service.run() - - barcode = "" - - while True: - with open(fifo_path, "r") as fifo: - keycode = fifo.read() - character = KEYMAP[keycode] - if character is "\n": - mqtt_service.client.publish( - topic, - payload=barcode, - qos=0, - retain=False - ) - barcode = "" - else: - barcode += character - - except KeyboardInterrupt: - pass - - -if __name__ == "__main__": - main() diff --git a/iot_barcode_transmitter b/iot_barcode_transmitter new file mode 100755 index 0000000..5b08111 --- /dev/null +++ b/iot_barcode_transmitter @@ -0,0 +1,46 @@ +#!/usr/bin/python3 + + +"""Executable to transmit the Barcode Data to MQTT without root Priviledges""" + + +from iot_barcode.static import KEYMAP +import iot_barcode.config as config +from iot_barcode.mqtt import MqttService + + +def main(): + try: + + # read config + cfg = config.get_config() + fifo_path = cfg["scanner"]["fifo_path"] + topic = cfg["mqtt"]["topic"] + + # setup mqtt + mqtt_service = MqttService(cfg) + mqtt_service.run() + + barcode = "" + + while True: + with open(fifo_path, "r") as fifo: + keycode = fifo.read() + character = KEYMAP[keycode] + if character is "\n": + mqtt_service.client.publish( + topic, + payload=barcode, + qos=0, + retain=False + ) + barcode = "" + else: + barcode += character + + except KeyboardInterrupt: + pass + + +if __name__ == "__main__": + main() diff --git a/systemd/iot-barcode-device-handler.service b/systemd/iot-barcode-device-handler.service new file mode 100644 index 0000000..caae8c0 --- /dev/null +++ b/systemd/iot-barcode-device-handler.service @@ -0,0 +1,12 @@ + +[Unit] +Description=A Daemon to read Barcodes from a specified Barcode Reader Device + +[Service] +Type=simple +ExecStart=/usr/bin/iot_barcode_device_handler +User=root +Group=root + +[Install] +WantedBy=iot-barcode.target diff --git a/systemd/iot-barcode-transmitter.service b/systemd/iot-barcode-transmitter.service new file mode 100644 index 0000000..8ed31fb --- /dev/null +++ b/systemd/iot-barcode-transmitter.service @@ -0,0 +1,14 @@ + +[Unit] +Description=A Daemon to transmit Barcodes to an MQTT Broker +Requires=iot-barcode-device-handler.service +After=iot-barcode-device-handler.service + +[Service] +Type=simple +ExecStart=/usr/bin/iot_barcode_transmitter +User=iotbarcode +Group=iotbarcode + +[Install] +WantedBy=iot-barcode.target diff --git a/systemd/iot-barcode.target b/systemd/iot-barcode.target new file mode 100644 index 0000000..4d5cd7e --- /dev/null +++ b/systemd/iot-barcode.target @@ -0,0 +1,10 @@ + +[Unit] +Description=Start this Target to start all IoT Barcode Services +Requires=multi-user.target +After=multi-user.target +AllowIsolate=yes + +[Install] +WantedBy=multi-user.target + -- cgit v1.2.3-70-g09d2