Add support for multiple serial devices
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
import os
|
||||
import logging
|
||||
# import time as sleep
|
||||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
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
|
||||
from barcode_server.util import barcode_event_to_json
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -13,8 +16,45 @@ class FileNotifier(BarcodeNotifier):
|
||||
|
||||
def __init__(self, path: str):
|
||||
super().__init__()
|
||||
self.inputDevicePathA = None
|
||||
self.inputDevicePathB = None
|
||||
self.path = path
|
||||
pathObject = Path(path)
|
||||
if not pathObject.exists():
|
||||
pathObject.mkdir(exist_ok=True)
|
||||
|
||||
# def ticks(dt):
|
||||
# return (dt - datetime(1, 1, 1)).total_seconds() * 10000000
|
||||
|
||||
@time(FILE_NOTIFIER_TIME)
|
||||
async def _send_event(self, event: BarcodeEvent):
|
||||
LOGGER.debug(f"Notified {self.path}: {event.barcode}")
|
||||
now = datetime.now()
|
||||
date_time = now.strftime("%Y-%m-%d")
|
||||
weekNumberOfYear = now.strftime("%Y_Week_%U")
|
||||
if self.inputDevicePathA is None:
|
||||
self.inputDevicePathA = event.input_device.path
|
||||
elif self.inputDevicePathB is None:
|
||||
self.inputDevicePathB = event.input_device.path
|
||||
weekPath = f"{self.path}/{weekNumberOfYear}"
|
||||
dateTimePath = f"{weekPath}/{date_time}"
|
||||
if event.input_device.path == self.inputDevicePathA:
|
||||
fullPath = f"{dateTimePath}/A"
|
||||
elif event.input_device.path == self.inputDevicePathB:
|
||||
fullPath = f"{dateTimePath}/B"
|
||||
else:
|
||||
fullPath = f"{dateTimePath}/Z"
|
||||
json = barcode_event_to_json(self.config.INSTANCE_ID.value, event)
|
||||
ticks = int((datetime.utcnow() - datetime(1, 1, 1)).total_seconds() * 10000000)
|
||||
pathObject = Path(weekPath)
|
||||
if not pathObject.exists():
|
||||
pathObject.mkdir(exist_ok=True)
|
||||
pathObject = Path(dateTimePath)
|
||||
if not pathObject.exists():
|
||||
pathObject.mkdir(exist_ok=True)
|
||||
pathObject = Path(fullPath)
|
||||
if not pathObject.exists():
|
||||
pathObject.mkdir(exist_ok=True)
|
||||
f = open(f"{fullPath}/{str(ticks)}.json", 'wb')
|
||||
f.write(json)
|
||||
f.close()
|
||||
LOGGER.debug(f"Notified {fullPath}: {event.barcode}")
|
||||
|
@ -1,6 +1,5 @@
|
||||
import serial
|
||||
import logging
|
||||
# import time as sleep
|
||||
|
||||
from prometheus_async.aio import time
|
||||
|
||||
@ -12,12 +11,25 @@ LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class SerialNotifier(BarcodeNotifier):
|
||||
|
||||
def __init__(self, path: str, usb: serial):
|
||||
def __init__(self, pathA: str, pathB: str):
|
||||
super().__init__()
|
||||
self.path = path
|
||||
self.usb = usb
|
||||
self.pathA = pathA
|
||||
self.pathB = pathB
|
||||
self.inputDevicePathA = None
|
||||
self.inputDevicePathB = None
|
||||
self.usbA = serial.Serial(pathA, 9600, timeout=2)
|
||||
if pathB is not None:
|
||||
self.usbB = serial.Serial(pathB, 9600, timeout=2)
|
||||
|
||||
@time(SERIAL_NOTIFIER_TIME)
|
||||
async def _send_event(self, event: BarcodeEvent):
|
||||
self.usb.write(event.barcode.encode())
|
||||
LOGGER.debug(f"Notified {self.path}: {event.barcode}")
|
||||
if self.inputDevicePathA is None:
|
||||
self.inputDevicePathA = event.input_device.path
|
||||
elif self.inputDevicePathB is None:
|
||||
self.inputDevicePathB = event.input_device.path
|
||||
if event.input_device.path == self.inputDevicePathA:
|
||||
self.usbA.write(event.barcode.encode())
|
||||
LOGGER.debug(f"Notified {self.pathA}: {self.inputDevicePathA}: {event.barcode}")
|
||||
elif event.input_device.path == self.inputDevicePathB:
|
||||
self.usbB.write(event.barcode.encode())
|
||||
LOGGER.debug(f"Notified {self.pathB}: {self.inputDevicePathB}: {event.barcode}")
|
||||
|
Reference in New Issue
Block a user