版本:V1.0 | Base URL:https://iot.lv.hzbjzn.com | 鉴权:X-Admin-Token
1. 通用规则
- 除
GET /api/health外,所有/api/*都要带管理令牌。 - 请求和响应均为 JSON;
Content-Type: application/json。 - 数据库时间字段是 UTC ISO8601 字符串,例如
2026-11-22T08:00:00Z。 - 设备命令签名里的
ts是毫秒时间戳。 - 错误统一返回
{"detail":"..."};HTTP 状态码见第 8 节。 - 管理令牌只放服务端或可信后台,不写进浏览器 localStorage、APK 或公开文档。
请求头:
X-Admin-Token: <TYG4_ADMIN_TOKEN>
Content-Type: application/json
2. 健康检查
GET /api/health
无需鉴权。返回:
{"ok": true, "mqtt": true, "ts": 1760000000}
mqtt 表示平台 MQTT 客户端已连接 Broker,不代表某台设备在线。
3. 概览
GET /api/stats
返回:
{"devices": 1, "online": 1, "commands": 3, "acked": 2, "errors": 1}
errors 统计 failed 和 timeout 命令。
4. 设备
4.1 设备列表
GET /api/devices
返回 items 数组:
{
"items": [
{
"id": 1,
"sn": "TYG4-250001",
"name": "出口闸机",
"site": "台州测试现场",
"model": "TY-G4",
"status": "active",
"firmware": "1.0.0",
"ip": "10.1.2.3",
"online": 1,
"last_seen": "2026-11-22T08:00:00Z",
"last_state": {"type": "heartbeat", "relay": [0, 0, 0, 0]},
"note": "现场调试",
"created_at": "2026-11-22T07:00:00Z",
"updated_at": "2026-11-22T08:00:00Z",
"channels": [
{"channel": 1, "name": "开闸", "action": "open", "pulse_ms": 800}
]
}
]
}
token_hash、secret_enc 已从响应中移除。
4.2 新建设备
POST /api/devices
请求:
{
"sn": "TYG4-250001",
"name": "出口闸机",
"site": "台州测试现场",
"model": "TY-G4",
"firmware": "1.0.0",
"note": "现场调试",
"channels": [
{"channel": 1, "name": "开闸", "action": "open", "pulse_ms": 800},
{"channel": 2, "name": "关闸", "action": "close", "pulse_ms": 800},
{"channel": 3, "name": "停止", "action": "stop", "pulse_ms": 800},
{"channel": 4, "name": "备用", "action": "", "pulse_ms": 800}
]
}
sn 可省略,平台随机生成;显式传入时必须是 TYG4-XXXXXX,XXXXXX 为 6 位大写字母/数字。返回:
{
"device": {"sn": "TYG4-250001", "status": "active", "...": "..."},
"token": "<DEVICE_TOKEN>",
"connect": {
"host": "iot.lv.hzbjzn.com",
"port": 8883,
"tls": true,
"client_id": "ty-g4-TYG4-250001",
"username": "TYG4-250001",
"password": "<DEVICE_TOKEN>",
"topics": {
"up": "v1/dev/TYG4-250001/up",
"down": "v1/dev/TYG4-250001/down",
"ota": "v1/dev/TYG4-250001/ota"
},
"qos": 1,
"keepalive": 60,
"lwt": {
"topic": "v1/dev/TYG4-250001/up",
"payload": {"v": 1, "type": "lwt", "sn": "TYG4-250001", "status": "offline"}
}
}
}
设备令牌只返回一次;丢失用 POST /api/devices/{sn}/token/rotate 轮换。重复创建同一 SN 返回 409。
4.3 设备详情
GET /api/devices/{sn}
返回单个设备对象,字段同上,含 channels。
4.4 修改设备
PATCH /api/devices/{sn}
可改字段:name、site、model、status、note、firmware。sn 不可改。
{"name": "入口闸机", "status": "active"}
注意:update_device 返回的设备对象不含 channels(代码当前实现);需要通道时再查 GET /api/devices/{sn}。
4.5 轮换设备令牌
POST /api/devices/{sn}/token/rotate
旧 token 立即失效;设备需要用新 token 重连。返回:
{"token": "<NEW_DEVICE_TOKEN>", "connect": {"...": "..."}}
4.6 设备连接参数
GET /api/devices/{sn}/connect
返回 host/port/tls/client_id/username/password/topics/qos/keepalive/lwt。其中 password 是占位字符串,不是真实 token;真实 token 只在创建/轮换时返回。
5. 命令
5.1 下发命令
POST /api/devices/{sn}/command
请求:
{
"channel": 1,
"action": "open",
"pulse_ms": 800,
"operator": "岗亭A",
"request_id": "gateA-open-20261122-001"
}
字段:
| 字段 | 必填 | 说明 |
|---|---|---|
channel |
否 | 1–4,默认 1 |
action |
是 | open / close / stop |
pulse_ms |
否 | 100–5000,默认 800 |
operator |
否 | 操作人/岗位,写审计,默认 web |
request_id |
否 | 1–64 位,字母数字 : _ -;同时作为命令主键 |
返回:
{
"command": {
"id": "gateA-open-20261122-001",
"sn": "TYG4-250001",
"channel": 1,
"action": "open",
"pulse_ms": 800,
"payload": "{\"v\":1,\"type\":\"cmd\",...}",
"status": "sent",
"result": "",
"attempts": 1,
"operator": "岗亭A",
"created_at": "2026-11-22T08:00:00Z",
"sent_at": "2026-11-22T08:00:01Z",
"acked_at": null
},
"sent": true,
"duplicate": false
}
- 设备在线:立即发布
down,status=sent。 - 设备离线:
sent=false,status=queued,最多排队 30 秒;上线后只发队列内未过期命令。 - 同一
request_id重复提交:返回原命令,duplicate=true,不重发。 - 同一
request_id已被其他设备/通道/动作占用:409。 - 设备已禁用:
409;通道不存在:400。
5.2 查询命令
GET /api/commands?sn={sn}&limit=50
返回 {"items":[...]},按 created_at 倒序;limit 1–200。命令状态:
queued → sent → acked;失败为 failed;12 秒无 ack 为 timeout;30 秒未发出为 expired。
6. 事件
GET /api/events?sn={sn}&limit=100
返回 {"items":[...]},按 id 倒序;limit 1–200。事件字段:
{
"id": 10,
"sn": "TYG4-250001",
"type": "heartbeat",
"payload": {"type": "heartbeat", "relay": [0, 0, 0, 0]},
"created_at": "2026-11-22T08:00:00Z"
}
7. OTA
POST /api/devices/{sn}/ota
请求:
{
"version": "1.0.1",
"url": "https://iot.lv.hzbjzn.com/ota/tyg4-1.0.1.bin",
"sha256": "<64位SHA256>"
}
返回:
{"task": {"id": "<32位hex>", "sn": "TYG4-250001", "version": "1.0.1", "status": "queued", "...": "..."}, "sent": true}
平台发布到 v1/dev/{SN}/ota。设备下载、校验签名、双分区升级、失败回滚。OTA 包必须由腾域私钥签名,平台只存公钥。
8. 状态码
| 状态码 | 场景 |
|---|---|
| 200 | 成功 |
| 400 | 参数错误、SN 格式错、通道不存在 |
| 401 | 管理令牌缺失或错误 |
| 404 | 设备不存在 |
| 409 | SN 已存在、设备已禁用、request_id 冲突 |
| 500 | 数据库/ Broker 内部错误、管理令牌未配置 |
9. curl 示例
# 健康检查
curl -sS https://iot.lv.hzbjzn.com/api/health
# 设备列表
curl -sS https://iot.lv.hzbjzn.com/api/devices \
-H "X-Admin-Token: $TYG4_ADMIN_TOKEN"
# 开闸
curl -sS -X POST https://iot.lv.hzbjzn.com/api/devices/TYG4-250001/command \
-H "X-Admin-Token: $TYG4_ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{"channel":1,"action":"open","pulse_ms":800,"request_id":"gateA-open-001"}'
10. 与规格书的差异
规格书 5.6 列出 /api/iot/* 四个接口,是面向停车 SaaS 的规划接口;当前 V1.0 平台实际提供本文档中的 /api/* 管理接口。停车 SaaS 对接时按本文档实现,或由平台增加 /api/iot/* 适配层。设备 ack 实际字段为 ok / error,不是规格书示例中的 code。