@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,67 @@
|
|
|
1
|
+
# 参考文档
|
|
2
|
+
|
|
3
|
+
本目录包含 JRSoft Subway 协议的参考资料。
|
|
4
|
+
|
|
5
|
+
## 📚 文档列表
|
|
6
|
+
|
|
7
|
+
### [API 参考](./api.md)
|
|
8
|
+
完整的 API 文档:
|
|
9
|
+
- 接口定义
|
|
10
|
+
- 类型说明
|
|
11
|
+
- 方法签名
|
|
12
|
+
- 参数详解
|
|
13
|
+
|
|
14
|
+
### [术语表](./glossary.md)
|
|
15
|
+
项目中使用的专业术语:
|
|
16
|
+
- 协议术语
|
|
17
|
+
- 系统组件
|
|
18
|
+
- 技术概念
|
|
19
|
+
- 业务术语
|
|
20
|
+
|
|
21
|
+
### [常见问题](./faq.md)
|
|
22
|
+
常见问题解答:
|
|
23
|
+
- 协议相关
|
|
24
|
+
- 集成问题
|
|
25
|
+
- 性能优化
|
|
26
|
+
- 故障排查
|
|
27
|
+
|
|
28
|
+
## 🔍 快速查找
|
|
29
|
+
|
|
30
|
+
### API 分类
|
|
31
|
+
|
|
32
|
+
#### 消息类型
|
|
33
|
+
- [BaseMessage](./api.md#basemessage)
|
|
34
|
+
- [RegisterMessage](./api.md#registermessage)
|
|
35
|
+
- [CommandMessage](./api.md#commandmessage)
|
|
36
|
+
- [CommandResponseMessage](./api.md#commandresponsemessage)
|
|
37
|
+
|
|
38
|
+
#### 工具函数
|
|
39
|
+
- [createSimpleCommand](./api.md#createsimplecommand)
|
|
40
|
+
- [createBatchCommand](./api.md#createbatchcommand)
|
|
41
|
+
- [validateMessage](./api.md#validatemessage)
|
|
42
|
+
|
|
43
|
+
#### 类型守卫
|
|
44
|
+
- [isCommandMessage](./api.md#iscommandmessage)
|
|
45
|
+
- [isErrorMessage](./api.md#iserrormessage)
|
|
46
|
+
|
|
47
|
+
### 常用术语
|
|
48
|
+
|
|
49
|
+
- **Gateway** - 中心路由节点
|
|
50
|
+
- **Edge** - 设备代理节点
|
|
51
|
+
- **Device** - 终端设备
|
|
52
|
+
- **Command** - 控制命令
|
|
53
|
+
- **Batch** - 批量操作
|
|
54
|
+
- **Progress Update** - 进度更新
|
|
55
|
+
|
|
56
|
+
## 📖 使用方式
|
|
57
|
+
|
|
58
|
+
1. **API 查询** - 需要查找特定接口时,查看 [API 参考](./api.md)
|
|
59
|
+
2. **概念理解** - 遇到不熟悉的术语时,查看 [术语表](./glossary.md)
|
|
60
|
+
3. **问题解决** - 遇到常见问题时,查看 [FAQ](./faq.md)
|
|
61
|
+
|
|
62
|
+
## 🔗 外部链接
|
|
63
|
+
|
|
64
|
+
- [TypeScript 文档](https://www.typescriptlang.org/docs/)
|
|
65
|
+
- [WebSocket 规范](https://datatracker.ietf.org/doc/html/rfc6455)
|
|
66
|
+
- [JSON Schema](https://json-schema.org/)
|
|
67
|
+
- [ISO 8601 时间格式](https://en.wikipedia.org/wiki/ISO_8601)
|
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
# API 参考文档
|
|
2
|
+
|
|
3
|
+
本文档提供 JRSoft Subway Protocol 的完整 API 参考。
|
|
4
|
+
|
|
5
|
+
## 目录
|
|
6
|
+
|
|
7
|
+
- [枚举类型](#枚举类型)
|
|
8
|
+
- [接口定义](#接口定义)
|
|
9
|
+
- [类型别名](#类型别名)
|
|
10
|
+
- [工厂函数](#工厂函数)
|
|
11
|
+
- [验证函数](#验证函数)
|
|
12
|
+
- [类型守卫](#类型守卫)
|
|
13
|
+
|
|
14
|
+
## 枚举类型
|
|
15
|
+
|
|
16
|
+
### MessageType
|
|
17
|
+
|
|
18
|
+
消息类型枚举,定义所有支持的消息类型。
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
export enum MessageType {
|
|
22
|
+
// 连接管理
|
|
23
|
+
REGISTER = 'REGISTER',
|
|
24
|
+
REGISTER_ACK = 'REGISTER_ACK',
|
|
25
|
+
UNREGISTER = 'UNREGISTER',
|
|
26
|
+
UNREGISTER_ACK = 'UNREGISTER_ACK',
|
|
27
|
+
|
|
28
|
+
// 心跳
|
|
29
|
+
HEARTBEAT = 'HEARTBEAT',
|
|
30
|
+
HEARTBEAT_ACK = 'HEARTBEAT_ACK',
|
|
31
|
+
|
|
32
|
+
// 命令执行
|
|
33
|
+
COMMAND = 'COMMAND',
|
|
34
|
+
COMMAND_RESPONSE = 'COMMAND_RESPONSE',
|
|
35
|
+
|
|
36
|
+
// 程序管理
|
|
37
|
+
PROGRAM = 'PROGRAM',
|
|
38
|
+
PROGRAM_RESPONSE = 'PROGRAM_RESPONSE',
|
|
39
|
+
|
|
40
|
+
// 进度更新
|
|
41
|
+
PROGRESS_UPDATE = 'PROGRESS_UPDATE',
|
|
42
|
+
|
|
43
|
+
// 错误
|
|
44
|
+
ERROR = 'ERROR'
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### ClientType
|
|
49
|
+
|
|
50
|
+
客户端类型枚举。
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
export enum ClientType {
|
|
54
|
+
DEVICE = 'DEVICE', // 终端设备
|
|
55
|
+
BACKEND = 'BACKEND', // 后端服务
|
|
56
|
+
EDGE = 'EDGE', // 边缘节点
|
|
57
|
+
GATEWAY = 'GATEWAY' // 网关服务
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### CommandType
|
|
62
|
+
|
|
63
|
+
命令类型枚举。
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
export enum CommandType {
|
|
67
|
+
SIMPLE = 'SIMPLE', // 简单命令(单次响应)
|
|
68
|
+
BATCH = 'BATCH', // 批量命令(多设备)
|
|
69
|
+
COMPLEX = 'COMPLEX' // 复杂命令(多次响应)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### OperationType
|
|
74
|
+
|
|
75
|
+
操作类型枚举。
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
export enum OperationType {
|
|
79
|
+
READ = 'READ', // 读取操作
|
|
80
|
+
WRITE = 'WRITE' // 写入操作
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Priority
|
|
85
|
+
|
|
86
|
+
优先级枚举。
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
export enum Priority {
|
|
90
|
+
LOW = 'LOW',
|
|
91
|
+
NORMAL = 'NORMAL',
|
|
92
|
+
HIGH = 'HIGH',
|
|
93
|
+
CRITICAL = 'CRITICAL'
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### CommandStatus
|
|
98
|
+
|
|
99
|
+
命令状态枚举。
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
export enum CommandStatus {
|
|
103
|
+
COMPLETED = 'COMPLETED', // 已完成
|
|
104
|
+
FAILED = 'FAILED', // 失败
|
|
105
|
+
TIMEOUT = 'TIMEOUT', // 超时
|
|
106
|
+
CANCELLED = 'CANCELLED', // 已取消
|
|
107
|
+
IN_PROGRESS = 'IN_PROGRESS' // 执行中
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### ProgressStatus
|
|
112
|
+
|
|
113
|
+
进度状态枚举。
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
export enum ProgressStatus {
|
|
117
|
+
PENDING = 'PENDING',
|
|
118
|
+
IN_PROGRESS = 'IN_PROGRESS',
|
|
119
|
+
PAUSED = 'PAUSED',
|
|
120
|
+
COMPLETED = 'COMPLETED',
|
|
121
|
+
FAILED = 'FAILED',
|
|
122
|
+
CANCELLED = 'CANCELLED'
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### ProgressPhase
|
|
127
|
+
|
|
128
|
+
进度阶段枚举。
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
export enum ProgressPhase {
|
|
132
|
+
DOWNLOAD = 'DOWNLOAD', // 下载
|
|
133
|
+
DECOMPRESS = 'DECOMPRESS', // 解压
|
|
134
|
+
PREPROCESS = 'PREPROCESS', // 预处理
|
|
135
|
+
FRAMES = 'FRAMES', // 创建帧
|
|
136
|
+
UPLOAD = 'UPLOAD', // 上传
|
|
137
|
+
STATS = 'STATS' // 统计
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### ReportLevel
|
|
142
|
+
|
|
143
|
+
日志级别枚举。
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
export enum ReportLevel {
|
|
147
|
+
DEBUG = 'DEBUG',
|
|
148
|
+
INFO = 'INFO',
|
|
149
|
+
WARNING = 'WARNING',
|
|
150
|
+
ERROR = 'ERROR',
|
|
151
|
+
CRITICAL = 'CRITICAL'
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### ProgramType
|
|
156
|
+
|
|
157
|
+
程序类型枚举。
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
export enum ProgramType {
|
|
161
|
+
DYNAMIC = 'DYNAMIC', // 动态节目
|
|
162
|
+
STATIC = 'STATIC' // 静态节目
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### ProgramDirection
|
|
167
|
+
|
|
168
|
+
程序方向枚举。
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
export enum ProgramDirection {
|
|
172
|
+
LEFT_TO_RIGHT = 'LEFT_TO_RIGHT',
|
|
173
|
+
RIGHT_TO_LEFT = 'RIGHT_TO_LEFT'
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## 接口定义
|
|
178
|
+
|
|
179
|
+
### BaseMessage
|
|
180
|
+
|
|
181
|
+
所有消息的基础接口。
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
export interface BaseMessage {
|
|
185
|
+
type: MessageType; // 消息类型
|
|
186
|
+
timestamp: string; // ISO 8601 时间戳
|
|
187
|
+
version: string; // 协议版本,固定 "1.0"
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### RegisterMessage
|
|
192
|
+
|
|
193
|
+
客户端注册消息。
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
export interface RegisterMessage extends BaseMessage {
|
|
197
|
+
type: MessageType.REGISTER;
|
|
198
|
+
clientId: string; // 客户端唯一标识
|
|
199
|
+
clientType: ClientType; // 客户端类型
|
|
200
|
+
clientInfo?: ClientInfo; // 客户端信息(可选)
|
|
201
|
+
edgeInfo?: EdgeInfo; // Edge 信息(设备通过 Edge 注册时)
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### RegisterAckMessage
|
|
206
|
+
|
|
207
|
+
注册确认消息。
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
export interface RegisterAckMessage extends BaseMessage {
|
|
211
|
+
type: MessageType.REGISTER_ACK;
|
|
212
|
+
clientId: string;
|
|
213
|
+
success: boolean;
|
|
214
|
+
sessionId?: string; // 会话ID(成功时)
|
|
215
|
+
error?: {
|
|
216
|
+
code: string;
|
|
217
|
+
message: string;
|
|
218
|
+
};
|
|
219
|
+
serverInfo?: {
|
|
220
|
+
version: string;
|
|
221
|
+
capabilities: string[];
|
|
222
|
+
currentLoad?: number;
|
|
223
|
+
maxClients?: number;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### CommandMessage
|
|
229
|
+
|
|
230
|
+
命令请求消息。
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
export interface CommandMessage extends BaseMessage {
|
|
234
|
+
type: MessageType.COMMAND;
|
|
235
|
+
requestRef: string; // 请求引用ID
|
|
236
|
+
targetClientId: string; // 目标客户端ID
|
|
237
|
+
command: Command; // 命令详情
|
|
238
|
+
priority: Priority; // 优先级
|
|
239
|
+
timeout: number; // 超时时间(毫秒)
|
|
240
|
+
retryCount?: number; // 重试次数
|
|
241
|
+
callback: string; // 回调URL
|
|
242
|
+
metadata?: Record<string, any>; // 元数据
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### CommandResponseMessage
|
|
247
|
+
|
|
248
|
+
命令响应消息。
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
export interface CommandResponseMessage extends BaseMessage {
|
|
252
|
+
type: MessageType.COMMAND_RESPONSE;
|
|
253
|
+
requestRef: string; // 原始请求引用
|
|
254
|
+
status: CommandStatus; // 执行状态
|
|
255
|
+
result?: CommandResult; // 执行结果
|
|
256
|
+
report?: ReportMessage; // 日志信息
|
|
257
|
+
executionTime?: number; // 执行时间(毫秒)
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### ProgressUpdateMessage
|
|
262
|
+
|
|
263
|
+
进度更新消息。
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
export interface ProgressUpdateMessage extends BaseMessage {
|
|
267
|
+
type: MessageType.PROGRESS_UPDATE;
|
|
268
|
+
requestRef: string;
|
|
269
|
+
status: ProgressStatus;
|
|
270
|
+
phase: ProgressPhase | string; // 支持自定义阶段
|
|
271
|
+
progress: number; // 0-100
|
|
272
|
+
sourceType: 'COMMAND' | 'SYSTEM';
|
|
273
|
+
context?: ProgramContext; // 程序上下文
|
|
274
|
+
command?: DeviceOperationRecord; // 设备操作记录
|
|
275
|
+
report?: ReportMessage; // 日志信息
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### ErrorMessage
|
|
280
|
+
|
|
281
|
+
错误消息。
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
export interface ErrorMessage extends BaseMessage {
|
|
285
|
+
type: MessageType.ERROR;
|
|
286
|
+
code: string; // 错误代码
|
|
287
|
+
message: string; // 错误描述
|
|
288
|
+
severity?: string; // 严重级别
|
|
289
|
+
category?: string; // 错误类别
|
|
290
|
+
context?: any; // 错误上下文
|
|
291
|
+
retryable?: boolean; // 是否可重试
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### 辅助接口
|
|
296
|
+
|
|
297
|
+
#### ClientInfo
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
export interface ClientInfo {
|
|
301
|
+
name?: string;
|
|
302
|
+
version?: string;
|
|
303
|
+
platform?: string;
|
|
304
|
+
capabilities?: string[];
|
|
305
|
+
deviceType?: string;
|
|
306
|
+
description?: string;
|
|
307
|
+
metadata?: Record<string, any>;
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
#### EdgeInfo
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
export interface EdgeInfo {
|
|
315
|
+
edgeId: string;
|
|
316
|
+
edgeVersion?: string;
|
|
317
|
+
connectionTime?: string;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
#### Command
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
export type Command = SpecificCommand | GenericCommand;
|
|
325
|
+
|
|
326
|
+
export interface GenericCommand {
|
|
327
|
+
commandType: CommandType;
|
|
328
|
+
commandCode: string;
|
|
329
|
+
deviceType?: string;
|
|
330
|
+
deviceId?: number | string;
|
|
331
|
+
operationType?: OperationType;
|
|
332
|
+
parameters?: Record<string, any>;
|
|
333
|
+
}
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
#### CommandResult
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
export interface CommandResult {
|
|
340
|
+
deviceType: string;
|
|
341
|
+
deviceId: number | string;
|
|
342
|
+
commandCode: string;
|
|
343
|
+
operationType: OperationType;
|
|
344
|
+
data: Record<string, any>;
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
#### ReportMessage
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
export interface ReportMessage {
|
|
352
|
+
level: ReportLevel;
|
|
353
|
+
message: string;
|
|
354
|
+
code?: string;
|
|
355
|
+
data?: Record<string, any>;
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
#### ProgramContext
|
|
360
|
+
|
|
361
|
+
```typescript
|
|
362
|
+
export interface ProgramContext {
|
|
363
|
+
taskId: string;
|
|
364
|
+
programId: string;
|
|
365
|
+
programName: string;
|
|
366
|
+
programNumber: number;
|
|
367
|
+
programType: ProgramType;
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
#### DeviceOperationRecord
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
export interface DeviceOperationRecord {
|
|
375
|
+
commandType: CommandType;
|
|
376
|
+
commandCode: string;
|
|
377
|
+
deviceType: string;
|
|
378
|
+
deviceId: number | string;
|
|
379
|
+
operationType: OperationType;
|
|
380
|
+
result?: Record<string, any>;
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
## 工厂函数
|
|
385
|
+
|
|
386
|
+
### createSimpleCommand
|
|
387
|
+
|
|
388
|
+
创建简单命令。
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
function createSimpleCommand(options: {
|
|
392
|
+
requestRef: string;
|
|
393
|
+
targetClientId: string;
|
|
394
|
+
commandCode: string;
|
|
395
|
+
deviceType: string;
|
|
396
|
+
deviceId: number | string;
|
|
397
|
+
operationType: OperationType;
|
|
398
|
+
parameters?: Record<string, any>;
|
|
399
|
+
priority?: Priority;
|
|
400
|
+
timeout?: number;
|
|
401
|
+
callback: string;
|
|
402
|
+
}): CommandMessage
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
**示例:**
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
const command = createSimpleCommand({
|
|
409
|
+
requestRef: 'req-123',
|
|
410
|
+
targetClientId: 'td-01',
|
|
411
|
+
commandCode: 'SET_BRIGHTNESS',
|
|
412
|
+
deviceType: 'screen',
|
|
413
|
+
deviceId: 1,
|
|
414
|
+
operationType: OperationType.WRITE,
|
|
415
|
+
parameters: { brightness: 80 },
|
|
416
|
+
callback: 'http://backend/callback'
|
|
417
|
+
});
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### createBatchCommand
|
|
421
|
+
|
|
422
|
+
创建批量命令。
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
function createBatchCommand(options: {
|
|
426
|
+
requestRef: string;
|
|
427
|
+
targetClientId: string;
|
|
428
|
+
commandCode: string;
|
|
429
|
+
deviceType: string;
|
|
430
|
+
deviceId: string; // 范围字符串,如 "1-10,20,30-40"
|
|
431
|
+
operationType: OperationType;
|
|
432
|
+
parameters?: Record<string, any>;
|
|
433
|
+
priority?: Priority;
|
|
434
|
+
timeout?: number;
|
|
435
|
+
callback: string;
|
|
436
|
+
}): CommandMessage
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### createComplexCommand
|
|
440
|
+
|
|
441
|
+
创建复杂命令。
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
function createComplexCommand(options: {
|
|
445
|
+
requestRef: string;
|
|
446
|
+
targetClientId: string;
|
|
447
|
+
commandCode: string;
|
|
448
|
+
parameters?: Record<string, any>;
|
|
449
|
+
priority?: Priority;
|
|
450
|
+
timeout?: number;
|
|
451
|
+
callback: string;
|
|
452
|
+
}): CommandMessage
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
### 强类型命令工厂
|
|
456
|
+
|
|
457
|
+
基于 C# 模型生成的强类型命令工厂。
|
|
458
|
+
|
|
459
|
+
```typescript
|
|
460
|
+
// LED 开关命令
|
|
461
|
+
function createLedSwitchCommand(
|
|
462
|
+
requestRef: string,
|
|
463
|
+
targetClientId: string,
|
|
464
|
+
deviceId: number,
|
|
465
|
+
switch: 'ON' | 'OFF',
|
|
466
|
+
operationType: 'read' | 'write'
|
|
467
|
+
): CommandMessage
|
|
468
|
+
|
|
469
|
+
// 更多强类型命令...
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
## 验证函数
|
|
473
|
+
|
|
474
|
+
### validateMessage
|
|
475
|
+
|
|
476
|
+
验证消息基本结构。
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
function validateMessage(message: any): message is BaseMessage
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### validateRegisterMessage
|
|
483
|
+
|
|
484
|
+
验证注册消息。
|
|
485
|
+
|
|
486
|
+
```typescript
|
|
487
|
+
function validateRegisterMessage(message: any): message is RegisterMessage
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
### validateCommandMessage
|
|
491
|
+
|
|
492
|
+
验证命令消息。
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
function validateCommandMessage(message: any): message is CommandMessage
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
## 类型守卫
|
|
499
|
+
|
|
500
|
+
### 消息类型守卫
|
|
501
|
+
|
|
502
|
+
```typescript
|
|
503
|
+
function isRegisterMessage(msg: any): msg is RegisterMessage
|
|
504
|
+
function isRegisterAckMessage(msg: any): msg is RegisterAckMessage
|
|
505
|
+
function isUnregisterMessage(msg: any): msg is UnregisterMessage
|
|
506
|
+
function isUnregisterAckMessage(msg: any): msg is UnregisterAckMessage
|
|
507
|
+
function isHeartbeatMessage(msg: any): msg is HeartbeatMessage
|
|
508
|
+
function isHeartbeatAckMessage(msg: any): msg is HeartbeatAckMessage
|
|
509
|
+
function isCommandMessage(msg: any): msg is CommandMessage
|
|
510
|
+
function isCommandResponseMessage(msg: any): msg is CommandResponseMessage
|
|
511
|
+
function isProgramMessage(msg: any): msg is ProgramMessage
|
|
512
|
+
function isProgramResponseMessage(msg: any): msg is ProgramResponseMessage
|
|
513
|
+
function isProgressUpdateMessage(msg: any): msg is ProgressUpdateMessage
|
|
514
|
+
function isErrorMessage(msg: any): msg is ErrorMessage
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
**使用示例:**
|
|
518
|
+
|
|
519
|
+
```typescript
|
|
520
|
+
const message = JSON.parse(websocketData);
|
|
521
|
+
|
|
522
|
+
if (isCommandMessage(message)) {
|
|
523
|
+
// TypeScript 知道 message 是 CommandMessage 类型
|
|
524
|
+
console.log(message.requestRef);
|
|
525
|
+
console.log(message.command.commandCode);
|
|
526
|
+
}
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
## 常量
|
|
530
|
+
|
|
531
|
+
### 协议版本
|
|
532
|
+
|
|
533
|
+
```typescript
|
|
534
|
+
export const PROTOCOL_VERSION = '1.0';
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
### 默认值
|
|
538
|
+
|
|
539
|
+
```typescript
|
|
540
|
+
export const DEFAULT_TIMEOUT = 30000; // 30秒
|
|
541
|
+
export const DEFAULT_PRIORITY = Priority.NORMAL;
|
|
542
|
+
export const HEARTBEAT_INTERVAL = 30000; // 30秒
|
|
543
|
+
export const HEARTBEAT_TIMEOUT = 90000; // 90秒
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
## 错误代码
|
|
547
|
+
|
|
548
|
+
### 设备错误
|
|
549
|
+
|
|
550
|
+
- `DEVICE_OFFLINE` - 设备离线
|
|
551
|
+
- `DEVICE_NOT_FOUND` - 设备不存在
|
|
552
|
+
- `DEVICE_BUSY` - 设备忙
|
|
553
|
+
- `DEVICE_ERROR` - 设备内部错误
|
|
554
|
+
|
|
555
|
+
### 命令错误
|
|
556
|
+
|
|
557
|
+
- `COMMAND_TIMEOUT` - 命令超时
|
|
558
|
+
- `COMMAND_INVALID` - 无效命令
|
|
559
|
+
- `COMMAND_FAILED` - 命令执行失败
|
|
560
|
+
- `COMMAND_CANCELLED` - 命令被取消
|
|
561
|
+
|
|
562
|
+
### 协议错误
|
|
563
|
+
|
|
564
|
+
- `PROTOCOL_INVALID` - 消息格式错误
|
|
565
|
+
- `PROTOCOL_VERSION` - 版本不兼容
|
|
566
|
+
- `PROTOCOL_SEQUENCE` - 消息顺序错误
|
|
567
|
+
|
|
568
|
+
### 系统错误
|
|
569
|
+
|
|
570
|
+
- `SYSTEM_OVERLOAD` - 系统过载
|
|
571
|
+
- `SYSTEM_MAINTENANCE` - 系统维护
|
|
572
|
+
- `SYSTEM_ERROR` - 内部错误
|