Make SERVER_HOST optional

This commit is contained in:
Mike Phares 2022-06-30 12:44:12 -07:00
parent 87e337a69d
commit 30dcd9cd23
7 changed files with 63 additions and 48 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
venv
venv
*.pyc

View File

@ -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

View File

@ -1 +1 @@
__version__ = "2.3.1"
__version__ = "2.3.2"

View File

@ -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)

View File

@ -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,

47
barcode_server/server.py Normal file
View File

@ -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)

View File

@ -1,4 +1,3 @@
import serial
import asyncio
import logging
from typing import Dict