@jiabaida/tools 1.0.7 → 1.0.10

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 (41) hide show
  1. package/dist/cjs/core/BleApiManager.js +1 -1
  2. package/dist/cjs/core/BleCmdAnalysis/BaseParamProtocol.js +1 -1
  3. package/dist/cjs/core/BleCmdAnalysis/BleCmdAnalysis.js +1 -1
  4. package/dist/cjs/core/BleCmdAnalysis/BleCmdDD.js +1 -1
  5. package/dist/cjs/core/BleCmdAnalysis/BleCmdDDA4.js +1 -1
  6. package/dist/cjs/core/BleCmdAnalysis/BleCmdFFAA.js +1 -1
  7. package/dist/cjs/core/BleCmdAnalysis/readAndSetParam.js +1 -1
  8. package/dist/cjs/core/BleDataProcess.js +1 -1
  9. package/dist/cjs/core/OtaUpgrade.js +1 -1
  10. package/dist/cjs/core/Queue.js +1 -0
  11. package/dist/cjs/core/mqttServer.js +1 -1
  12. package/dist/cjs/core/tcpServer.js +1 -0
  13. package/dist/cjs/index.js +1 -1
  14. package/dist/esm/core/BleApiManager.js +1 -1
  15. package/dist/esm/core/BleCmdAnalysis/BaseParamProtocol.js +1 -1
  16. package/dist/esm/core/BleCmdAnalysis/BleCmdAnalysis.js +1 -1
  17. package/dist/esm/core/BleCmdAnalysis/BleCmdDD.js +1 -1
  18. package/dist/esm/core/BleCmdAnalysis/BleCmdDDA4.js +1 -1
  19. package/dist/esm/core/BleCmdAnalysis/BleCmdFFAA.js +1 -1
  20. package/dist/esm/core/BleCmdAnalysis/BleCmdHVES.js +1 -1
  21. package/dist/esm/core/BleCmdAnalysis/readAndSetParam.js +1 -1
  22. package/dist/esm/core/BleDataProcess.js +1 -1
  23. package/dist/esm/core/OtaUpgrade.js +1 -1
  24. package/dist/esm/core/Queue.js +1 -0
  25. package/dist/esm/core/mqttServer.js +1 -1
  26. package/dist/esm/core/tcpServer.js +1 -0
  27. package/dist/esm/index.js +1 -1
  28. package/package.json +1 -1
  29. package/src/core/BleApiManager.js +33 -15
  30. package/src/core/BleCmdAnalysis/BaseParamProtocol.js +116 -32
  31. package/src/core/BleCmdAnalysis/BleCmdAnalysis.js +58 -2
  32. package/src/core/BleCmdAnalysis/BleCmdDD.js +88 -19
  33. package/src/core/BleCmdAnalysis/BleCmdDDA4.js +154 -227
  34. package/src/core/BleCmdAnalysis/BleCmdFFAA.js +1 -0
  35. package/src/core/BleCmdAnalysis/readAndSetParam.js +133 -16
  36. package/src/core/BleDataProcess.js +15 -11
  37. package/src/core/OtaUpgrade.js +356 -350
  38. package/src/core/Queue.js +34 -0
  39. package/src/core/mqttServer.js +75 -5
  40. package/src/core/tcpServer.js +341 -0
  41. package/src/index.js +1 -0
@@ -0,0 +1,34 @@
1
+ class Queue {
2
+ constructor() {
3
+ this._items = [];
4
+ }
5
+
6
+ enqueue(item) {
7
+ this._items.push(item);
8
+ }
9
+
10
+ dequeue() {
11
+ return this._items.shift();
12
+ }
13
+
14
+ head() {
15
+ return this._items[0];
16
+ }
17
+
18
+ tail() {
19
+ return this._items[this._items.length - 1];
20
+ }
21
+
22
+ isEmpty() {
23
+ return !this._items.length;
24
+ }
25
+
26
+ size() {
27
+ return this._items.length;
28
+ }
29
+
30
+ clear() {
31
+ this._items = [];
32
+ }
33
+ }
34
+ export default Queue
@@ -9,7 +9,15 @@ import { Transfer } from './Transfer.js';
9
9
 
10
10
  // 新的MQTT服务类
