修复代码, 现在可以正常接收平台心跳包并返回设备状态
This commit is contained in:
99
RELAY_API.md
99
RELAY_API.md
@@ -103,6 +103,29 @@ ws://[server_address]:[port]/ws/device?device_id=[device_id]
|
||||
- `command`: 指令名称,固定为"query_all_device_status"
|
||||
- `timestamp`: 指令发送时间
|
||||
|
||||
### 3.4 心跳包指令
|
||||
|
||||
平台向中继设备发送心跳包指令,用于检测设备连接状态并获取下级设备状态信息。
|
||||
|
||||
**请求格式**
|
||||
```json
|
||||
{
|
||||
"type": "command",
|
||||
"command": "heartbeat",
|
||||
"data": {
|
||||
"timestamp": 1672545600
|
||||
},
|
||||
"timestamp": "2023-01-01T12:00:00Z"
|
||||
}
|
||||
```
|
||||
|
||||
**参数说明**
|
||||
- `type`: 消息类型,固定为"command"
|
||||
- `command`: 指令名称,固定为"heartbeat"
|
||||
- `data`: 指令数据
|
||||
- `timestamp`: 时间戳(Unix时间戳格式)
|
||||
- `timestamp`: 指令发送时间
|
||||
|
||||
## 4. 响应接口
|
||||
|
||||
### 4.1 设备控制响应
|
||||
@@ -167,6 +190,48 @@ ws://[server_address]:[port]/ws/device?device_id=[device_id]
|
||||
}
|
||||
```
|
||||
|
||||
### 4.4 心跳包响应
|
||||
|
||||
中继设备响应心跳包指令,返回自身及下级设备的状态信息。
|
||||
|
||||
**响应格式**
|
||||
```json
|
||||
{
|
||||
"type": "response",
|
||||
"command": "heartbeat",
|
||||
"data": {
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "relay-001",
|
||||
"device_type": "relay",
|
||||
"status": "running"
|
||||
},
|
||||
{
|
||||
"device_id": "fan-001",
|
||||
"device_type": "fan",
|
||||
"status": "running"
|
||||
},
|
||||
{
|
||||
"device_id": "curtain-001",
|
||||
"device_type": "water_curtain",
|
||||
"status": "stopped"
|
||||
}
|
||||
]
|
||||
},
|
||||
"timestamp": "2023-01-01T12:00:05Z"
|
||||
}
|
||||
```
|
||||
|
||||
**参数说明**
|
||||
- `type`: 消息类型,固定为"response"
|
||||
- `command`: 指令名称,固定为"heartbeat"
|
||||
- `data`: 响应数据
|
||||
- `devices`: 设备列表
|
||||
- `device_id`: 设备唯一标识符
|
||||
- `device_type`: 设备类型
|
||||
- `status`: 设备状态(如: running, stopped, online, offline等)
|
||||
- `timestamp`: 平台发送的时间戳, 需要原封不动的返回
|
||||
|
||||
## 5. 请求-响应机制
|
||||
|
||||
平台在发送指令后会等待中继设备的响应,超时时间由配置文件决定,默认为5秒。
|
||||
@@ -206,6 +271,8 @@ websocket:
|
||||
|
||||
## 7. 响应结构定义
|
||||
|
||||
平台提供统一的响应结构定义,用于处理中继设备返回的响应:
|
||||
|
||||
### 7.1 CommandResponse 结构体
|
||||
|
||||
```go
|
||||
@@ -230,7 +297,31 @@ type CommandResponse struct {
|
||||
}
|
||||
```
|
||||
|
||||
### 7.2 响应处理规则
|
||||
### 7.2 ParseData 方法
|
||||
|
||||
CommandResponse结构体提供了ParseData方法,用于将响应数据解析到指定的结构体中:
|
||||
|
||||
```go
|
||||
func (cr *CommandResponse) ParseData(target interface{}) error
|
||||
```
|
||||
|
||||
使用示例:
|
||||
```go
|
||||
// 定义目标结构体
|
||||
type DeviceStatus struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// 解析响应数据
|
||||
var status DeviceStatus
|
||||
if err := response.ParseData(&status); err != nil {
|
||||
// 处理错误
|
||||
}
|
||||
```
|
||||
|
||||
### 7.3 响应处理规则
|
||||
|
||||
1. `Status` 字段:表示操作的整体状态,如 "success"、"failed" 等
|
||||
2. `Message` 字段:提供人类可读的操作结果描述
|
||||
@@ -251,6 +342,7 @@ type CommandResponse struct {
|
||||
|------|------|
|
||||
| fan | 风机设备 |
|
||||
| water_curtain | 水帘设备 |
|
||||
| relay | 中继设备 |
|
||||
|
||||
## 10. 动作说明
|
||||
|
||||
@@ -266,4 +358,7 @@ type CommandResponse struct {
|
||||
| success | 操作成功 |
|
||||
| failed | 操作失败 |
|
||||
| running | 设备运行中 |
|
||||
| stopped | 设备已停止 |
|
||||
| stopped | 设备已停止 |
|
||||
| online | 设备在线 |
|
||||
| offline | 设备离线 |
|
||||
| active | 设备激活 |
|
||||
Reference in New Issue
Block a user