Weiterarbeit am Tracker
This commit is contained in:
		
							parent
							
								
									b1d0687952
								
							
						
					
					
						commit
						612783454e
					
				|  | @ -4,6 +4,10 @@ Tracker with MQTT client | |||
| 
 | ||||
|  TODO get data from mqtt thread | ||||
| 
 | ||||
| Verbindungsabbrüche | ||||
|   - on_disconnect | ||||
|    | ||||
| 
 | ||||
| """ | ||||
| 
 | ||||
| import cairo | ||||
|  | @ -14,27 +18,64 @@ class Tracker(Page): | |||
|     def __init__(self, pageno, cfg, appdata, boatdata): | ||||
|         super().__init__(pageno, cfg, boatdata) | ||||
|         self._appdata = appdata | ||||
|         self.bv_lat = boatdata.getRef("LAT") | ||||
|         self.bv_lon = boatdata.getRef("LON") | ||||
|         self.bv_sog = boatdata.getRef("SOG") | ||||
|         self.buttonlabel[1] = 'MODE' | ||||
|         print(cfg) | ||||
|         self.buttonlabel[2] = 'ON' | ||||
| 
 | ||||
|     def handle_key(self, buttonid): | ||||
|         if buttonid == 1: | ||||
|              | ||||
|             pass | ||||
|         elif buttonid == 2: | ||||
|             # Tracking ein/-ausschalten | ||||
|             if self._appdata.track.is_active(): | ||||
|                 self._appdata.track.set_active(False) | ||||
|                 self.buttonlabel[2] = 'ON' | ||||
|             else: | ||||
|                 self._appdata.track.set_active(True) | ||||
|                 self.buttonlabel[2] = 'OFF' | ||||
| 
 | ||||
|     def draw(self, ctx): | ||||
|         # Name | ||||
|         ctx.select_font_face("Ubuntu", cairo.FontSlant.NORMAL, cairo.FontWeight.BOLD) | ||||
|         ctx.set_font_size(32)  | ||||
|         ctx.move_to(20, 100) | ||||
|         ctx.move_to(20, 80) | ||||
|         ctx.show_text("Tracker") | ||||
| 
 | ||||
|         ctx.set_font_size(16) | ||||
|         ctx.move_to(20, 120) | ||||
|         ctx.show_text("Type: ") | ||||
|         ctx.show_text(self._appdata.track.ttype) | ||||
| 
 | ||||
|         ctx.move_to(20, 140) | ||||
|         ctx.show_text("active: ") | ||||
|         if self._appdata.track.is_active(): | ||||
|             ctx.show_text("yes") | ||||
|         else: | ||||
|             ctx.show_text("no") | ||||
| 
 | ||||
|         ctx.move_to(20, 160) | ||||
|         ctx.show_text("Lat=") | ||||
|         ctx.show_text(self.bv_lat.format()) | ||||
|         ctx.move_to(20, 180) | ||||
|         ctx.show_text("Lon=") | ||||
|         ctx.show_text(self.bv_lon.format()) | ||||
|         ctx.move_to(20, 200) | ||||
|         ctx.show_text("Sog=") | ||||
|         ctx.show_text(self.bv_sog.format()) | ||||
| 
 | ||||
|         # Mögliche Regatten | ||||
|         x = 250 | ||||
|         y = 160 | ||||
|         ctx.move_to(x, y - 24) | ||||
|         ctx.show_text("Regatten") | ||||
|         for r in self._appdata.track.hero_get_races(): | ||||
|             ctx.move_to(x, y) | ||||
|             ctx.show_text(r) | ||||
|             y += 20 | ||||
|         if y == 160: | ||||
|             ctx.move_to(x, y) | ||||
|             ctx.show_text("keine") | ||||
|              | ||||
|  |  | |||
							
								
								
									
										37
									
								
								tracker.py
								
								
								
								
							
							
						
						
									
										37
									
								
								tracker.py
								
								
								
								
							|  | @ -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 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue