-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplotserver.py
More file actions
executable file
·483 lines (390 loc) · 15.8 KB
/
Copy pathplotserver.py
File metadata and controls
executable file
·483 lines (390 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
'''
NMEA 0183 TCP Server for Marine GPS/Plotter Integration.
Refactored with proper resource management and thread safety.
© August Linnman, 2025, email: august@linnman.net
MIT License (see LICENSE file)
'''
import socket
import threading
import time
from datetime import datetime, timezone
from typing import Optional, List, Tuple
# pylint: disable=R0902
class NMEAServer:
"""Thread-safe NMEA 0183 server implementation"""
def __init__(self, host='0.0.0.0', port=10110):
self.host = host
self.port = port
self.clients: List[socket.socket] = []
self.clients_lock = threading.Lock()
self.running = False
self.server_socket: Optional[socket.socket] = None
# Position data with thread safety
self.position_lock = threading.Lock()
self.latitude = 0.0
self.longitude = 0.0
self.last_update: Optional[datetime] = None
# Track threads for proper cleanup
self.broadcast_thread: Optional[threading.Thread] = None
self.accept_thread: Optional[threading.Thread] = None
self.client_threads: List[threading.Thread] = []
def calculate_checksum(self, sentence: str) -> str:
"""Calculate NMEA checksum (XOR of all characters)"""
checksum = 0
for char in sentence:
checksum ^= ord(char)
return f"{checksum:02X}"
def format_coordinate(self, coord: float, is_longitude: bool = False) -> str:
"""Convert decimal degrees to NMEA format"""
abs_coord = abs(coord)
degrees = int(abs_coord)
minutes = (abs_coord - degrees) * 60
if is_longitude:
return f"{degrees:03d}{minutes:06.3f}"
else:
return f"{degrees:02d}{minutes:06.3f}"
def create_gga_sentence(self) -> str:
"""Create NMEA GGA sentence (Global Positioning System Fix Data)"""
now = datetime.now(timezone.utc)
time_str = now.strftime("%H%M%S.%f")[:-3]
with self.position_lock:
lat = self.latitude
lon = self.longitude
lat_str = self.format_coordinate(abs(lat))
lat_dir = 'N' if lat >= 0 else 'S'
lon_str = self.format_coordinate(abs(lon), True)
lon_dir = 'E' if lon >= 0 else 'W'
sentence = \
f"GPGGA,{time_str},{lat_str},{lat_dir},{lon_str},{lon_dir},1,08,1.0,10.0,M,0.0,M,,"
checksum = self.calculate_checksum(sentence)
return f"${sentence}*{checksum}\r\n"
def create_rmc_sentence(self) -> str:
"""Create NMEA RMC sentence"""
now = datetime.now(timezone.utc)
time_str = now.strftime("%H%M%S.%f")[:-3]
date_str = now.strftime("%d%m%y")
with self.position_lock:
lat = self.latitude
lon = self.longitude
lat_str = self.format_coordinate(abs(lat))
lat_dir = 'N' if lat >= 0 else 'S'
lon_str = self.format_coordinate(abs(lon), True)
lon_dir = 'E' if lon >= 0 else 'W'
sentence =\
f"GPRMC,{time_str},A,{lat_str},{lat_dir},{lon_str},{lon_dir},0.0,0.0,{date_str},0.0,E"
checksum = self.calculate_checksum(sentence)
return f"${sentence}*{checksum}\r\n"
def update_position(self, latitude: float, longitude: float):
"""Thread-safe position update"""
with self.position_lock:
self.latitude = latitude
self.longitude = longitude
self.last_update = datetime.now()
# print(f"Position updated: {latitude:.6f}, {longitude:.6f}") REMOVED
def handle_client(self, client_socket: socket.socket, address: Tuple[str, int]):
"""Handle individual client connection"""
print(f"Client connected from {address}")
with self.clients_lock:
self.clients.append(client_socket)
try:
# Keep connection alive while server is running
while self.running:
time.sleep(1)
# pylint: disable=W0718
except Exception as e:
# pylint: enable=W0718
print(f"Client {address} error: {e}")
finally:
with self.clients_lock:
if client_socket in self.clients:
self.clients.remove(client_socket)
try:
client_socket.close()
# pylint: disable=W0702
except:
pass
# pylint: enable=W0702
print(f"Client {address} disconnected")
def broadcast_data(self):
"""Broadcast NMEA data to all connected clients"""
print("Broadcast thread started")
while self.running:
with self.position_lock:
has_position = self.last_update is not None
if has_position:
with self.clients_lock:
clients_copy = self.clients.copy()
if clients_copy:
gga_sentence = self.create_gga_sentence()
rmc_sentence = self.create_rmc_sentence()
data = (gga_sentence + rmc_sentence).encode('ascii')
disconnected = []
for client in clients_copy:
try:
client.send(data)
# pylint: disable=W0718
except Exception as e:
# pylint: enable=W0718
print(f"Error sending to client: {e}")
disconnected.append(client)
# Clean up disconnected clients
if disconnected:
with self.clients_lock:
for client in disconnected:
if client in self.clients:
self.clients.remove(client)
try:
client.close()
# pylint: disable=W0702
except:
pass
# pylint: disable=W0702
time.sleep(1) # Send data every second
print("Broadcast thread stopped")
def _accept_connections(self):
"""Accept incoming client connections (runs in separate thread)"""
print("Accept thread started")
while self.running:
try:
# Set timeout so we can check self.running periodically
if self.server_socket:
self.server_socket.settimeout(1.0)
try:
try:
client_socket, address = self.server_socket.accept()
except socket.timeout:
continue
# Create and track client thread
client_thread = threading.Thread(
target=self.handle_client,
args=(client_socket, address),
name=f"Client-{address}",
daemon=True
)
self.client_threads.append(client_thread)
client_thread.start()
except socket.timeout:
continue # Check self.running again
# pylint: disable=W0718
except Exception as e:
# pylint: enable=W0718
if self.running:
print(f"Error accepting connection: {e}")
break
print("Accept thread stopped")
def start(self):
"""Start the NMEA server"""
if self.running:
print("Server already running")
return
self.running = True
print(f"Starting NMEA server on {self.host}:{self.port}")
try:
# Create and configure server socket
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(5)
print(f"NMEA server listening on {self.host}:{self.port}")
# Start broadcast thread
self.broadcast_thread = threading.Thread(
target=self.broadcast_data,
name="NMEA-Broadcast",
daemon=True
)
self.broadcast_thread.start()
# Start accept thread
self.accept_thread = threading.Thread(
target=self._accept_connections,
name="NMEA-Accept",
daemon=True
)
self.accept_thread.start()
print("NMEA server started successfully")
except Exception as e:
print(f"Server startup error: {e}")
self.stop()
raise
def stop(self):
"""Stop the NMEA server and clean up all resources"""
if not self.running:
return
print("Stopping NMEA server...")
self.running = False
# Close server socket (this will break accept loop)
if self.server_socket:
try:
self.server_socket.close()
# pylint: disable=W0718
except Exception as e:
# pylint: enable=W0718
print(f"Error closing server socket: {e}")
self.server_socket = None
# Wait for accept thread
if self.accept_thread and self.accept_thread.is_alive():
self.accept_thread.join(timeout=3.0)
if self.accept_thread.is_alive():
print("Warning: Accept thread didn't stop cleanly")
# Wait for broadcast thread
if self.broadcast_thread and self.broadcast_thread.is_alive():
self.broadcast_thread.join(timeout=3.0)
if self.broadcast_thread.is_alive():
print("Warning: Broadcast thread didn't stop cleanly")
# Close all client connections
with self.clients_lock:
for client in self.clients:
try:
client.close()
# pylint: disable=W0702
except:
pass
# pylint: enable=W0702
self.clients.clear()
# Wait for client threads (with timeout to avoid infinite wait)
for thread in self.client_threads:
if thread.is_alive():
thread.join(timeout=1.0)
self.client_threads.clear()
print("NMEA server stopped")
class PlotServerManager:
"""
Singleton manager for NMEA plot server lifecycle.
Ensures only one server instance runs and provides proper cleanup.
"""
_instance = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
instance = super().__new__(cls)
# Initialize the flag here so __init__ can check it
instance._initialized = False
cls._instance = instance
return cls._instance
def __init__(self):
if self._initialized:
return
self.server: Optional[NMEAServer] = None
self.server_thread: Optional[threading.Thread] = None
self.is_running = False
self.kill_timer: Optional[threading.Timer] = None
self._initialized = True
print("PlotServerManager initialized")
def start(self):
"""Start the plot server (idempotent - safe to call multiple times)"""
with self._lock:
if self.is_running:
print("PlotServerManager: Server already running, resetting kill timer")
self._reset_kill_timer()
return
print("PlotServerManager: Starting new server")
# Create new server instance
self.server = NMEAServer(host='0.0.0.0', port=10110)
self.is_running = True # ← Change this line
# Start server in dedicated thread
self.server_thread = threading.Thread(
target=self.server.start,
name="PlotServerMain",
daemon=True
)
self.server_thread.start()
# Start automatic shutdown timer
self._reset_kill_timer()
print("PlotServerManager: Server started")
def _reset_kill_timer(self):
"""Reset the automatic shutdown timer (20 seconds of inactivity)"""
# Cancel existing timer
if self.kill_timer:
self.kill_timer.cancel()
# Create new timer
self.kill_timer = threading.Timer(20.0, self._auto_shutdown)
self.kill_timer.daemon = True
self.kill_timer.start()
def _auto_shutdown(self):
"""Automatically shutdown server after inactivity"""
print("PlotServerManager: Auto-shutdown triggered (20s inactivity)")
self.stop()
def update_position(self, lat: float, lon: float):
"""Update position and reset kill timer"""
with self._lock:
if self.server and self.is_running:
self.server.update_position(lat, lon)
self._reset_kill_timer()
else:
print("PlotServerManager: Server not running, cannot update position")
def stop(self):
"""Stop the plot server and clean up resources"""
with self._lock:
if not self.is_running:
print("PlotServerManager: Server not running, nothing to stop")
return
print("PlotServerManager: Stopping server")
# Cancel kill timer
if self.kill_timer:
self.kill_timer.cancel()
self.kill_timer = None
# Stop server
if self.server:
self.server.stop()
self.server = None
# Wait for server thread
if self.server_thread and self.server_thread.is_alive():
self.server_thread.join(timeout=5.0)
if self.server_thread.is_alive():
print("PlotServerManager: Warning - server thread didn't stop cleanly")
self.is_running = False
self.server_thread = None
print("PlotServerManager: Server stopped and cleaned up")
def get_status(self) -> dict:
"""Get current server status (for debugging)"""
with self._lock:
status: dict[str, int | bool] = {
'is_running': self.is_running,
'server_thread_alive':\
self.server_thread.is_alive() if self.server_thread else False,
'has_server': self.server is not None,
}
if self.server:
with self.server.clients_lock:
status['client_count'] = len(self.server.clients)
with self.server.position_lock:
status['has_position'] = self.server.last_update is not None
return status
# pylint: enable=R0902
# Create singleton instance
plot_server = PlotServerManager()
# Convenience functions for backward compatibility
def start_plotserver():
"""Start the plot server"""
plot_server.start()
def kill_plotserver():
"""Stop the plot server"""
plot_server.stop()
def update_plot_position(lat: float, lon: float):
"""Update the plot server with new coordinates"""
plot_server.update_position(lat, lon)
def main():
"""Example usage"""
print("Starting NMEA server test...")
try:
plot_server.start()
# Simulate position updates
while True:
try:
lat = float(input("Enter latitude (or 'q' to quit): "))
lon = float(input("Enter longitude: "))
plot_server.update_position(lat, lon)
# Show status
status = plot_server.get_status()
print(f"Server status: {status}")
except ValueError:
print("Invalid coordinates")
except (KeyboardInterrupt, EOFError):
break
finally:
print("\nShutting down...")
plot_server.stop()
print("Done!")
if __name__ == "__main__":
main()