import argparse import file_io import iof_xml import serial import subprocess import os import otime import iof_xml import pdf from pdf import format_m_s from rich import print import search_tui # 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(): parser = argparse.ArgumentParser(description='Otime very alpha version 😁') subparsers = parser.add_subparsers(dest='command') parser_init = subparsers.add_parser('init', help='Create project files') parser_init.add_argument('--entries', required=True, dest='entries_file', action='store', default='./', help='The xml file with entries') 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') parser_init.add_argument('--xml', required=False, dest='xml_path', action='store', default=None, help='Where the xml result file should be saved') parser_init = subparsers.add_parser('viewer', help='View otime data') parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./', help='specify a directory') parser_init.add_argument('--xml', required=False, dest='xml_path', action='store', default=None, help='Where the xml result file should be saved') args = parser.parse_args() if not args.xml_path: args.xml_path = args.dir + '/output' match args.command: case 'init': init_dir(args.dir, args.entries_file, args.courses_file) case 'run': run(args.port, args.dir, args.xml_path) case 'viewer': search_tui.main(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 csv_db_path = project_dir + '/runners.csv' config_path = project_dir + '/config.yaml' mtr_path = project_dir + 'mtr.yaml' event = iof_xml.event_from_xml_entries(entries_xml_file) event.courses = iof_xml.courses_from_xml(courses_xml_file) print(f'Read {len(event.runners)} runners, {len(event.card_dumps)} ecards, {len(event.courses)} courses and {len(event.o_classes)} classes') print('Remember to manually link the courses and classes in the config file') file_io.write_runners_csv(event, csv_db_path) file_io.write_config(event, config_path) file_io.write_card_dumps(event, mtr_path) os.makedirs(project_dir+'/output') subprocess.run(['git', 'init', project_dir]) subprocess.run(['git', 'add', './*'], cwd=project_dir) subprocess.run(['git', 'commit', '-m', f'Project initiated by otime'], cwd=project_dir) def run(port='/dev/ttyUSB0', project_dir='./', xml_path='./output/'): mtr = serial.Serial(port=port, baudrate=9600, timeout=40) config_path = project_dir + '/config.yaml' mtr_path = project_dir + '/mtr.yaml' csv_path = project_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)) subprocess.run(['git', 'add', './*'], cwd=project_dir, stdout=subprocess.DEVNULL) subprocess.run(['git', 'commit', '-m', f'Added {card_dump.card}'], cwd=project_dir, stdout=subprocess.DEVNULL) iof_xml.create_result_file(event, xml_path + '/results.xml') pdf.create_result_list(event, project_dir + '/output/results.pdf') 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 is None: return card_dump result = event.get_runner_result(runner.id) if result is None: return '🧐 Dette skal ikke skje...' if result.status == 'OK': result_text = f'[green]Place: {result.place}. Time: [bold][yellow]{format_m_s(result.total_time)}[/yellow][/bold][/green]' elif result.status == 'MissingPunch': result_text = f'[red]Missed Control! Time: [bold][yellow]{format_m_s(result.total_time)}[/yellow][/bold][/red]' else: result_text = f'[blue]{result.status}, Time: [bold][yellow]{result.total_time}[/yellow][/bold][/blue]' return result_text + f' [blue]{result.fullname()}[/blue], {result.o_class}, {result.club}, [italic]Card: {result.card_id}[/italic]' if __name__ == '__main__': main()