fix: 对接GPS数据
This commit is contained in:
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
from collections import deque
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
@@ -10,14 +9,13 @@ from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from .common import (
|
||||
GEOSTREAM_JSON_PATH,
|
||||
GEOSTREAM_STALE_SECONDS,
|
||||
VIDEO_TRAILER_COORDINATE_FORMAT,
|
||||
WORKSPACE_ROOT,
|
||||
load_omnisocket_config,
|
||||
utc_iso_now,
|
||||
)
|
||||
from .control import ControlArbiter, NativeUdpControlIngress, OmniSocketControlSender
|
||||
from .video import OmniSocketVideoReceiver
|
||||
from .video import FrameTrailerMetadata, OmniSocketVideoReceiver
|
||||
|
||||
|
||||
LOCAL_SAMPLE_INTERVAL_MS = 500
|
||||
@@ -50,46 +48,45 @@ def _coerce_float(value: Any, default: float = 0.0) -> float:
|
||||
|
||||
|
||||
class GpsDataService:
|
||||
def __init__(self, receiver: OmniSocketVideoReceiver) -> None:
|
||||
self._receiver = receiver
|
||||
|
||||
def get_latest(self) -> dict[str, Any]:
|
||||
payload = self._read_geostream_payload()
|
||||
if payload is not None:
|
||||
payload["source_mode"] = "geostream-json"
|
||||
payload["updated_at"] = utc_iso_now()
|
||||
return payload
|
||||
metadata = self._receiver.get_latest_frame_metadata()
|
||||
if metadata is None:
|
||||
return self._build_waiting_payload()
|
||||
return self._build_payload_from_metadata(metadata)
|
||||
|
||||
return self._build_simulated_payload()
|
||||
|
||||
def _read_geostream_payload(self) -> dict[str, Any] | None:
|
||||
if not GEOSTREAM_JSON_PATH.exists():
|
||||
return None
|
||||
|
||||
age_seconds = time.time() - GEOSTREAM_JSON_PATH.stat().st_mtime
|
||||
if age_seconds > GEOSTREAM_STALE_SECONDS:
|
||||
return None
|
||||
|
||||
try:
|
||||
with GEOSTREAM_JSON_PATH.open("r", encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return None
|
||||
|
||||
def _build_simulated_payload(self) -> dict[str, Any]:
|
||||
tick = time.time() / 12.0
|
||||
latitude = 31.2304 + math.sin(tick) * 0.0014
|
||||
longitude = 121.4737 + math.cos(tick) * 0.0018
|
||||
def _build_waiting_payload(self) -> dict[str, Any]:
|
||||
return {
|
||||
"has_fix": False,
|
||||
"utc_time": "--:--:--",
|
||||
"latitude": None,
|
||||
"longitude": None,
|
||||
"satellites": None,
|
||||
"altitude_m": None,
|
||||
"coordinate_system": "WGS84",
|
||||
"source_sentence": "VIDEO_TRAILER",
|
||||
"raw_coordinate_format": VIDEO_TRAILER_COORDINATE_FORMAT,
|
||||
"source_mode": "video-frame-trailer-waiting",
|
||||
"updated_at": "",
|
||||
}
|
||||
|
||||
def _build_payload_from_metadata(self, metadata: FrameTrailerMetadata) -> dict[str, Any]:
|
||||
timestamp_seconds = metadata.timestamp_ns / 1_000_000_000
|
||||
updated_at = _utc_from_epoch(metadata.received_at) or ""
|
||||
return {
|
||||
"has_fix": True,
|
||||
"utc_time": datetime.now(timezone.utc).strftime("%H:%M:%S"),
|
||||
"latitude": round(latitude, 6),
|
||||
"longitude": round(longitude, 6),
|
||||
"satellites": 14 + int((math.sin(tick * 0.7) + 1.0) * 2),
|
||||
"altitude_m": round(6.5 + math.cos(tick * 0.5) * 1.2, 2),
|
||||
"utc_time": datetime.fromtimestamp(timestamp_seconds, timezone.utc).strftime("%H:%M:%S"),
|
||||
"latitude": round(metadata.latitude, 6),
|
||||
"longitude": round(metadata.longitude, 6),
|
||||
"satellites": None,
|
||||
"altitude_m": None,
|
||||
"coordinate_system": "WGS84",
|
||||
"source_sentence": "SIMULATED",
|
||||
"raw_coordinate_format": "decimal degrees",
|
||||
"source_mode": "simulated",
|
||||
"updated_at": utc_iso_now(),
|
||||
"source_sentence": "VIDEO_TRAILER",
|
||||
"raw_coordinate_format": VIDEO_TRAILER_COORDINATE_FORMAT,
|
||||
"source_mode": "video-frame-trailer",
|
||||
"updated_at": updated_at,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user