2026-03-17 01:14:40 +00:00
|
|
|
|
"""
|
|
|
|
|
|
Location Service - 位置数据服务
|
|
|
|
|
|
Provides query operations for GPS / LBS / WIFI location records.
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from app.models import LocationRecord
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_locations(
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
device_id: int | None = None,
|
|
|
|
|
|
location_type: str | None = None,
|
|
|
|
|
|
start_time: datetime | None = None,
|
|
|
|
|
|
end_time: datetime | None = None,
|
|
|
|
|
|
page: int = 1,
|
|
|
|
|
|
page_size: int = 20,
|
|
|
|
|
|
) -> tuple[list[LocationRecord], int]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取位置记录列表(分页)/ Get paginated location records.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
----------
|
|
|
|
|
|
db : AsyncSession
|
|
|
|
|
|
Database session.
|
|
|
|
|
|
device_id : int, optional
|
|
|
|
|
|
Filter by device ID.
|
|
|
|
|
|
location_type : str, optional
|
|
|
|
|
|
Filter by location type (gps, lbs, wifi, gps_4g, lbs_4g, wifi_4g).
|
|
|
|
|
|
start_time : datetime, optional
|
|
|
|
|
|
Filter records after this time.
|
|
|
|
|
|
end_time : datetime, optional
|
|
|
|
|
|
Filter records before this time.
|
|
|
|
|
|
page : int
|
|
|
|
|
|
Page number (1-indexed).
|
|
|
|
|
|
page_size : int
|
|
|
|
|
|
Number of items per page.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
|
-------
|
|
|
|
|
|
tuple[list[LocationRecord], int]
|
|
|
|
|
|
(list of location records, total count)
|
|
|
|
|
|
"""
|
|
|
|
|
|
query = select(LocationRecord)
|
|
|
|
|
|
count_query = select(func.count(LocationRecord.id))
|
|
|
|
|
|
|
|
|
|
|
|
if device_id is not None:
|
|
|
|
|
|
query = query.where(LocationRecord.device_id == device_id)
|
|
|
|
|
|
count_query = count_query.where(LocationRecord.device_id == device_id)
|
|
|
|
|
|
|
|
|
|
|
|
if location_type:
|
|
|
|
|
|
query = query.where(LocationRecord.location_type == location_type)
|
|
|
|
|
|
count_query = count_query.where(LocationRecord.location_type == location_type)
|
|
|
|
|
|
|
|
|
|
|
|
if start_time:
|
|
|
|
|
|
query = query.where(LocationRecord.recorded_at >= start_time)
|
|
|
|
|
|
count_query = count_query.where(LocationRecord.recorded_at >= start_time)
|
|
|
|
|
|
|
|
|
|
|
|
if end_time:
|
|
|
|
|
|
query = query.where(LocationRecord.recorded_at <= end_time)
|
|
|
|
|
|
count_query = count_query.where(LocationRecord.recorded_at <= end_time)
|
|
|
|
|
|
|
|
|
|
|
|
total_result = await db.execute(count_query)
|
|
|
|
|
|
total = total_result.scalar() or 0
|
|
|
|
|
|
|
|
|
|
|
|
offset = (page - 1) * page_size
|
|
|
|
|
|
query = query.order_by(LocationRecord.recorded_at.desc()).offset(offset).limit(page_size)
|
|
|
|
|
|
result = await db.execute(query)
|
|
|
|
|
|
records = list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
|
|
return records, total
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_latest_location(
|
|
|
|
|
|
db: AsyncSession, device_id: int
|
|
|
|
|
|
) -> LocationRecord | None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取设备最新位置 / Get the most recent location for a device.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
----------
|
|
|
|
|
|
db : AsyncSession
|
|
|
|
|
|
Database session.
|
|
|
|
|
|
device_id : int
|
|
|
|
|
|
Device ID.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
|
-------
|
|
|
|
|
|
LocationRecord | None
|
|
|
|
|
|
"""
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(LocationRecord)
|
|
|
|
|
|
.where(LocationRecord.device_id == device_id)
|
|
|
|
|
|
.order_by(LocationRecord.recorded_at.desc())
|
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
)
|
|
|
|
|
|
return result.scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_device_track(
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
device_id: int,
|
|
|
|
|
|
start_time: datetime,
|
|
|
|
|
|
end_time: datetime,
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
max_points: int = 10000,
|
2026-03-17 01:14:40 +00:00
|
|
|
|
) -> list[LocationRecord]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取设备轨迹 / Get device movement track within a time range.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
----------
|
|
|
|
|
|
db : AsyncSession
|
|
|
|
|
|
Database session.
|
|
|
|
|
|
device_id : int
|
|
|
|
|
|
Device ID.
|
|
|
|
|
|
start_time : datetime
|
|
|
|
|
|
Start of time range.
|
|
|
|
|
|
end_time : datetime
|
|
|
|
|
|
End of time range.
|
|
|
|
|
|
|
|
|
|
|
|
Returns
|
|
|
|
|
|
-------
|
|
|
|
|
|
list[LocationRecord]
|
|
|
|
|
|
Location records ordered by recorded_at ascending (chronological).
|
|
|
|
|
|
"""
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(LocationRecord)
|
|
|
|
|
|
.where(
|
|
|
|
|
|
LocationRecord.device_id == device_id,
|
|
|
|
|
|
LocationRecord.recorded_at >= start_time,
|
|
|
|
|
|
LocationRecord.recorded_at <= end_time,
|
|
|
|
|
|
)
|
|
|
|
|
|
.order_by(LocationRecord.recorded_at.asc())
|
Add batch management APIs, API security, rate limiting, and optimizations
- Batch device CRUD: POST /api/devices/batch (create 500), PUT /api/devices/batch (update 500),
POST /api/devices/batch-delete (delete 100) with WHERE IN bulk queries
- Batch command: POST /api/commands/batch with model_validator mutual exclusion
- API key auth (X-API-Key header, secrets.compare_digest timing-safe)
- Rate limiting via SlowAPIMiddleware (60/min default, 30/min writes)
- Real client IP extraction (X-Forwarded-For / CF-Connecting-IP)
- Global exception handler (no stack trace leaks, passes HTTPException through)
- CORS with auto-disable credentials on wildcard origins
- Schema validation: IMEI pattern, lat/lon ranges, Literal enums, MAC/UUID patterns
- Heartbeats router, per-ID endpoints for locations/attendance/bluetooth
- Input dedup in batch create, result ordering preserved
- Baidu reverse geocoding, Gaode map tiles with WGS84→GCJ02 conversion
- Device detail panel with feature toggles and command controls
- Side panel for location/beacon pages with auto-select active device
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-20 09:18:43 +00:00
|
|
|
|
.limit(max_points)
|
2026-03-17 01:14:40 +00:00
|
|
|
|
)
|
|
|
|
|
|
return list(result.scalars().all())
|