@zucker-framework/gateway 1.0.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.
@@ -0,0 +1,304 @@
1
+ import { Transport } from '@zucker-framework/network';
2
+ import { UpstreamMessage, DownstreamMessage, ThingManager, ThingsDataService } from '@zucker-framework/things';
3
+ import { Logger } from '@nestjs/common';
4
+
5
+ /**
6
+ * 设备会话 — 对应 jetlinks DeviceSession
7
+ *
8
+ * 代表一个已连接设备的网络会话,负责消息的收发与状态跟踪。
9
+ */
10
+
11
+ /**
12
+ * 设备会话状态
13
+ */
14
+ declare enum SessionState {
15
+ /** 会话正常,设备在线 */
16
+ CONNECTED = "connected",
17
+ /** 会话已关闭 */
18
+ CLOSED = "closed",
19
+ /** 会话空闲(超时告警) */
20
+ IDLE = "idle"
21
+ }
22
+ /**
23
+ * 设备会话接口 — 对应 jetlinks DeviceSession
24
+ */
25
+ interface DeviceSession {
26
+ /** 会话唯一 ID */
27
+ readonly sessionId: string;
28
+ /** 关联的设备(物)ID */
29
+ readonly thingId: string;
30
+ /** 所属网关 ID */
31
+ readonly gatewayId: string;
32
+ /** 传输协议 */
33
+ readonly transport: Transport;
34
+ /** 会话创建时间 */
35
+ readonly connectTime: number;
36
+ /** 最后活跃时间 */
37
+ lastPingTime: number;
38
+ /** 当前状态 */
39
+ state: SessionState;
40
+ /**
41
+ * 发送下行消息(原始字节/对象)到设备
42
+ * 对应 jetlinks DeviceSession.send()
43
+ */
44
+ send(payload: unknown): Promise<void>;
45
+ /**
46
+ * 关闭会话
47
+ * 对应 jetlinks DeviceSession.close()
48
+ */
49
+ close(): Promise<void>;
50
+ /**
51
+ * 更新心跳
52
+ * 对应 jetlinks DeviceSession.ping()
53
+ */
54
+ ping(): void;
55
+ /**
56
+ * 是否存活
57
+ * 对应 jetlinks DeviceSession.isAlive()
58
+ */
59
+ isAlive(): boolean;
60
+ }
61
+ /**
62
+ * 子设备会话 — 对应 jetlinks ChildDeviceSession
63
+ *
64
+ * 通过父设备(如网关设备)代理的子设备会话。
65
+ */
66
+ interface ChildDeviceSession extends DeviceSession {
67
+ /** 父设备 ID */
68
+ readonly parentThingId: string;
69
+ }
70
+
71
+ /**
72
+ * 消息编解码器 — 对应 jetlinks ProtocolSupport / MessageCodec
73
+ *
74
+ * 负责原始协议消息 ↔ ThingMessage 的互转。
75
+ * 不同协议(MQTT/HTTP/TCP)提供不同的 Codec 实现。
76
+ */
77
+
78
+ /**
79
+ * 上行消息解码上下文 — 对应 jetlinks MessageDecodeContext
80
+ */
81
+ interface DecodeContext {
82
+ /** 接收到的原始消息 */
83
+ payload: unknown;
84
+ /** 原始主题/路径/端点(MQTT topic、HTTP path 等) */
85
+ topic?: string;
86
+ /** 请求头/消息头 */
87
+ headers?: Record<string, string>;
88
+ /** 关联的设备会话 */
89
+ session: DeviceSession;
90
+ }
91
+ /**
92
+ * 下行消息编码上下文 — 对应 jetlinks MessageEncodeContext
93
+ */
94
+ interface EncodeContext {
95
+ /** 要发送的平台消息 */
96
+ message: DownstreamMessage;
97
+ /** 关联的设备会话 */
98
+ session: DeviceSession;
99
+ }
100
+ /**
101
+ * 编码结果
102
+ */
103
+ interface EncodedMessage {
104
+ /** 编码后的载荷 */
105
+ payload: unknown;
106
+ /** 目标主题/路径 */
107
+ topic?: string;
108
+ /** 附加头信息 */
109
+ headers?: Record<string, string>;
110
+ }
111
+ /**
112
+ * 消息编解码器接口 — 对应 jetlinks DeviceMessageCodec / ProtocolSupport
113
+ */
114
+ interface MessageCodec {
115
+ /**
116
+ * 解码上行消息(原始 → ThingMessage[])
117
+ * 对应 jetlinks DeviceMessageCodec.decode()
118
+ * 一条原始消息可能解析出多条逻辑消息(如批量属性上报)
119
+ */
120
+ decode(context: DecodeContext): Promise<UpstreamMessage[]>;
121
+ /**
122
+ * 编码下行消息(ThingMessage → 原始)
123
+ * 对应 jetlinks DeviceMessageCodec.encode()
124
+ */
125
+ encode(context: EncodeContext): Promise<EncodedMessage>;
126
+ }
127
+ /**
128
+ * 协议支持描述 — 对应 jetlinks ProtocolSupport
129
+ */
130
+ interface ProtocolSupport {
131
+ /** 协议唯一 ID */
132
+ readonly id: string;
133
+ /** 协议名称 */
134
+ readonly name: string;
135
+ /** 支持的传输协议 schema 列表,如 ['mqtt', 'http', 'tcp'] */
136
+ readonly transports: string[];
137
+ /** 获取对应传输协议的编解码器 */
138
+ getCodec(transport: string): MessageCodec | undefined;
139
+ }
140
+ /**
141
+ * 协议注册表 — 对应 jetlinks ProtocolSupportManager
142
+ */
143
+ declare class ProtocolRegistry {
144
+ private readonly protocols;
145
+ register(protocol: ProtocolSupport): void;
146
+ unregister(id: string): void;
147
+ get(id: string): ProtocolSupport | undefined;
148
+ getAll(): ProtocolSupport[];
149
+ }
150
+
151
+ /**
152
+ * 设备网关接口 — 对应 jetlinks DeviceGateway
153
+ *
154
+ * 网关是协议层(network)与业务层(things)的桥接器:
155
+ * - 接收设备连接,建立 DeviceSession
156
+ * - 调用 MessageCodec 解码上行消息,发布到消息总线
157
+ * - 接收下行消息,编码后通过 DeviceSession 发送给设备
158
+ */
159
+
160
+ /**
161
+ * 网关状态
162
+ */
163
+ declare enum GatewayState {
164
+ STARTING = "starting",
165
+ RUNNING = "running",
166
+ PAUSED = "paused",
167
+ STOPPED = "stopped"
168
+ }
169
+ /**
170
+ * 网关配置
171
+ */
172
+ interface DeviceGatewayConfig {
173
+ id: string;
174
+ name: string;
175
+ /** 关联的 network 配置 ID */
176
+ networkId: string;
177
+ /** 使用的协议 ID */
178
+ protocolId: string;
179
+ /** 传输 schema(与 protocol.getCodec(schema) 对应)*/
180
+ transport: string;
181
+ }
182
+ /**
183
+ * 设备网关接口 — 对应 jetlinks DeviceGateway
184
+ */
185
+ interface DeviceGateway {
186
+ readonly config: DeviceGatewayConfig;
187
+ readonly state: GatewayState;
188
+ /** 启动网关 */
189
+ start(): Promise<void>;
190
+ /** 暂停(不再接受新连接,已有连接保持)*/
191
+ pause(): Promise<void>;
192
+ /** 停止网关并关闭所有连接 */
193
+ shutdown(): Promise<void>;
194
+ /** 获取活跃会话数量 */
195
+ getSessionCount(): number;
196
+ /** 获取指定设备的会话 */
197
+ getSession(thingId: string): DeviceSession | undefined;
198
+ /** 发送下行消息到指定设备 */
199
+ sendDownstream(thingId: string, payload: unknown): Promise<void>;
200
+ }
201
+ /**
202
+ * 抽象网关基类 — 提供会话管理 + 消息路由公共逻辑
203
+ * 子类只需实现协议特定的 start/pause/shutdown
204
+ *
205
+ * 对应 jetlinks AbstractDeviceGateway
206
+ */
207
+ declare abstract class AbstractDeviceGateway implements DeviceGateway {
208
+ readonly config: DeviceGatewayConfig;
209
+ protected readonly protocol: ProtocolSupport;
210
+ protected readonly thingManager: ThingManager;
211
+ protected readonly dataService: ThingsDataService;
212
+ protected readonly logger: Logger;
213
+ protected readonly sessions: Map<string, DeviceSession>;
214
+ state: GatewayState;
215
+ constructor(config: DeviceGatewayConfig, protocol: ProtocolSupport, thingManager: ThingManager, dataService: ThingsDataService);
216
+ abstract start(): Promise<void>;
217
+ abstract pause(): Promise<void>;
218
+ abstract shutdown(): Promise<void>;
219
+ getSessionCount(): number;
220
+ getSession(thingId: string): DeviceSession | undefined;
221
+ sendDownstream(thingId: string, payload: unknown): Promise<void>;
222
+ /**
223
+ * 处理设备连接 — 注册会话并触发上线
224
+ */
225
+ protected onSessionConnected(session: DeviceSession): Promise<void>;
226
+ /**
227
+ * 处理设备断开 — 移除会话并触发离线
228
+ */
229
+ protected onSessionClosed(session: DeviceSession): Promise<void>;
230
+ /**
231
+ * 处理上行原始消息 — 解码后路由到对应处理器
232
+ * 对应 jetlinks AbstractDeviceGateway.doMessage()
233
+ */
234
+ protected handleIncoming(context: DecodeContext): Promise<void>;
235
+ /**
236
+ * 将解码后的消息路由到业务处理 — 对应 jetlinks DeviceMessageBus.publish()
237
+ */
238
+ private routeMessage;
239
+ /**
240
+ * 关闭所有会话
241
+ */
242
+ protected closeAllSessions(): Promise<void>;
243
+ }
244
+
245
+ /**
246
+ * HTTP 网关实现
247
+ *
248
+ * 用法:
249
+ * ```typescript
250
+ * const gateway = new HttpDeviceGateway(config, protocol, thingManager, dataService);
251
+ * await gateway.start();
252
+ *
253
+ * // 当收到 HTTP 请求时:
254
+ * await gateway.handleRequest('device-001', requestBody);
255
+ * ```
256
+ */
257
+ declare class HttpDeviceGateway extends AbstractDeviceGateway {
258
+ constructor(config: DeviceGatewayConfig, protocol: ProtocolSupport, thingManager: ThingManager, dataService: ThingsDataService);
259
+ start(): Promise<void>;
260
+ pause(): Promise<void>;
261
+ shutdown(): Promise<void>;
262
+ /**
263
+ * 处理来自设备的 HTTP 请求
264
+ * 由 NestJS Controller 调用
265
+ *
266
+ * @param thingId 设备 ID(从路径参数或认证信息中提取)
267
+ * @param body 请求体
268
+ */
269
+ handleRequest(thingId: string, body: unknown): Promise<void>;
270
+ }
271
+
272
+ declare class GatewayManager {
273
+ readonly protocolRegistry: ProtocolRegistry;
274
+ private readonly logger;
275
+ private readonly gateways;
276
+ constructor(protocolRegistry: ProtocolRegistry);
277
+ register(gateway: DeviceGateway): void;
278
+ unregister(gatewayId: string): void;
279
+ get(gatewayId: string): DeviceGateway | undefined;
280
+ getAll(): DeviceGateway[];
281
+ /** 启动所有已注册的网关 */
282
+ startAll(): Promise<void>;
283
+ /** 停止所有网关 */
284
+ shutdownAll(): Promise<void>;
285
+ /** 汇总统计 */
286
+ getSummary(): {
287
+ total: number;
288
+ running: number;
289
+ stopped: number;
290
+ totalSessions: number;
291
+ };
292
+ }
293
+
294
+ /**
295
+ * GatewayModule — 协议网关 NestJS 模块
296
+ *
297
+ * 提供:
298
+ * - ProtocolRegistry(协议注册表)
299
+ * - GatewayManager(网关生命周期管理)
300
+ */
301
+ declare class GatewayModule {
302
+ }
303
+
304
+ export { AbstractDeviceGateway, type ChildDeviceSession, type DecodeContext, type DeviceGateway, type DeviceGatewayConfig, type DeviceSession, type EncodeContext, type EncodedMessage, GatewayManager, GatewayModule, GatewayState, HttpDeviceGateway, type MessageCodec, ProtocolRegistry, type ProtocolSupport, SessionState };