From 9e35cc55af0930e57f7f18fe99c1a659dac469b2 Mon Sep 17 00:00:00 2001 From: Trygve Date: Sun, 19 Nov 2023 17:54:27 +0100 Subject: [PATCH] =?UTF-8?q?F=C3=B8rste=20veson=20av=20mtr=20leser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otime/file_io.py | 11 +++++++---- otime/main.py | 43 ++++++++++++++++++++++++++++++++++++++++--- otime/otime.py | 3 ++- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/otime/file_io.py b/otime/file_io.py index 0607a13..b61ce24 100644 --- a/otime/file_io.py +++ b/otime/file_io.py @@ -26,16 +26,19 @@ def write_runners_csv(event, file_path): f.write(f'{i.id};{i.status_override};{i.first}, {i.last};{i.o_class};{i.club};{i.card_id};{i.fork};{i.start_time}\n') def event_from_yaml_and_csv(config_path, mtr_path, csv_path): - with open(mtr_path, 'r') as f: - card_dumps = load(f, Loader=Loader) - + try: + with open(mtr_path, 'r') as f: + card_dumps = load(f, Loader=Loader) + except FileNotFoundError: + card_dumps=[] + with open(config_path, 'r') as f: event = load(f, Loader=Loader) with open(csv_path, 'r') as f: data = [i.split(';') for i in f.readlines()] for i in data: i[2] = i[2].split(',') - runners = [otime.Runner(id=i[0], status_override=i[1], first=i[2][0], last=i[2][1].strip(), o_class=i[3], club=i[4], card_id=i[5], fork=i[6], start_time=i[7]) for i in data] + runners = [otime.Runner(id=i[0], status_override=i[1], first=i[2][0], last=i[2][1].strip(), o_class=i[3], club=i[4], card_id=int(i[5]), fork=i[6], start_time=i[7]) for i in data] event.card_dumps = card_dumps event.runners = runners return event \ No newline at end of file diff --git a/otime/main.py b/otime/main.py index 2785b8c..d63ab37 100644 --- a/otime/main.py +++ b/otime/main.py @@ -1,6 +1,8 @@ import argparse import file_io import iof_xml +import serial +import otime # Main entrypoint for now. Cli with two options; init will make the files needed and run will start the program from the specified directory def main(): @@ -12,12 +14,19 @@ def main(): parser_init.add_argument('--courses', required=True, dest='courses_file', action='store', default='./', help='The xml with courses') parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./', - help='Specify a directort, if not set the files are created in the current directory') + parser_init = subparsers.add_parser('run', help='run otime') + parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./', help='specify a directory') + parser_init.add_argument('--port', required=False, dest='port', action='store', help='specify a serial port') + + args = parser.parse_args() - if args.command == 'init': - init_dir(args.dir, args.entries_file, args.courses_file) + match args.command: + case 'init': + init_dir(args.dir, args.entries_file, args.courses_file) + case 'run': + run(args.port, args.dir) def init_dir(project_dir, entries_xml_file, courses_xml_file): # Lager mappe med en config fil, en csv fil med løpere og en fil med mtr data @@ -32,5 +41,33 @@ def init_dir(project_dir, entries_xml_file, courses_xml_file): file_io.write_config(event, config_path) file_io.write_card_dumps(event, mtr_path) +def run(port='/dev/ttyUSB0', dir='./'): + mtr = serial.Serial(port=port, baudrate=9600, timeout=40) + config_path = dir + '/config.yaml' + mtr_path = dir + '/mtr.yaml' + csv_path = dir + '/runners.csv' + while True: + if mtr.in_waiting > 0: + mtr.read_until(expected=b'\xFF\xFF\xFF\xFF') + size = mtr.read(size=1) + if size == b'\xe6': + event = file_io.event_from_yaml_and_csv(config_path, mtr_path, csv_path) + meat = mtr.read(229) + full = b'\xFF\xFF\xFF\xFF' + size + meat + card_dump = otime.CardDump.from_mtr_bytes(full) + event.card_dumps.append(card_dump) + file_io.write_card_dumps(event, mtr_path) + print(runner_info(event, card_dump)) + elif size == b'\x37': + meat = mtr.read(55) + inspect(status) + +def runner_info(event, card_dump): + runner = next((i for i in event.runners if str(i.card_id) == str(card_dump.card)), None) + if runner: + return f'{runner.fullname()}, {runner.o_class}, {runner.club}, {event.get_runner_status(runner.id)}, {event.get_runner_time(runner.id)}' + else: + return card_dump + if __name__ == '__main__': main() \ No newline at end of file diff --git a/otime/otime.py b/otime/otime.py index dc6c2b0..d7fd7ee 100644 --- a/otime/otime.py +++ b/otime/otime.py @@ -2,7 +2,6 @@ import copy import datetime import re import xml.etree.ElementTree as etree -import pdf class Runner: def __init__(self, id: int, first: str, last: str, club=None, club_id=None, @@ -266,6 +265,8 @@ class Event: runner = self.get_runner(id) if runner.status_override: return runner.status_override o_class = self.get_o_class(runner.o_class) + if not o_class: + return 'Inactive' course = self.get_course(o_class.course) if self.get_card_dump(runner.card_id) == None: return 'Active'