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(): parser = argparse.ArgumentParser(description='Otime very alpha version 😁') subparsers = parser.add_subparsers(dest='command') parser_init = subparsers.add_parser('init', help='INIT') 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') args = parser.parse_args() 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 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) file_io.write_runners_csv(event, csv_db_path) 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()