按设备回执确认音量调整完成
This commit is contained in:
@@ -138,6 +138,19 @@ class DeviceVolumeUpdateResponse(BaseModel):
|
||||
device_id: str
|
||||
level: int
|
||||
msg_id: str
|
||||
time: int
|
||||
status: str
|
||||
|
||||
|
||||
class DeviceVolumeCommandStatusResponse(BaseModel):
|
||||
device_id: str
|
||||
level: int
|
||||
msg_id: str
|
||||
time: int
|
||||
status: str
|
||||
current_level: int | None = None
|
||||
response_status: str | None = None
|
||||
error: str | None = None
|
||||
|
||||
|
||||
class DeviceSleepScheduleUpdateRequest(BaseModel):
|
||||
@@ -549,7 +562,7 @@ async def set_device_volume(
|
||||
request: Request,
|
||||
current_user_id: int = Depends(get_current_user_id),
|
||||
) -> DeviceVolumeUpdateResponse:
|
||||
msg_id = await device_service.set_device_volume(
|
||||
command_state = await device_service.set_device_volume(
|
||||
device_id=device_id,
|
||||
user_id=current_user_id,
|
||||
level=payload.level,
|
||||
@@ -563,10 +576,34 @@ async def set_device_volume(
|
||||
"user_id": current_user_id,
|
||||
"device_id": device_id,
|
||||
"level": payload.level,
|
||||
"msg_id": msg_id,
|
||||
"msg_id": command_state["msg_id"],
|
||||
"time": command_state["time"],
|
||||
"status": command_state["status"],
|
||||
},
|
||||
)
|
||||
return DeviceVolumeUpdateResponse(device_id=device_id, level=payload.level, msg_id=msg_id)
|
||||
return DeviceVolumeUpdateResponse(
|
||||
device_id=device_id,
|
||||
level=payload.level,
|
||||
msg_id=command_state["msg_id"],
|
||||
time=command_state["time"],
|
||||
status=command_state["status"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{device_id}/volume/{command_time}", response_model=DeviceVolumeCommandStatusResponse)
|
||||
async def get_device_volume_command_status(
|
||||
device_id: str,
|
||||
command_time: int,
|
||||
current_user_id: int = Depends(get_current_user_id),
|
||||
) -> DeviceVolumeCommandStatusResponse:
|
||||
command_state = await device_service.get_device_volume_command_status(
|
||||
device_id=device_id,
|
||||
user_id=current_user_id,
|
||||
command_time=command_time,
|
||||
)
|
||||
if not command_state:
|
||||
raise HTTPException(status_code=404, detail="volume command not found")
|
||||
return DeviceVolumeCommandStatusResponse(**command_state)
|
||||
|
||||
|
||||
@router.post("/{device_id}/sleep-schedule", response_model=DeviceSleepScheduleResponse)
|
||||
|
||||
@@ -55,8 +55,12 @@ async def set_volume(
|
||||
):
|
||||
await device_volume_manager.set_volume(req.device_id, req.level)
|
||||
service = await _get_service()
|
||||
msg_id = await service.send_volume_command(req.device_id, req.level)
|
||||
return CommandResponse(msg_id=msg_id, device_id=req.device_id)
|
||||
command_state = await service.send_volume_command(req.device_id, req.level)
|
||||
return CommandResponse(
|
||||
msg_id=command_state["msg_id"],
|
||||
device_id=req.device_id,
|
||||
time=command_state["time"],
|
||||
)
|
||||
|
||||
|
||||
@router.post("/ota", response_model=CommandResponse, summary="OTA升级")
|
||||
|
||||
@@ -59,6 +59,7 @@ class CommandResponse(BaseModel):
|
||||
message: str = "success"
|
||||
msg_id: str = Field(..., description="消息ID")
|
||||
device_id: str = Field(..., description="设备ID")
|
||||
time: Optional[int] = Field(None, description="命令时间戳")
|
||||
|
||||
|
||||
class DeviceResponseData(BaseModel):
|
||||
|
||||
@@ -185,7 +185,7 @@ class DeviceService(DatabaseServiceBase):
|
||||
device_id: str,
|
||||
user_id: int,
|
||||
level: int,
|
||||
) -> str:
|
||||
) -> Mapping[str, Any]:
|
||||
await self.ensure_device_access(device_id=device_id, user_id=user_id)
|
||||
|
||||
from handlers.mqtt_handler import TalkingQMQTTService
|
||||
@@ -195,6 +195,22 @@ class DeviceService(DatabaseServiceBase):
|
||||
raise HTTPException(status_code=503, detail="MQTT 服务未初始化")
|
||||
return await service.send_volume_command(device_id, level)
|
||||
|
||||
async def get_device_volume_command_status(
|
||||
self,
|
||||
*,
|
||||
device_id: str,
|
||||
user_id: int,
|
||||
command_time: int,
|
||||
) -> Mapping[str, Any] | None:
|
||||
await self.ensure_device_access(device_id=device_id, user_id=user_id)
|
||||
|
||||
from handlers.mqtt_handler import TalkingQMQTTService
|
||||
|
||||
service = await TalkingQMQTTService.get_instance()
|
||||
if service is None:
|
||||
raise HTTPException(status_code=503, detail="MQTT 服务未初始化")
|
||||
return await service.get_volume_command_status(device_id, command_time)
|
||||
|
||||
async def set_sleep_schedule(
|
||||
self,
|
||||
*,
|
||||
|
||||
@@ -46,21 +46,23 @@ device/TalkingQ_xxx/response
|
||||
2. 音量调节
|
||||
device/TalkingQ_xxx/command
|
||||
服务器下发:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
}
|
||||
}
|
||||
device/TalkingQ_xxx/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
}
|
||||
}
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
device/TalkingQ_xxx/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
|
||||
3. OTA升级
|
||||
device/TalkingQ_xxx/command
|
||||
|
||||
@@ -46,21 +46,23 @@ device/TalkingQ_xxx/response
|
||||
2. 音量调节
|
||||
device/TalkingQ_xxx/command
|
||||
服务器下发:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
}
|
||||
}
|
||||
device/TalkingQ_xxx/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
}
|
||||
}
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
device/TalkingQ_xxx/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
|
||||
3. OTA升级
|
||||
device/TalkingQ_xxx/command
|
||||
@@ -288,4 +290,4 @@ emqx_DPWQyk
|
||||
{
|
||||
"device_id": "TalkingQ_XQSN00001005",
|
||||
"url": "http://175.24.73.253:80/assets/audio/test_zh.mp3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,21 +46,23 @@ device/imei/response
|
||||
2. 音量调节
|
||||
device/imei/command
|
||||
服务器下发:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
}
|
||||
}
|
||||
device/imei/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
}
|
||||
}
|
||||
{
|
||||
"msg_id": "002",
|
||||
"params": {
|
||||
"level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
device/imei/response
|
||||
4G模块响应:
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {
|
||||
"current_level": 70
|
||||
},
|
||||
"time": 1717560000000
|
||||
}
|
||||
|
||||
3. OTA升级
|
||||
device/imei/command
|
||||
|
||||
@@ -26,6 +26,10 @@ from services.offline_audio_cache import offline_audio_cache
|
||||
from services.task_manager import task_manager
|
||||
from utils.logger import session_logger as logger
|
||||
|
||||
VOLUME_COMMAND_TIMEOUT_MS = 10 * 1000
|
||||
VOLUME_COMMAND_RETENTION_MS = 10 * 60 * 1000
|
||||
|
||||
|
||||
class TalkingQMQTTService:
|
||||
_instance = None
|
||||
_lock = asyncio.Lock()
|
||||
@@ -44,6 +48,9 @@ class TalkingQMQTTService:
|
||||
self._connected = False
|
||||
self._connect_lock = asyncio.Lock()
|
||||
self._message_task: Optional[asyncio.Task] = None
|
||||
self._volume_command_lock = asyncio.Lock()
|
||||
self._volume_command_states: Dict[tuple[str, int], dict] = {}
|
||||
self._last_volume_command_time_ms = 0
|
||||
|
||||
self._msg_handlers: Dict[str, Callable[[str, dict], Awaitable[None]]] = {
|
||||
"000": self._handle_device_info,
|
||||
@@ -201,15 +208,38 @@ class TalkingQMQTTService:
|
||||
)
|
||||
|
||||
async def _handle_volume_response(self, device_id: str, payload: dict):
|
||||
command_time = self._parse_command_time(payload.get("time"))
|
||||
if payload.get("status") != "success":
|
||||
await self._mark_volume_command_response(
|
||||
device_id=device_id,
|
||||
command_time=command_time,
|
||||
status="failed",
|
||||
response_status=payload.get("status"),
|
||||
error=str(payload),
|
||||
)
|
||||
logger.warning(device_id, "", f"[volume] command failed: {payload}")
|
||||
return
|
||||
|
||||
data = payload.get("data", {})
|
||||
current_level = data.get("current_level")
|
||||
if current_level is None:
|
||||
await self._mark_volume_command_response(
|
||||
device_id=device_id,
|
||||
command_time=command_time,
|
||||
status="failed",
|
||||
response_status=payload.get("status"),
|
||||
error="missing current_level",
|
||||
)
|
||||
return
|
||||
logger.info(device_id, "", f"[音量] 设备 {device_id} 当前音量: {current_level}")
|
||||
await self._mark_volume_command_response(
|
||||
device_id=device_id,
|
||||
command_time=command_time,
|
||||
status="completed",
|
||||
current_level=current_level,
|
||||
response_status=payload.get("status"),
|
||||
error=None,
|
||||
)
|
||||
|
||||
await self._schedule_persistence(
|
||||
device_id,
|
||||
@@ -617,12 +647,113 @@ class TalkingQMQTTService:
|
||||
await self._publish(f"device/{device_id}/command", {"msg_id": "001"})
|
||||
return "001"
|
||||
|
||||
async def send_volume_command(self, device_id: str, level: int) -> str:
|
||||
await self._publish(
|
||||
def _parse_command_time(self, value) -> int | None:
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
command_time = int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return command_time if command_time > 0 else None
|
||||
|
||||
def _current_utc_timestamp_ms(self) -> int:
|
||||
return int(time.time() * 1000)
|
||||
|
||||
async def _next_volume_command_time(self) -> int:
|
||||
async with self._volume_command_lock:
|
||||
command_time = max(self._current_utc_timestamp_ms(), self._last_volume_command_time_ms + 1)
|
||||
self._last_volume_command_time_ms = command_time
|
||||
return command_time
|
||||
|
||||
async def _record_volume_command(self, *, device_id: str, level: int, command_time: int) -> dict:
|
||||
state = {
|
||||
"device_id": device_id,
|
||||
"msg_id": "002",
|
||||
"level": level,
|
||||
"time": command_time,
|
||||
"status": "sent",
|
||||
"current_level": None,
|
||||
"response_status": None,
|
||||
"error": None,
|
||||
"created_at": command_time,
|
||||
"updated_at": command_time,
|
||||
}
|
||||
async with self._volume_command_lock:
|
||||
self._volume_command_states[(device_id, command_time)] = dict(state)
|
||||
self._cleanup_volume_command_states_locked()
|
||||
return state
|
||||
|
||||
def _cleanup_volume_command_states_locked(self):
|
||||
cutoff = self._current_utc_timestamp_ms() - VOLUME_COMMAND_RETENTION_MS
|
||||
expired_keys = [
|
||||
key
|
||||
for key, state in self._volume_command_states.items()
|
||||
if int(state.get("updated_at") or state.get("created_at") or 0) < cutoff
|
||||
]
|
||||
for key in expired_keys:
|
||||
self._volume_command_states.pop(key, None)
|
||||
|
||||
async def _mark_volume_command_response(
|
||||
self,
|
||||
*,
|
||||
device_id: str,
|
||||
command_time: int | None,
|
||||
status: str,
|
||||
current_level=None,
|
||||
response_status=None,
|
||||
error: str | None,
|
||||
) -> None:
|
||||
if command_time is None:
|
||||
return
|
||||
async with self._volume_command_lock:
|
||||
state = self._volume_command_states.get((device_id, command_time))
|
||||
if not state:
|
||||
return
|
||||
state.update(
|
||||
{
|
||||
"status": status,
|
||||
"current_level": current_level,
|
||||
"response_status": response_status,
|
||||
"error": error,
|
||||
"updated_at": self._current_utc_timestamp_ms(),
|
||||
}
|
||||
)
|
||||
|
||||
async def get_volume_command_status(self, device_id: str, command_time: int) -> dict | None:
|
||||
async with self._volume_command_lock:
|
||||
self._cleanup_volume_command_states_locked()
|
||||
state = self._volume_command_states.get((device_id, command_time))
|
||||
if state and state.get("status") == "sent":
|
||||
now = self._current_utc_timestamp_ms()
|
||||
if now - int(state.get("created_at") or command_time) > VOLUME_COMMAND_TIMEOUT_MS:
|
||||
state.update(
|
||||
{
|
||||
"status": "timeout",
|
||||
"error": "device response timeout",
|
||||
"updated_at": now,
|
||||
}
|
||||
)
|
||||
return dict(state) if state else None
|
||||
|
||||
async def send_volume_command(self, device_id: str, level: int) -> dict:
|
||||
command_time = await self._next_volume_command_time()
|
||||
state = await self._record_volume_command(device_id=device_id, level=level, command_time=command_time)
|
||||
published = await self._publish(
|
||||
f"device/{device_id}/command",
|
||||
{"msg_id": "002", "params": {"level": level}},
|
||||
{"msg_id": "002", "params": {"level": level}, "time": command_time},
|
||||
)
|
||||
return "002"
|
||||
if not published:
|
||||
await self._mark_volume_command_response(
|
||||
device_id=device_id,
|
||||
command_time=command_time,
|
||||
status="failed",
|
||||
response_status=None,
|
||||
error="publish failed",
|
||||
)
|
||||
state["status"] = "failed"
|
||||
state["error"] = "publish failed"
|
||||
state["updated_at"] = self._current_utc_timestamp_ms()
|
||||
return state
|
||||
|
||||
async def send_ota_command(self, device_id: str, url: str, version: str) -> str:
|
||||
await self._publish(
|
||||
|
||||
98
talkingq-url/tests/test_volume_command_status.py
Normal file
98
talkingq-url/tests/test_volume_command_status.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import pytest
|
||||
|
||||
from handlers.mqtt_handler import TalkingQMQTTService
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_includes_time_and_completes_on_matching_response(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
published = []
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
published.append((topic, payload))
|
||||
return True
|
||||
|
||||
async def fake_schedule_persistence(device_id, label, coro):
|
||||
del device_id, label
|
||||
coro.close()
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_schedule_persistence", fake_schedule_persistence)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: 1717560000000)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
|
||||
assert command["msg_id"] == "002"
|
||||
assert command["time"] == 1717560000000
|
||||
assert command["status"] == "sent"
|
||||
assert published == [
|
||||
(
|
||||
"device/TalkingQ_XQSN00001005/command",
|
||||
{"msg_id": "002", "params": {"level": 70}, "time": 1717560000000},
|
||||
)
|
||||
]
|
||||
|
||||
await service._handle_volume_response(
|
||||
"TalkingQ_XQSN00001005",
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {"current_level": 70},
|
||||
"time": 1717560000000,
|
||||
},
|
||||
)
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", 1717560000000)
|
||||
assert status["status"] == "completed"
|
||||
assert status["current_level"] == 70
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_response_without_time_does_not_complete_command(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
del topic, payload
|
||||
return True
|
||||
|
||||
async def fake_schedule_persistence(device_id, label, coro):
|
||||
del device_id, label
|
||||
coro.close()
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_schedule_persistence", fake_schedule_persistence)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: 1717560000000)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
await service._handle_volume_response(
|
||||
"TalkingQ_XQSN00001005",
|
||||
{
|
||||
"msg_id": "002",
|
||||
"status": "success",
|
||||
"data": {"current_level": 70},
|
||||
},
|
||||
)
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", command["time"])
|
||||
assert status["status"] == "sent"
|
||||
assert status["current_level"] is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_volume_command_status_times_out(monkeypatch):
|
||||
service = TalkingQMQTTService({"broker": "127.0.0.1"})
|
||||
now = 1717560000000
|
||||
|
||||
async def fake_publish(topic, payload):
|
||||
del topic, payload
|
||||
return True
|
||||
|
||||
monkeypatch.setattr(service, "_publish", fake_publish)
|
||||
monkeypatch.setattr(service, "_current_utc_timestamp_ms", lambda: now)
|
||||
|
||||
command = await service.send_volume_command("TalkingQ_XQSN00001005", 70)
|
||||
now += 10001
|
||||
|
||||
status = await service.get_volume_command_status("TalkingQ_XQSN00001005", command["time"])
|
||||
assert status["status"] == "timeout"
|
||||
assert status["error"] == "device response timeout"
|
||||
Reference in New Issue
Block a user