Appearance
心跳、指令与 ACK
心跳与在线判定
| 项 | 要求 |
|---|---|
| Topic | /{namespace}/v1/{DEVICE_SN}/heartbeat,QoS 0 |
| 周期 | 不超过 45 秒,建议 30 秒 |
timestamp | Unix 秒,每次取当前值,不得写死 |
| 时钟 | 设备须配置 NTP 校时 |
| 首帧 | 上线握手完成后立即发第一帧 |
- MQTT 协议层的
PINGREQ是保活,不能替代业务心跳;在线状态由业务心跳决定。 - 平台以 60 秒无心跳判离线;后台扫描周期约 30 秒,所以心跳超过 45 秒容易抖动。
心跳报文:
json
{
"message_id": "hb-001",
"timestamp": 1795224250,
"from_id": "{DEVICE_SN}",
"to_id": "{server_001}",
"type": "heartbeat",
"action": "heartbeat",
"data": {
"battery": 85,
"memory": 42,
"cpu": 18,
"temperature": 36,
"signal": -62,
"uptime": 86400
}
}指令处理四步
- 订阅
/{namespace}/v1/{DEVICE_SN}/command(QoS 1)。 - 校验下行报文:
to_id必须等于本机{DEVICE_ID}(或约定的 SN),不一致直接丢弃。 - 执行指令。
- 发布回执到
.../response,原样带回message_id(OTA 除外)。
成功 ACK:
json
{
"message_id": "<收到的指令 message_id>",
"timestamp": 1795224250,
"from_id": "{DEVICE_ID}",
"to_id": "{server_001}",
"type": "control",
"action": "<指令的 action>",
"ack": { "code": 200, "msg": "success" },
"data": {}
}ack.code | 含义 |
|---|---|
200 | 成功,业务成功的唯一判据 |
| 非 200 | 失败,ack.msg 写可读原因,如 sd not mounted、mode invalid |
type=control指令须在 10 秒内回执;耗时任务可先回ack.code=200表示受理,再用data.status=uploading/data.progress多次上报进度。- 平台侧表现:设备离线返回
409 device_offline,未及时回执返回504 command_timeout。
指令速查
下行(command)
| action | type | 关键 data |
|---|---|---|
real_video_on / real_video_off | control | 可选 resolution;开流附带 video_server/node_id |
real_audio_on / real_audio_off | control | — |
talk_on / talk_off | control | spk_voice_server(必填)、session_token、音视频地址 |
ptz | devices | dir、steps/laps、lens_index |
play_file / stop_play_file | control | file、folder、client_request_id |
tf_space / tf_dates / tf_hours / tf_files | control | 卡浏览 |
record_start / record_stop / snapshot | control | lens_index |
light_on / light_off / light_brightness / light_color_temp | control | channel / brightness / color_temp / tone |
night_mode / reboot / reset | control | — |
ota_upgrade | control | version/fileId/url/size/sha256 |
time_zone / server_info / media_server / cloud_package | devices | 索取类回包 |
上行(response)
| type | action | 关键字段 |
|---|---|---|
heartbeat | heartbeat | 运行指标 |
devices | info | sn/version/product/caps/product_model |
devices | presigned_upload | kind(event/video_temp)、file_name、file_size、content_type |
control | 同下行 action | message_id 一致 + ack:{code,msg} |
event | motion_alarm / device_anomaly / record_file / delete_folder | 见设备协议文档 11.5 |
OTA:特殊 ACK 规则
设备侧以 MQTT 下发的 ota_upgrade 为准;HTTP API 的 action=ota(data 透传)走普通 control ACK,固件优先实现前者。
json
{
"message_id": "<uuid>",
"type": "control",
"action": "ota_upgrade",
"data": {
"version": "1.2.3",
"fileId": "{FILE_ID}",
"url": "https://<OTA 域名>/firmware/{FILE_ID}",
"size": 8388608,
"sha256": "{SHA256}"
}
}流程:下载 → 校验 size 与 sha256 → 上报 ACK。
| 规则 | 说明 |
|---|---|
| ACK 位置 | 必须用 data.ack.{code,msg}(服务端只解析这里) |
message_id | 不要求与下行一致;服务端按 SN + 最近一条 Dispatched 任务匹配 |
| 成功 | data.ack.code = 200 → 任务进入 Downloaded(progress=100) |
| 失败 | 非 200 → 回 Pending 重试,或达 max_retry 后 Failed |
| 超时 | 不受 10 秒约束 |
ACK 语义
OTA 的 ACK 表示"已收到指令 + 下载已完成",不是安装/重启完成。当前协议没有增量进度上报。
成功:
json
{"message_id":"ota-ack-001","type":"control","action":"ota_upgrade","data":{"ack":{"code":200,"msg":"download completed"},"version":"1.2.3"}}失败(非 200 即可,msg 写清原因):
json
{"message_id":"ota-ack-002","type":"control","action":"ota_upgrade","data":{"ack":{"code":507,"msg":"insufficient storage"}}}