Tracker weiter verbessert

This commit is contained in:
Thomas Hooge 2025-09-22 18:35:18 +02:00
parent f47379b6d3
commit f6f515ea14
2 changed files with 20 additions and 19 deletions

View File

@ -139,10 +139,13 @@ class RaceTracker(Page):
elif buttonid == 4:
if self.mode == 'C':
# Set / Select regatta
# TODO Nur möglich wenn nicht in einer anderen Wettfahrt gerade aktiv
if self.menupos > 0:
self.raceid = self.races[self.menupos - 1] # Nullbasiert
last_id = self.app.track.hero_raceid
self.app.track.hero_raceid = self.raceid
print(f"Selected race '{self.raceid}'")
self.app.track.hero_mqtt_subscribe(self.raceid, last_id)
self.log.info(f"Selected race '{self.raceid}'")
return True
elif buttonid == 5:
if self.mode == 'N':

View File

@ -212,12 +212,21 @@ class Tracker():
"""
def hero_mqtt_subscribe(self, raceid, lastid=None):
topicbase = f"regattahero/racestatus/{self.hero_orgid}"
if lastid is not None:
topic = '/'.join((topicbase, lastid))
self.client.unsubscribe(topic)
self.log.info(f"Sent unsubscribe for '{topic}'")
topic = '/'.join((topicbase, raceid))
self.client.subscribe(topic)
self.log.info(f"Sent subscribe for '{topic}'")
def hero_giveup(self):
# TODO nach Aufgabe noch ein paar Pakete senden bis dier
# Tracker dann abgestellt wird.
self.hero_givenup = True
def hero_play_audio(self, event, lang='de'):
"""
Ein Event ist mit einer Audiodatei gekoppelt. Das Event trägt
@ -347,9 +356,8 @@ class Tracker():
def mqtt_on_connect(self, client, userdata, flags, rc):
self.mqtt_connected = False
if rc == 0:
self.log.info(f"MQTT connected, subscribing to topics")
client.subscribe("regattahero/orgstatus/thomas")
client.subscribe("regattahero/racestatus/thomas/#")
self.log.info("MQTT connected, subscribing to organisation topic")
client.subscribe(f"regattahero/orgstatus/{self.hero_orgid}")
self.mqtt_connected = True
elif rc == 1:
self.log.error("MQTT connection refused, unacceptable protocol version")
@ -366,6 +374,7 @@ class Tracker():
def mqtt_on_message(self, client, userdata, msg):
"""
Meldungen vom Server empfangen
TODO raceid über userdata? dann topic prüfen?
"""
if self.trace:
@ -374,7 +383,7 @@ class Tracker():
self.trace_fh.write(msg.payload.decode())
self.trace_fh.write("\n\n")
self.trace_fh.flush()
if msg.topic == "regattahero/orgstatus/thomas":
if msg.topic == f"regattahero/orgstatus/{self.hero_orgid}":
# kommt alle 10s
self.hero_orgstatus = json.loads(msg.payload)
@ -393,7 +402,7 @@ class Tracker():
if self.hero_orgstatus['message']:
# TODO Alarm-Funktion nutzen?
pass
elif msg.topic.startswith("regattahero/racestatus/thomas"):
elif msg.topic.startswith(f"regattahero/racestatus/{self.hero_orgid}"):
# kommt alle 1s
# dem Topic angehängt ist noch die raceid
payload = json.loads(msg.payload)
@ -452,17 +461,6 @@ class Tracker():
def mqtt_tracker(self, cfg, boat, appdata, boatdata):
self.log.info("MQTT tracker enabled")
self.appdata = appdata
"""
self.boatid = boat['uuid']
self.sailno = boat['sailno']
self.boatname = boat['name']
self.boatclass = boat['class']
self.handicap = boat['handicap']
self.club = boat['club']
self.team = boat['team']
self.trace = cfg['trace']
self.hero_orgid = cfg['username']
"""
self.client.username_pw_set(username=cfg['mqtt_user'], password=cfg['mqtt_pass'])
try:
self.client.connect(cfg['mqtt_host'], cfg['mqtt_port'], 60)