feat(map): derive station regions from GPS
This commit is contained in:
@@ -28,6 +28,7 @@ OPEN_PLATFORM_BASE_URL = os.getenv("OPEN_PLATFORM_BASE_URL", "https://open.d.lno
|
||||
OPEN_PLATFORM_APP_KEY = os.getenv("OPEN_PLATFORM_APP_KEY", "").strip()
|
||||
UPSTREAM_TIMEOUT_SECONDS = float(os.getenv("UPSTREAM_TIMEOUT_SECONDS", "15"))
|
||||
STATION_CACHE_SECONDS = int(os.getenv("STATION_NAVIGATION_CACHE_SECONDS", "120"))
|
||||
VEHICLE_MAP_INTERNAL_BASE_URL = os.getenv("VEHICLE_MAP_INTERNAL_BASE_URL", "http://127.0.0.1:20800").rstrip("/")
|
||||
_cache_lock = threading.Lock()
|
||||
_cache = {}
|
||||
|
||||
@@ -91,27 +92,24 @@ def _cached(key, ttl_seconds, loader):
|
||||
return value
|
||||
|
||||
|
||||
def _public_station(station):
|
||||
"""Positive allowlist: do not add operational or hydrogen fields here."""
|
||||
fields = (
|
||||
"id", "name", "shortName", "province", "city", "district", "address",
|
||||
"longitude", "latitude", "cooperative",
|
||||
)
|
||||
return {field: station.get(field) for field in fields if station.get(field) is not None}
|
||||
|
||||
|
||||
def _load_station_directory():
|
||||
stations = _post_open_platform("/api/v1/hydrogen-stations/query", {})
|
||||
directory = [_public_station(station) for station in stations]
|
||||
return {
|
||||
"status": "ok",
|
||||
"asOf": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
||||
"summary": {
|
||||
"totalStations": len(directory),
|
||||
"cooperativeStations": sum(1 for station in directory if station.get("cooperative")),
|
||||
},
|
||||
"stations": directory,
|
||||
}
|
||||
"""Use the operations map's GPS-resolved public station directory.
|
||||
|
||||
The map service owns reverse-geocoding and its persistent cache, keeping
|
||||
the public navigation view and the operations view on identical boundaries.
|
||||
"""
|
||||
request = Request(
|
||||
VEHICLE_MAP_INTERNAL_BASE_URL + "/api/stations",
|
||||
headers={"Accept": "application/json", "User-Agent": "lingniu-station-navigation/1.0"},
|
||||
)
|
||||
try:
|
||||
with urlopen(request, timeout=UPSTREAM_TIMEOUT_SECONDS) as response:
|
||||
payload = json.load(response)
|
||||
except (HTTPError, URLError, ValueError) as exc:
|
||||
raise RuntimeError("vehicle map station directory unavailable: {}".format(exc)) from exc
|
||||
if payload.get("status") != "ok" or not isinstance(payload.get("stations"), list):
|
||||
raise RuntimeError("vehicle map returned an invalid station directory")
|
||||
return payload
|
||||
|
||||
|
||||
def _prewarm_station_directory_cache():
|
||||
|
||||
Reference in New Issue
Block a user