forked from Trygve/otime
Lagde enkel loop som sjekker db og mtr.
This commit is contained in:
parent
8e5c5351ba
commit
643e3623e0
73
event_mgr.py
73
event_mgr.py
@ -1,14 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
import otime
|
||||
from config import config
|
||||
import datetime
|
||||
import os
|
||||
from time import sleep
|
||||
import serial
|
||||
import binascii
|
||||
import pickle
|
||||
import asyncio
|
||||
import argparse
|
||||
from difflib import Differ
|
||||
from watchfiles import awatch
|
||||
from config import config
|
||||
from rich import print
|
||||
from rich import inspect
|
||||
from rich.console import Console
|
||||
@ -58,6 +58,12 @@ def assign_card_r_to_runner(runners, card_r):
|
||||
if n.card == card_r.card:
|
||||
n.card_r = card_r
|
||||
print(runner_info(n))
|
||||
break
|
||||
else:
|
||||
cont = f'Brikke ikke registrert på noen løper\nId: {card_r.card}\nPoster: {card_r.controls}'
|
||||
panel = Panel.fit(cont, title=str(card_r.card), border_style='white')
|
||||
print(panel)
|
||||
|
||||
|
||||
def runner_info(runner):
|
||||
time = str(datetime.timedelta(seconds=runner.totaltime()))
|
||||
@ -73,30 +79,11 @@ def runner_info(runner):
|
||||
panel = Panel.fit(cont, title=runner.fullname())
|
||||
return panel
|
||||
|
||||
async def main():
|
||||
async def mtr_listener():
|
||||
mtr = serial.Serial(port=config['port'], baudrate=9600, timeout=40)
|
||||
while True:
|
||||
mtr.read_until(expected=b'\xFF\xFF\xFF\xFF')
|
||||
size = mtr.read(size=1)
|
||||
if size == b'\xe6':
|
||||
meat = mtr.read(229)
|
||||
full = b'\xFF\xFF\xFF\xFF' + size + meat
|
||||
card_r = otime.card_punch.from_mtr_bytes(full)
|
||||
event.card_punches.append(card_r)
|
||||
assign_card_r_to_runner(event.runners, card_r)
|
||||
elif size == b'\x37':
|
||||
meat = mtr.read(55)
|
||||
status = mtrreader.MtrStatusMessage(meat)
|
||||
inspect(status)
|
||||
print(status.year(),status.month(),status.day(),status.hours(),status.minutes())
|
||||
asyncio.create_task(mtr_listener())
|
||||
|
||||
async for changes in awatch(config['db_file']):
|
||||
print('😵')
|
||||
def read_db_changes():
|
||||
global event
|
||||
global db_file
|
||||
global db_file_u
|
||||
print('DB file changed')
|
||||
db_file_u = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
|
||||
d = Differ()
|
||||
result = list(d.compare(db_file, db_file_u))
|
||||
@ -109,15 +96,12 @@ async def main():
|
||||
changed = []
|
||||
for line in result:
|
||||
if line[:1] == '+':
|
||||
#print(line)
|
||||
clean = str(line).replace('+ ', '').split(';')
|
||||
runner = otime.runner.from_string(clean, event.o_classes)
|
||||
inspect(runner)
|
||||
if runner.first != None:
|
||||
added_raw.append(runner)
|
||||
|
||||
elif line[:1] == '-':
|
||||
#print(line)
|
||||
clean = str(line).replace('- ', '').split(';')
|
||||
runner = otime.runner.from_string(clean, event.o_classes)
|
||||
if runner.first != None:
|
||||
@ -138,6 +122,8 @@ async def main():
|
||||
event.runners.extend(added)
|
||||
for n in added:
|
||||
print(f'[green]Added: {n.fullname()}, {n.o_class.name}, {n.card}[green]')
|
||||
if n.status != 'Active':
|
||||
print(runner_info(n))
|
||||
|
||||
for r in removed:
|
||||
for n in event.runners:
|
||||
@ -150,10 +136,41 @@ async def main():
|
||||
if n.id == runner.id:
|
||||
event.runners[i] = runner
|
||||
print(f'[blue]Changed: {runner.fullname()}, {runner.o_class.name}, {runner.card}')
|
||||
if n.status != 'Active':
|
||||
print(runner_info(n))
|
||||
event.match_runners_cards()
|
||||
event.get_xml_res().write(config['xml_res_file'])
|
||||
def read_mtr(mtr):
|
||||
global event
|
||||
if mtr.in_waiting > 0:
|
||||
mtr.read_until(expected=b'\xFF\xFF\xFF\xFF')
|
||||
size = mtr.read(size=1)
|
||||
if size == b'\xe6':
|
||||
meat = mtr.read(229)
|
||||
full = b'\xFF\xFF\xFF\xFF' + size + meat
|
||||
card_r = otime.card_punch.from_mtr_bytes(full)
|
||||
event.card_punches.append(card_r)
|
||||
assign_card_r_to_runner(event.runners, card_r)
|
||||
elif size == b'\x37':
|
||||
meat = mtr.read(55)
|
||||
status = mtrreader.MtrStatusMessage(meat)
|
||||
inspect(status)
|
||||
print(status.year(),status.month(),status.day(),status.hours(),status.minutes())
|
||||
event.get_xml_res().write(config['xml_res_file'])
|
||||
def main():
|
||||
global event
|
||||
global db_file
|
||||
global db_file_u
|
||||
mtr = serial.Serial(port=config['port'], baudrate=9600, timeout=40)
|
||||
db_file_date = os.path.getctime(config['db_file'])
|
||||
while True:
|
||||
if db_file_date != os.path.getctime(config['db_file']):
|
||||
db_file_date = os.path.getctime(config['db_file'])
|
||||
read_db_changes()
|
||||
read_mtr(mtr)
|
||||
sleep(0.1)
|
||||
|
||||
db_file_u = None
|
||||
if __name__ == "__main__":
|
||||
start_parse()
|
||||
asyncio.run(main())
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user