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()
|
|
|
|
|
|
|
|
|
|
|
|
|
Add WebSocket, multi API key, geocoding proxy, beacon map picker, and comprehensive bug fixes
- Multi API Key + permission system (read/write/admin) with SHA-256 hash
- WebSocket real-time push (location, alarm, device_status, attendance, bluetooth)
- Geocoding proxy endpoints for Amap POI search and reverse geocode
- Beacon modal map-based location picker with search and click-to-select
- GCJ-02 ↔ WGS-84 bidirectional coordinate conversion
- Data cleanup scheduler (configurable retention days)
- Fix GPS longitude sign inversion (course_status bit 11: 0=East, 1=West)
- Fix 2G CellID 2→3 bytes across all protocols (0x28, 0x2C, parser.py)
- Fix parser loop guards, alarm_source field length, CommandLog.sent_at
- Fix geocoding IMEI parameterization, require_admin import
- Improve API error messages for 422 validation errors
- Remove beacon floor/area fields (consolidated into name)
via [HAPI](https://hapi.run)
Co-Authored-By: HAPI <noreply@hapi.run>
2026-03-24 05:10:05 +00:00
|
|
|
|
async def get_batch_latest_locations(
|
|
|
|
|
|
db: AsyncSession, device_ids: list[int]
|
|
|
|
|
|
) -> list[LocationRecord]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
批量获取多设备最新位置 / Get the most recent location for each device in the list.
|
|
|
|
|
|
|
|
|
|
|
|
Uses a subquery with MAX(id) GROUP BY device_id for efficiency.
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not device_ids:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
# Subquery: max id per device_id
|
|
|
|
|
|
subq = (
|
|
|
|
|
|
select(func.max(LocationRecord.id).label("max_id"))
|
|
|
|
|
|
.where(LocationRecord.device_id.in_(device_ids))
|
|
|
|
|
|
.group_by(LocationRecord.device_id)
|
|
|
|
|
|
.subquery()
|
|
|
|
|
|
)
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(LocationRecord).where(LocationRecord.id.in_(select(subq.c.max_id)))
|
|
|
|
|
|
)
|
|
|
|
|
|
return list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_all_online_latest_locations(
|
|
|
|
|
|
db: AsyncSession,
|
|
|
|
|
|
) -> list[LocationRecord]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
获取所有在线设备的最新位置 / Get latest location for all online devices.
|
|
|
|
|
|
"""
|
|
|
|
|
|
from app.models import Device
|
|
|
|
|
|
|
|
|
|
|
|
# Get online device IDs
|
|
|
|
|
|
online_result = await db.execute(
|
|
|
|
|
|
select(Device.id).where(Device.status == "online")
|
|
|
|
|
|
)
|
|
|
|
|
|
online_ids = [row[0] for row in online_result.all()]
|
|
|
|
|
|
if not online_ids:
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
return await get_batch_latest_locations(db, online_ids)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-17 01:14:40 +00:00
|
|
|
|
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())
|