修改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,11 +23,29 @@ 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:
|
||||||
|
# 每次进入小程序地图页面获取最新位置坐标,等待设备上报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_identity, row = await location_service.report_device_location(
|
||||||
device_id=device_id,
|
device_id=device_id,
|
||||||
serial_number=device_serial,
|
serial_number=device_serial,
|
||||||
payload=payload,
|
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",
|
||||||
@@ -56,3 +76,5 @@ async def create_device_location_report(
|
|||||||
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