Weiterarbeit am Tracker

This commit is contained in:
2025-09-12 15:28:01 +02:00
parent b1d0687952
commit 612783454e
2 changed files with 73 additions and 9 deletions

View File

@@ -17,6 +17,7 @@ import os
import time
import paho.mqtt.client as mqtt
import json
import socket
class Tracker():
@@ -26,6 +27,9 @@ class Tracker():
if trackertype not in validtypes:
raise TypeError(f"Invalid tracker type: '{valtype}'. Only supported: {validtypes}")
self.ttype = trackertype
self.appdata = None
self.activated = False
self.trace = False # Debugging
self.trace_fh = None # File Handle der Tracedatei
@@ -38,6 +42,22 @@ class Tracker():
self.tspos = None # timestamp (hh:ss:mm) as datetime.time
self.sog = None
self.hero_orgstatus = None
self.hero_racestatus = None # Akluelle Regatta
def is_server_active(self, hostname, port):
"""
ohne Netzwerkverbindung wirft socket.gethostbyname eine Exception,
ggf. "Erro -3 temporary failure in name resolution"
"""
try:
host = socket.gethostbyname(hostname) # DNS Lookup
s = socket.create_connection((host, port), 2)
s.close()
return True
except Exception:
pass
return False
def is_active(self):
return self.activated
@@ -49,12 +69,17 @@ class Tracker():
# Positionsabfrage für die Payload
# LAT, LON, TSPOS, SOG
return (self.lat, self.lon, self.tspos, self.sog)
"""
def hero_add_race(self, raceid):
self.races.add(raceid)
def hero_set_races(self, newraces):
self.races = set(newraces)
"""
def hero_get_races(self):
if not self.hero_orgstatus:
return []
return [r for r in self.hero_orgstatus['races']]
def mqtt_on_connect(self, client, userdata, flags, rc):
print(f"MQTT connected with result code {rc}")
@@ -85,18 +110,15 @@ class Tracker():
self.trace_fh.flush()
if msg.topic == "regattahero/orgstatus/thomas":
# kommt alle 10s
orgstatus = json.loads(msg.payload)
if orgstatus['allLogout']:
self.hero_orgstatus = json.loads(msg.payload)
if self.hero_orgstatus['allLogout']:
print("All logout received!")
client.disconnect()
sys.exit(0) # TODO nur die MQTT-Task beenden
if orgstatus['message']:
if self.hero_orgstatus['message']:
# TODO Alarm-Funktion nutzen?
print("Nachricht der Wettfahrtkeitung:")
print(orgstatus['message'])
print(orgstatus['races'])
#for r in orgstatus['races']:
# print(f"Race: {r}")
elif msg.topic.startswith("regattahero/racestatus/thomas"):
# kommt alle 1s
# dem Topic angehängt ist noch die raceid
@@ -136,6 +158,7 @@ class Tracker():
def mqtt_tracker(self, cfg, boat, appdata, boatdata):
print("MQTT tracker enabled")
self.appdata = appdata
self.trace = cfg['trace']
client = mqtt.Client()
client.on_connect = self.mqtt_on_connect