Make SERVER_HOST optional
This commit is contained in:
@ -1 +1 @@
|
||||
__version__ = "2.3.1"
|
||||
__version__ = "2.3.2"
|
||||
|
@ -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)
|
||||
|
@ -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
47
barcode_server/server.py
Normal 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)
|
@ -1,4 +1,3 @@
|
||||
import serial
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Dict
|
||||
|
Reference in New Issue
Block a user