11
11
  export class MqttServer {
12
- constructor({ mqttClient = {}, MQTT_HOST = 'wxs://mqtt.jiabaida.com/mqtt', DELAY_TIME = 300, networkModelUrl = 'https://cloud.jiabaida.com/bluetooth/server/device/networkModel', request }) {
12
+ constructor(options = {}) {
13
+ const {
14
+ mqttClient = {},
15
+ MQTT_HOST = 'wxs://mqtt.jiabaida.com/mqtt',
16
+ DELAY_TIME = 300,
17
+ networkModelUrl = 'https://cloud.jiabaida.com/bluetooth/server/device/networkModel',
18
+ request,
19
+ store = null,
20
+ } = options;
13
21
  this.timer = null;
14
22
  this.productKey = 'bms_wifi';
15
23
  this.is0f = false;
@@ -23,9 +31,40 @@ export class MqttServer {
23
31
  this.DELAY_TIME = DELAY_TIME;
24
32
  this.networkModelUrl = networkModelUrl;
25
33
  this.request = request; // 必须传入的http请求方法
34
+ this.$store = store;
35
+ this.setPassthroughType = 1; // 1 - 被动回复 0 - 主动回复
36
+ this.currentCommand = "";
37
+ this.verify3LevelPasswordFn = async (deviceObj, ciphertext) => {}
26
38
  // ...existing code...
27
39
  }
28
40
 
41
+ initData(options = {}) {
42
+ const {
43
+ mqttClient = {},
44
+ MQTT_HOST = 'wxs://mqtt.jiabaida.com/mqtt',
45
+ DELAY_TIME = 300,
46
+ networkModelUrl = 'https://cloud.jiabaida.com/bluetooth/server/device/networkModel',
47
+ request,
48
+ store = null,
49
+ verify3LevelPasswordFn = async () => {},
50
+ } = options;
51
+ this.timer = null;
52
+ this.productKey = 'bms_wifi';
53
+ this.is0f = false;
54
+ this.publishObj = {};
55
+ this.cmdCode = '';
56
+ this.callbackId = '';
57
+ this.mqttCode = '';
58
+ this.mqttPublish = false;
59
+ this.mqttClient = mqttClient;
60
+ this.MQTT_HOST = MQTT_HOST;
61
+ this.DELAY_TIME = DELAY_TIME;
62
+ this.networkModelUrl = networkModelUrl;
63
+ this.request = request; // 必须传入的http请求方法
64
+ this.$store = store;
65
+ this.verify3LevelPasswordFn = verify3LevelPasswordFn;
66
+ }
67
+
29
68
  // 获取参数方法
30
69
  async getParams(item) {
31
70
  try {
@@ -82,12 +121,14 @@ export class MqttServer {
82
121
  handleMqttConnect(client, deviceObj) {
83
122
  console.log('MQTT 连接成功 client', client);
84
123
  console.log('MQTT 连接成功 deviceObj', deviceObj);
85
- this.storeMqttClient(client);
124
+ this.storeMqttClient(client, deviceObj);
86
125
  this.publishInOrder(client, deviceObj).then(() => this.subscribeInOrder(client));
87
126
  }
88
127
 
89
- storeMqttClient(client) {
128
+ storeMqttClient(client, deviceObj) {
90
129
  this.mqttClient[`${client.options.deviceId}_mqtt`] = client;
130
+ this.mqttClient[`${client.options.deviceId}_mqtt`].deviceObj = deviceObj;
131
+ // this.$store?.commit('setMqttClient', this.mqttClient);
91
132
  console.log('存储的 MQTT 客户端:', this.mqttClient);
92
133
  }
93
134
 
@@ -151,6 +192,8 @@ export class MqttServer {
151
192
  }
152
193
  }
153
194
 
195
+
196
+
154
197
  async processMessageAsync(client, message) {
155
198
  const msg = JSON.parse(message);
156
199
  this.callbackId = msg.callbackId;
@@ -158,6 +201,8 @@ export class MqttServer {
158
201
  let resultArray = [];
159
202
  let codeValue = [];
160
203
  if (this.cmdCode === 'jbdPWD') {
204
+ const deviceObj = this.mqttClient[`${client.options.deviceId}_mqtt`]?.deviceObj || {};
205
+ await this.verify3LevelPasswordFn(deviceObj, deviceObj.macAddr);
161
206
  const cmdMap = {
162
207
  2: 'FFAA23010125',
163
208
  3: 'FFAA1F010121',
@@ -174,6 +219,7 @@ export class MqttServer {
174
219
  try {
175
220
  if (resultArray[0]?.toUpperCase() === 'FF' && resultArray[1]?.toUpperCase() === 'AA') {
176
221
  } else if (resultArray[1]?.toUpperCase() === '78' || resultArray[1]?.toUpperCase() === '50') {
222
+ this.currentCommand = resultArray.slice(0, 4).map(o => `0x${o}`)
177
223
  } else {
178
224
  }
179
225
  const publishObj = {
@@ -189,6 +235,7 @@ export class MqttServer {
189
235
  if (this.cmdCode.toLowerCase().includes('wifi_')) {
190
236
  await this.publishATHandle(msg.value);
191
237
  } else {
238
+ console.log('resultArray', resultArray)
192
239
  this.writePublishInfoCmd(resultArray);
193
240
  await sleep(this.DELAY_TIME);
194
241
  }
@@ -212,6 +259,7 @@ export class MqttServer {
212
259
  if (isLast) {
213
260
  await sleep(300);
214
261
  this.mqttPublish = false;
262
+ this.$store?.commit('setMqttPublish', false);
215
263
  uni.removeStorageSync && uni.removeStorageSync(`${deviceId}_mqttPublish`);
216
264
  }
217
265
  } catch (error) {
@@ -219,10 +267,22 @@ export class MqttServer {
219
267
  }
220
268
  }
221
269
 
270
+ closeMqttByDeviceId(deviceId) {
271
+ const client = this.mqttClient[`${deviceId}_mqtt`];
272
+ console.log('closeMqttByDeviceId', this.mqttClient, client)
273
+ if (client) {
274
+ client.end(() => {
275
+ this.mqttClient[`${deviceId}_mqtt`] = null;
276
+ console.log(`MQTT 连接已关闭,设备ID: ${deviceId}`);
277
+ });
278
+ }
279
+ }
280
+
222
281
  handleMqttClose(client, res) {
223
282
  console.log('MQTT 连接关闭', res, client);
224
283
  client.end(() => {
225
284
  this.mqttClient[`${client.options.deviceId}_mqtt`] = null;
285
+ // this.$store?.commit('setMqttClient', this.mqttClient);
226
286
  });
227
287
  }
228
288
 
@@ -238,11 +298,15 @@ export class MqttServer {
238
298
  callbackId: this.publishObj.callbackId,
239
299
  });
240
300
  if (client) {
241
- client.publish(`/${this.publishObj.productKey}/${this.publishObj.clientId}/plat/passthrough/reply`, msg, () => {});
301
+ console.log('publishHandle', client, msg, this.publishObj)
302
+ client.publish(`/${this.publishObj.productKey}/${this.publishObj.clientId}/plat/passthrough/reply`, msg, () => {
303
+ console.log('发布回复主题完成', msg)
304
+ });
242
305
  }
243
306
  if (this.publishObj.isLast) {
244
307
  await sleep(300);
245
308
  this.mqttPublish = false;
309
+ this.$store?.commit('setMqttPublish', false);
246
310
  uni.removeStorageSync && uni.removeStorageSync(`${this.publishObj.deviceId}_mqttPublish`);
247
311
  }
248
312
  }
@@ -266,8 +330,10 @@ export class MqttServer {
266
330
  async writePublishInfoCmd(values) {
267
331
  clearTimeout(this.timer);
268
332
  this.mqttPublish = true;
333
+ this.$store?.commit('setMqttPublish', true);
269
334
  this.mqttCode = values[2];
270
335
  let res = null;
336
+ this.setPassthroughType = 1;
271
337
  // 下发指令逻辑
272
338
  if (values[0]?.toUpperCase() === 'FF' && values[1]?.toUpperCase() === 'AA') {
273
339
  res = await set_PlanCMD_FFAA(this.publishObj.deviceId, values.join(''));
@@ -277,20 +343,24 @@ export class MqttServer {
277
343
  // DD
278
344
  res = await set_PlanCMD_DD(this.publishObj.deviceId, values.join(''));
279
345
  }
346
+ console.log('res', res)
280
347
  const content = res?.data || []
281
348
  let code = Transfer.decimalToHex(content[1]);
282
349
  if (content[0] == 0xff && content[1] == 0xaa) {
283
350
  code = Transfer.decimalToHex(content[2]);
284
351
  }
285
- console.log('code: 回复的code', code);
352
+ console.log('code: 回复的code', code, this.mqttCode);
286
353
  // mqtt连接正常且是平台下发的指令 需上报数据到后端
287
354
  if (code.toUpperCase() == this.mqttCode.toUpperCase()) {
288
355
  this.publishHandle(res?.dataStr);
289
356
  }
290
357
  this.timer = setTimeout(() => {
291
358
  this.mqttPublish = false;
359
+ this.$store?.commit('setMqttPublish', false);
292
360
  }, 30000);
293
361
  }
294
362
  }
295
363
 
364
+ export const mqttServer = new MqttServer()
365
+
296
366
  // export default MqttServer;
@@ -0,0 +1,341 @@
1
+ // import store from '@/store/index';
2
+ import {
3
+ reactive
4
+ } from "vue";
5
+ import { set_PlanCMD_DD } from './BleCmdAnalysis/BleCmdDD';
6
+ import { set_PlanCMD_FFAA } from './BleCmdAnalysis/BleCmdFFAA';
7
+ import { getCMDESInfoAsync } from './BleCmdAnalysis/BleCmdHVES.js';
8
+ import { sleep } from './BleDataProcess.js';
9
+ import Queue from './Queue.js';
10
+ import { Transfer } from './Transfer.js';
11
+ export const tcpSend = reactive({
12
+ isSending: false, //是否在下发平台指令
13
+ isLast: false,
14
+ code: null,
15
+ hasHeartbeat: false,
16
+ tcpClose: false,
17
+ devices: {}
18
+ })
19
+ // / 创建一个队列来存储接收到的消息
20
+ const messageQueue = new Queue();
21
+ let timer = null
22
+ export const tcpServer = {
23
+ version: '2.8.2', // 每次上传需要手动修改此 版本号
24
+ socketOpen: false, //是否建立连接
25
+ macAddr: '',
26
+ txnNo: '',
27
+ baseDelay: 1000, // 初始重试延迟(毫秒)
28
+ maxDelay: 60 * 1000, // 最大重试延迟(毫秒)
29
+ retryCount: 0, // 重试次数
30
+ url: {
31
+ address: 'cloud.jiabaida.com',
32
+ port: 10241
33
+ },
34
+ url1: {
35
+ address: '192.168.80.36',
36
+ port: 10240
37
+ },
38
+ timers: {},
39
+ heartBeatTimers: {},
40
+ tcpSockets: {},
41
+ isRetryings: {},
42
+ socketOpens: {},
43
+ deviceObj: {
44
+ macAddr: '',
45
+ deviceId: ''
46
+ },
47
+ async verify3LevelPasswordFn(deviceObj, ciphertext){},
48
+ async initTCPConnection(deviceObj) {
49
+ const { macAddr, deviceId } = deviceObj
50
+ this.deviceObj = deviceObj
51
+ console.log('macAddr: init-------tcp', macAddr, deviceId);
52
+ return new Promise((resolve, reject) => {
53
+ try {
54
+ tcpSend.devices[macAddr] = {
55
+ macAddr,
56
+ deviceId
57
+ }
58
+ this.tcpSockets[`${deviceId}_socket`] = wx.createTCPSocket();
59
+ console.log('this.tcpSockets[`${deviceId}_socket`]: ', this.tcpSockets[`${deviceId}_socket`]);
60
+ this.tcpSockets[`${deviceId}_socket`].connect(this.url);
61
+ this.tcpSockets[`${deviceId}_socket`].onConnect(() => {
62
+ this.listenerConnect(macAddr, deviceId)
63
+ resolve(true)
64
+ });
65
+
66
+ this.tcpSockets[`${deviceId}_socket`].onMessage((message) => {
67
+ console.log('接收到消息:', message);
68
+ // 处理接收到的消息
69
+ // 将buffer转为常规数组 十六进制
70
+ const intArr = Array.prototype.map.call(
71
+ new Uint8Array(message.message),
72
+ function (bit) {
73
+ return bit;
74
+ }
75
+ );
76
+ // 将十六进制转为对应的字符
77
+ let valueString = String.fromCharCode(...intArr)
78
+ console.log(valueString, 'valueString');
79
+ // messageQueue.enqueue(valueString);
80
+ // this.processQueue()
81
+ this.processMessageAsync(valueString)
82
+ });
83
+
84
+ this.tcpSockets[`${deviceId}_socket`].onError((error) => {
85
+ this.listenerError(macAddr, deviceId, error)
86
+ reject(error)
87
+ });
88
+
89
+ this.tcpSockets[`${deviceId}_socket`].onClose(this.listenerClose(deviceId));
90
+ } catch (error) {
91
+ console.error('error: tcp连接失败', error);
92
+ reject(error)
93
+ }
94
+ })
95
+ },
96
+ listenerConnect(macAddr, deviceId) {
97
+ this.socketOpens[`open_${deviceId}`] = true
98
+ this.isRetryings[`retry_${deviceId}`] = false
99
+ console.log('TCP 连接已建立');
100
+ clearInterval(this.timers[`timer_${deviceId}`])
101
+ this.timers[`timer_${deviceId}`] = null
102
+ // 连接建立后,开始发送心跳包
103
+ this.sendHeartbeat(macAddr, deviceId);
104
+ },
105
+ listenerError(macAddr, deviceId, error) {
106
+ this.socketOpens[`open_${deviceId}`] = false
107
+ console.log('TCP 连接错误:', deviceId, error);
108
+ this.stopHeartBeat(deviceId)
109
+ if (!this.isRetryings[`retry_${deviceId}`]) {
110
+ this.retryCount = 0
111
+ // 重连
112
+ this.reConnect(macAddr, deviceId, true)
113
+ }
114
+ },
115
+ listenerClose(deviceId) {
116
+ this.socketOpens[`open_${deviceId}`] = false
117
+ console.log('TCP 连接已关闭');
118
+ },
119
+ // 关闭tcp连接
120
+ closeTcpSocket(deviceId) {
121
+ try {
122
+ clearInterval(this.timers[`timer_${deviceId}`])
123
+ this.timers[`timer_${deviceId}`] = null
124
+ if (this.tcpSockets[`${deviceId}_socket`]) {
125
+ this.socketOpens[`open_${deviceId}`] = false
126
+ this.stopHeartBeat(deviceId)
127
+ this.tcpSockets[`${deviceId}_socket`].offMessage()
128
+ this.tcpSockets[`${deviceId}_socket`].offConnect()
129
+ this.tcpSockets[`${deviceId}_socket`].offClose()
130
+ this.tcpSockets[`${deviceId}_socket`].offError()
131
+ this.tcpSockets[`${deviceId}_socket`].close()
132
+ this.tcpSockets[`${deviceId}_socket`] = null
133
+ }
134
+ } catch (error) {
135
+ console.error('error: closeTcpSocket', error);
136
+ }
137
+ },
138
+ async reConnect(macAddr, deviceId, closeSocket = false) {
139
+ console.log('重连 this.macAddr', macAddr);
140
+ this.isRetryings[`retry_${deviceId}`] = true
141
+ // 关闭旧的连接再重连
142
+ if (closeSocket) this.tcpSockets[`${deviceId}_socket`].close()
143
+ this.tcpSockets[`${deviceId}_socket`].connect(this.url);
144
+ const delay = Math.min(this.baseDelay * Math.pow(2, this.retryCount), this.maxDelay);
145
+ console.log(`准备 ${delay}ms 后重试...`);
146
+ this.timers[`timer_${deviceId}`] = setTimeout(() => {
147
+ this.reConnect(macAddr, deviceId);
148
+ }, delay);
149
+ this.retryCount++
150
+ },
151
+ sendHeartbeat(macAddr, deviceId) {
152
+ let tokenStr = uni.getStorageSync('token')
153
+ let token = tokenStr.replace('Bearer ', '')
154
+ const heartbeatMsg = {
155
+ command: 96, //业务操作码,96代表心跳包
156
+ data: {
157
+ token,
158
+ appVersion: "xiaoxiang:" + this.version, //小程序版本号
159
+ macAddr,
160
+ },
161
+ txnNo: +new Date(), //当前时间戳
162
+ isAnonymous: 0, //是否匿名登录
163
+ };
164
+ console.log('heartbeatMsg: ', heartbeatMsg);
165
+ tcpSend.hasHeartbeat = false
166
+ this.tcpSockets[`${deviceId}_socket`].write(JSON.stringify(heartbeatMsg));
167
+ this.heartBeatTimers[`heartTimer_${deviceId}`] = setInterval(() => {
168
+ heartbeatMsg.txnNo = +new Date() //当前时间戳
169
+ //##console.log(heartbeatMsg, 'heartbeatMsg');
170
+ tcpSend.hasHeartbeat = false
171
+ this.tcpSockets[`${deviceId}_socket`].write(JSON.stringify(heartbeatMsg));
172
+ }, 30000); // 每5秒发送一次心跳包
173
+ },
174
+ // 清除心跳上报计时器
175
+ stopHeartBeat(deviceId) {
176
+ if (this.heartBeatTimers[`heartTimer_${deviceId}`]) {
177
+ clearInterval(this.heartBeatTimers[`heartTimer_${deviceId}`]);
178
+ this.heartBeatTimers[`heartTimer_${deviceId}`] = null;
179
+ }
180
+ },
181
+ //上报基础 信息
182
+ sendReportData(data) {
183
+ const reportData = {
184
+ command: 999, //业务操作码,999代表上报
185
+ data: {
186
+ value: data.value,
187
+ cmdCode: 'jbd0F',
188
+ bluetoothName: data.bluetoothName,
189
+ },
190
+ macAddr: data.macAddr,
191
+ txnNo: +new Date(), //当前时间戳
192
+ };
193
+ console.log(reportData, 'reportData111上报基础数据');
194
+ this.tcpSockets[`${data.deviceId}_socket`].write(JSON.stringify(reportData));
195
+ },
196
+ // 执行完平台下发的指令后上报数据
197
+ sendWriteReportData(data, deviceId) {
198
+ console.warn('执行后上报数据data: ', data);
199
+ const reportData = {
200
+ command: 121, //业务操作码,代表执行上报
201
+ data: {
202
+ macAddr: this.macAddr,
203
+ content: data,
204
+ },
205
+ txnNo: this.txnNo, //callbackId
206
+ };
207
+ //##console.log(reportData, 'WriteReportData执行指令后上报数据');
208
+ this.tcpSockets[`${deviceId}_socket`].write(JSON.stringify(reportData));
209
+ if (tcpSend.isLast) {
210
+ tcpSend.isSending = false
211
+ }
212
+ },
213
+ // 处理队列中的消息
214
+ processQueue() {
215
+ if (!tcpSend.isSending) {
216
+ this.processNextMessage();
217
+ }
218
+ },
219
+ async processNextMessage() {
220
+ //##console.log('messageQueue-------', messageQueue);
221
+
222
+ if (!messageQueue.isEmpty()) {
223
+ const message = messageQueue.dequeue();
224
+ //##console.log('Processing:', message);
225
+ // 异步处理消息
226
+ this.processMessageAsync(message).then(() => {
227
+ this.processNextMessage();
228
+ });
229
+ } else {
230
+ await sleep(300)
231
+ tcpSend.isSending = false;
232
+ //##console.log('tcpSend.isSending发送完成', tcpSend.isSending);
233
+ }
234
+ },
235
+ async processMessageAsync(message) {
236
+ clearTimeout(timer)
237
+ // 假设这里是异步操作,例如写入数据库
238
+ return new Promise(async (resolve) => {
239
+ try {
240
+ console.log('Message processed:', message);
241
+ let msgObj = JSON.parse(message)
242
+ console.warn('----------msgObj后台下发的指令: -----------', msgObj);
243
+ // 为120,即接收到后台下发的指令
244
+ if (msgObj.command == 120) {
245
+ let value = msgObj.data.content
246
+ if (!value) return
247
+ let resultArray = []
248
+ let codeValue = ''
249
+ if (msgObj.data.cmdCode == 'jbdPWD') {
250
+ // 先验证三级
251
+ console.log('this.deviceObj: 验证三级密码', this.deviceObj);
252
+ await this.verify3LevelPasswordFn(this.deviceObj, this.deviceObj.macAddr)
253
+ console.log('this.deviceObj: 验证三级密码', this.deviceObj);
254
+ let cmdContent = ''
255
+ switch (value) {
256
+ case '2': // 清除一级密码
257
+ cmdContent = "FFAA23010125";
258
+ break
259
+ case '3': // 恢复二级临时密码
260
+ cmdContent = "FFAA1F010121";
261
+ break;
262
+ case '4': // 恢复出厂设置
263
+ cmdContent = "FFAA20010122";
264
+ break;
265
+ case '5': // 清除APPKEY启用标志
266
+ cmdContent = "FFAA23010226";
267
+ break;
268
+ default:
269
+ break;
270
+ }
271
+ codeValue = cmdContent
272
+ } else {
273
+ codeValue = value
274
+ }
275
+ // 使用正则表达式将字符串两两分隔为数组
276
+ resultArray = codeValue.match(/.{1,2}/g);
277
+ console.log('resultArray: ', value, resultArray, codeValue);
278
+ // 给蓝牙设备发送指令
279
+ this.macAddr = msgObj.data.macAddr
280
+ let deviceId = null
281
+ for (let key in tcpSend.devices) {
282
+ if (tcpSend.devices[key].macAddr == this.macAddr) {
283
+ deviceId = tcpSend.devices[key].deviceId
284
+ }
285
+ }
286
+ console.log('deviceId: ', deviceId);
287
+ this.txnNo = msgObj.txnNo
288
+ tcpSend.isLast = msgObj.isLast
289
+ this.writePublishInfoCmd(resultArray, deviceId)
290
+ } else if (msgObj.command == 97) {
291
+ tcpSend.hasHeartbeat = true
292
+ }
293
+ // 超时 30s 未收到指令反馈 认为指令发送失败
294
+ timer = setTimeout(() => {
295
+ tcpSend.isSending = false
296
+ }, 30000)
297
+ resolve(true);
298
+ } catch (error) {
299
+ console.log('error: ', error);
300
+
301
+ }
302
+ });
303
+ },
304
+ // 发送接收的指令
305
+ async writePublishInfoCmd(values, deviceId) {
306
+ tcpSend.isSending = true
307
+ tcpSend.code = values[2]
308
+ // 向蓝牙设备发送一个0x00的16进制数据,读取指令
309
+ let length = values.length
310
+ const buffer = new ArrayBuffer(length);
311
+ const dataView = new DataView(buffer);
312
+ values.forEach((el, index) => {
313
+ // 循环,取每一个十六进制值 添加到buffer
314
+ dataView.setUint8(index, '0x' + el);
315
+ })
316
+ console.log(buffer, values, "读取指令-----------------------");
317
+
318
+ let res = null;
319
+ // 下发指令逻辑
320
+ if (values[0]?.toUpperCase() === 'FF' && values[1]?.toUpperCase() === 'AA') {
321
+ res = await set_PlanCMD_FFAA(deviceId, values.join(''));
322
+ } else if (values[1]?.toUpperCase() === '78' || values[1]?.toUpperCase() === '50') {
323
+ res = await getCMDESInfoAsync(deviceId, values.join(''));
324
+ } else {
325
+ // DD
326
+ res = await set_PlanCMD_DD(deviceId, values.join(''));
327
+ }
328
+ const content = res?.data || []
329
+ let code = Transfer.decimalToHex(content[1]);
330
+ if (content[0] == 0xff && content[1] == 0xaa) {
331
+ code = Transfer.decimalToHex(content[2]);
332
+ }
333
+ console.log('code: 回复的code', code);
334
+ if (code.toUpperCase() == tcpSend?.code?.toUpperCase()) {
335
+ let hexValue = content
336
+ ?.map((num) => Transfer.decimalToHex(num))
337
+ ?.join("");
338
+ this.sendWriteReportData(hexValue, deviceId)
339
+ }
340
+ },
341
+ }
package/src/index.js CHANGED
@@ -9,5 +9,6 @@ export * from './core/keyAndPwdManager';
9
9
  export * from './core/mqttServer.js';
10
10
  export * from './core/OtaUpgrade';
11
11
  export * from './core/rsaEncrypt';
12
+ export * from './core/tcpServer.js';
12
13
  export * from './core/TelinkApi';
13
14
  export * from './core/Transfer';