e66f7fed8c
- wx_poc_signal 进程内存信号(device_id→source), 只对 WX_POC_TEST_DEVICE_ID 生效 - 收图 image → set_pending(测试设备, meituan) - POST /device/heartbeat 返回 HeartbeatResponse, 该设备有信号则带回 pending_compare 并清除 - 心跳 schema OkResponse→HeartbeatResponse, 老前端不读 body 向后兼容 - WX_POC_TEST_DEVICE_ID 空=不触发任何设备(部署零风险, 待填测试机 id) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
"""设备注册 / 心跳 endpoint(无障碍保护存活检测)。
|
|
|
|
路由前缀 /api/v1/device,需 Bearer 鉴权(设备绑登录用户)。
|
|
POST /register 注册设备 / 更新 registration_id(App 前台、拿到 push token 时调)
|
|
POST /heartbeat 上报心跳(无障碍服务存活时周期调,刷新存活)
|
|
|
|
后端 heartbeat_monitor_worker 据此发现心跳超时的设备并极光推送告警。
|
|
见 spec: spec/accessibility-liveness-push.md。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from app.api.deps import CurrentUser, DbSession
|
|
from app.core import wx_poc_signal
|
|
from app.repositories import device as device_repo
|
|
from app.schemas.device import (
|
|
DeviceOut,
|
|
DeviceRegisterRequest,
|
|
HeartbeatRequest,
|
|
HeartbeatResponse,
|
|
LivenessAckRequest,
|
|
LivenessOut,
|
|
OkResponse,
|
|
PendingCompare,
|
|
)
|
|
|
|
logger = logging.getLogger("shagua.device")
|
|
|
|
# PoC:源平台代号 → Android 包名(前端 launch 用)。当前只用美团。
|
|
_SOURCE_PACKAGES = {"meituan": "com.sankuai.meituan"}
|
|
|
|
router = APIRouter(prefix="/api/v1/device", tags=["device"])
|
|
|
|
|
|
@router.post("/register", response_model=DeviceOut, summary="注册设备/更新推送token")
|
|
def register_device(
|
|
req: DeviceRegisterRequest,
|
|
user: CurrentUser,
|
|
db: DbSession,
|
|
) -> DeviceOut:
|
|
device = device_repo.register_or_update(
|
|
db,
|
|
user_id=user.id,
|
|
device_id=req.device_id,
|
|
registration_id=req.registration_id,
|
|
platform=req.platform,
|
|
app_version=req.app_version,
|
|
)
|
|
logger.info(
|
|
"device register user_id=%d device_id=%s reg=%s",
|
|
user.id,
|
|
req.device_id,
|
|
bool(req.registration_id),
|
|
)
|
|
return DeviceOut.model_validate(device)
|
|
|
|
|
|
@router.post("/heartbeat", response_model=HeartbeatResponse, summary="上报心跳")
|
|
def report_heartbeat(
|
|
req: HeartbeatRequest,
|
|
user: CurrentUser,
|
|
db: DbSession,
|
|
) -> HeartbeatResponse:
|
|
device_repo.touch_heartbeat(
|
|
db,
|
|
user_id=user.id,
|
|
device_id=req.device_id,
|
|
accessibility_enabled=req.accessibility_enabled,
|
|
registration_id=req.registration_id,
|
|
)
|
|
# PoC:该设备有"待比价"信号则带回(只对写死的测试设备生效, 取走即清、只弹一次)
|
|
source = wx_poc_signal.pop_pending(req.device_id)
|
|
pending = None
|
|
if source:
|
|
pending = PendingCompare(
|
|
source_platform=source,
|
|
source_package=_SOURCE_PACKAGES.get(source, ""),
|
|
)
|
|
logger.info("heartbeat 下发比价信号 device=%s source=%s", req.device_id, source)
|
|
return HeartbeatResponse(pending_compare=pending)
|
|
|
|
|
|
@router.get("/liveness", response_model=LivenessOut, summary="查询本机掉线告警(后置检测)")
|
|
def get_liveness(
|
|
device_id: str,
|
|
user: CurrentUser,
|
|
db: DbSession,
|
|
) -> LivenessOut:
|
|
"""客户端进 App 时拉取: 本机是否被判定掉线过(kill_alert_pending)。
|
|
|
|
设备从未注册过 → 返回默认(无告警)。命中 → 客户端弹「开启自启动」引导, 之后调 /liveness/ack 清。
|
|
见 spec: spec/accessibility-liveness-pull-prompt.md。
|
|
"""
|
|
device = device_repo.get_device(db, user_id=user.id, device_id=device_id)
|
|
if device is None:
|
|
return LivenessOut()
|
|
return LivenessOut.model_validate(device)
|
|
|
|
|
|
@router.post("/liveness/ack", response_model=OkResponse, summary="确认已提醒(清掉线告警)")
|
|
def ack_liveness(
|
|
req: LivenessAckRequest,
|
|
user: CurrentUser,
|
|
db: DbSession,
|
|
) -> OkResponse:
|
|
"""客户端已弹过「开启自启动」引导 → 清掉「待提醒」标记(下次真掉线 worker 再置)。"""
|
|
device_repo.ack_kill_alert(db, user_id=user.id, device_id=req.device_id)
|
|
return OkResponse()
|