350 lines
7.5 KiB
Python
350 lines
7.5 KiB
Python
import threading
|
|
import time
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from gps import create_gps_source, parse_nmea
|
|
|
|
|
|
class GPSTestService:
|
|
|
|
def __init__(
|
|
self,
|
|
cfg,
|
|
callback=None
|
|
):
|
|
|
|
self.cfg = cfg
|
|
self.callback = callback
|
|
self.source = None
|
|
self.source_lock = threading.Lock()
|
|
|
|
self.thread = None
|
|
self.stop_event = threading.Event()
|
|
|
|
self.running = False
|
|
self.state = "READY"
|
|
|
|
self.nmea_received = False
|
|
self.fix_received = False
|
|
|
|
self.nmea_count = 0
|
|
self.fix_count = 0
|
|
|
|
self.latitude = None
|
|
self.longitude = None
|
|
|
|
self.error = None
|
|
|
|
|
|
def emit(self, message):
|
|
|
|
if self.callback:
|
|
self.callback(message)
|
|
|
|
|
|
def start(self):
|
|
|
|
if (
|
|
self.thread is not None
|
|
and
|
|
self.thread.is_alive()
|
|
):
|
|
return False
|
|
|
|
self.stop_event.clear()
|
|
|
|
self.running = True
|
|
self.state = "STARTING"
|
|
self.nmea_received = False
|
|
self.fix_received = False
|
|
self.nmea_count = 0
|
|
self.fix_count = 0
|
|
self.latitude = None
|
|
self.longitude = None
|
|
self.error = None
|
|
|
|
self.thread = threading.Thread(
|
|
target=self.worker,
|
|
daemon=True
|
|
)
|
|
|
|
self.thread.start()
|
|
|
|
return True
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.stop_event.set()
|
|
|
|
with self.source_lock:
|
|
source = self.source
|
|
|
|
if source is None:
|
|
return
|
|
|
|
connection = getattr(
|
|
source,
|
|
"socket",
|
|
None
|
|
)
|
|
|
|
if connection is not None:
|
|
|
|
try:
|
|
connection.shutdown(2)
|
|
|
|
except OSError:
|
|
pass
|
|
|
|
try:
|
|
source.close()
|
|
|
|
except Exception as error:
|
|
|
|
self.emit(
|
|
f"[GPS TEST] Stop close error: {error}"
|
|
)
|
|
|
|
def worker(self):
|
|
|
|
source = None
|
|
file = None
|
|
|
|
try:
|
|
|
|
self.emit(
|
|
"[GPS TEST] Starting GPS test "
|
|
"from saved configuration."
|
|
)
|
|
|
|
source = create_gps_source(
|
|
self.cfg
|
|
)
|
|
|
|
with self.source_lock:
|
|
self.source = source
|
|
|
|
if self.stop_event.is_set():
|
|
return
|
|
|
|
self.emit(
|
|
"[GPS TEST] Connecting to GPS source..."
|
|
)
|
|
|
|
file = source.open()
|
|
if self.stop_event.is_set():
|
|
return
|
|
|
|
self.state = "CONNECTED"
|
|
|
|
self.emit(
|
|
"[GPS TEST] GPS source connected. "
|
|
"Waiting for NMEA..."
|
|
)
|
|
|
|
last_no_fix_message = 0
|
|
|
|
while not self.stop_event.is_set():
|
|
|
|
if hasattr(
|
|
source,
|
|
"readline"
|
|
):
|
|
|
|
line = source.readline()
|
|
|
|
else:
|
|
|
|
line = file.readline()
|
|
|
|
if not line:
|
|
|
|
if self.stop_event.wait(0.1):
|
|
break
|
|
|
|
continue
|
|
|
|
self.nmea_count += 1
|
|
self.nmea_received = True
|
|
|
|
if self.state == "CONNECTED":
|
|
|
|
self.state = "NMEA"
|
|
|
|
self.emit(
|
|
"[GPS TEST] NMEA received. "
|
|
"Waiting for valid FIX..."
|
|
)
|
|
|
|
received = datetime.now(
|
|
timezone.utc
|
|
)
|
|
|
|
gps = parse_nmea(
|
|
line,
|
|
received
|
|
)
|
|
|
|
if gps is not None:
|
|
|
|
first_fix = not self.fix_received
|
|
|
|
self.fix_count += 1
|
|
self.fix_received = True
|
|
self.state = "FIX"
|
|
self.latitude = gps.latitude
|
|
self.longitude = gps.longitude
|
|
|
|
self.emit(
|
|
"[GPS TEST] FIX received: "
|
|
f"lat={gps.latitude:.6f} "
|
|
f"lon={gps.longitude:.6f}"
|
|
)
|
|
|
|
if first_fix:
|
|
|
|
self.emit(
|
|
"[GPS TEST] GPS is ready "
|
|
"for scanning."
|
|
)
|
|
|
|
elif self.fix_count % 10 == 0:
|
|
|
|
self.emit(
|
|
"[GPS TEST] GPS is ready "
|
|
"for scanning. "
|
|
f"FIX: lat={gps.latitude:.6f} "
|
|
f"lon={gps.longitude:.6f}"
|
|
)
|
|
|
|
elif (
|
|
self.nmea_received
|
|
and
|
|
not self.fix_received
|
|
and
|
|
self.nmea_count % 10 == 0
|
|
and
|
|
time.monotonic() - last_no_fix_message >= 5
|
|
):
|
|
|
|
last_no_fix_message = (
|
|
time.monotonic()
|
|
)
|
|
|
|
self.emit(
|
|
"[GPS TEST] GPS is not ready: "
|
|
"NMEA received, but no valid FIX yet. "
|
|
f"NMEA: {self.nmea_count}"
|
|
)
|
|
|
|
except Exception as error:
|
|
|
|
if not self.stop_event.is_set():
|
|
|
|
self.state = "ERROR"
|
|
self.error = str(error)
|
|
|
|
self.emit(
|
|
f"[GPS TEST] GPS source error: {error}"
|
|
)
|
|
|
|
finally:
|
|
|
|
if file is not None:
|
|
|
|
try:
|
|
file.close()
|
|
|
|
except Exception as error:
|
|
|
|
self.emit(
|
|
f"[GPS TEST] File close error: {error}"
|
|
)
|
|
|
|
if source is not None:
|
|
|
|
try:
|
|
source.close()
|
|
|
|
except Exception as error:
|
|
|
|
self.emit(
|
|
f"[GPS TEST] Close error: {error}"
|
|
)
|
|
|
|
with self.source_lock:
|
|
|
|
if self.source is source:
|
|
self.source = None
|
|
self.running = False
|
|
|
|
if self.state != "ERROR":
|
|
|
|
self.state = "STOPPED"
|
|
|
|
self.emit(
|
|
"[GPS TEST] Test stopped."
|
|
)
|
|
|
|
|
|
def run_gps_test(cfg):
|
|
|
|
def output(message):
|
|
|
|
print(
|
|
message,
|
|
flush=True
|
|
)
|
|
|
|
service = GPSTestService(
|
|
cfg,
|
|
callback=output
|
|
)
|
|
|
|
service.start()
|
|
|
|
try:
|
|
|
|
while service.running:
|
|
|
|
time.sleep(0.2)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print(
|
|
"[GPS TEST] Test interrupted by user.",
|
|
flush=True
|
|
)
|
|
|
|
if service.fix_received:
|
|
|
|
print(
|
|
"[GPS TEST] Result: "
|
|
"GPS is ready for scanning.",
|
|
flush=True
|
|
)
|
|
|
|
print(
|
|
"[GPS TEST] Last position: "
|
|
f"lat={service.latitude:.6f} "
|
|
f"lon={service.longitude:.6f}",
|
|
flush=True
|
|
)
|
|
|
|
else:
|
|
|
|
print(
|
|
"[GPS TEST] Result: "
|
|
"GPS is not ready for scanning.",
|
|
flush=True
|
|
)
|
|
|
|
service.stop()
|
|
|
|
while service.running:
|
|
|
|
time.sleep(0.1)
|
|
|
|
return 0 if service.fix_received else 1 |