@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,180 @@
|
|
|
1
|
+
# Gateway 集成指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
本指南说明如何在 jrsoft-subway-gateway 中使用统一的 WebSocket 协议包。
|
|
6
|
+
|
|
7
|
+
## 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
cd jrsoft-subway-gateway
|
|
11
|
+
npm install ../jrsoft-subway-protocol --save
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## 使用方式
|
|
15
|
+
|
|
16
|
+
### 1. 导入类型
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import {
|
|
20
|
+
MessageType,
|
|
21
|
+
RegisterMessage,
|
|
22
|
+
CommandMessage,
|
|
23
|
+
MessageFactory,
|
|
24
|
+
MessageValidator,
|
|
25
|
+
// ... 其他类型
|
|
26
|
+
} from '@jrsoft/subway-protocol';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 2. 处理消息
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// 使用枚举判断消息类型
|
|
33
|
+
switch (message.type) {
|
|
34
|
+
case MessageType.REGISTER:
|
|
35
|
+
handleRegister(message);
|
|
36
|
+
break;
|
|
37
|
+
case MessageType.COMMAND:
|
|
38
|
+
handleCommand(message);
|
|
39
|
+
break;
|
|
40
|
+
// ...
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 3. 创建响应
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
// 直接创建消息对象
|
|
48
|
+
const response: RegisterAckMessage = {
|
|
49
|
+
type: MessageType.REGISTER_ACK,
|
|
50
|
+
clientId: message.clientId,
|
|
51
|
+
success: true,
|
|
52
|
+
timestamp: new Date().toISOString()
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// 或使用消息工厂
|
|
56
|
+
const command = MessageFactory.createCommandMessage(
|
|
57
|
+
requestRef,
|
|
58
|
+
clientId,
|
|
59
|
+
commandDetails,
|
|
60
|
+
{ priority: Priority.HIGH }
|
|
61
|
+
);
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 4. 验证消息
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
if (!MessageValidator.validateCommandMessage(message)) {
|
|
68
|
+
throw new Error('Invalid command message');
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Gateway 特有扩展
|
|
73
|
+
|
|
74
|
+
Gateway 在协议包基础上扩展了以下类型:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// 设备连接信息
|
|
78
|
+
export interface DeviceConnection {
|
|
79
|
+
ws: WebSocket;
|
|
80
|
+
lastPing: number;
|
|
81
|
+
status: 'connected' | 'disconnecting' | 'reconnecting';
|
|
82
|
+
connectionTime: number;
|
|
83
|
+
reconnectAttempts: number;
|
|
84
|
+
metadata?: Record<string, unknown>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 设备映射
|
|
88
|
+
export type DeviceMap = Map<string, DeviceConnection>;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Edge 代理支持
|
|
92
|
+
|
|
93
|
+
协议包内置了 Edge 代理支持,一个 Edge 节点可以管理一套或多套隧道媒体广告播放系统的设备端:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
// Edge 注册
|
|
97
|
+
const edgeRegister: RegisterMessage = {
|
|
98
|
+
type: MessageType.REGISTER,
|
|
99
|
+
clientId: "edge-01",
|
|
100
|
+
clientType: ClientType.EDGE,
|
|
101
|
+
clientInfo: {
|
|
102
|
+
version: "1.0",
|
|
103
|
+
capabilities: ["device-proxy", "batch-command"]
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// Edge 转发命令到具体设备
|
|
108
|
+
const edgeCommand: EdgeDeviceMessage = {
|
|
109
|
+
type: MessageType.COMMAND,
|
|
110
|
+
edgeId: "edge-station-01",
|
|
111
|
+
targetDeviceId: "device-123",
|
|
112
|
+
command: { /* ... */ }
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### 进度更新处理
|
|
117
|
+
|
|
118
|
+
Gateway 需要正确处理和转发进度更新消息:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import {
|
|
122
|
+
isProgressUpdateMessage,
|
|
123
|
+
ProgressStatus,
|
|
124
|
+
ProgressPhase
|
|
125
|
+
} from '@jrsoft/subway-protocol';
|
|
126
|
+
|
|
127
|
+
// 处理进度更新
|
|
128
|
+
function handleProgressUpdate(message: ProgressUpdateMessage, clientId: string) {
|
|
129
|
+
// 记录日志
|
|
130
|
+
if (message.log) {
|
|
131
|
+
logger.log(message.log.level,
|
|
132
|
+
`[${clientId}] ${message.log.code || ''}: ${message.message}`,
|
|
133
|
+
message.log.data
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 根据状态处理
|
|
138
|
+
switch (message.status) {
|
|
139
|
+
case ProgressStatus.FAILED:
|
|
140
|
+
// 记录失败信息
|
|
141
|
+
errorTracker.record(clientId, message.requestRef, message.log);
|
|
142
|
+
break;
|
|
143
|
+
|
|
144
|
+
case ProgressStatus.COMPLETED:
|
|
145
|
+
// 更新完成统计
|
|
146
|
+
metrics.recordCompletion(clientId, message.phase, message.timestamp);
|
|
147
|
+
break;
|
|
148
|
+
|
|
149
|
+
case ProgressStatus.PAUSED:
|
|
150
|
+
case ProgressStatus.CANCELLED:
|
|
151
|
+
// 通知相关方
|
|
152
|
+
notifyStatusChange(clientId, message.requestRef, message.status);
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// 转发到后端
|
|
157
|
+
forwardToBackend(message, clientId);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 进度汇总
|
|
161
|
+
function aggregateProgress(requestRef: string): ProgressSummary {
|
|
162
|
+
const updates = progressCache.get(requestRef) || [];
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
totalProgress: calculateOverallProgress(updates),
|
|
166
|
+
currentPhase: getCurrentPhase(updates),
|
|
167
|
+
status: getOverallStatus(updates),
|
|
168
|
+
logs: extractLogs(updates),
|
|
169
|
+
deviceCommands: extractDeviceCommands(updates)
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 注意事项
|
|
175
|
+
|
|
176
|
+
1. **使用 v1.0 标准协议** - 统一的协议定义
|
|
177
|
+
2. **使用枚举类型** - 避免硬编码字符串
|
|
178
|
+
3. **时间戳格式** - 使用 ISO 8601 格式
|
|
179
|
+
4. **消息验证** - 使用内置验证器
|
|
180
|
+
5. **类型安全** - 充分利用 TypeScript 类型系统
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# WebSocket 协议统一迁移指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
本指南帮助将 Gateway 和 Backend 迁移到统一的 WebSocket 协议(v2.0)。新协议设计为向后兼容,支持渐进式升级。
|
|
6
|
+
|
|
7
|
+
## 主要变更
|
|
8
|
+
|
|
9
|
+
### 1. 注册消息的统一
|
|
10
|
+
|
|
11
|
+
**当前差异:**
|
|
12
|
+
- Gateway: 只需要 `type` 和 `clientId`
|
|
13
|
+
- Backend: 需要 `clientType`、`clientInfo` 等额外字段
|
|
14
|
+
|
|
15
|
+
**统一方案:**
|
|
16
|
+
```typescript
|
|
17
|
+
// 简单注册(向后兼容)
|
|
18
|
+
{
|
|
19
|
+
"type": "REGISTER",
|
|
20
|
+
"clientId": "device001"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 完整注册(新协议)
|
|
24
|
+
{
|
|
25
|
+
"type": "REGISTER",
|
|
26
|
+
"clientId": "backend-server",
|
|
27
|
+
"clientType": "BACKEND",
|
|
28
|
+
"clientInfo": {
|
|
29
|
+
"version": "1.0.0",
|
|
30
|
+
"platform": "nodejs",
|
|
31
|
+
"capabilities": ["command", "program"]
|
|
32
|
+
},
|
|
33
|
+
"timestamp": "2024-01-01T00:00:00Z",
|
|
34
|
+
"version": "2.0"
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. 命令状态的统一
|
|
39
|
+
|
|
40
|
+
**当前差异:**
|
|
41
|
+
- Gateway: 包含 `in_progress` 状态
|
|
42
|
+
- Backend: 不包含 `in_progress` 状态
|
|
43
|
+
|
|
44
|
+
**统一方案:**
|
|
45
|
+
- 所有状态都被支持,组件可选择性使用
|
|
46
|
+
|
|
47
|
+
## 迁移步骤
|
|
48
|
+
|
|
49
|
+
### 第一阶段:添加协议包依赖
|
|
50
|
+
|
|
51
|
+
1. **安装共享协议包**
|
|
52
|
+
```bash
|
|
53
|
+
# 在 Gateway 项目
|
|
54
|
+
cd jrsoft-subway-gateway
|
|
55
|
+
npm install ../jrsoft-subway-protocol
|
|
56
|
+
|
|
57
|
+
# 在 Backend 项目
|
|
58
|
+
cd jrsoft-subway-backend
|
|
59
|
+
npm install ../jrsoft-subway-protocol
|
|
60
|
+
|
|
61
|
+
# 在 Edge 项目
|
|
62
|
+
cd jrsoft-subway-edge
|
|
63
|
+
npm install ../jrsoft-subway-protocol
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 第二阶段:更新 Gateway(保持向后兼容)
|
|
67
|
+
|
|
68
|
+
1. **更新消息处理器以支持扩展字段**
|
|
69
|
+
```typescript
|
|
70
|
+
// src/ws/websocket-handler.ts
|
|
71
|
+
import { RegisterMessage, MessageValidator } from '@jrsoft/subway-protocol';
|
|
72
|
+
|
|
73
|
+
private handleRegister(ws: WebSocket, message: RegisterMessage) {
|
|
74
|
+
const { clientId, clientType, clientInfo } = message;
|
|
75
|
+
|
|
76
|
+
// 存储扩展信息(如果提供)
|
|
77
|
+
const client = {
|
|
78
|
+
ws,
|
|
79
|
+
clientId,
|
|
80
|
+
clientType: clientType || 'device', // 默认为device
|
|
81
|
+
clientInfo: clientInfo || {},
|
|
82
|
+
registeredAt: new Date()
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
this.clients.set(clientId, client);
|
|
86
|
+
|
|
87
|
+
// 发送确认,包含sessionId
|
|
88
|
+
const ack = {
|
|
89
|
+
type: 'register_ack',
|
|
90
|
+
clientId,
|
|
91
|
+
success: true,
|
|
92
|
+
sessionId: generateSessionId(),
|
|
93
|
+
timestamp: new Date().toISOString()
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
ws.send(JSON.stringify(ack));
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
2. **添加协议版本协商**
|
|
101
|
+
```typescript
|
|
102
|
+
// 在连接建立时检查协议版本
|
|
103
|
+
ws.on('message', (data) => {
|
|
104
|
+
const message = JSON.parse(data.toString());
|
|
105
|
+
|
|
106
|
+
// 记录客户端协议版本
|
|
107
|
+
if (message.version) {
|
|
108
|
+
ws.protocolVersion = message.version;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 处理消息...
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### 第三阶段:更新 Backend(使用新协议)
|
|
116
|
+
|
|
117
|
+
1. **使用共享协议定义**
|
|
118
|
+
```typescript
|
|
119
|
+
// src/services/gateway.service.ts
|
|
120
|
+
import {
|
|
121
|
+
MessageFactory,
|
|
122
|
+
MessageType,
|
|
123
|
+
ClientType
|
|
124
|
+
} from '@jrsoft/subway-protocol';
|
|
125
|
+
|
|
126
|
+
private registerWithGateway() {
|
|
127
|
+
const registerMsg = MessageFactory.createRegisterMessage(
|
|
128
|
+
this.clientId,
|
|
129
|
+
ClientType.BACKEND,
|
|
130
|
+
{
|
|
131
|
+
version: '1.0.0',
|
|
132
|
+
platform: 'nodejs',
|
|
133
|
+
capabilities: ['command', 'program']
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
this.ws.send(JSON.stringify(registerMsg));
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
2. **处理简化的响应**
|
|
142
|
+
```typescript
|
|
143
|
+
private handleMessage(data: any) {
|
|
144
|
+
const message = JSON.parse(data);
|
|
145
|
+
|
|
146
|
+
switch (message.type) {
|
|
147
|
+
case 'register_ack':
|
|
148
|
+
// 检查是否有sessionId(新协议)
|
|
149
|
+
if (message.sessionId) {
|
|
150
|
+
this.sessionId = message.sessionId;
|
|
151
|
+
}
|
|
152
|
+
break;
|
|
153
|
+
// ...
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### 第四阶段:测试和验证
|
|
159
|
+
|
|
160
|
+
1. **创建协议兼容性测试**
|
|
161
|
+
```typescript
|
|
162
|
+
// test/protocol-compatibility.test.ts
|
|
163
|
+
describe('Protocol Compatibility', () => {
|
|
164
|
+
it('should handle v1.0 simple registration', async () => {
|
|
165
|
+
const msg = { type: 'register', clientId: 'test' };
|
|
166
|
+
// 验证Gateway能处理
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should handle v2.0 full registration', async () => {
|
|
170
|
+
const msg = MessageFactory.createRegisterMessage(
|
|
171
|
+
'test',
|
|
172
|
+
ClientType.BACKEND,
|
|
173
|
+
{ version: '1.0.0' }
|
|
174
|
+
);
|
|
175
|
+
// 验证Gateway能处理
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## 协议实现要求
|
|
181
|
+
|
|
182
|
+
1. **支持两种注册格式**
|
|
183
|
+
- 简单格式:仅 `type` 和 `clientId`
|
|
184
|
+
- 完整格式:包含所有扩展字段
|
|
185
|
+
|
|
186
|
+
2. **使用统一的消息类型**
|
|
187
|
+
- 使用 MessageType 枚举确保类型安全
|
|
188
|
+
- 避免硬编码字符串
|
|
189
|
+
|
|
190
|
+
3. **处理可选字段**
|
|
191
|
+
- `timestamp`、`version` 等字段为可选
|
|
192
|
+
- 缺失时使用合理的默认值
|
|
193
|
+
|
|
194
|
+
## 监控和回滚计划
|
|
195
|
+
|
|
196
|
+
1. **添加协议版本监控**
|
|
197
|
+
```typescript
|
|
198
|
+
// 记录每个连接的协议版本
|
|
199
|
+
logger.info('Client connected', {
|
|
200
|
+
clientId,
|
|
201
|
+
protocolVersion: message.version || '1.0',
|
|
202
|
+
clientType: message.clientType || 'unknown'
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
2. **准备回滚脚本**
|
|
207
|
+
- 保留旧的协议处理代码
|
|
208
|
+
- 通过特性开关控制新协议启用
|
|
209
|
+
|
|
210
|
+
## 时间表
|
|
211
|
+
|
|
212
|
+
- **第1周**: 部署更新的 Gateway(向后兼容)
|
|
213
|
+
- **第2周**: 逐步升级 Backend 使用新协议
|
|
214
|
+
- **第3周**: 升级 Edge 和其他客户端
|
|
215
|
+
- **第4周**: 监控和优化
|
|
216
|
+
|
|
217
|
+
## 常见问题
|
|
218
|
+
|
|
219
|
+
**Q: 旧版本客户端会受影响吗?**
|
|
220
|
+
A: 不会。新协议完全向后兼容,旧客户端可继续使用简单注册。
|
|
221
|
+
|
|
222
|
+
**Q: 如何知道客户端使用的协议版本?**
|
|
223
|
+
A: 检查消息中的 `version` 字段,缺失则为 v1.0。
|
|
224
|
+
|
|
225
|
+
**Q: 性能会受影响吗?**
|
|
226
|
+
A: 几乎没有影响。额外字段是可选的,不会增加必需的网络开销。
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# 示例文档
|
|
2
|
+
|
|
3
|
+
本目录包含 JRSoft Subway 协议的各种使用示例和代码片段。
|
|
4
|
+
|
|
5
|
+
## 📚 文档列表
|
|
6
|
+
|
|
7
|
+
### [命令示例](./command-examples.md)
|
|
8
|
+
各种命令类型的完整示例:
|
|
9
|
+
- Simple 命令示例
|
|
10
|
+
- Batch 命令示例
|
|
11
|
+
- Complex 命令示例
|
|
12
|
+
- 强类型命令示例
|
|
13
|
+
|
|
14
|
+
### [流程示例](./flow-examples.md)
|
|
15
|
+
完整的业务流程示例:
|
|
16
|
+
- 设备控制流程
|
|
17
|
+
- 批量操作流程
|
|
18
|
+
- 健康检查流程
|
|
19
|
+
- 节目上传流程
|
|
20
|
+
|
|
21
|
+
### [代码片段](./code-snippets.md)
|
|
22
|
+
常用的代码片段:
|
|
23
|
+
- 连接管理
|
|
24
|
+
- 错误处理
|
|
25
|
+
- 消息验证
|
|
26
|
+
- 性能优化
|
|
27
|
+
|
|
28
|
+
## 🚀 快速查找
|
|
29
|
+
|
|
30
|
+
### 按功能分类
|
|
31
|
+
|
|
32
|
+
#### 设备控制
|
|
33
|
+
- [LED 开关控制](#led-控制)
|
|
34
|
+
- [亮度调节](#亮度调节)
|
|
35
|
+
- [颜色设置](#颜色设置)
|
|
36
|
+
- [播放控制](#播放控制)
|
|
37
|
+
|
|
38
|
+
#### 批量操作
|
|
39
|
+
- [批量设置颜色](#批量颜色设置)
|
|
40
|
+
- [批量开关控制](#批量开关)
|
|
41
|
+
- [批量状态查询](#批量查询)
|
|
42
|
+
|
|
43
|
+
#### 系统管理
|
|
44
|
+
- [健康检查](#健康检查)
|
|
45
|
+
- [配置更新](#配置更新)
|
|
46
|
+
- [日志收集](#日志收集)
|
|
47
|
+
|
|
48
|
+
## 💡 使用提示
|
|
49
|
+
|
|
50
|
+
1. **复制粘贴友好** - 所有示例都可以直接复制使用
|
|
51
|
+
2. **完整可运行** - 每个示例都包含必要的导入和配置
|
|
52
|
+
3. **注释详细** - 关键代码都有详细注释
|
|
53
|
+
4. **错误处理** - 包含错误处理的最佳实践
|
|
54
|
+
|
|
55
|
+
## 📋 示例模板
|
|
56
|
+
|
|
57
|
+
### TypeScript/JavaScript
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// 导入必要的模块
|
|
61
|
+
import {
|
|
62
|
+
createLedSwitchCommand,
|
|
63
|
+
GatewayClient,
|
|
64
|
+
Priority
|
|
65
|
+
} from '@jrsoft/subway-protocol';
|
|
66
|
+
|
|
67
|
+
// 创建客户端
|
|
68
|
+
const client = new GatewayClient('ws://localhost:18081', 'backend-001');
|
|
69
|
+
|
|
70
|
+
// 连接并发送命令
|
|
71
|
+
async function example() {
|
|
72
|
+
try {
|
|
73
|
+
await client.connect();
|
|
74
|
+
|
|
75
|
+
const command = createLedSwitchCommand(
|
|
76
|
+
'req-001',
|
|
77
|
+
'td-01',
|
|
78
|
+
1,
|
|
79
|
+
'ON',
|
|
80
|
+
'write'
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const response = await client.sendCommand(command);
|
|
84
|
+
console.log('Response:', response);
|
|
85
|
+
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.error('Error:', error);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Python
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import asyncio
|
|
96
|
+
import websockets
|
|
97
|
+
from datetime import datetime
|
|
98
|
+
|
|
99
|
+
async def send_command():
|
|
100
|
+
async with websockets.connect('ws://localhost:18081') as websocket:
|
|
101
|
+
# 注册
|
|
102
|
+
await websocket.send(json.dumps({
|
|
103
|
+
"type": "REGISTER",
|
|
104
|
+
"clientId": "backend-001",
|
|
105
|
+
"clientType": "BACKEND",
|
|
106
|
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
|
107
|
+
"version": "1.0"
|
|
108
|
+
}))
|
|
109
|
+
|
|
110
|
+
# 发送命令
|
|
111
|
+
command = {
|
|
112
|
+
"type": "COMMAND",
|
|
113
|
+
"requestRef": "cmd-001",
|
|
114
|
+
"targetClientId": "td-01",
|
|
115
|
+
"command": {
|
|
116
|
+
"commandType": "SIMPLE",
|
|
117
|
+
"commandCode": "LedSwitch",
|
|
118
|
+
"deviceType": "pillar",
|
|
119
|
+
"deviceId": 1,
|
|
120
|
+
"operationType": "WRITE",
|
|
121
|
+
"parameters": {"switch": "ON"}
|
|
122
|
+
},
|
|
123
|
+
"priority": "NORMAL",
|
|
124
|
+
"timeout": 5000,
|
|
125
|
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
|
126
|
+
"version": "1.0"
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
await websocket.send(json.dumps(command))
|
|
130
|
+
|
|
131
|
+
# 接收响应
|
|
132
|
+
response = await websocket.recv()
|
|
133
|
+
print(f"Response: {response}")
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## 🔗 相关资源
|
|
137
|
+
|
|
138
|
+
- [协议规范](../01-protocol/) - 了解协议细节
|
|
139
|
+
- [命令系统](../02-commands/) - 了解命令类型
|
|
140
|
+
- [API 参考](../06-reference/api.md) - 完整的 API 文档
|
|
141
|
+
- [示例代码仓库](../../examples/) - 完整的示例项目
|