64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Header, Request
|
|
from sqlalchemy.orm import Session
|
|
|
|
try:
|
|
from app.db import get_db
|
|
from app.schemas.location import DeviceLocationReportRequest, DeviceLocationReportResponse
|
|
from app.service.location import report_device_location
|
|
except ModuleNotFoundError:
|
|
from db import get_db
|
|
from schemas.location import DeviceLocationReportRequest, DeviceLocationReportResponse
|
|
from service.location import report_device_location
|
|
|
|
|
|
router = APIRouter(prefix="/device-location", tags=["device-location"])
|
|
logger = logging.getLogger("app.device_location")
|
|
|
|
|
|
@router.post("/{device_id}/reports", response_model=DeviceLocationReportResponse)
|
|
def create_device_location_report(
|
|
device_id: str,
|
|
payload: DeviceLocationReportRequest,
|
|
request: Request,
|
|
device_serial: str = Header(alias="X-Device-Serial", min_length=1),
|
|
db: Session = Depends(get_db),
|
|
) -> DeviceLocationReportResponse:
|
|
device_identity, row = report_device_location(
|
|
db=db,
|
|
device_id=device_id,
|
|
serial_number=device_serial,
|
|
payload=payload,
|
|
)
|
|
|
|
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"],
|
|
)
|