Compare commits

...

2 Commits

Author SHA1 Message Date
7fab832321 mtr simulator 2023-11-19 17:54:43 +01:00
9e35cc55af Første veson av mtr leser 2023-11-19 17:54:27 +01:00
5 changed files with 129 additions and 8 deletions

View File

@ -26,8 +26,11 @@ 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') 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): def event_from_yaml_and_csv(config_path, mtr_path, csv_path):
try:
with open(mtr_path, 'r') as f: with open(mtr_path, 'r') as f:
card_dumps = load(f, Loader=Loader) card_dumps = load(f, Loader=Loader)
except FileNotFoundError:
card_dumps=[]
with open(config_path, 'r') as f: with open(config_path, 'r') as f:
event = load(f, Loader=Loader) event = load(f, Loader=Loader)
@ -35,7 +38,7 @@ def event_from_yaml_and_csv(config_path, mtr_path, csv_path):
with open(csv_path, 'r') as f: with open(csv_path, 'r') as f:
data = [i.split(';') for i in f.readlines()] data = [i.split(';') for i in f.readlines()]
for i in data: i[2] = i[2].split(',') 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.card_dumps = card_dumps
event.runners = runners event.runners = runners
return event return event

View File

@ -1,6 +1,8 @@
import argparse import argparse
import file_io import file_io
import iof_xml 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 # 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(): def main():
@ -12,12 +14,19 @@ def main():
parser_init.add_argument('--courses', required=True, dest='courses_file', action='store', default='./', parser_init.add_argument('--courses', required=True, dest='courses_file', action='store', default='./',
help='The xml with courses') help='The xml with courses')
parser_init.add_argument('--dir', required=False, dest='dir', action='store', default='./', 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') 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() args = parser.parse_args()
if args.command == 'init': match args.command:
case 'init':
init_dir(args.dir, args.entries_file, args.courses_file) 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): 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 # 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_config(event, config_path)
file_io.write_card_dumps(event, mtr_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__': if __name__ == '__main__':
main() main()

View File

@ -2,7 +2,6 @@ import copy
import datetime import datetime
import re import re
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
import pdf
class Runner: class Runner:
def __init__(self, id: int, first: str, last: str, club=None, club_id=None, 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) runner = self.get_runner(id)
if runner.status_override: return runner.status_override if runner.status_override: return runner.status_override
o_class = self.get_o_class(runner.o_class) o_class = self.get_o_class(runner.o_class)
if not o_class:
return 'Inactive'
course = self.get_course(o_class.course) course = self.get_course(o_class.course)
if self.get_card_dump(runner.card_id) == None: if self.get_card_dump(runner.card_id) == None:
return 'Active' return 'Active'

View File

@ -0,0 +1,2 @@
#!/bin/bash
socat -d -d pty,rawer,b9600 pty,rawer,b9600

View File

@ -0,0 +1,78 @@
import sys
sys.path.insert(0, '../../otime')
import argparse
import serial
import otime
from rich import inspect
def main():
global package_n
package_n = 0
argparser = argparse.ArgumentParser(
description=("Acts as a live mtr reading data line by line from a log file and sending them over serial"))
argparser.add_argument('port', help='Serial port identifier')
argparser.add_argument(
'-f', '--file',
help=(
"Mtr log file to use"))
argparser.add_argument(
'-v', '--verbose', action='store_true', help='Verbose output')
args = argparser.parse_args()
port = serial.Serial(port=args.port, baudrate=9600)
event = otime.Event(0, 'NoName')
event.read_mtr_file(args.file)
for card in event.card_dumps:
input('Press enter to send next card')
if args.verbose == True:
inspect(card)
data = card_to_bytes(card)
port.write(data)
def card_to_bytes(card):
global package_n
package_n += 1
msg = b'\xFF\xFF\xFF\xFF\xE6\x4D\x9C\x3E'
year = int(str(card.read_time.year)[2:4])
month = card.read_time.month
day = card.read_time.day
hour = card.read_time.hour
minute = card.read_time.minute
second = card.read_time.second
msg += year.to_bytes(1, byteorder='little')
msg += month.to_bytes(1, byteorder='little')
msg += day.to_bytes(1, byteorder='little')
msg += hour.to_bytes(1, byteorder='little')
msg += minute.to_bytes(1, byteorder='little')
msg += second.to_bytes(1, byteorder='little')
#ms
msg += b'\x00\x00'
msg += package_n.to_bytes(4, byteorder='little')
msg += card.card.to_bytes(3, byteorder='little')
# Gidder ikke å finne ut hva Producweek Producyear er
msg += b'\x00\x00'
# Hopper overogså over ECardHeadSum
msg += b'\x00'
for n in range(50):
try:
control = card.controls[n]
split = card.splits[n]
except:
control = 0
split = 0
msg += control.to_bytes(1, byteorder='little')
msg += split.to_bytes(2, byteorder='little')
# 56 byte string
msg += int(0).to_bytes(56, byteorder='little')
# Checksum
csum = sum(msg) % 256
msg += csum.to_bytes(1, byteorder='little')
msg += b'\x00'
return msg
if __name__ == '__main__':
main()