@thejrsoft/subway-protocol 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ACK_MESSAGES_IMPLEMENTATION_SUMMARY.md +128 -0
- package/ACK_MESSAGE_DESIGN.md +457 -0
- package/CHANGELOG.md +58 -0
- package/COMMAND_VALIDATION_RULES.md +178 -0
- package/DOCUMENTATION_REORGANIZATION_SUMMARY.md +81 -0
- package/DOCUMENTATION_STRUCTURE.md +106 -0
- package/GATEWAY_MIGRATION_GUIDE.md +130 -0
- package/GATEWAY_PROTOCOL_COMPARISON.md +216 -0
- package/INTEGRATION_GUIDE.md +190 -0
- package/OPTIONAL_FIELDS_WITHOUT_DEFAULTS.md +97 -0
- package/PROTOCOL_UTILS_USAGE.md +278 -0
- package/README.md +237 -0
- package/TYPE_FIXES_SUMMARY.md +210 -0
- package/UPDATE_ENUM_VALUES.md +139 -0
- package/dist/asyncapi-sync.d.ts +47 -0
- package/dist/asyncapi-sync.d.ts.map +1 -0
- package/dist/asyncapi-sync.js +85 -0
- package/dist/asyncapi-sync.js.map +1 -0
- package/dist/command-factory.d.ts +62 -0
- package/dist/command-factory.d.ts.map +1 -0
- package/dist/command-factory.js +137 -0
- package/dist/command-factory.js.map +1 -0
- package/dist/command-types.d.ts +27 -0
- package/dist/command-types.d.ts.map +1 -0
- package/dist/command-types.js +31 -0
- package/dist/command-types.js.map +1 -0
- package/dist/index.d.ts +403 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +413 -0
- package/dist/index.js.map +1 -0
- package/dist/message-validator.d.ts +102 -0
- package/dist/message-validator.d.ts.map +1 -0
- package/dist/message-validator.js +640 -0
- package/dist/message-validator.js.map +1 -0
- package/dist/protocol-utils.d.ts +108 -0
- package/dist/protocol-utils.d.ts.map +1 -0
- package/dist/protocol-utils.js +293 -0
- package/dist/protocol-utils.js.map +1 -0
- package/docs/01-protocol/README.md +45 -0
- package/docs/01-protocol/design-rationale.md +198 -0
- package/docs/01-protocol/message-types.md +669 -0
- package/docs/01-protocol/specification.md +1466 -0
- package/docs/02-commands/README.md +56 -0
- package/docs/02-commands/batch-command.md +435 -0
- package/docs/02-commands/complex-command.md +537 -0
- package/docs/02-commands/simple-command.md +332 -0
- package/docs/02-commands/typed-commands.md +362 -0
- package/docs/03-architecture/README.md +66 -0
- package/docs/03-architecture/device-protocol.md +430 -0
- package/docs/03-architecture/edge-proxy.md +727 -0
- package/docs/03-architecture/routing-flow.md +893 -0
- package/docs/04-integration/README.md +144 -0
- package/docs/04-integration/backend-guide.md +551 -0
- package/docs/04-integration/edge-guide.md +684 -0
- package/docs/04-integration/gateway-guide.md +180 -0
- package/docs/04-integration/migration-guide.md +226 -0
- package/docs/05-examples/README.md +141 -0
- package/docs/05-examples/progress-update-examples.md +757 -0
- package/docs/06-reference/README.md +67 -0
- package/docs/06-reference/api.md +572 -0
- package/docs/06-reference/faq.md +302 -0
- package/docs/06-reference/glossary.md +232 -0
- package/examples/backend-upgrade.ts +279 -0
- package/examples/edge-multi-device.ts +513 -0
- package/examples/gateway-upgrade.ts +150 -0
- package/examples/protocol-implementation.ts +715 -0
- package/package.json +48 -0
- package/scripts/validate-asyncapi.ts +78 -0
- package/src/__tests__/protocol.test.ts +297 -0
- package/src/asyncapi-sync.ts +84 -0
- package/src/command-factory.ts +183 -0
- package/src/command-types.ts +72 -0
- package/src/edge-proxy.ts +494 -0
- package/src/gateway-extensions.ts +278 -0
- package/src/index.ts +792 -0
- package/src/message-validator.ts +726 -0
- package/src/protocol-utils.ts +355 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
# Complex 命令执行流程详解
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
Complex 类型命令是 JRSoft Subway 协议中用于处理需要持续响应的场景,如健康检查、监播数据导出、系统日志收集等。
|
|
6
|
+
|
|
7
|
+
### 核心特性
|
|
8
|
+
|
|
9
|
+
1. **持续响应** - 设备可以在执行过程中发送多个 `progress_update` 消息
|
|
10
|
+
2. **阶段报告** - 每个进度更新可以报告不同阶段的结果
|
|
11
|
+
3. **结构化日志** - 使用 ReportMessage 提供不同级别的日志信息
|
|
12
|
+
4. **最终结束** - 必须发送 `command_response` 标志命令执行结束
|
|
13
|
+
|
|
14
|
+
### 适用场景
|
|
15
|
+
|
|
16
|
+
- **健康检查** - 逐步检查各个子系统状态
|
|
17
|
+
- **监播数据导出** - 分批导出大量数据
|
|
18
|
+
- **系统日志收集** - 持续收集和报告日志
|
|
19
|
+
- **配置同步** - 逐步同步多个设备配置
|
|
20
|
+
- **状态监控** - 实时监控设备状态变化
|
|
21
|
+
|
|
22
|
+
## 执行流程
|
|
23
|
+
|
|
24
|
+
### 完整执行流程
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
┌─────────┐ ┌─────────┐ ┌──────┐ ┌────────┐
|
|
28
|
+
│ Backend │ │ Gateway │ │ Edge │ │ Device │
|
|
29
|
+
└────┬────┘ └────┬────┘ └───┬──┘ └────┬───┘
|
|
30
|
+
│ │ │ │
|
|
31
|
+
│ 1. command │ │ │
|
|
32
|
+
│ (complex) │ │ │
|
|
33
|
+
├─────────────►│ │ │
|
|
34
|
+
│ │ │ │
|
|
35
|
+
│ │ 2. route to │ │
|
|
36
|
+
│ │ edge01 │ │
|
|
37
|
+
│ ├─────────────►│ │
|
|
38
|
+
│ │ │ │
|
|
39
|
+
│ │ │ 3. forward │
|
|
40
|
+
│ │ │ to td01 │
|
|
41
|
+
│ │ ├───────────►│
|
|
42
|
+
│ │ │ │
|
|
43
|
+
│ │ │ │ 4. start
|
|
44
|
+
│ │ │ │ execution
|
|
45
|
+
│ │ │ ├─┐
|
|
46
|
+
│ │ │ │ │
|
|
47
|
+
│ │ │ │◄┘
|
|
48
|
+
│ │ │ │
|
|
49
|
+
│ │ │ 5. progress│
|
|
50
|
+
│ │ │ (phase 1) │
|
|
51
|
+
│ │ │◄───────────┤
|
|
52
|
+
│ │ │ │
|
|
53
|
+
│ │ 6. progress │ │
|
|
54
|
+
│ │◄─────────────┤ │
|
|
55
|
+
│ │ │ │
|
|
56
|
+
│ 7. progress │ │ │
|
|
57
|
+
│◄─────────────┤ │ │
|
|
58
|
+
│ │ │ │
|
|
59
|
+
│ │ │ 8. progress│
|
|
60
|
+
│ │ │ (phase 2) │
|
|
61
|
+
│ │ │◄───────────┤
|
|
62
|
+
│ │ │ │
|
|
63
|
+
│ │ 9. progress │ │
|
|
64
|
+
│ │◄─────────────┤ │
|
|
65
|
+
│ │ │ │
|
|
66
|
+
│ 10. progress │ │ │
|
|
67
|
+
│◄─────────────┤ │ │
|
|
68
|
+
│ │ │ │
|
|
69
|
+
│ │ │ ... │
|
|
70
|
+
│ │ │ │
|
|
71
|
+
│ │ │ 11. final │
|
|
72
|
+
│ │ │ response │
|
|
73
|
+
│ │ │◄───────────┤
|
|
74
|
+
│ │ │ │
|
|
75
|
+
│ │ 12. response │ │
|
|
76
|
+
│ │◄─────────────┤ │
|
|
77
|
+
│ │ │ │
|
|
78
|
+
│ 13. response │ │ │
|
|
79
|
+
│◄─────────────┤ │ │
|
|
80
|
+
│ │ │ │
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## 典型应用场景
|
|
84
|
+
|
|
85
|
+
### 1. 健康检查(HealthCheck)
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{
|
|
89
|
+
"type": "COMMAND",
|
|
90
|
+
"requestRef": "health-check-001",
|
|
91
|
+
"targetClientId": "td-01",
|
|
92
|
+
"command": {
|
|
93
|
+
"commandType": "COMPLEX",
|
|
94
|
+
"commandCode": "HealthCheck",
|
|
95
|
+
"parameters": {
|
|
96
|
+
"switchStatus": true, // 检查开关状态
|
|
97
|
+
"switchConfigInformation": true, // 检查配置信息
|
|
98
|
+
"synchronizerStatus": true, // 检查同步器状态
|
|
99
|
+
"pillarStatus": true // 检查立柱状态
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
"priority": "NORMAL",
|
|
103
|
+
"timeout": 30000
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 2. 监播数据导出
|
|
108
|
+
|
|
109
|
+
```json
|
|
110
|
+
{
|
|
111
|
+
"type": "COMMAND",
|
|
112
|
+
"requestRef": "monitor-export-001",
|
|
113
|
+
"targetClientId": "screen-01",
|
|
114
|
+
"command": {
|
|
115
|
+
"commandType": "COMPLEX",
|
|
116
|
+
"commandCode": "ExportMonitorData",
|
|
117
|
+
"parameters": {
|
|
118
|
+
"startTime": "2024-01-01T00:00:00Z",
|
|
119
|
+
"endTime": "2024-01-20T23:59:59Z",
|
|
120
|
+
"format": "csv",
|
|
121
|
+
"includeScreenshots": true
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
"priority": "LOW",
|
|
125
|
+
"timeout": 300000 // 5分钟
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 3. 系统日志收集
|
|
130
|
+
|
|
131
|
+
```json
|
|
132
|
+
{
|
|
133
|
+
"type": "COMMAND",
|
|
134
|
+
"requestRef": "log-collect-001",
|
|
135
|
+
"targetClientId": "td-01",
|
|
136
|
+
"command": {
|
|
137
|
+
"commandType": "COMPLEX",
|
|
138
|
+
"commandCode": "CollectSystemLogs",
|
|
139
|
+
"parameters": {
|
|
140
|
+
"logLevel": "Udebug",
|
|
141
|
+
"maxLines": 10000,
|
|
142
|
+
"components": ["network", "hardware", "application"]
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
"priority": "NORMAL",
|
|
146
|
+
"timeout": 60000
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## 进度更新消息
|
|
151
|
+
|
|
152
|
+
### 健康检查的进度更新
|
|
153
|
+
|
|
154
|
+
```json
|
|
155
|
+
// 第一个进度:检查开关状态
|
|
156
|
+
{
|
|
157
|
+
"type": "PROGRESS_UPDATE",
|
|
158
|
+
"requestRef": "health-check-001",
|
|
159
|
+
"status": "IN_PROGRESS",
|
|
160
|
+
"phase": "checking_switch",
|
|
161
|
+
"progress": 25,
|
|
162
|
+
"sourceType": "COMMAND",
|
|
163
|
+
"command": {
|
|
164
|
+
"commandType": "SIMPLE",
|
|
165
|
+
"commandCode": "READ_SWITCH_STATUS",
|
|
166
|
+
"deviceType": "controller",
|
|
167
|
+
"deviceId": 1,
|
|
168
|
+
"operationType": "READ",
|
|
169
|
+
"result": {
|
|
170
|
+
"switchCount": 8,
|
|
171
|
+
"activeCount": 6,
|
|
172
|
+
"inactiveCount": 2,
|
|
173
|
+
"switches": [
|
|
174
|
+
{"id": 1, "status": "on", "lastChange": "2024-01-20T08:00:00Z"},
|
|
175
|
+
{"id": 2, "status": "on", "lastChange": "2024-01-20T08:00:00Z"},
|
|
176
|
+
// ...
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
"report": {
|
|
181
|
+
"level": "INFO",
|
|
182
|
+
"message": "开关状态检查完成"
|
|
183
|
+
},
|
|
184
|
+
"timestamp": "2024-01-20T10:00:01Z",
|
|
185
|
+
"version": "1.0"
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 第二个进度:读取配置信息
|
|
189
|
+
{
|
|
190
|
+
"type": "PROGRESS_UPDATE",
|
|
191
|
+
"requestRef": "health-check-001",
|
|
192
|
+
"status": "IN_PROGRESS",
|
|
193
|
+
"phase": "reading_config",
|
|
194
|
+
"progress": 50,
|
|
195
|
+
"sourceType": "COMMAND",
|
|
196
|
+
"command": {
|
|
197
|
+
"commandType": "SIMPLE",
|
|
198
|
+
"commandCode": "READ_CONFIG",
|
|
199
|
+
"deviceType": "controller",
|
|
200
|
+
"deviceId": 1,
|
|
201
|
+
"operationType": "READ",
|
|
202
|
+
"result": {
|
|
203
|
+
"version": "2.1.0",
|
|
204
|
+
"lastUpdate": "2024-01-15T10:00:00Z",
|
|
205
|
+
"parameters": {
|
|
206
|
+
"brightness": 80,
|
|
207
|
+
"colorMode": "RGB",
|
|
208
|
+
"refreshRate": 60
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
"report": {
|
|
213
|
+
"level": "INFO",
|
|
214
|
+
"message": "配置信息读取完成"
|
|
215
|
+
},
|
|
216
|
+
"timestamp": "2024-01-20T10:00:02Z",
|
|
217
|
+
"version": "1.0"
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 更多进度更新...
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### 监播数据导出的进度更新
|
|
224
|
+
|
|
225
|
+
```json
|
|
226
|
+
{
|
|
227
|
+
"type": "PROGRESS_UPDATE",
|
|
228
|
+
"requestRef": "monitor-export-001",
|
|
229
|
+
"status": "IN_PROGRESS",
|
|
230
|
+
"phase": "exporting_data",
|
|
231
|
+
"progress": 30,
|
|
232
|
+
"sourceType": "SYSTEM",
|
|
233
|
+
"report": {
|
|
234
|
+
"level": "INFO",
|
|
235
|
+
"message": "正在导出第3天的数据,共20天",
|
|
236
|
+
"data": {
|
|
237
|
+
"currentDay": 3,
|
|
238
|
+
"totalDays": 20,
|
|
239
|
+
"recordsExported": 15000,
|
|
240
|
+
"estimatedTotal": 100000
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
"timestamp": "2024-01-20T10:05:00Z",
|
|
244
|
+
"version": "1.0"
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## 最终响应
|
|
249
|
+
|
|
250
|
+
### 成功响应
|
|
251
|
+
|
|
252
|
+
```json
|
|
253
|
+
{
|
|
254
|
+
"type": "COMMAND_RESPONSE",
|
|
255
|
+
"requestRef": "health-check-001",
|
|
256
|
+
"status": "COMPLETED",
|
|
257
|
+
"result": {
|
|
258
|
+
"deviceType": "controller",
|
|
259
|
+
"deviceId": "td-01",
|
|
260
|
+
"commandCode": "HealthCheck",
|
|
261
|
+
"operationType": "READ",
|
|
262
|
+
"data": {
|
|
263
|
+
"summary": "健康检查完成",
|
|
264
|
+
"overallStatus": "healthy",
|
|
265
|
+
"checksPerformed": 4,
|
|
266
|
+
"checksPassed": 4,
|
|
267
|
+
"checksFailed": 0,
|
|
268
|
+
"details": {
|
|
269
|
+
"switchStatus": "passed",
|
|
270
|
+
"configStatus": "passed",
|
|
271
|
+
"synchronizerStatus": "passed",
|
|
272
|
+
"pillarStatus": "passed"
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
"executionTime": 5200,
|
|
277
|
+
"timestamp": "2024-01-20T10:00:05Z",
|
|
278
|
+
"version": "1.0"
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### 部分失败响应
|
|
283
|
+
|
|
284
|
+
```json
|
|
285
|
+
{
|
|
286
|
+
"type": "COMMAND_RESPONSE",
|
|
287
|
+
"requestRef": "health-check-001",
|
|
288
|
+
"status": "COMPLETED",
|
|
289
|
+
"result": {
|
|
290
|
+
"deviceType": "controller",
|
|
291
|
+
"deviceId": "td-01",
|
|
292
|
+
"commandCode": "HealthCheck",
|
|
293
|
+
"operationType": "READ",
|
|
294
|
+
"data": {
|
|
295
|
+
"summary": "健康检查完成,发现问题",
|
|
296
|
+
"overallStatus": "Uwarning",
|
|
297
|
+
"checksPerformed": 4,
|
|
298
|
+
"checksPassed": 3,
|
|
299
|
+
"checksFailed": 1,
|
|
300
|
+
"details": {
|
|
301
|
+
"switchStatus": "passed",
|
|
302
|
+
"configStatus": "passed",
|
|
303
|
+
"synchronizerStatus": "Ufailed",
|
|
304
|
+
"pillarStatus": "passed"
|
|
305
|
+
},
|
|
306
|
+
"errors": [
|
|
307
|
+
{
|
|
308
|
+
"component": "synchronizer",
|
|
309
|
+
"Uerror": "Synchronizer offline",
|
|
310
|
+
"timestamp": "2024-01-20T10:00:03Z"
|
|
311
|
+
}
|
|
312
|
+
]
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
"report": {
|
|
316
|
+
"level": "WARNING",
|
|
317
|
+
"message": "健康检查发现1个问题",
|
|
318
|
+
"code": "HEALTH_CHECK_WARNING"
|
|
319
|
+
},
|
|
320
|
+
"executionTime": 5200,
|
|
321
|
+
"timestamp": "2024-01-20T10:00:05Z",
|
|
322
|
+
"version": "1.0"
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## 设备端实现指南
|
|
327
|
+
|
|
328
|
+
### 基本实现模式
|
|
329
|
+
|
|
330
|
+
```javascript
|
|
331
|
+
class ComplexCommandHandler {
|
|
332
|
+
async handleHealthCheck(command, progressCallback) {
|
|
333
|
+
const totalSteps = 4;
|
|
334
|
+
let currentStep = 0;
|
|
335
|
+
const results = {};
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
// Step 1: 检查开关状态
|
|
339
|
+
if (command.parameters.switchStatus) {
|
|
340
|
+
currentStep++;
|
|
341
|
+
const switchResult = await this.checkSwitchStatus();
|
|
342
|
+
|
|
343
|
+
await progressCallback({
|
|
344
|
+
phase: 'checking_switch',
|
|
345
|
+
progress: (currentStep / totalSteps) * 100,
|
|
346
|
+
command: {
|
|
347
|
+
commandType: 'simple',
|
|
348
|
+
commandCode: 'READ_SWITCH_STATUS',
|
|
349
|
+
deviceType: 'controller',
|
|
350
|
+
deviceId: this.deviceId,
|
|
351
|
+
operationType: 'read',
|
|
352
|
+
result: switchResult
|
|
353
|
+
},
|
|
354
|
+
report: {
|
|
355
|
+
level: 'info',
|
|
356
|
+
message: '开关状态检查完成'
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
results.switchStatus = switchResult;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Step 2: 读取配置信息
|
|
364
|
+
if (command.parameters.switchConfigInformation) {
|
|
365
|
+
currentStep++;
|
|
366
|
+
const configResult = await this.readConfiguration();
|
|
367
|
+
|
|
368
|
+
await progressCallback({
|
|
369
|
+
phase: 'reading_config',
|
|
370
|
+
progress: (currentStep / totalSteps) * 100,
|
|
371
|
+
command: {
|
|
372
|
+
commandType: 'simple',
|
|
373
|
+
commandCode: 'READ_CONFIG',
|
|
374
|
+
deviceType: 'controller',
|
|
375
|
+
deviceId: this.deviceId,
|
|
376
|
+
operationType: 'read',
|
|
377
|
+
result: configResult
|
|
378
|
+
},
|
|
379
|
+
report: {
|
|
380
|
+
level: 'info',
|
|
381
|
+
message: '配置信息读取完成'
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
results.configStatus = configResult;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// Step 3 & 4: 继续其他检查...
|
|
389
|
+
|
|
390
|
+
// 返回最终结果
|
|
391
|
+
return {
|
|
392
|
+
summary: '健康检查完成',
|
|
393
|
+
overallStatus: 'healthy',
|
|
394
|
+
checksPerformed: currentStep,
|
|
395
|
+
checksPassed: currentStep,
|
|
396
|
+
checksFailed: 0,
|
|
397
|
+
details: results
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
} catch (error) {
|
|
401
|
+
// 错误处理
|
|
402
|
+
await progressCallback({
|
|
403
|
+
phase: 'error',
|
|
404
|
+
progress: (currentStep / totalSteps) * 100,
|
|
405
|
+
status: 'failed',
|
|
406
|
+
log: {
|
|
407
|
+
level: 'error',
|
|
408
|
+
message: error.message,
|
|
409
|
+
code: error.code
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
throw error;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### 进度回调函数
|
|
420
|
+
|
|
421
|
+
```javascript
|
|
422
|
+
async function sendProgressUpdate(requestRef, progressData) {
|
|
423
|
+
const progressUpdate = {
|
|
424
|
+
type: 'progress_update',
|
|
425
|
+
requestRef,
|
|
426
|
+
status: progressData.status || 'in_progress',
|
|
427
|
+
phase: progressData.phase,
|
|
428
|
+
progress: progressData.progress,
|
|
429
|
+
sourceType: progressData.command ? 'command' : 'system',
|
|
430
|
+
command: progressData.command,
|
|
431
|
+
report: progressData.report,
|
|
432
|
+
timestamp: new Date().toISOString(),
|
|
433
|
+
version: '1.0'
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
await this.sendToEdge(progressUpdate);
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
## 最佳实践
|
|
441
|
+
|
|
442
|
+
### 1. 进度粒度控制
|
|
443
|
+
|
|
444
|
+
- 避免发送过多的进度更新(建议每个阶段一次)
|
|
445
|
+
- 重要状态变化时才发送更新
|
|
446
|
+
- 使用有意义的 phase 名称
|
|
447
|
+
|
|
448
|
+
### 2. 错误处理
|
|
449
|
+
|
|
450
|
+
```javascript
|
|
451
|
+
// 遇到非致命错误,继续执行但记录警告
|
|
452
|
+
if (minorError) {
|
|
453
|
+
await progressCallback({
|
|
454
|
+
phase: currentPhase,
|
|
455
|
+
progress: currentProgress,
|
|
456
|
+
log: {
|
|
457
|
+
level: 'warning',
|
|
458
|
+
message: '检测到非关键问题',
|
|
459
|
+
code: 'MINOR_ERROR',
|
|
460
|
+
data: { error: minorError.message }
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
// 继续执行
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// 遇到致命错误,立即停止并报告
|
|
467
|
+
if (fatalError) {
|
|
468
|
+
await progressCallback({
|
|
469
|
+
phase: currentPhase,
|
|
470
|
+
progress: currentProgress,
|
|
471
|
+
status: 'failed',
|
|
472
|
+
log: {
|
|
473
|
+
level: 'error',
|
|
474
|
+
message: '致命错误,检查终止',
|
|
475
|
+
code: 'FATAL_ERROR',
|
|
476
|
+
data: { error: fatalError.message }
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
throw fatalError;
|
|
480
|
+
}
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
### 3. 超时处理
|
|
484
|
+
|
|
485
|
+
```javascript
|
|
486
|
+
// 设置整体超时
|
|
487
|
+
const timeoutHandle = setTimeout(() => {
|
|
488
|
+
throw new Error('Complex command timeout');
|
|
489
|
+
}, command.timeout || 30000);
|
|
490
|
+
|
|
491
|
+
try {
|
|
492
|
+
// 执行命令
|
|
493
|
+
const result = await this.executeComplexCommand(command);
|
|
494
|
+
clearTimeout(timeoutHandle);
|
|
495
|
+
return result;
|
|
496
|
+
} catch (error) {
|
|
497
|
+
clearTimeout(timeoutHandle);
|
|
498
|
+
throw error;
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### 4. 资源管理
|
|
503
|
+
|
|
504
|
+
```javascript
|
|
505
|
+
// 确保资源正确释放
|
|
506
|
+
let resources = null;
|
|
507
|
+
try {
|
|
508
|
+
resources = await this.allocateResources();
|
|
509
|
+
// 执行操作
|
|
510
|
+
return await this.performOperation(resources);
|
|
511
|
+
} finally {
|
|
512
|
+
if (resources) {
|
|
513
|
+
await this.releaseResources(resources);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
## 与 Simple 命令的区别
|
|
519
|
+
|
|
520
|
+
| 特性 | Simple 命令 | Complex 命令 |
|
|
521
|
+
|------|------------|-------------|
|
|
522
|
+
| 响应次数 | 一次 | 多次(进度更新 + 最终响应) |
|
|
523
|
+
| 适用场景 | 快速操作 | 长时间运行操作 |
|
|
524
|
+
| 进度反馈 | 无 | 有(progress_update) |
|
|
525
|
+
| 实现复杂度 | 低 | 中等 |
|
|
526
|
+
| 超时设置 | 通常较短 | 通常较长 |
|
|
527
|
+
|
|
528
|
+
## 总结
|
|
529
|
+
|
|
530
|
+
Complex 命令为需要持续反馈的操作提供了标准化的实现模式。通过 progress_update 消息,可以实时向用户展示操作进度,提供更好的用户体验。设备端实现时需要注意:
|
|
531
|
+
|
|
532
|
+
1. 合理划分执行阶段
|
|
533
|
+
2. 及时发送进度更新
|
|
534
|
+
3. 正确处理错误情况
|
|
535
|
+
4. 最终必须发送 command_response
|
|
536
|
+
|
|
537
|
+
这种设计既保证了协议的灵活性,又维持了实现的简洁性。
|