@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.
Files changed (78) hide show
  1. package/ACK_MESSAGES_IMPLEMENTATION_SUMMARY.md +128 -0
  2. package/ACK_MESSAGE_DESIGN.md +457 -0
  3. package/CHANGELOG.md +58 -0
  4. package/COMMAND_VALIDATION_RULES.md +178 -0
  5. package/DOCUMENTATION_REORGANIZATION_SUMMARY.md +81 -0
  6. package/DOCUMENTATION_STRUCTURE.md +106 -0
  7. package/GATEWAY_MIGRATION_GUIDE.md +130 -0
  8. package/GATEWAY_PROTOCOL_COMPARISON.md +216 -0
  9. package/INTEGRATION_GUIDE.md +190 -0
  10. package/OPTIONAL_FIELDS_WITHOUT_DEFAULTS.md +97 -0
  11. package/PROTOCOL_UTILS_USAGE.md +278 -0
  12. package/README.md +237 -0
  13. package/TYPE_FIXES_SUMMARY.md +210 -0
  14. package/UPDATE_ENUM_VALUES.md +139 -0
  15. package/dist/asyncapi-sync.d.ts +47 -0
  16. package/dist/asyncapi-sync.d.ts.map +1 -0
  17. package/dist/asyncapi-sync.js +85 -0
  18. package/dist/asyncapi-sync.js.map +1 -0
  19. package/dist/command-factory.d.ts +62 -0
  20. package/dist/command-factory.d.ts.map +1 -0
  21. package/dist/command-factory.js +137 -0
  22. package/dist/command-factory.js.map +1 -0
  23. package/dist/command-types.d.ts +27 -0
  24. package/dist/command-types.d.ts.map +1 -0
  25. package/dist/command-types.js +31 -0
  26. package/dist/command-types.js.map +1 -0
  27. package/dist/index.d.ts +403 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +413 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/message-validator.d.ts +102 -0
  32. package/dist/message-validator.d.ts.map +1 -0
  33. package/dist/message-validator.js +640 -0
  34. package/dist/message-validator.js.map +1 -0
  35. package/dist/protocol-utils.d.ts +108 -0
  36. package/dist/protocol-utils.d.ts.map +1 -0
  37. package/dist/protocol-utils.js +293 -0
  38. package/dist/protocol-utils.js.map +1 -0
  39. package/docs/01-protocol/README.md +45 -0
  40. package/docs/01-protocol/design-rationale.md +198 -0
  41. package/docs/01-protocol/message-types.md +669 -0
  42. package/docs/01-protocol/specification.md +1466 -0
  43. package/docs/02-commands/README.md +56 -0
  44. package/docs/02-commands/batch-command.md +435 -0
  45. package/docs/02-commands/complex-command.md +537 -0
  46. package/docs/02-commands/simple-command.md +332 -0
  47. package/docs/02-commands/typed-commands.md +362 -0
  48. package/docs/03-architecture/README.md +66 -0
  49. package/docs/03-architecture/device-protocol.md +430 -0
  50. package/docs/03-architecture/edge-proxy.md +727 -0
  51. package/docs/03-architecture/routing-flow.md +893 -0
  52. package/docs/04-integration/README.md +144 -0
  53. package/docs/04-integration/backend-guide.md +551 -0
  54. package/docs/04-integration/edge-guide.md +684 -0
  55. package/docs/04-integration/gateway-guide.md +180 -0
  56. package/docs/04-integration/migration-guide.md +226 -0
  57. package/docs/05-examples/README.md +141 -0
  58. package/docs/05-examples/progress-update-examples.md +757 -0
  59. package/docs/06-reference/README.md +67 -0
  60. package/docs/06-reference/api.md +572 -0
  61. package/docs/06-reference/faq.md +302 -0
  62. package/docs/06-reference/glossary.md +232 -0
  63. package/examples/backend-upgrade.ts +279 -0
  64. package/examples/edge-multi-device.ts +513 -0
  65. package/examples/gateway-upgrade.ts +150 -0
  66. package/examples/protocol-implementation.ts +715 -0
  67. package/package.json +48 -0
  68. package/scripts/validate-asyncapi.ts +78 -0
  69. package/src/__tests__/protocol.test.ts +297 -0
  70. package/src/asyncapi-sync.ts +84 -0
  71. package/src/command-factory.ts +183 -0
  72. package/src/command-types.ts +72 -0
  73. package/src/edge-proxy.ts +494 -0
  74. package/src/gateway-extensions.ts +278 -0
  75. package/src/index.ts +792 -0
  76. package/src/message-validator.ts +726 -0
  77. package/src/protocol-utils.ts +355 -0
  78. package/tsconfig.json +24 -0
