@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,278 @@
|
|
|
1
|
+
# ProtocolUtils 使用指南
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`ProtocolUtils` 提供了一系列工具方法,用于处理协议中的枚举转换、消息类型判断和请求引用管理。
|
|
6
|
+
|
|
7
|
+
## 功能分类
|
|
8
|
+
|
|
9
|
+
### 1. 状态和枚举转换
|
|
10
|
+
|
|
11
|
+
#### 基础转换方法
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ProtocolUtils, CommandStatus, Priority, ClientType } from '@jrsoft/subway-protocol';
|
|
15
|
+
|
|
16
|
+
// CommandStatus 转换
|
|
17
|
+
const status = ProtocolUtils.stringToStatus('completed'); // => CommandStatus.COMPLETED
|
|
18
|
+
const statusStr = ProtocolUtils.statusToString(CommandStatus.FAILED); // => 'FAILED'
|
|
19
|
+
|
|
20
|
+
// Priority 转换
|
|
21
|
+
const priority = ProtocolUtils.priorityToEnum('high'); // => Priority.HIGH
|
|
22
|
+
const priorityStr = ProtocolUtils.enumToPriority(Priority.CRITICAL); // => 'CRITICAL'
|
|
23
|
+
|
|
24
|
+
// ClientType 转换
|
|
25
|
+
const clientType = ProtocolUtils.clientTypeToEnum('device'); // => ClientType.DEVICE
|
|
26
|
+
const typeStr = ProtocolUtils.enumToClientType(ClientType.BACKEND); // => 'BACKEND'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### 安全转换方法(不抛出异常)
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// 返回枚举值或 null
|
|
33
|
+
const status = ProtocolUtils.tryParseStatus('invalid'); // => null
|
|
34
|
+
const priority = ProtocolUtils.tryParsePriority('HIGH'); // => Priority.HIGH
|
|
35
|
+
const clientType = ProtocolUtils.tryParseClientType('backend'); // => ClientType.BACKEND
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### 2. 消息类型判断
|
|
39
|
+
|
|
40
|
+
增强的类型判断方法,不仅检查类型还验证结构:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
// 检查是否为命令消息
|
|
44
|
+
if (ProtocolUtils.isCommandMessage(message)) {
|
|
45
|
+
// TypeScript 知道 message 是 CommandMessage
|
|
46
|
+
console.log(message.command.commandCode);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 检查是否为进度更新
|
|
50
|
+
if (ProtocolUtils.isProgressUpdate(message)) {
|
|
51
|
+
console.log(`Progress: ${message.progress}%`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 检查是否为程序消息
|
|
55
|
+
if (ProtocolUtils.isProgramMessage(message)) {
|
|
56
|
+
console.log(message.command.parameters.programName);
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 3. 请求引用管理
|
|
61
|
+
|
|
62
|
+
#### 生成请求引用
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// 基础生成
|
|
66
|
+
const ref1 = ProtocolUtils.generateRequestRef();
|
|
67
|
+
// => "1703123456789-a1b2c3d4e"
|
|
68
|
+
|
|
69
|
+
// 带前缀生成
|
|
70
|
+
const ref2 = ProtocolUtils.generateRequestRef('backend');
|
|
71
|
+
// => "backend-1703123456789-f5g6h7i8j"
|
|
72
|
+
|
|
73
|
+
// 结构化生成
|
|
74
|
+
const ref3 = ProtocolUtils.generateStructuredRequestRef('gateway', 'sendCommand');
|
|
75
|
+
// => "gateway:sendcommand:1703123456789:k9l0m1"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
#### 提取和解析请求引用
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// 从消息中提取请求引用
|
|
82
|
+
const requestRef = ProtocolUtils.extractRequestRef(message);
|
|
83
|
+
|
|
84
|
+
// 解析请求引用
|
|
85
|
+
const parsed = ProtocolUtils.parseRequestRef('backend-1703123456789-a1b2c3d4e');
|
|
86
|
+
// => { prefix: 'backend', timestamp: 1703123456789, random: 'a1b2c3d4e' }
|
|
87
|
+
|
|
88
|
+
const structured = ProtocolUtils.parseRequestRef('gateway:sendcommand:1703123456789:k9l0m1');
|
|
89
|
+
// => { service: 'gateway', operation: 'sendcommand', timestamp: 1703123456789, random: 'k9l0m1' }
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 4. 辅助方法
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// 获取消息类型的友好名称
|
|
96
|
+
const typeName = ProtocolUtils.getMessageTypeName(message);
|
|
97
|
+
// => "Command Response"
|
|
98
|
+
|
|
99
|
+
// 检查是否为请求消息(需要响应)
|
|
100
|
+
if (ProtocolUtils.isRequestMessage(message)) {
|
|
101
|
+
// 处理需要响应的消息
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 检查是否为响应消息
|
|
105
|
+
if (ProtocolUtils.isResponseMessage(message)) {
|
|
106
|
+
// 处理响应消息
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## 实际使用场景
|
|
111
|
+
|
|
112
|
+
### 场景 1:处理外部 API 数据
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
// 从 REST API 接收的数据
|
|
116
|
+
const apiData = {
|
|
117
|
+
status: 'completed', // 小写
|
|
118
|
+
priority: 'HIGH',
|
|
119
|
+
client_type: 'backend' // 下划线格式
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// 转换为协议格式
|
|
123
|
+
const command = {
|
|
124
|
+
status: ProtocolUtils.stringToStatus(apiData.status),
|
|
125
|
+
priority: ProtocolUtils.priorityToEnum(apiData.priority),
|
|
126
|
+
clientType: ProtocolUtils.clientTypeToEnum(apiData.client_type.replace('_', ''))
|
|
127
|
+
};
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 场景 2:请求追踪和日志
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
class CommandService {
|
|
134
|
+
async sendCommand(command: Command, targetClientId: string) {
|
|
135
|
+
// 生成可追踪的请求 ID
|
|
136
|
+
const requestRef = ProtocolUtils.generateStructuredRequestRef('backend', 'command');
|
|
137
|
+
|
|
138
|
+
const message = MessageFactory.createCommandMessage(
|
|
139
|
+
requestRef,
|
|
140
|
+
targetClientId,
|
|
141
|
+
command,
|
|
142
|
+
'/api/callback'
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
logger.info(`Sending command ${requestRef}`);
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
const response = await this.gateway.send(message);
|
|
149
|
+
|
|
150
|
+
// 从响应中提取请求引用进行关联
|
|
151
|
+
const responseRef = ProtocolUtils.extractRequestRef(response);
|
|
152
|
+
logger.info(`Received response for ${responseRef}`);
|
|
153
|
+
|
|
154
|
+
} catch (error) {
|
|
155
|
+
logger.error(`Command ${requestRef} failed:`, error);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### 场景 3:消息路由和处理
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
class MessageRouter {
|
|
165
|
+
handleMessage(message: AnyMessage) {
|
|
166
|
+
// 获取友好的消息类型名称用于日志
|
|
167
|
+
const messageType = ProtocolUtils.getMessageTypeName(message);
|
|
168
|
+
logger.info(`Processing ${messageType} message`);
|
|
169
|
+
|
|
170
|
+
// 根据消息类型路由
|
|
171
|
+
if (ProtocolUtils.isRequestMessage(message)) {
|
|
172
|
+
this.handleRequest(message);
|
|
173
|
+
} else if (ProtocolUtils.isResponseMessage(message)) {
|
|
174
|
+
this.handleResponse(message);
|
|
175
|
+
} else if (ProtocolUtils.isProgressUpdate(message)) {
|
|
176
|
+
this.handleProgress(message as ProgressUpdateMessage);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private handleProgress(message: ProgressUpdateMessage) {
|
|
181
|
+
const requestRef = ProtocolUtils.extractRequestRef(message);
|
|
182
|
+
const parsed = ProtocolUtils.parseRequestRef(requestRef!);
|
|
183
|
+
|
|
184
|
+
if (parsed?.service === 'backend') {
|
|
185
|
+
// 处理来自 backend 的进度更新
|
|
186
|
+
this.notifyBackendProgress(message);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### 场景 4:错误处理和重试
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
class ErrorHandler {
|
|
196
|
+
handleError(error: any, originalMessage?: AnyMessage) {
|
|
197
|
+
let errorMessage: ErrorMessage;
|
|
198
|
+
|
|
199
|
+
if (error.code && error.message) {
|
|
200
|
+
// 创建错误消息
|
|
201
|
+
errorMessage = MessageFactory.createErrorMessage(
|
|
202
|
+
error.code,
|
|
203
|
+
error.message,
|
|
204
|
+
originalMessage ? ProtocolUtils.extractRequestRef(originalMessage) : undefined
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 安全地转换错误级别
|
|
209
|
+
const severity = ProtocolUtils.tryParseStatus(error.severity || 'FAILED');
|
|
210
|
+
if (severity) {
|
|
211
|
+
// 处理特定严重程度的错误
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### 场景 5:协议版本兼容
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
class LegacyAdapter {
|
|
221
|
+
// 处理旧版本的消息格式
|
|
222
|
+
adaptLegacyMessage(legacyMsg: any): AnyMessage | null {
|
|
223
|
+
// 旧版本可能使用不同的状态值
|
|
224
|
+
const status = this.mapLegacyStatus(legacyMsg.status);
|
|
225
|
+
const modernStatus = ProtocolUtils.tryParseStatus(status);
|
|
226
|
+
|
|
227
|
+
if (!modernStatus) {
|
|
228
|
+
logger.warn(`Unknown legacy status: ${legacyMsg.status}`);
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 构建现代消息格式
|
|
233
|
+
// ...
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private mapLegacyStatus(legacyStatus: string): string {
|
|
237
|
+
const mapping: Record<string, string> = {
|
|
238
|
+
'done': 'COMPLETED',
|
|
239
|
+
'error': 'FAILED',
|
|
240
|
+
'pending': 'IN_PROGRESS'
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
return mapping[legacyStatus] || legacyStatus;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## 最佳实践
|
|
249
|
+
|
|
250
|
+
1. **使用安全转换方法处理不可信输入**
|
|
251
|
+
```typescript
|
|
252
|
+
const status = ProtocolUtils.tryParseStatus(userInput) || CommandStatus.FAILED;
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
2. **生成结构化的请求引用便于追踪**
|
|
256
|
+
```typescript
|
|
257
|
+
const ref = ProtocolUtils.generateStructuredRequestRef(serviceName, operationName);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
3. **使用增强的类型判断进行完整验证**
|
|
261
|
+
```typescript
|
|
262
|
+
if (ProtocolUtils.isCommandMessage(message)) {
|
|
263
|
+
// 不仅类型正确,结构也已验证
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
4. **在日志中使用友好的消息类型名称**
|
|
268
|
+
```typescript
|
|
269
|
+
logger.info(`${ProtocolUtils.getMessageTypeName(message)} processed`);
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
5. **解析请求引用以获取上下文信息**
|
|
273
|
+
```typescript
|
|
274
|
+
const parsed = ProtocolUtils.parseRequestRef(requestRef);
|
|
275
|
+
if (parsed?.service === 'critical-service') {
|
|
276
|
+
// 优先处理关键服务的请求
|
|
277
|
+
}
|
|
278
|
+
```
|
package/README.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# JRSoft Subway WebSocket 协议
|
|
2
|
+
|
|
3
|
+
统一的 WebSocket 协议定义,供 Gateway、Backend 和 Edge 组件使用。
|
|
4
|
+
|
|
5
|
+
## 📋 概述
|
|
6
|
+
|
|
7
|
+
JRSoft Subway Protocol 是一个以 Gateway 为中心的实时通信协议,设计原则:
|
|
8
|
+
- **简单性优先** - 基础功能使用最简消息格式
|
|
9
|
+
- **渐进增强** - 通过可选字段支持高级功能
|
|
10
|
+
- **统一标准** - 所有组件使用 v1.0 协议
|
|
11
|
+
- **类型安全** - 完整的 TypeScript 支持
|
|
12
|
+
|
|
13
|
+
## 🏗️ 系统架构
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
┌─────────────┐ ┌─────────────┐ ┌──────────┐ ┌──────────┐
|
|
17
|
+
│ Backend │────▶│ Gateway │────▶│ Edge │────▶│ Device │
|
|
18
|
+
│ (18082) │◀────│ (18081) │◀────│ (Proxy) │◀────│(Client) │
|
|
19
|
+
└─────────────┘ └─────────────┘ └──────────┘ └──────────┘
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## 📚 文档结构
|
|
23
|
+
|
|
24
|
+
### [01. 协议规范](./docs/01-protocol/)
|
|
25
|
+
- [协议规范](./docs/01-protocol/specification.md) - 完整的协议定义
|
|
26
|
+
- [消息类型](./docs/01-protocol/message-types.md) - 所有消息类型详解
|
|
27
|
+
- [设计理念](./docs/01-protocol/design-rationale.md) - 协议设计决策
|
|
28
|
+
|
|
29
|
+
### [02. 命令系统](./docs/02-commands/)
|
|
30
|
+
- [Simple 命令](./docs/02-commands/simple-command.md) - 点对点命令
|
|
31
|
+
- [Batch 命令](./docs/02-commands/batch-command.md) - 批量控制命令
|
|
32
|
+
- [Complex 命令](./docs/02-commands/complex-command.md) - 复杂交互命令
|
|
33
|
+
- [强类型命令](./docs/02-commands/typed-commands.md) - TypeScript 类型安全
|
|
34
|
+
|
|
35
|
+
### [03. 架构设计](./docs/03-architecture/)
|
|
36
|
+
- [Edge 代理](./docs/03-architecture/edge-proxy.md) - Edge 节点设计
|
|
37
|
+
- [设备协议](./docs/03-architecture/device-protocol.md) - 设备连接规范
|
|
38
|
+
- [消息路由](./docs/03-architecture/routing-flow.md) - 路由机制详解
|
|
39
|
+
|
|
40
|
+
### [04. 集成指南](./docs/04-integration/)
|
|
41
|
+
- [Gateway 集成](./docs/04-integration/gateway-guide.md) - Gateway 实施指南
|
|
42
|
+
- [Backend 集成](./docs/04-integration/backend-guide.md) - Backend 实施指南
|
|
43
|
+
- [Edge 集成](./docs/04-integration/edge-guide.md) - Edge 实施指南
|
|
44
|
+
- [迁移指南](./docs/04-integration/migration-guide.md) - 版本迁移说明
|
|
45
|
+
|
|
46
|
+
### [05. 示例代码](./docs/05-examples/)
|
|
47
|
+
- 命令示例 - 各类命令的使用示例
|
|
48
|
+
- 流程示例 - 完整的业务流程
|
|
49
|
+
- 代码片段 - 常用代码模板
|
|
50
|
+
|
|
51
|
+
### [06. 参考文档](./docs/06-reference/)
|
|
52
|
+
- API 参考 - 完整的 API 文档
|
|
53
|
+
- 术语表 - 专业术语解释
|
|
54
|
+
- 常见问题 - FAQ
|
|
55
|
+
|
|
56
|
+
## 🚀 快速开始
|
|
57
|
+
|
|
58
|
+
### 安装
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm install jrsoft-subway-protocol
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 基本使用
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import {
|
|
68
|
+
createSimpleCommand,
|
|
69
|
+
validateMessage,
|
|
70
|
+
GatewayClient
|
|
71
|
+
} from 'jrsoft-subway-protocol';
|
|
72
|
+
|
|
73
|
+
// 创建命令
|
|
74
|
+
const command = createSimpleCommand({
|
|
75
|
+
targetClientId: 'td-01', // 直接指定设备ID
|
|
76
|
+
commandCode: 'SET_BRIGHTNESS',
|
|
77
|
+
deviceType: 'screen',
|
|
78
|
+
deviceId: 1,
|
|
79
|
+
operationType: 'write',
|
|
80
|
+
parameters: { brightness: 80 }
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// 验证消息
|
|
84
|
+
const valid = validateMessage(command);
|
|
85
|
+
|
|
86
|
+
// 发送命令
|
|
87
|
+
const client = new GatewayClient('ws://localhost:18081');
|
|
88
|
+
const response = await client.sendCommand(command);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 强类型命令
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { createLedSwitchCommand } from 'jrsoft-subway-protocol';
|
|
95
|
+
|
|
96
|
+
// TypeScript 提供完整的类型检查和智能提示
|
|
97
|
+
const ledCommand = createLedSwitchCommand(
|
|
98
|
+
'req-123', // requestRef
|
|
99
|
+
'td-01', // targetClientId (设备ID)
|
|
100
|
+
15, // deviceId (子硬件ID)
|
|
101
|
+
'ON', // switch: 'ON' | 'OFF'
|
|
102
|
+
'write' // operationType
|
|
103
|
+
);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## 🔑 核心特性
|
|
107
|
+
|
|
108
|
+
### 消息类型
|
|
109
|
+
- **register** - 客户端注册
|
|
110
|
+
- **command** - 命令请求
|
|
111
|
+
- **command_response** - 命令响应
|
|
112
|
+
- **progress_update** - 进度更新
|
|
113
|
+
- **program** - 节目上传
|
|
114
|
+
- **heartbeat** - 心跳保活
|
|
115
|
+
- **error** - 错误消息
|
|
116
|
+
|
|
117
|
+
### 命令类型
|
|
118
|
+
- **Simple** - 单次请求响应的简单命令
|
|
119
|
+
- **Batch** - 批量控制多个子硬件
|
|
120
|
+
- **Complex** - 支持多次响应的复杂命令
|
|
121
|
+
|
|
122
|
+
### 客户端类型
|
|
123
|
+
- **BACKEND** - 业务后端服务
|
|
124
|
+
- **GATEWAY** - 中心路由网关
|
|
125
|
+
- **EDGE** - 设备代理节点
|
|
126
|
+
- **DEVICE** - 终端设备
|
|
127
|
+
|
|
128
|
+
## 🛠️ 开发工具
|
|
129
|
+
|
|
130
|
+
### 类型定义
|
|
131
|
+
```typescript
|
|
132
|
+
// 所有类型定义都已导出
|
|
133
|
+
import type {
|
|
134
|
+
CommandMessage,
|
|
135
|
+
CommandResponseMessage,
|
|
136
|
+
ProgressUpdateMessage
|
|
137
|
+
} from 'jrsoft-subway-protocol';
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 验证工具
|
|
141
|
+
```typescript
|
|
142
|
+
// 消息验证
|
|
143
|
+
validateMessage(message);
|
|
144
|
+
|
|
145
|
+
// 类型守卫
|
|
146
|
+
if (isCommandMessage(message)) {
|
|
147
|
+
// TypeScript knows this is CommandMessage
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### 工厂函数
|
|
152
|
+
```typescript
|
|
153
|
+
// 创建各种消息
|
|
154
|
+
createRegisterMessage(clientId, clientType);
|
|
155
|
+
createHeartbeatMessage(clientId);
|
|
156
|
+
createErrorMessage(code, message);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 📦 项目结构
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
jrsoft-subway-protocol/
|
|
163
|
+
├── src/ # 源代码
|
|
164
|
+
│ ├── index.ts # 主入口,协议定义
|
|
165
|
+
│ ├── command-types.ts # 强类型命令定义
|
|
166
|
+
│ ├── command-factory.ts # 命令工厂函数
|
|
167
|
+
│ └── __tests__/ # 单元测试
|
|
168
|
+
├── docs/ # 文档
|
|
169
|
+
│ ├── 01-protocol/ # 协议规范
|
|
170
|
+
│ ├── 02-commands/ # 命令系统
|
|
171
|
+
│ ├── 03-architecture/ # 架构设计
|
|
172
|
+
│ ├── 04-integration/ # 集成指南
|
|
173
|
+
│ ├── 05-examples/ # 示例代码
|
|
174
|
+
│ └── 06-reference/ # 参考文档
|
|
175
|
+
├── examples/ # 示例项目
|
|
176
|
+
└── package.json # 项目配置
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## 📊 版本信息
|
|
180
|
+
|
|
181
|
+
- **当前版本**: 1.0.6
|
|
182
|
+
- **协议版本**: 1.0
|
|
183
|
+
- **最低 Node.js**: 14.0.0
|
|
184
|
+
- **TypeScript**: 4.5+
|
|
185
|
+
|
|
186
|
+
## 🌟 核心功能
|
|
187
|
+
|
|
188
|
+
### 连接管理
|
|
189
|
+
- 客户端注册与注销(含 ACK 确认)
|
|
190
|
+
- 心跳保活机制(含服务器状态)
|
|
191
|
+
- Edge 代理设备注册
|
|
192
|
+
- 会话管理和认证
|
|
193
|
+
|
|
194
|
+
### 命令系统
|
|
195
|
+
- **Simple 命令** - 快速点对点控制
|
|
196
|
+
- **Batch 命令** - 高效批量操作
|
|
197
|
+
- **Complex 命令** - 复杂交互流程
|
|
198
|
+
- **强类型命令** - 基于 C# 模型的类型安全
|
|
199
|
+
|
|
200
|
+
### 进度报告
|
|
201
|
+
- 统一的 progress_update 机制
|
|
202
|
+
- 支持自定义阶段
|
|
203
|
+
- 结构化日志(ReportMessage)
|
|
204
|
+
- 设备操作追踪
|
|
205
|
+
|
|
206
|
+
### 程序管理
|
|
207
|
+
- 大文件上传支持
|
|
208
|
+
- 多阶段进度反馈
|
|
209
|
+
- 断点续传能力
|
|
210
|
+
- 完整性校验
|
|
211
|
+
|
|
212
|
+
## 🤝 贡献指南
|
|
213
|
+
|
|
214
|
+
1. Fork 本仓库
|
|
215
|
+
2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
|
|
216
|
+
3. 提交更改 (`git commit -m 'Add amazing feature'`)
|
|
217
|
+
4. 推送到分支 (`git push origin feature/amazing-feature`)
|
|
218
|
+
5. 创建 Pull Request
|
|
219
|
+
|
|
220
|
+
## 📝 更新日志
|
|
221
|
+
|
|
222
|
+
查看 [CHANGELOG.md](./CHANGELOG.md) 了解版本更新历史。
|
|
223
|
+
|
|
224
|
+
## 📄 许可证
|
|
225
|
+
|
|
226
|
+
本项目采用 MIT 许可证 - 查看 [LICENSE](./LICENSE) 文件了解详情。
|
|
227
|
+
|
|
228
|
+
## 🔗 相关项目
|
|
229
|
+
|
|
230
|
+
- [jrsoft-subway-gateway](https://github.com/jrsoft/subway-gateway) - Gateway 实现
|
|
231
|
+
- [jrsoft-subway-backend](https://github.com/jrsoft/subway-backend) - Backend 实现
|
|
232
|
+
- [jrsoft-subway-edge](https://github.com/jrsoft/subway-edge) - Edge 实现
|
|
233
|
+
|
|
234
|
+
## 💬 联系方式
|
|
235
|
+
|
|
236
|
+
- 问题反馈: [GitHub Issues](https://github.com/jrsoft/subway-protocol/issues)
|
|
237
|
+
- 邮件: tech@jrsoft.com
|