修改GPS数据上报超时时间3秒
This commit is contained in:
@@ -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"],
|
||||
)
|
||||
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}")
|
||||
|
||||
Reference in New Issue
Block a user