From 51501bde193adffd2ba5c9f62e9e8279cd854da9 Mon Sep 17 00:00:00 2001 From: HycJack <772403255@qq.com> Date: Wed, 29 Apr 2026 04:46:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9GPS=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E8=B6=85=E6=97=B6=E6=97=B6=E9=97=B43?= =?UTF-8?q?=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../banban/routers/device_location.py | 92 ++++++++++++------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/talkingq-url/banban/routers/device_location.py b/talkingq-url/banban/routers/device_location.py index e48dd11..52da2f1 100644 --- a/talkingq-url/banban/routers/device_location.py +++ b/talkingq-url/banban/routers/device_location.py @@ -1,7 +1,9 @@ import logging +import asyncio +from datetime import datetime, timedelta -from fastapi import APIRouter, Depends, Header, Request - +from fastapi import APIRouter, Depends, Header, Request, HTTPException +from handlers.mqtt_handler import TalkingQMQTTService try: from banban.schemas.location import DeviceLocationReportRequest, DeviceLocationReportResponse from banban.service.location import location_service @@ -21,38 +23,58 @@ async def create_device_location_report( request: Request, device_serial: str = Header(alias="X-Device-Serial", min_length=1), ) -> DeviceLocationReportResponse: - device_identity, row = await location_service.report_device_location( - device_id=device_id, - serial_number=device_serial, - payload=payload, - ) + # 每次进入小程序地图页面获取最新位置坐标,等待设备上报GPS数据,再查询数据库坐标 + service = await TalkingQMQTTService.get_instance() + if service is None: + raise HTTPException(status_code=503, detail="MQTT 服务未初始化") + await service.send_gps_query(device_id) + try: + # 每隔1s 获取一次GPS数据,最多3次 + i=0 + while i<3: + device_identity, row = await location_service.report_device_location( + device_id=device_id, + serial_number=device_serial, + payload=payload, + ) + if row is None: + await asyncio.sleep(1) + else: + if row["updated_at"] is not None \ + and (datetime.now() - row['updated_at']) <= timedelta(seconds=10): + break + i+=1 + if row is None: + raise HTTPException(status_code=408, detail="GPS数据上报超时") - logger.info( - "device location reported", - extra={ - "event": "device_location_report", - "request_id": getattr(request.state, "request_id", None), - "device_id": device_id, - "child_id": device_identity.child_id, - "lat": float(row["lat"]), - "lng": float(row["lng"]), - }, - ) + logger.info( + "device location reported", + extra={ + "event": "device_location_report", + "request_id": getattr(request.state, "request_id", None), + "device_id": device_id, + "child_id": device_identity.child_id, + "lat": float(row["lat"]), + "lng": float(row["lng"]), + }, + ) - return DeviceLocationReportResponse( - child_id=int(row["child_id"]), - child_name=device_identity.child_name, - device_id=str(row["device_id"]), - coord_type=str(row["coord_type"]), - lat=float(row["lat"]), - lng=float(row["lng"]), - accuracy_m=row["accuracy_m"], - altitude_m=float(row["altitude_m"]) if row["altitude_m"] is not None else None, - speed_mps=float(row["speed_mps"]) if row["speed_mps"] is not None else None, - heading_deg=row["heading_deg"], - source=int(row["source"]), - battery_pct=row["battery_pct"], - device_time=row["device_time"], - server_time=row["server_time"], - updated_at=row["updated_at"], - ) \ No newline at end of file + return DeviceLocationReportResponse( + child_id=int(row["child_id"]), + child_name=device_identity.child_name, + device_id=str(row["device_id"]), + coord_type=str(row["coord_type"]), + lat=float(row["lat"]), + lng=float(row["lng"]), + accuracy_m=row["accuracy_m"], + altitude_m=float(row["altitude_m"]) if row["altitude_m"] is not None else None, + speed_mps=float(row["speed_mps"]) if row["speed_mps"] is not None else None, + heading_deg=row["heading_deg"], + source=int(row["source"]), + battery_pct=row["battery_pct"], + device_time=row["device_time"], + server_time=row["server_time"], + updated_at=row["updated_at"], + ) + except Exception as e: + raise HTTPException(status_code=408, detail=f"GPS数据上报失败: {e}")