From 30dcd9cd23acdc1a4c776d2a6338541a3729db4f Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Thu, 30 Jun 2022 12:44:12 -0700 Subject: [PATCH] Make SERVER_HOST optional --- .gitignore | 3 ++- barcode_server.yaml | 42 ++------------------------------- barcode_server/__init__.py | 2 +- barcode_server/cli.py | 8 +++++-- barcode_server/config.py | 8 ++++--- barcode_server/server.py | 47 +++++++++++++++++++++++++++++++++++++ barcode_server/webserver.py | 1 - 7 files changed, 63 insertions(+), 48 deletions(-) create mode 100644 barcode_server/server.py diff --git a/.gitignore b/.gitignore index 6b75623..1e12e8e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea -venv \ No newline at end of file +venv +*.pyc \ No newline at end of file diff --git a/barcode_server.yaml b/barcode_server.yaml index 3e85012..d96f2d8 100644 --- a/barcode_server.yaml +++ b/barcode_server.yaml @@ -7,15 +7,6 @@ barcode_server: # if omitted, this will be a UUIDv4 id: cash-register-1 - # (optional) server configuration - server: - # (optional) the IP address to listen on for incoming connections - host: "127.0.0.1" - # (optional) the Port to listen on - port: 9654 - # (optional) API-Token which has to be provided by connecting clients - api_token: "EmUSqjXGfnQwn5wn6CpzJRZgoazMTRbMNgH7CXwkQG7Ph7stex" - # (optional) Time period to retry delivering failed queued events before giving up and dropping the event drop_event_queue_after: 2h # (optional) Time to wait between retries @@ -32,38 +23,9 @@ barcode_server: # 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 - # url: "http://dummy.restapiexample.com/api/v1/create" - # The request method to use - # method: POST - # Headers to set on each request - # headers: - # - "X-Auth-Token: MY_HEADERS" - - # (optional) MQTT push configuration - # mqtt: - # MQTT server host address - # host: "mqtt.mydomain.com" - # (optional) MQTT server port - # port: 1883 - # (optional) Client ID of this barcode-server instance to provide to the MQTT server - # client_id: "barcode-server" - # MQTT topic to push events to - # topic: "barcode-server/barcode" - # Username to use when connecting to the MQTT server - # user: "myuser" - # Password to use when connecting to the MQTT server - # password: "mypassword" - # (optional) QoS value of event messages - # qos: 2 - # (optional) Whether to instruct the MQTT server to remember event messages between restarts (of the MQTT server) - # retain: True - # A list of regex patterns to match USB device names against devices: - - ".*ode.*" + - ".*Bar Code Scanner.*" # A list of absolute file paths to devices device_paths: #- "/dev/input/barcode_scanner" @@ -71,4 +33,4 @@ barcode_server: # (optional) Statistics configuration stats: # (optional) port to provide statistics on - port: 8000 + port: 8009 diff --git a/barcode_server/__init__.py b/barcode_server/__init__.py index 3a5935a..ef6497d 100644 --- a/barcode_server/__init__.py +++ b/barcode_server/__init__.py @@ -1 +1 @@ -__version__ = "2.3.1" +__version__ = "2.3.2" diff --git a/barcode_server/cli.py b/barcode_server/cli.py index 8a7e6ee..9ae44ce 100644 --- a/barcode_server/cli.py +++ b/barcode_server/cli.py @@ -51,6 +51,7 @@ def c_run(): from barcode_server.barcode import BarcodeReader from barcode_server.config import AppConfig from barcode_server.webserver import WebServer + from barcode_server.server import Server signal.signal(signal.SIGINT, signal_handler) @@ -64,15 +65,18 @@ def c_run(): LOGGER.info(f"Instance ID: {config.INSTANCE_ID.value}") barcode_reader = BarcodeReader(config) - webserver = WebServer(config, barcode_reader) # start prometheus server if config.STATS_PORT.value is not None: LOGGER.info("Starting statistics webserver...") start_http_server(config.STATS_PORT.value) + if config.SERVER_HOST.value is None: + runner = Server(config, barcode_reader) + else: + runner = WebServer(config, barcode_reader) tasks = asyncio.gather( - webserver.start(), + runner.start(), ) loop.run_until_complete(tasks) diff --git a/barcode_server/config.py b/barcode_server/config.py index eed2770..55c154d 100644 --- a/barcode_server/config.py +++ b/barcode_server/config.py @@ -70,8 +70,8 @@ class AppConfig(ConfigBase): CONFIG_NODE_SERVER, "host" ], - default=DEFAULT_SERVER_HOST, - secret=True) + required=False + ) SERVER_PORT = IntConfigEntry( key_path=[ @@ -80,7 +80,8 @@ class AppConfig(ConfigBase): CONFIG_NODE_PORT ], range=Range(1, 65534), - default=DEFAULT_SERVER_PORT) + default=DEFAULT_SERVER_PORT + ) SERVER_API_TOKEN = StringConfigEntry( key_path=[ @@ -146,6 +147,7 @@ class AppConfig(ConfigBase): ], required=False ) + MQTT_PORT = IntConfigEntry( key_path=[ CONFIG_NODE_ROOT, diff --git a/barcode_server/server.py b/barcode_server/server.py new file mode 100644 index 0000000..ae68b32 --- /dev/null +++ b/barcode_server/server.py @@ -0,0 +1,47 @@ +import asyncio +import logging +from typing import Dict + +from barcode_server.barcode import BarcodeReader, BarcodeEvent +from barcode_server.config import AppConfig +from barcode_server.const import * +from barcode_server.notifier import BarcodeNotifier +from barcode_server.notifier.serial import SerialNotifier +from barcode_server.notifier.file import FileNotifier +from barcode_server.stats import REST_TIME_DEVICES, WEBSOCKET_CLIENT_COUNT +from barcode_server.util import input_device_to_dict + +LOGGER = logging.getLogger(__name__) + +class Server: + + def __init__(self, config: AppConfig, barcode_reader: BarcodeReader): + self.config = config + + self.barcode_reader = barcode_reader + self.barcode_reader.add_listener(self.on_barcode) + + self.notifiers: Dict[str, BarcodeNotifier] = {} + + if config.SERIAL_PATH_A.value is not None: + serial_notifier = SerialNotifier(config.SERIAL_PATH_A.value, config.SERIAL_PATH_B.value) + self.notifiers["serial"] = serial_notifier + + if config.FILE_PATH.value is not None: + file_notifier = FileNotifier(config.FILE_PATH.value) + self.notifiers["file"] = file_notifier + + async def start(self): + # start detecting and reading barcode scanners + await self.barcode_reader.start() + # start notifier queue processors + for key, notifier in self.notifiers.items(): + LOGGER.debug(f"Starting notifier: {key}") + await notifier.start() + + # wait forever + return await asyncio.Event().wait() + + async def on_barcode(self, event: BarcodeEvent): + for key, notifier in self.notifiers.items(): + await notifier.add_event(event) diff --git a/barcode_server/webserver.py b/barcode_server/webserver.py index 970a72a..be4610b 100644 --- a/barcode_server/webserver.py +++ b/barcode_server/webserver.py @@ -1,4 +1,3 @@ -import serial import asyncio import logging from typing import Dict