VAG-ZZA-Realtime/vag_zza.py

68 lines
1.8 KiB
Python

import serial
import time
from difflib import SequenceMatcher
from vag_zza_map import *
class VagZza:
def __init__(self, port):
self.port = serial.Serial(port, baudrate=9600)
def home(self):
self.port.write(b"HOME\n")
time.sleep(0.05)
def update(self):
self.port.write(b"START\n")
time.sleep(0.05)
def set_unit(self, pos, code):
self.port.write(f"UNIT:{pos}:{code}\n".encode('ascii'))
time.sleep(0.05)
def set_line(self, line):
code = None
for c, text in MAP_LINE.items():
if text.lower() == line.lower():
code = c
break
if code is None:
raise ValueError(f"Line text not found: {line}")
self.set_unit(0, code)
self.set_unit(3, code)
def set_via(self, via):
code = None
for c, text in MAP_VIA.items():
if text.lower() == via.lower():
code = c
break
if code is None:
raise ValueError(f"Via text not found: {via}")
self.set_unit(1, code)
self.set_unit(4, code)
def set_destination(self, dest):
code = None
for c, text in MAP_DESTINATION.items():
if text.lower() == dest.lower():
code = c
break
if code is None:
raise ValueError(f"Destination text not found: {dest}")
self.set_unit(2, code)
self.set_unit(5, code)
def get_closest_destination(self, dest):
match = ""
match_score = 0.0
for text in MAP_DESTINATION.values():
score = SequenceMatcher(None, dest, text).ratio()
if score > 0.6 and score > match_score:
print(text, score)
match = text
match_score = score
return match