@@ -0,0 +1,669 @@
1
+ # 消息类型详解
2
+
3
+ 本文档详细介绍 JRSoft Subway WebSocket 协议中的所有消息类型。
4
+
5
+ ## 消息类型总览
6
+
7
+ 协议包含以下消息类型:
8
+
9
+ | 消息类型 | 方向 | 说明 |
10
+ |---------|------|------|
11
+ | **连接管理** |
12
+ | REGISTER | Client → Server | 客户端注册 |
13
+ | REGISTER_ACK | Server → Client | 注册确认 |
14
+ | UNREGISTER | Client → Server | 客户端注销 |
15
+ | UNREGISTER_ACK | Server → Client | 注销确认 |
16
+ | **心跳保活** |
17
+ | HEARTBEAT | Client → Server | 心跳请求 |
18
+ | HEARTBEAT_ACK | Server → Client | 心跳确认 |
19
+ | **命令执行** |
20
+ | COMMAND | Client → Client | 命令请求 |
21
+ | COMMAND_RESPONSE | Client → Client | 命令响应 |
22
+ | **程序管理** |
23
+ | PROGRAM | Client → Client | 程序上传请求 |
24
+ | PROGRAM_RESPONSE | Client → Client | 程序上传响应 |
25
+ | **进度报告** |
26
+ | PROGRESS_UPDATE | Client → Client | 进度更新 |
27
+ | **错误处理** |
28
+ | ERROR | Server → Client | 错误消息 |
29
+
30
+ ## 消息类型枚举
31
+
32
+ ```typescript
33
+ export enum MessageType {
34
+ // 连接管理
35
+ REGISTER = 'REGISTER', // 客户端注册
36
+ REGISTER_ACK = 'REGISTER_ACK', // 注册确认
37
+ UNREGISTER = 'UNREGISTER', // 客户端注销
38
+ UNREGISTER_ACK = 'UNREGISTER_ACK', // 注销确认
39
+
40
+ // 心跳
41
+ HEARTBEAT = 'HEARTBEAT', // 心跳请求
42
+ HEARTBEAT_ACK = 'HEARTBEAT_ACK', // 心跳确认
43
+
44
+ // 命令执行
45
+ COMMAND = 'COMMAND', // 命令请求
46
+ COMMAND_RESPONSE = 'COMMAND_RESPONSE', // 命令响应
47
+
48
+ // 程序管理
49
+ PROGRAM = 'PROGRAM', // 程序上传请求
50
+ PROGRAM_RESPONSE = 'PROGRAM_RESPONSE', // 程序上传响应
51
+
52
+ // 进度更新
53
+ PROGRESS_UPDATE = 'PROGRESS_UPDATE', // 进度更新
54
+
55
+ // 错误
56
+ ERROR = 'ERROR' // 错误消息
57
+ }
58
+ ```
59
+
60
+ ## 基础消息结构
61
+
62
+ 所有消息都继承自 BaseMessage:
63
+
64
+ ```typescript
65
+ interface BaseMessage {
66
+ type: MessageType;
67
+ timestamp: string; // ISO 8601 格式
68
+ version: string; // 协议版本,如 "1.0"
69
+ }
70
+ ```
71
+
72
+ ## 1. 注册消息 (register)
73
+
74
+ 客户端连接后必须立即发送注册消息。
75
+
76
+ ### 消息结构
77
+
78
+ ```typescript
79
+ interface RegisterMessage extends BaseMessage {
80
+ type: MessageType.REGISTER;
81
+ clientId: string;
82
+ clientType: ClientType;
83
+ clientInfo?: {
84
+ name?: string;
85
+ version?: string;
86
+ description?: string;
87
+ capabilities?: string[];
88
+ metadata?: Record<string, any>;
89
+ };
90
+ }
91
+ ```
92
+
93
+ ### 示例
94
+
95
+ ```json
96
+ {
97
+ "type": "REGISTER",
98
+ "clientId": "edge-001",
99
+ "clientType": "EDGE",
100
+ "clientInfo": {
101
+ "name": "隧道出口 Edge 节点",
102
+ "version": "1.0.0",
103
+ "capabilities": ["device-proxy", "batch-command"],
104
+ "metadata": {
105
+ "location": "tunnel-exit-1",
106
+ "deviceCount": 50
107
+ }
108
+ },
109
+ "timestamp": "2024-01-20T10:00:00Z",
110
+ "version": "1.0"
111
+ }
112
+ ```
113
+
114
+ ### Edge 设备注册
115
+
116
+ 当设备通过 Edge 节点注册时,会包含额外的 Edge 信息:
117
+
118
+ ```json
119
+ {
120
+ "type": "REGISTER",
121
+ "clientId": "td-01",
122
+ "clientType": "DEVICE",
123
+ "edgeInfo": {
124
+ "edgeId": "edge-001",
125
+ "edgeVersion": "1.0.0",
126
+ "connectionTime": "2024-01-20T09:59:00Z"
127
+ },
128
+ "clientInfo": {
129
+ "version": "1.0.0",
130
+ "deviceType": "media_player",
131
+ "capabilities": ["play", "stop", "status_report"]
132
+ },
133
+ "timestamp": "2024-01-20T10:00:01Z",
134
+ "version": "1.0"
135
+ }
136
+ ```
137
+
138
+ ## 2. 注册确认消息 (register_ack)
139
+
140
+ 服务器响应客户端注册请求。
141
+
142
+ ### 消息结构
143
+
144
+ ```typescript
145
+ interface RegisterAckMessage extends BaseMessage {
146
+ type: MessageType.REGISTER_ACK;
147
+ clientId: string;
148
+ success: boolean;
149
+ sessionId?: string;
150
+ error?: {
151
+ code: string;
152
+ message: string;
153
+ };
154
+ serverInfo?: {
155
+ version: string;
156
+ capabilities: string[];
157
+ currentLoad?: number;
158
+ maxClients?: number;
159
+ };
160
+ }
161
+ ```
162
+
163
+ ### 示例 - 成功注册
164
+
165
+ ```json
166
+ {
167
+ "type": "REGISTER_ACK",
168
+ "clientId": "edge-001",
169
+ "success": true,
170
+ "sessionId": "sess-123456",
171
+ "serverInfo": {
172
+ "version": "1.0.0",
173
+ "capabilities": ["device_proxy", "batch_command", "status_report"],
174
+ "currentLoad": 45,
175
+ "maxClients": 1000
176
+ },
177
+ "timestamp": "2024-01-20T10:00:01Z",
178
+ "version": "1.0"
179
+ }
180
+ ```
181
+
182
+ ### 示例 - 注册失败
183
+
184
+ ```json
185
+ {
186
+ "type": "REGISTER_ACK",
187
+ "clientId": "edge-001",
188
+ "success": false,
189
+ "error": {
190
+ "code": "DUPLICATE_CLIENT_ID",
191
+ "message": "客户端ID已存在"
192
+ },
193
+ "timestamp": "2024-01-20T10:00:01Z",
194
+ "version": "1.0"
195
+ }
196
+ ```
197
+
198
+ ## 3. 注销消息 (unregister)
199
+
200
+ 客户端主动断开连接前发送。
201
+
202
+ ### 消息结构
203
+
204
+ ```typescript
205
+ interface UnregisterMessage extends BaseMessage {
206
+ type: MessageType.UNREGISTER;
207
+ clientId: string;
208
+ reason?: string;
209
+ }
210
+ ```
211
+
212
+ ### 示例
213
+
214
+ ```json
215
+ {
216
+ "type": "UNREGISTER",
217
+ "clientId": "edge-001",
218
+ "reason": "shutdown",
219
+ "timestamp": "2024-01-20T11:00:00Z",
220
+ "version": "1.0"
221
+ }
222
+ ```
223
+
224
+ ## 4. 注销确认消息 (unregister_ack)
225
+
226
+ 服务器确认客户端注销。
227
+
228
+ ### 消息结构
229
+
230
+ ```typescript
231
+ interface UnregisterAckMessage extends BaseMessage {
232
+ type: MessageType.UNREGISTER_ACK;
233
+ clientId: string;
234
+ success: boolean;
235
+ cleanupInfo?: {
236
+ messagesProcessed: number;
237
+ pendingMessages: number;
238
+ connectionDuration: number;
239
+ };
240
+ }
241
+ ```
242
+
243
+ ### 示例
244
+
245
+ ```json
246
+ {
247
+ "type": "UNREGISTER_ACK",
248
+ "clientId": "edge-001",
249
+ "success": true,
250
+ "cleanupInfo": {
251
+ "messagesProcessed": 1520,
252
+ "pendingMessages": 0,
253
+ "connectionDuration": 3600
254
+ },
255
+ "timestamp": "2024-01-20T11:00:00Z",
256
+ "version": "1.0"
257
+ }
258
+ ```
259
+
260
+ ## 5. 心跳消息 (heartbeat)
261
+
262
+ 用于保持连接活跃,检测连接状态。
263
+
264
+ ### 消息结构
265
+
266
+ ```typescript
267
+ interface HeartbeatMessage extends BaseMessage {
268
+ type: MessageType.HEARTBEAT;
269
+ clientId: string;
270
+ sequence: number;
271
+ clientTime: string;
272
+ }
273
+ ```
274
+
275
+ ### 示例
276
+
277
+ ```json
278
+ {
279
+ "type": "HEARTBEAT",
280
+ "clientId": "backend-001",
281
+ "sequence": 123,
282
+ "clientTime": "2024-01-20T10:00:30Z",
283
+ "timestamp": "2024-01-20T10:00:30Z",
284
+ "version": "1.0"
285
+ }
286
+ ```
287
+
288
+ ## 6. 心跳确认消息 (heartbeat_ack)
289
+
290
+ 服务器响应心跳请求。
291
+
292
+ ### 消息结构
293
+
294
+ ```typescript
295
+ interface HeartbeatAckMessage extends BaseMessage {
296
+ type: MessageType.HEARTBEAT_ACK;
297
+ clientId: string;
298
+ sequence: number;
299
+ clientTime: string;
300
+ serverTime: string;
301
+ latency?: number;
302
+ serverStatus?: {
303
+ healthy: boolean;
304
+ activeConnections: number;
305
+ messageQueueSize: number;
306
+ cpuUsage?: number;
307
+ memoryUsage?: number;
308
+ };
309
+ }
310
+ ```
311
+
312
+ ### 示例
313
+
314
+ ```json
315
+ {
316
+ "type": "HEARTBEAT_ACK",
317
+ "clientId": "backend-001",
318
+ "sequence": 123,
319
+ "clientTime": "2024-01-20T10:00:30Z",
320
+ "serverTime": "2024-01-20T10:00:30.150Z",
321
+ "latency": 15,
322
+ "serverStatus": {
323
+ "healthy": true,
324
+ "activeConnections": 120,
325
+ "messageQueueSize": 45,
326
+ "cpuUsage": 23.5,
327
+ "memoryUsage": 45.2
328
+ },
329
+ "timestamp": "2024-01-20T10:00:30Z",
330
+ "version": "1.0"
331
+ }
332
+ ```
333
+
334
+ ## 7. 错误消息 (error)
335
+
336
+ 用于报告各种错误情况。
337
+
338
+ ### 消息结构
339
+
340
+ ```typescript
341
+ interface ErrorMessage extends BaseMessage {
342
+ type: MessageType.ERROR;
343
+ code: string;
344
+ message: string;
345
+ severity?: string; // low, medium, high, critical
346
+ category?: string; // device, command, protocol, system
347
+ context?: any;
348
+ retryable?: boolean;
349
+ requestRef?: string; // 关联的请求引用
350
+ }
351
+ ```
352
+
353
+ ### 常见错误码
354
+
355
+ - `INVALID_MESSAGE` - 消息格式错误
356
+ - `UNAUTHORIZED` - 未授权
357
+ - `TARGET_NOT_FOUND` - 目标客户端不存在
358
+ - `TIMEOUT` - 操作超时
359
+ - `INTERNAL_ERROR` - 内部错误
360
+
361
+ ### 示例
362
+
363
+ ```json
364
+ {
365
+ "type": "ERROR",
366
+ "code": "TARGET_NOT_FOUND",
367
+ "message": "目标客户端 device-01 不存在",
368
+ "requestRef": "cmd-123",
369
+ "timestamp": "2024-01-20T10:00:00Z",
370
+ "version": "1.0"
371
+ }
372
+ ```
373
+
374
+ ## 8. 命令消息 (command)
375
+
376
+ 用于发送各种控制命令。
377
+
378
+ ### 消息结构
379
+
380
+ ```typescript
381
+ interface CommandMessage extends BaseMessage {
382
+ type: MessageType.COMMAND;
383
+ requestRef: string;
384
+ targetClientId?: string; // Simple/Complex 命令必需
385
+ command: Command;
386
+ callback?: string;
387
+ priority: Priority;
388
+ timeout: number;
389
+ }
390
+
391
+ interface Command {
392
+ commandType: CommandType; // simple, batch, complex
393
+ commandCode: string;
394
+ deviceType?: string;
395
+ deviceId?: string | number;
396
+ operationType?: OperationType;
397
+ parameters?: Record<string, any>;
398
+ }
399
+ ```
400
+
401
+ ### 示例 - Simple 命令
402
+
403
+ ```json
404
+ {
405
+ "type": "COMMAND",
406
+ "requestRef": "cmd-123",
407
+ "targetClientId": "td-01",
408
+ "command": {
409
+ "commandType": "SIMPLE",
410
+ "commandCode": "SET_BRIGHTNESS",
411
+ "deviceType": "screen",
412
+ "deviceId": 1,
413
+ "operationType": "WRITE",
414
+ "parameters": {
415
+ "brightness": 80
416
+ }
417
+ },
418
+ "priority": "NORMAL",
419
+ "timeout": 5000,
420
+ "timestamp": "2024-01-20T10:00:00Z",
421
+ "version": "1.0"
422
+ }
423
+ ```
424
+
425
+ ## 9. 命令响应消息 (command_response)
426
+
427
+ 命令执行后的响应。
428
+
429
+ ### 消息结构
430
+
431
+ ```typescript
432
+ interface CommandResponseMessage extends BaseMessage {
433
+ type: MessageType.COMMAND_RESPONSE;
434
+ requestRef: string;
435
+ status: CommandStatus;
436
+ result?: CommandResult;
437
+ report?: ReportMessage;
438
+ executionTime?: number;
439
+ }
440
+
441
+ interface CommandResult {
442
+ deviceType: string;
443
+ deviceId: string | number;
444
+ commandCode: string;
445
+ operationType: OperationType;
446
+ data: Record<string, any>;
447
+ }
448
+ ```
449
+
450
+ ### 示例 - 成功响应
451
+
452
+ ```json
453
+ {
454
+ "type": "COMMAND_RESPONSE",
455
+ "requestRef": "cmd-123",
456
+ "status": "COMPLETED",
457
+ "result": {
458
+ "deviceType": "screen",
459
+ "deviceId": 1,
460
+ "commandCode": "SET_BRIGHTNESS",
461
+ "operationType": "WRITE",
462
+ "data": {
463
+ "previousValue": 60,
464
+ "currentValue": 80
465
+ }
466
+ },
467
+ "executionTime": 150,
468
+ "timestamp": "2024-01-20T10:00:01Z",
469
+ "version": "1.0"
470
+ }
471
+ ```
472
+
473
+ ## 10. 进度更新消息 (progress_update)
474
+
475
+ 用于长时间运行操作的进度报告。
476
+
477
+ ### 消息结构
478
+
479
+ ```typescript
480
+ interface ProgressUpdateMessage extends BaseMessage {
481
+ type: MessageType.PROGRESS_UPDATE;
482
+ requestRef: string;
483
+ status: ProgressStatus;
484
+ phase: ProgressPhase | string; // 支持预定义阶段或自定义阶段
485
+ progress: number; // 0-100
486
+ sourceType: 'COMMAND' | 'SYSTEM';
487
+ command?: DeviceOperationRecord;
488
+ context?: ProgramContext;
489
+ report?: ReportMessage;
490
+ }
491
+ ```
492
+
493
+ ### 进度阶段
494
+
495
+ 预定义的进度阶段(ProgressPhase):
496
+ - `DOWNLOAD` - 下载文件
497
+ - `DECOMPRESS` - 解压文件
498
+ - `PREPROCESS` - 预处理
499
+ - `FRAMES` - 创建帧
500
+ - `UPLOAD` - 上传到设备
501
+ - `STATS` - 统计信息
502
+
503
+ 也支持自定义阶段名称,如:
504
+ - `"VALIDATE"` - 验证阶段
505
+ - `"BACKUP"` - 备份阶段
506
+ - `"ROLLBACK"` - 回滚阶段
507
+
508
+ ### 示例 - 批量命令进度
509
+
510
+ ```json
511
+ {
512
+ "type": "PROGRESS_UPDATE",
513
+ "requestRef": "batch-123",
514
+ "status": "IN_PROGRESS",
515
+ "phase": "FRAMES",
516
+ "progress": 20,
517
+ "sourceType": "COMMAND",
518
+ "command": {
519
+ "commandType": "SIMPLE",
520
+ "commandCode": "SET_COLOR",
521
+ "deviceType": "pillar",
522
+ "deviceId": 2,
523
+ "operationType": "WRITE",
524
+ "result": {
525
+ "success": true,
526
+ "color": "#FF0000"
527
+ }
528
+ },
529
+ "report": {
530
+ "level": "INFO",
531
+ "message": "光柱 2 颜色设置成功"
532
+ },
533
+ "timestamp": "2024-01-20T10:00:02Z",
534
+ "version": "1.0"
535
+ }
536
+ ```
537
+
538
+ ## 11. 程序上传消息 (program)
539
+
540
+ 用于上传节目内容到设备。
541
+
542
+ ### 消息结构
543
+
544
+ ```typescript
545
+ interface ProgramMessage extends BaseMessage {
546
+ type: MessageType.PROGRAM;
547
+ requestRef: string;
548
+ targetClientId: string;
549
+ command: {
550
+ commandCode: string;
551
+ parameters: ProgramParameters;
552
+ };
553
+ }
554
+
555
+ interface ProgramParameters {
556
+ taskId: string; // Snowflake ID
557
+ programId: string; // Snowflake ID
558
+ programName: string;
559
+ programNumber: number; // 1-10
560
+ programType: 'DYNAMIC' | 'STATIC';
561
+ fileUrl: string;
562
+ fileHash: string;
563
+ hashAlgorithm: 'MD5' | 'SHA256';
564
+ width: number;
565
+ height: number;
566
+ direction: 'LEFT_TO_RIGHT' | 'RIGHT_TO_LEFT';
567
+ publishTime: string;
568
+ unpublishTime: string;
569
+ }
570
+ ```
571
+
572
+ ### 示例
573
+
574
+ ```json
575
+ {
576
+ "type": "PROGRAM",
577
+ "requestRef": "prog-456",
578
+ "targetClientId": "screen-01",
579
+ "command": {
580
+ "commandCode": "UPLOAD_PROGRAM",
581
+ "parameters": {
582
+ "taskId": "7423847329847329847",
583
+ "programId": "7423847329847329848",
584
+ "programName": "春节促销广告",
585
+ "programNumber": 1,
586
+ "programType": "DYNAMIC",
587
+ "fileUrl": "https://storage.example.com/programs/spring-festival.zip",
588
+ "fileHash": "d41d8cd98f00b204e9800998ecf8427e",
589
+ "hashAlgorithm": "MD5",
590
+ "width": 1920,
591
+ "height": 1080,
592
+ "direction": "LEFT_TO_RIGHT",
593
+ "publishTime": "2024-01-20T00:00:00Z",
594
+ "unpublishTime": "2024-02-20T23:59:59Z"
595
+ }
596
+ },
597
+ "timestamp": "2024-01-20T10:00:00Z",
598
+ "version": "1.0"
599
+ }
600
+ ```
601
+
602
+ ## 12. 程序响应消息 (program_response)
603
+
604
+ 节目处理完成后的响应。
605
+
606
+ ### 消息结构
607
+
608
+ ```typescript
609
+ interface ProgramResponseMessage extends BaseMessage {
610
+ type: MessageType.PROGRAM_RESPONSE;
611
+ requestRef: string;
612
+ status: 'COMPLETED' | 'FAILED';
613
+ context: ProgramContext;
614
+ report?: ReportMessage;
615
+ }
616
+ ```
617
+
618
+ ### 示例
619
+
620
+ ```json
621
+ {
622
+ "type": "PROGRAM_RESPONSE",
623
+ "requestRef": "prog-456",
624
+ "status": "COMPLETED",
625
+ "context": {
626
+ "taskId": "7423847329847329847",
627
+ "programId": "7423847329847329848",
628
+ "programName": "春节促销广告",
629
+ "programNumber": 1,
630
+ "programType": "DYNAMIC"
631
+ },
632
+ "report": {
633
+ "level": "INFO",
634
+ "message": "节目上传并部署成功"
635
+ },
636
+ "timestamp": "2024-01-20T10:05:00Z",
637
+ "version": "1.0"
638
+ }
639
+ ```
640
+
641
+ ## 消息验证
642
+
643
+ 所有消息都应该通过验证后再处理:
644
+
645
+ ```typescript
646
+ // 使用提供的验证函数
647
+ import { validateMessage } from 'jrsoft-subway-protocol';
648
+
649
+ try {
650
+ const validMessage = validateMessage(rawMessage);
651
+ // 处理验证后的消息
652
+ } catch (error) {
653
+ // 处理验证错误
654
+ }
655
+ ```
656
+
657
+ ## 最佳实践
658
+
659
+ 1. **始终验证消息** - 使用提供的验证函数确保消息格式正确
660
+ 2. **使用类型守卫** - 使用 `isCommandMessage()` 等函数进行类型检查
661
+ 3. **处理所有消息类型** - 确保您的实现能处理所有可能的消息类型
662
+ 4. **保持向后兼容** - 使用 version 字段处理协议升级
663
+ 5. **及时响应** - 特别是心跳消息,应该立即响应
664
+
665
+ ## 相关文档
666
+
667
+ - [协议规范](./specification.md) - 完整的协议定义
668
+ - [命令系统](../02-commands/) - 详细的命令类型说明
669
+ - [API 参考](../06-reference/api.md) - 编程接口文档