La til lesing av mtr statusmelding
This commit is contained in:
parent
5e899fca62
commit
6f822c52eb
44
otime/cli.py
44
otime/cli.py
@ -1,5 +1,6 @@
|
||||
import argparse
|
||||
import file_io
|
||||
import datetime
|
||||
import iof_xml
|
||||
import serial
|
||||
import subprocess
|
||||
@ -12,6 +13,7 @@ from rich import print
|
||||
import search_tui
|
||||
|
||||
from rich.traceback import install
|
||||
from rich import inspect
|
||||
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
|
||||
@ -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 = 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()
|
||||
|
||||
@ -61,7 +65,12 @@ def main():
|
||||
gen(args.dir, args.xml_path)
|
||||
case 'mtr':
|
||||
mtr = serial.Serial(port=args.port, baudrate=9600, timeout=40)
|
||||
mtr.write(b'/SA')
|
||||
if args.mtr_spool:
|
||||
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):
|
||||
# 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':
|
||||
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)
|
||||
message = b'\xFF\xFF\xFF\xFF' + size + meat
|
||||
card_dump = otime.CardDump.from_mtr_bytes(message)
|
||||
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)
|
||||
|
||||
if not is_checksum_valid(message):
|
||||
print('[red]Checksum is not valid![red]')
|
||||
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)
|
||||
meat = mtr.read(54)
|
||||
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/'):
|
||||
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]'
|
||||
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__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user