La til lesing av mtr statusmelding

This commit is contained in:
Trygve 2023-12-05 22:39:12 +01:00
parent 5e899fca62
commit 6f822c52eb

View File

@ -1,5 +1,6 @@
import argparse import argparse
import file_io import file_io
import datetime
import iof_xml import iof_xml
import serial import serial
import subprocess import subprocess
@ -12,6 +13,7 @@ from rich import print
import search_tui import search_tui
from rich.traceback import install from rich.traceback import install
from rich import inspect
install(show_locals=True) install(show_locals=True)
# 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
@ -40,7 +42,9 @@ def main():
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.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('mtr', help='run mtr commands') parser_init = subparsers.add_parser('mtr', help='run mtr commands')
parser_init.add_argument('--port', required=False, dest='port', action='store', help='specify a serial port') parser_init.add_argument('--port', required=True, dest='port', action='store', help='specify a serial port')
parser_init.add_argument('--spool', required=False, dest='mtr_spool', action='store_true', help='Spool all mtr data')
parser_init.add_argument('--status', required=False, dest='mtr_status', action='store_true', help='Spool all mtr data')
args = parser.parse_args() args = parser.parse_args()
@ -61,7 +65,12 @@ def main():
gen(args.dir, args.xml_path) gen(args.dir, args.xml_path)
case 'mtr': case 'mtr':
mtr = serial.Serial(port=args.port, baudrate=9600, timeout=40) mtr = serial.Serial(port=args.port, baudrate=9600, timeout=40)
if args.mtr_spool:
mtr.write(b'/SA') mtr.write(b'/SA')
if args.mtr_status:
mtr.write(b'/ST')
case other:
parser.print_help()
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
@ -96,20 +105,34 @@ def run(port='/dev/ttyUSB0', project_dir='./', xml_path='./output/'):
if size == b'\xe6': if size == b'\xe6':
event = file_io.event_from_yaml_and_csv(config_path, mtr_path, csv_path) event = file_io.event_from_yaml_and_csv(config_path, mtr_path, csv_path)
meat = mtr.read(229) meat = mtr.read(229)
full = b'\xFF\xFF\xFF\xFF' + size + meat message = b'\xFF\xFF\xFF\xFF' + size + meat
card_dump = otime.CardDump.from_mtr_bytes(full) card_dump = otime.CardDump.from_mtr_bytes(message)
event.card_dumps.append(card_dump) event.card_dumps.append(card_dump)
file_io.write_card_dumps(event, mtr_path) file_io.write_card_dumps(event, mtr_path)
print(runner_info(event, card_dump)) print(runner_info(event, card_dump))
subprocess.run(['git', 'add', './*'], cwd=project_dir, stdout=subprocess.DEVNULL) 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) subprocess.run(['git', 'commit', '-m', f'Added {card_dump.card}'], cwd=project_dir, stdout=subprocess.DEVNULL)
if not is_checksum_valid(message):
print('[red]Checksum is not valid![red]')
iof_xml.create_result_file(event, xml_path + '/results.xml') iof_xml.create_result_file(event, xml_path + '/results.xml')
pdf.create_result_list(event, project_dir + '/output/results.pdf') pdf.create_result_list(event, project_dir + '/output/results.pdf')
elif size == b'\x37': elif size == b'\x37':
meat = mtr.read(55) meat = mtr.read(54)
inspect(status) message = b'\xFF\xFF\xFF\xFF' + size + meat
mtr_id = int.from_bytes(message[6:8], 'little')
year = int.from_bytes(message[8:9], 'little')
month = int.from_bytes(message[9:10], 'little')
day = int.from_bytes(message[10:11], 'little')
hours =int.from_bytes(message[11:12], 'little')
minutes = int.from_bytes(message[12:13], 'little')
seconds = int.from_bytes(message[13:14], 'little')
milliseconds =int.from_bytes(message[14:16], 'little')
battery_status = int.from_bytes(message[16:17], 'little')
time = datetime.datetime(year, month, day, hours, minutes, seconds, milliseconds)
print(f'MTR status message: id: {mtr_id}, time: {time}, battery: {battery_status}')
def gen(project_dir='./', xml_path='./output/'): def gen(project_dir='./', xml_path='./output/'):
config_path = project_dir + '/config.yaml' config_path = project_dir + '/config.yaml'
@ -137,5 +160,12 @@ def runner_info(event, card_dump):
result_text = f'[blue]{result.status}, Time: [bold][yellow]{result.total_time}[/yellow][/bold][/blue]' 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]' return result_text + f' [blue]{result.fullname()}[/blue], {result.o_class}, {result.club}, [italic]Card: {result.card_id}[/italic]'
def is_checksum_valid(message):
# Hentet fra https://github.com/knutsiem/mtr-log-extractor
checksum = int.from_bytes(message[232:233], 'little')
# calculate checksum for message bytes up until checksum
calculated_checksum = sum(message[:232]) % 256
return checksum == calculated_checksum
if __name__ == '__main__': if __name__ == '__main__':
main() main()