From f6c206e0b1404361ff18e2d9694696b26b5cd1c6 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Mon, 27 Jun 2022 11:58:39 -0700 Subject: [PATCH] Add file --- barcode_server.yaml | 7 ++++++- barcode_server/config.py | 9 +++++++++ barcode_server/const.py | 1 + barcode_server/notifier/file.py | 20 ++++++++++++++++++++ barcode_server/webserver.py | 4 ++++ 5 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 barcode_server/notifier/file.py diff --git a/barcode_server.yaml b/barcode_server.yaml index 064d1fc..ea6c59c 100644 --- a/barcode_server.yaml +++ b/barcode_server.yaml @@ -21,11 +21,16 @@ barcode_server: # (optional) Time to wait between retries retry_interval: 2s - # (optional) HTTP push configuration + # (optional) Serial push configuration serial: # URL to send events to using a request path: "/dev/ttyUSB0" + # (optional) file push configuration + file: + # URL to send events to using a request + path: "/var/www/barcode-server" + # (optional) HTTP push configuration # http: # URL to send events to using a request diff --git a/barcode_server/config.py b/barcode_server/config.py index 096267e..7afbbac 100644 --- a/barcode_server/config.py +++ b/barcode_server/config.py @@ -222,6 +222,15 @@ class AppConfig(ConfigBase): required=False ) + FILE_PATH = StringConfigEntry( + key_path=[ + CONFIG_NODE_ROOT, + CONFIG_NODE_FILE, + "path" + ], + required=False + ) + DEVICE_PATTERNS = ListConfigEntry( item_type=RegexConfigEntry, item_args={ diff --git a/barcode_server/const.py b/barcode_server/const.py index f0cc157..de1e0a1 100644 --- a/barcode_server/const.py +++ b/barcode_server/const.py @@ -23,6 +23,7 @@ CONFIG_NODE_SERVER = "server" CONFIG_NODE_HTTP = "http" CONFIG_NODE_MQTT = "mqtt" CONFIG_NODE_SERIAL = "serial" +CONFIG_NODE_FILE = "file" CONFIG_NODE_STATS = "stats" CONFIG_NODE_PORT = "port" diff --git a/barcode_server/notifier/file.py b/barcode_server/notifier/file.py new file mode 100644 index 0000000..b23a058 --- /dev/null +++ b/barcode_server/notifier/file.py @@ -0,0 +1,20 @@ +import logging +# import time as sleep + +from prometheus_async.aio import time + +from barcode_server.barcode import BarcodeEvent +from barcode_server.notifier import BarcodeNotifier +from barcode_server.stats import FILE_NOTIFIER_TIME + +LOGGER = logging.getLogger(__name__) + +class FileNotifier(BarcodeNotifier): + + def __init__(self, path: str): + super().__init__() + self.path = path + + @time(FILE_NOTIFIER_TIME) + async def _send_event(self, event: BarcodeEvent): + LOGGER.debug(f"Notified {self.path}: {event.barcode}") diff --git a/barcode_server/webserver.py b/barcode_server/webserver.py index e3757f4..fe80ad0 100644 --- a/barcode_server/webserver.py +++ b/barcode_server/webserver.py @@ -63,6 +63,10 @@ class Webserver: serial_notifier = SerialNotifier(config.SERIAL_PATH.value, self.usb) self.notifiers["serial"] = serial_notifier + if config.FILE_PATH.value is not None: + file_notifier = SerialNotifier(config.FILE_PATH.value) + self.notifiers["file"] = file_notifier + async def start(self): # start detecting and reading barcode scanners await self.barcode_reader.start()