Lagde enkel loop som sjekker db og mtr.

This commit is contained in:
Trygve 2022-04-11 19:23:31 +02:00
parent 8e5c5351ba
commit 643e3623e0

View File

@ -1,14 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
import otime import otime
from config import config
import datetime import datetime
import os
from time import sleep
import serial import serial
import binascii import binascii
import pickle import pickle
import asyncio
import argparse import argparse
from difflib import Differ from difflib import Differ
from watchfiles import awatch
from config import config
from rich import print from rich import print
from rich import inspect from rich import inspect
from rich.console import Console from rich.console import Console
@ -58,6 +58,12 @@ def assign_card_r_to_runner(runners, card_r):
if n.card == card_r.card: if n.card == card_r.card:
n.card_r = card_r n.card_r = card_r
print(runner_info(n)) 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): def runner_info(runner):
time = str(datetime.timedelta(seconds=runner.totaltime())) time = str(datetime.timedelta(seconds=runner.totaltime()))
@ -73,87 +79,98 @@ def runner_info(runner):
panel = Panel.fit(cont, title=runner.fullname()) panel = Panel.fit(cont, title=runner.fullname())
return panel return panel
async def main(): def read_db_changes():
async def mtr_listener(): global event
mtr = serial.Serial(port=config['port'], baudrate=9600, timeout=40) global db_file
while True: global db_file_u
mtr.read_until(expected=b'\xFF\xFF\xFF\xFF') print('DB file changed')
size = mtr.read(size=1) db_file_u = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
if size == b'\xe6': d = Differ()
meat = mtr.read(229) result = list(d.compare(db_file, db_file_u))
full = b'\xFF\xFF\xFF\xFF' + size + meat db_file = db_file_u
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']): added_raw = []
print('😵') removed_raw = []
global event added = []
global db_file removed = []
global db_file_u changed = []
db_file_u = open(config['db_file'], 'r', encoding='latin_1').read().splitlines() for line in result:
d = Differ() if line[:1] == '+':
result = list(d.compare(db_file, db_file_u)) clean = str(line).replace('+ ', '').split(';')
db_file = db_file_u runner = otime.runner.from_string(clean, event.o_classes)
if runner.first != None:
added_raw.append(runner)
added_raw = [] elif line[:1] == '-':
removed_raw = [] clean = str(line).replace('- ', '').split(';')
added = [] runner = otime.runner.from_string(clean, event.o_classes)
removed = [] if runner.first != None:
changed = [] removed_raw.append(runner)
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] == '-': for plus in added_raw:
#print(line) for index, minus in enumerate(removed_raw):
clean = str(line).replace('- ', '').split(';') if plus.id == minus.id:
runner = otime.runner.from_string(clean, event.o_classes) changed.append(plus)
if runner.first != None: removed_raw[index] = plus
removed_raw.append(runner) break
for plus in added_raw: added = [x for x in added_raw if x not in changed]
for index, minus in enumerate(removed_raw): removed = [x for x in removed_raw if x not in changed]
if plus.id == minus.id: #print(len(added_raw),len(removed_raw))
changed.append(plus) #print(len(added),len(removed),len(changed))
removed_raw[index] = plus
break
added = [x for x in added_raw if x not in changed] event.runners.extend(added)
removed = [x for x in removed_raw if x not in changed] for n in added:
#print(len(added_raw),len(removed_raw)) print(f'[green]Added: {n.fullname()}, {n.o_class.name}, {n.card}[green]')
#print(len(added),len(removed),len(changed)) if n.status != 'Active':
print(runner_info(n))
event.runners.extend(added) for r in removed:
for n in added: for n in event.runners:
print(f'[green]Added: {n.fullname()}, {n.o_class.name}, {n.card}[green]') if n.id == r.id:
print(f'[red]Removed: {n.fullname()}, {n.o_class.name}, {n.card}')
event.runners.remove(n)
for r in removed: for change in changed:
for n in event.runners: for i, n in enumerate(event.runners):
if n.id == r.id: if n.id == runner.id:
print(f'[red]Removed: {n.fullname()}, {n.o_class.name}, {n.card}') event.runners[i] = runner
event.runners.remove(n) print(f'[blue]Changed: {runner.fullname()}, {runner.o_class.name}, {runner.card}')
if n.status != 'Active':
for change in changed: print(runner_info(n))
for i, n in enumerate(event.runners): event.match_runners_cards()
if n.id == runner.id: event.get_xml_res().write(config['xml_res_file'])
event.runners[i] = runner def read_mtr(mtr):
print(f'[blue]Changed: {runner.fullname()}, {runner.o_class.name}, {runner.card}') global event
event.match_runners_cards() 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']) 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 db_file_u = None
if __name__ == "__main__": if __name__ == "__main__":
start_parse() start_parse()
asyncio.run(main()) main()