@jiangtaste/baiwei-sdk 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.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +360 -0
  3. package/dist/core/BaiweiClient.d.ts +38 -0
  4. package/dist/core/BaiweiClient.js +94 -0
  5. package/dist/core/BaiweiSession.d.ts +56 -0
  6. package/dist/core/BaiweiSession.js +229 -0
  7. package/dist/core/Client.d.ts +74 -0
  8. package/dist/core/Client.js +127 -0
  9. package/dist/core/MessageAssembler.d.ts +17 -0
  10. package/dist/core/MessageAssembler.js +76 -0
  11. package/dist/core/PendingRequestStore.d.ts +18 -0
  12. package/dist/core/PendingRequestStore.js +33 -0
  13. package/dist/core/Session.d.ts +43 -0
  14. package/dist/core/Session.js +330 -0
  15. package/dist/core/SessionConnector.d.ts +14 -0
  16. package/dist/core/SessionConnector.js +62 -0
  17. package/dist/index.d.ts +11 -0
  18. package/dist/index.js +27 -0
  19. package/dist/services/BaseService.d.ts +11 -0
  20. package/dist/services/BaseService.js +17 -0
  21. package/dist/services/ControlService.d.ts +33 -0
  22. package/dist/services/ControlService.js +67 -0
  23. package/dist/services/DeviceService.d.ts +26 -0
  24. package/dist/services/DeviceService.js +51 -0
  25. package/dist/services/GatewayService.d.ts +11 -0
  26. package/dist/services/GatewayService.js +33 -0
  27. package/dist/services/RoomService.d.ts +11 -0
  28. package/dist/services/RoomService.js +27 -0
  29. package/dist/services/SceneService.d.ts +12 -0
  30. package/dist/services/SceneService.js +39 -0
  31. package/dist/services/UserService.d.ts +21 -0
  32. package/dist/services/UserService.js +70 -0
  33. package/dist/transport/TcpClient.d.ts +38 -0
  34. package/dist/transport/TcpClient.js +315 -0
  35. package/dist/types/device-catalog.d.ts +42 -0
  36. package/dist/types/device-catalog.js +16 -0
  37. package/dist/types/device-state.d.ts +56 -0
  38. package/dist/types/device-state.js +2 -0
  39. package/dist/types/index.d.ts +8 -0
  40. package/dist/types/index.js +24 -0
  41. package/dist/types/messages.d.ts +53 -0
  42. package/dist/types/messages.js +32 -0
  43. package/dist/types/options.d.ts +27 -0
  44. package/dist/types/options.js +7 -0
  45. package/dist/types/room.d.ts +9 -0
  46. package/dist/types/room.js +2 -0
  47. package/dist/types/scene.d.ts +22 -0
  48. package/dist/types/scene.js +2 -0
  49. package/dist/types/user.d.ts +17 -0
  50. package/dist/types/user.js +2 -0
  51. package/dist/types.d.ts +212 -0
  52. package/dist/types.js +50 -0
  53. package/dist/utils/IdGenerator.d.ts +19 -0
  54. package/dist/utils/IdGenerator.js +49 -0
  55. package/dist/utils/MessageIdGenerator.d.ts +4 -0
  56. package/dist/utils/MessageIdGenerator.js +12 -0
  57. package/dist/utils/logger.d.ts +33 -0
  58. package/dist/utils/logger.js +115 -0
  59. package/dist/utils/time.d.ts +2 -0
  60. package/dist/utils/time.js +14 -0
  61. package/package.json +45 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paul Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,360 @@
