修改GPS数据上报超时时间3秒
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
import logging
|
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:
|
try:
|
||||||
from banban.schemas.location import DeviceLocationReportRequest, DeviceLocationReportResponse
|
from banban.schemas.location import DeviceLocationReportRequest, DeviceLocationReportResponse
|
||||||
from banban.service.location import location_service
|
from banban.service.location import location_service
|
||||||
@@ -21,38 +23,58 @@ async def create_device_location_report(
|
|||||||
request: Request,
|
request: Request,
|
||||||
device_serial: str = Header(alias="X-Device-Serial", min_length=1),
|
device_serial: str = Header(alias="X-Device-Serial", min_length=1),
|
||||||
) -> DeviceLocationReportResponse:
|
) -> DeviceLocationReportResponse:
|
||||||
device_identity, row = await location_service.report_device_location(
|
# 每次进入小程序地图页面获取最新位置坐标,等待设备上报GPS数据,再查询数据库坐标
|
||||||
device_id=device_id,
|
service = await TalkingQMQTTService.get_instance()
|
||||||
serial_number=device_serial,
|
if service is None:
|
||||||
payload=payload,
|
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(
|
logger.info(
|
||||||
"device location reported",
|
"device location reported",
|
||||||
extra={
|
extra={
|
||||||
"event": "device_location_report",
|
"event": "device_location_report",
|
||||||
"request_id": getattr(request.state, "request_id", None),
|
"request_id": getattr(request.state, "request_id", None),
|
||||||
"device_id": device_id,
|
"device_id": device_id,
|
||||||
"child_id": device_identity.child_id,
|
"child_id": device_identity.child_id,
|
||||||
"lat": float(row["lat"]),
|
"lat": float(row["lat"]),
|
||||||
"lng": float(row["lng"]),
|
"lng": float(row["lng"]),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
return DeviceLocationReportResponse(
|
return DeviceLocationReportResponse(
|
||||||
child_id=int(row["child_id"]),
|
child_id=int(row["child_id"]),
|
||||||
child_name=device_identity.child_name,
|
child_name=device_identity.child_name,
|
||||||
device_id=str(row["device_id"]),
|
device_id=str(row["device_id"]),
|
||||||
coord_type=str(row["coord_type"]),
|
coord_type=str(row["coord_type"]),
|
||||||
lat=float(row["lat"]),
|
lat=float(row["lat"]),
|
||||||
lng=float(row["lng"]),
|
lng=float(row["lng"]),
|
||||||
accuracy_m=row["accuracy_m"],
|
accuracy_m=row["accuracy_m"],
|
||||||
altitude_m=float(row["altitude_m"]) if row["altitude_m"] is not None else None,
|
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,
|
speed_mps=float(row["speed_mps"]) if row["speed_mps"] is not None else None,
|
||||||
heading_deg=row["heading_deg"],
|
heading_deg=row["heading_deg"],
|
||||||
source=int(row["source"]),
|
source=int(row["source"]),
|
||||||
battery_pct=row["battery_pct"],
|
battery_pct=row["battery_pct"],
|
||||||
device_time=row["device_time"],
|
device_time=row["device_time"],
|
||||||
server_time=row["server_time"],
|
server_time=row["server_time"],
|
||||||
updated_at=row["updated_at"],
|
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