La til mtr lytter. Den kjører ikke i paralell med db lytteren enda.

This commit is contained in:
Trygve 2022-04-10 20:26:12 +02:00
parent d734a00774
commit 8e5c5351ba

View File

@ -1,18 +1,21 @@
#!/usr/bin/env python
import otime
#import mtr_log_extractor as mle
import json
import datetime
import serial
import binascii
import pickle
import asyncio
import argparse
from difflib import Differ
from watchgod import awatch
from watchfiles import awatch
from config import config
from rich import print
from rich import inspect
from rich.console import Console
from rich.columns import Columns
from rich.table import Table
from rich.panel import Panel
from rich.console import Group
def save(obj):
f = open(config['otime_file'], "wb")
@ -35,10 +38,8 @@ def start_parse():
def start_event():
global event
event = otime.event(0, config['event_name'])
event.import_ttime_cnf('/home/trygve/Documents/sprintcup040330/sprintcup040330.cnf')
event.import_ttime_cnf(config['cnf_file'])
event.import_ttime_db(config['db_file'])
event.import_mtr_file('/home/trygve/Documents/sprintcup040330/sprintcup040330.log')
event.match_runners_cards()
save(event)
global db_file
global db_file_u
@ -52,8 +53,47 @@ def load_event(ot_file):
global db_file_u
db_file = open(config['db_file'], 'r', encoding='latin_1').read().splitlines()
def assign_card_r_to_runner(runners, card_r):
for n in runners:
if n.card == card_r.card:
n.card_r = card_r
print(runner_info(n))
def runner_info(runner):
time = str(datetime.timedelta(seconds=runner.totaltime()))
if runner.status() == 'Disqualified':
dsqp = Panel.fit(f'Løype: {runner.o_class.course.codes}\nRegistrert: {runner.card_r.controls}', border_style='red')
cont = Group(f'Status: {runner.status()}',dsqp)
panel = Panel.fit(cont, title=runner.fullname(), border_style='red')
elif runner.status() == 'OK':
cont = f'Status: {runner.status()}\nTid: {time}'
panel = Panel.fit(cont, title=runner.fullname(), border_style='green')
elif runner.status() == 'Active':
cont = ''
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('😵')
global event
global db_file
global db_file_u
@ -73,14 +113,14 @@ async def main():
clean = str(line).replace('+ ', '').split(';')
runner = otime.runner.from_string(clean, event.o_classes)
inspect(runner)
if runner.firstname:
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.firstname:
if runner.first != None:
removed_raw.append(runner)
for plus in added_raw:
@ -113,7 +153,6 @@ async def main():
event.match_runners_cards()
event.get_xml_res().write(config['xml_res_file'])
db_file_u = None
if __name__ == "__main__":
start_parse()