1
+ # baiwei-sdk
2
+
3
+ `baiwei-sdk` 现在收敛为一个面向百微智能网关的 TCP 长连接通信转换层。
4
+
5
+ 它默认只做几件事:
6
+
7
+ - 建立和维护 TCP 长连接
8
+ - 完成协议编解码
9
+ - 关联请求/响应
10
+ - 汇总分片消息
11
+ - 透传网关 `report`
12
+ - 保存登录后的 `token`
13
+
14
+ 它默认不再做这些事:
15
+
16
+ - 不维护设备/房间/场景的长期缓存
17
+ - 不自动帮业务层同步状态
18
+ - 不把 SDK 本身做成状态中心
19
+
20
+ ## 安装
21
+
22
+ ```bash
23
+ npm install baiwei-sdk
24
+ ```
25
+
26
+ ## 适用场景
27
+
28
+ 这个 SDK 适合放在更上层的库或服务下面,作为“百微网关协议适配层”:
29
+
30
+ - 在 `NestJS` 服务里做网关接入
31
+ - 在 `Homebridge` / `Home Assistant` 插件里做设备桥接
32
+ - 在你自己的 `IoT Hub` 或家庭中台里做设备查询、控制和上报接收
33
+ - 在脚本工具里做调试、巡检、排障
34
+
35
+ 如果你的目标是:
36
+
37
+ - 维持 TCP 长连接
38
+ - 请求设备、房间、场景
39
+ - 控制设备
40
+ - 接收网关 `report`
41
+
42
+ 那么可以直接使用这个 SDK。
43
+
44
+ 如果你的目标是:
45
+
46
+ - 做设备缓存
47
+ - 做状态中心
48
+ - 做自动同步和状态广播
49
+ - 做插件平台或业务编排
50
+
51
+ 这些建议放在你自己的上层库里实现,而不是塞进 SDK 本身。
52
+
53
+ ## 快速开始
54
+
55
+ ### 1. 管理员登录
56
+
57
+ ```ts
58
+ import {
59
+ BaiweiClient,
60
+ BaiweiProductType,
61
+ LightState,
62
+ MessageResponse,
63
+ } from "baiwei-sdk";
64
+
65
+ async function main() {
66
+ const client = new BaiweiClient({
67
+ host: "1.2.3.4", // 网关IP
68
+ port: 12345, // 网关端口
69
+ gatewaySN: "设备序列号", // MAC地址去掉空格
70
+ timeoutMs: 5_000,
71
+ });
72
+
73
+ client.on("report", (message) => {
74
+ const device = client.controlService.getReportedDeviceState(
75
+ message as MessageResponse
76
+ );
77
+
78
+ if (device) {
79
+ console.log("device report", device.device_id, device.device_status);
80
+ }
81
+ });
82
+
83
+ await client.connect();
84
+ await client.userService.login();
85
+
86
+ const devices = await client.deviceService.getDevicesByType(
87
+ BaiweiProductType.ON_OFF_LIGHT
88
+ );
89
+
90
+ const states = await client.controlService.getDeviceStatesByType<LightState>(
91
+ BaiweiProductType.ON_OFF_LIGHT
92
+ );
93
+
94
+ console.log("devices", devices.length);
95
+ console.log("states", states.device_list?.length ?? 0);
96
+ }
97
+
98
+ main().catch(console.error);
99
+ ```
100
+
101
+ ### 2. 普通用户账号密码登录
102
+
103
+ ```ts
104
+ import { BaiweiClient } from "baiwei-sdk";
105
+
106
+ async function main() {
107
+ const client = new BaiweiClient({
108
+ host: "192.168.4.14",
109
+ port: 12345,
110
+ gatewaySN: "304a2656b37e",
111
+ });
112
+
113
+ await client.connect();
114
+ await client.userService.login({
115
+ userName: "alice",
116
+ userPwd: "123456",
117
+ });
118
+
119
+ const rooms = await client.roomService.listRooms();
120
+ console.log("rooms", rooms.length);
121
+ }
122
+
123
+ main().catch(console.error);
124
+ ```
125
+
126
+ ## API 边界
127
+
128
+ 你可以直接把它当作“协议适配器”来用:
129
+
130
+ - `client.connect()` / `client.disconnect()` 负责连接生命周期
131
+ - `client.request()` 允许你直接发原始协议请求
132
+ - `client.on("report", handler)` 接收网关主动上报
133
+ - `userService` / `deviceService` / `controlService` / `roomService` / `sceneService` / `gatewayService` 只保留单次请求封装
134
+
135
+ ## 当前设计原则
136
+
137
+ - SDK 只保留通信所需的最小状态:连接、token、pending request、分片组装
138
+ - 业务缓存、状态合并、同步策略交给上层网关服务实现
139
+ - 如果后续要接 Home Assistant、NestJS、Homebridge,这一层都可以继续复用
140
+
141
+ ## 其他库如何使用这个 SDK
142
+
143
+ 最推荐的方式是:**把它当作底层驱动,不要直接把业务逻辑写进 SDK。**
144
+
145
+ ### 接入思路
146
+
147
+ 通常可以分成三层:
148
+
149
+ 1. `baiwei-sdk`
150
+ 只负责 TCP、协议、请求/响应、report。
151
+ 2. 你的网关适配层
152
+ 负责缓存设备目录、维护状态、重连后的重新同步、事件分发。
153
+ 3. 你的业务层 / 插件层
154
+ 负责 UI、自动化、告警、场景编排、第三方平台桥接。
155
+
156
+ ### 在服务端项目中使用
157
+
158
+ 如果你在 `NestJS`、`Express`、`Fastify` 之类的服务端项目里接入,建议封装一个长期存活的网关服务:
159
+
160
+ ```ts
161
+ import {
162
+ BaiweiClient,
163
+ BaiweiProductType,
164
+ MessageResponse,
165
+ } from "baiwei-sdk";
166
+
167
+ export class BaiweiGatewayService {
168
+ private readonly client = new BaiweiClient({
169
+ host: process.env.BAIWEI_HOST!,
170
+ port: Number(process.env.BAIWEI_PORT!),
171
+ gatewaySN: process.env.BAIWEI_GATEWAY_SN!,
172
+ timeoutMs: 5000,
173
+ });
174
+
175
+ async start() {
176
+ this.client.on("report", (message) => {
177
+ this.handleReport(message as MessageResponse);
178
+ });
179
+
180
+ await this.client.connect();
181
+ await this.client.userService.login({
182
+ userName: process.env.BAIWEI_USER_NAME!,
183
+ userPwd: process.env.BAIWEI_USER_PWD!,
184
+ });
185
+ }
186
+
187
+ async listLights() {
188
+ return this.client.deviceService.getDevicesByType(
189
+ BaiweiProductType.ON_OFF_LIGHT
190
+ );
191
+ }
192
+
193
+ async stop() {
194
+ this.client.disconnect();
195
+ }
196
+
197
+ private handleReport(message: MessageResponse) {
198
+ const device = this.client.controlService.getReportedDeviceState(message);
199
+ if (!device) return;
200
+
201
+ console.log("device report", device.device_id, device.device_status);
202
+ }
203
+ }
204
+ ```
205
+
206
+ 这种模式适合:
207
+
208
+ - 你的服务要长期在线
209
+ - 你要统一管理连接和登录
210
+ - 你要把上报转成自己的事件总线
211
+
212
+ ### 在 Homebridge / Home Assistant 类插件中使用
213
+
214
+ 插件层通常不要让每个 accessory / entity 自己建连接。更合理的方式是:
215
+
216
+ - 整个插件只维护一个 `BaiweiClient`
217
+ - 启动时统一登录
218
+ - 用 `deviceService` 拉一次设备目录
219
+ - 用 `controlService` 查询状态或发送控制
220
+ - 用 `report` 更新插件内部缓存
221
+
222
+ 也就是说,这个 SDK 更适合作为“单网关单连接”的底层驱动,而不是每个设备实例各自使用。
223
+
224
+ ### 在你自己的领域库中二次封装
225
+
226
+ 如果你准备做一个更高层的库,比如:
227
+
228
+ - `baiwei-homebridge-adapter`
229
+ - `baiwei-nest-gateway`
230
+ - `baiwei-state-manager`
231
+
232
+ 建议在上层封装这些能力:
233
+
234
+ - 设备缓存:启动后拉取一次 `deviceService.listDevices()`
235
+ - 状态缓存:收到 `report` 时合并到内存状态
236
+ - 自动重同步:重连后重新登录、重新拉目录、重新拉状态
237
+ - 业务事件:对外发出 `device-updated`、`room-updated`、`scene-triggered`
238
+ - 权限与多租户:如果普通用户和管理员权限不同,这一层做权限隔离
239
+
240
+ SDK 自己则尽量保持“薄”,避免和你的业务模型耦合。
241
+
242
+ ## 推荐封装方式
243
+
244
+ ### 1. 做一个单例 client
245
+
246
+ 不要在每次请求里都 `new BaiweiClient()`;更推荐在进程生命周期内复用一个实例。
247
+
248
+ ### 2. 登录逻辑集中管理
249
+
250
+ 建议只在一处完成:
251
+
252
+ - `client.connect()`
253
+ - `client.userService.login()`
254
+
255
+ 不要让业务层到处散落登录逻辑。
256
+
257
+ ### 3. 把 report 当事件源,不要当最终状态
258
+
259
+ `report` 只是网关主动上报的消息流。上层如果要形成完整状态,仍然建议自己维护缓存和合并策略。
260
+
261
+ ### 4. 尽量让业务代码依赖你自己的适配层
262
+
263
+ 也就是说,业务代码最好依赖:
264
+
265
+ ```ts
266
+ gateway.listLights()
267
+ gateway.turnOn(deviceId)
268
+ gateway.getRoomDevices(roomId)
269
+ ```
270
+
271
+ 而不是在每个业务模块里直接操作 `client.deviceService`、`client.controlService`。
272
+
273
+ 这样你以后换协议、换缓存策略、换权限模型,改动面会小很多。
274
+
275
+ ## 原始协议请求
276
+
277
+ 如果现成 service 还没覆盖某个接口,可以直接走 `client.request()`:
278
+
279
+ ```ts
280
+ import { BaiweiClient, MsgClass, MsgName, MsgType } from "baiwei-sdk";
281
+
282
+ const response = await client.request({
283
+ msgClass: MsgClass.CONTROL_MGMT,
284
+ msgName: MsgName.DEVICE_STATE_GET,
285
+ msgType: MsgType.GET,
286
+ payload: {
287
+ device: {
288
+ type: "On/Off Light",
289
+ },
290
+ },
291
+ });
292
+ ```
293
+
294
+ 这对你做上层库很有用:即使 SDK 还没补好某个 service,你也能先继续开发。
295
+
296
+ ## 调试 SDK
297
+
298
+ 仓库里已经带了一个可直接运行的调试入口:[examples/debug.ts](/Users/paul/OrbStack/ubuntu/opt/baiwei-sdk/examples/debug.ts:1)。
299
+
300
+ 先准备连接参数:
301
+
302
+ ```bash
303
+ export BAIWEI_HOST=192.168.4.14
304
+ export BAIWEI_PORT=12345
305
+ export BAIWEI_GATEWAY_SN=304a2656b37e
306
+ export BAIWEI_USER_PWD=888888
307
+ export BAIWEI_DEBUG=1
308
+ ```
309
+
310
+ 如果要测试普通用户登录,还可以额外设置:
311
+
312
+ ```bash
313
+ export BAIWEI_USER_NAME=alice
314
+ export BAIWEI_USER_PWD=123456
315
+ ```
316
+
317
+ 然后可以直接跑这些命令:
318
+
319
+ ```bash
320
+ npm run debug -- overview
321
+ npm run debug -- devices
322
+ npm run debug -- devices ON_OFF_LIGHT
323
+ npm run debug -- states ON_OFF_LIGHT
324
+ npm run debug -- state <deviceId> ON_OFF_LIGHT
325
+ npm run debug -- rooms
326
+ npm run debug -- scenes
327
+ npm run debug -- reports
328
+ npm run debug -- permit-join 60
329
+ npm run debug -- call-scene 1
330
+ ```
331
+
332
+ 如果你要直接调原始协议请求,用 `raw`:
333
+
334
+ ```bash
335
+ export BAIWEI_MSG_CLASS=CONTROL_MGMT
336
+ export BAIWEI_MSG_NAME=DEVICE_STATE_GET
337
+ export BAIWEI_MSG_TYPE=GET
338
+ export BAIWEI_RAW_PAYLOAD='{"device":{"type":"On/Off Light"}}'
339
+
340
+ npm run debug -- raw
341
+ ```
342
+
343
+ 几个调试建议:
344
+
345
+ - 连通性先看 `overview`,它会快速验证连接、登录、设备/房间/场景查询。
346
+ - 想观察网关主动上报时,用 `reports`,保持连接直到你按 `Ctrl+C`。
347
+ - 想看 SDK 底层收发和报文切包,打开 `BAIWEI_DEBUG=1`。
348
+ - `state` 和 `states` 在当前无状态设计下都需要显式传设备类型。
349
+
350
+ ## 小结
351
+
352
+ 一句话概括:`baiwei-sdk` 更适合作为“百微网关协议驱动层”,而不是最终业务层。
353
+
354
+ 如果你在别的库里使用它,最佳实践通常是:
355
+
356
+ - 复用单连接
357
+ - 集中登录
358
+ - 自己做缓存
359
+ - 自己做事件模型
360
+ - 需要时直接走原始协议请求
@@ -0,0 +1,38 @@
1
+ import { EventEmitter } from "events";
2
+ import { ControlService } from "../services/ControlService";
3
+ import { DeviceService } from "../services/DeviceService";
4
+ import { GatewayService } from "../services/GatewayService";
5
+ import { RoomService } from "../services/RoomService";
6
+ import { SceneService } from "../services/SceneService";
7
+ import { UserService } from "../services/UserService";
8
+ import { BaiweiClientOptions, MessageRequest } from "../types";
9
+ /**
10
+ * SDK 对外主入口。
11
+ * 负责组装 session 和各类 service,并把底层事件转发给上层调用者。
12
+ */
13
+ export declare class BaiweiClient extends EventEmitter {
14
+ readonly options: BaiweiClientOptions;
15
+ private readonly session;
16
+ private log;
17
+ private readonly onSessionConnected;
18
+ private readonly onSessionDisconnect;
19
+ private readonly onSessionReport;
20
+ private readonly onSessionError;
21
+ readonly userService: UserService;
22
+ readonly deviceService: DeviceService;
23
+ readonly gatewayService: GatewayService;
24
+ readonly controlService: ControlService;
25
+ readonly sceneService: SceneService;
26
+ readonly roomService: RoomService;
27
+ constructor(options: BaiweiClientOptions);
28
+ /** 返回当前是否已建立可用连接。 */
29
+ isConnected(): boolean;
30
+ /** 建立到网关的连接,失败时记录日志并继续抛错。 */
31
+ connect(): Promise<void>;
32
+ /** 不抛异常的连接封装,适合脚本或探测式调用。 */
33
+ connectSafe(): Promise<boolean>;
34
+ disconnect(): void;
35
+ request<T>(message: MessageRequest): Promise<T>;
36
+ /** 统一补齐 session 运行所需的默认值。 */
37
+ private buildSessionOptions;
38
+ }
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaiweiClient = void 0;
4
+ const events_1 = require("events");
5
+ const ControlService_1 = require("../services/ControlService");
6
+ const DeviceService_1 = require("../services/DeviceService");
7
+ const GatewayService_1 = require("../services/GatewayService");
8
+ const RoomService_1 = require("../services/RoomService");
9
+ const SceneService_1 = require("../services/SceneService");
10
+ const UserService_1 = require("../services/UserService");
11
+ const types_1 = require("../types");
12
+ const logger_1 = require("../utils/logger");
13
+ const BaiweiSession_1 = require("./BaiweiSession");
14
+ /**
15
+ * SDK 对外主入口。
16
+ * 负责组装 session 和各类 service,并把底层事件转发给上层调用者。
17
+ */
18
+ class BaiweiClient extends events_1.EventEmitter {
19
+ options;
20
+ session;
21
+ log;
22
+ onSessionConnected = () => this.emit("connected");
23
+ onSessionDisconnect = (reason) => this.emit("disconnect", reason);
24
+ onSessionReport = (message) => this.emit("report", message);
25
+ onSessionError = (error) => {
26
+ if (this.listenerCount("error") > 0) {
27
+ this.emit("error", error);
28
+ }
29
+ };
30
+ userService;
31
+ deviceService;
32
+ gatewayService;
33
+ controlService;
34
+ sceneService;
35
+ roomService;
36
+ constructor(options) {
37
+ super();
38
+ this.options = options;
39
+ this.log = (0, logger_1.createLogger)(options.logger, { prefix: "Baiwei" });
40
+ const session = new BaiweiSession_1.BaiweiSession(this.buildSessionOptions());
41
+ this.userService = new UserService_1.UserService(session);
42
+ this.deviceService = new DeviceService_1.DeviceService(session);
43
+ this.gatewayService = new GatewayService_1.GatewayService(session);
44
+ this.controlService = new ControlService_1.ControlService(session);
45
+ this.sceneService = new SceneService_1.SceneService(session);
46
+ this.roomService = new RoomService_1.RoomService(session);
47
+ session.on("connected", this.onSessionConnected);
48
+ session.on("disconnect", this.onSessionDisconnect);
49
+ session.on("report", this.onSessionReport);
50
+ session.on("error", this.onSessionError);
51
+ this.session = session;
52
+ }
53
+ /** 返回当前是否已建立可用连接。 */
54
+ isConnected() {
55
+ return this.session.isConnected();
56
+ }
57
+ /** 建立到网关的连接,失败时记录日志并继续抛错。 */
58
+ async connect() {
59
+ try {
60
+ await this.session.connect();
61
+ }
62
+ catch (error) {
63
+ const errorMessage = error instanceof Error ? error.message : String(error);
64
+ this.log.error("Failed to connect to gateway:", errorMessage);
65
+ throw error;
66
+ }
67
+ }
68
+ /** 不抛异常的连接封装,适合脚本或探测式调用。 */
69
+ async connectSafe() {
70
+ try {
71
+ await this.connect();
72
+ return true;
73
+ }
74
+ catch {
75
+ return false;
76
+ }
77
+ }
78
+ disconnect() {
79
+ this.session.disconnect();
80
+ }
81
+ request(message) {
82
+ return this.session.request(message);
83
+ }
84
+ /** 统一补齐 session 运行所需的默认值。 */
85
+ buildSessionOptions() {
86
+ return {
87
+ ...this.options,
88
+ clientId: this.options.clientId ?? types_1.DEFAULT_CLIENT_ID,
89
+ timeoutMs: this.options.timeoutMs ?? types_1.DEFAULT_TIMEOUT_MS,
90
+ logger: this.log,
91
+ };
92
+ }
93
+ }
94
+ exports.BaiweiClient = BaiweiClient;
@@ -0,0 +1,56 @@
1
+ import EventEmitter from "events";
2
+ import { BaiweiSessionOptions, MessageRequest } from "../types";
3
+ /**
4
+ * 会话编排层。
5
+ * 负责连接、请求派发、响应匹配、分片合并和 report 转发。
6
+ */
7
+ export declare class BaiweiSession extends EventEmitter {
8
+ private readonly tcp;
9
+ private readonly options;
10
+ private readonly log;
11
+ private listenersAttached;
12
+ private token?;
13
+ private connecting?;
14
+ private onTcpMessage;
15
+ private onTcpConnected;
16
+ private onTcpDisconnect;
17
+ private onTcpError;
18
+ private onTcpProtocolError;
19
+ private readonly idGenerator;
20
+ private readonly pending;
21
+ private readonly assembler;
22
+ private readonly connector;
23
+ constructor(options: BaiweiSessionOptions);
24
+ /** 返回 TCP 层当前是否可用。 */
25
+ isConnected(): boolean;
26
+ /** 幂等连接:重复调用时复用同一个连接过程。 */
27
+ connect(): Promise<void>;
28
+ /** 登录成功后保存 token,后续请求会自动带上。 */
29
+ updateToken(token?: string): void;
30
+ disconnect(): void;
31
+ /** 构造协议请求并注册 pending,等待后续 response 匹配回填。 */
32
+ request<T>(req: MessageRequest): Promise<T>;
33
+ /** 把 SDK 内部请求结构转换成网关协议报文。 */
34
+ private createMessage;
35
+ private handleDisconnect;
36
+ /** 只在构造时绑定一次底层事件,避免重复注册。 */
37
+ private bindTcpListeners;
38
+ /** 断线时统一清理请求与分片状态,避免悬空定时器和脏数据。 */
39
+ private clearRuntimeState;
40
+ /** 统一处理来自 TCP 层的完整 JSON 消息。 */
41
+ private onMessage;
42
+ /** 只接受来自目标网关且带有合法 msg_id 的消息。 */
43
+ private validateMessage;
44
+ /** 网关返回非 0 状态码时,优先回填给对应请求。 */
45
+ private handleGatewayError;
46
+ /** 分片逻辑委托给 assembler,session 只保留编排职责。 */
47
+ private handleFragmentation;
48
+ /** 按消息类型分发到 response/report 两条处理链。 */
49
+ private handleMessageResponse;
50
+ /** response 通过 msgId 命中 pending 请求并完成 Promise。 */
51
+ private handleResponse;
52
+ /** report 不做状态缓存,保持原样向上层抛出。 */
53
+ private handleReport;
54
+ /** 统一发出 Error 对象,避免到处重复 new Error。 */
55
+ private emitError;
56
+ }