@jiabaida/tools 1.0.8 → 1.1.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 (37) 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/mqttServer.js +1 -1
  11. package/dist/cjs/core/tcpServer.js +1 -1
  12. package/dist/cjs/index.js +1 -1
  13. package/dist/esm/core/BleApiManager.js +1 -1
  14. package/dist/esm/core/BleCmdAnalysis/BaseParamProtocol.js +1 -1
  15. package/dist/esm/core/BleCmdAnalysis/BleCmdAnalysis.js +1 -1
  16. package/dist/esm/core/BleCmdAnalysis/BleCmdDD.js +1 -1
  17. package/dist/esm/core/BleCmdAnalysis/BleCmdDDA4.js +1 -1
  18. package/dist/esm/core/BleCmdAnalysis/BleCmdFFAA.js +1 -1
  19. package/dist/esm/core/BleCmdAnalysis/BleCmdHVES.js +1 -1
  20. package/dist/esm/core/BleCmdAnalysis/readAndSetParam.js +1 -1
  21. package/dist/esm/core/BleDataProcess.js +1 -1
  22. package/dist/esm/core/OtaUpgrade.js +1 -1
  23. package/dist/esm/core/mqttServer.js +1 -1
  24. package/dist/esm/core/tcpServer.js +1 -1
  25. package/dist/esm/index.js +1 -1
  26. package/package.json +1 -1
  27. package/src/core/BleApiManager.js +33 -15
  28. package/src/core/BleCmdAnalysis/BaseParamProtocol.js +59 -34
  29. package/src/core/BleCmdAnalysis/BleCmdAnalysis.js +58 -2
  30. package/src/core/BleCmdAnalysis/BleCmdDD.js +16 -6
  31. package/src/core/BleCmdAnalysis/BleCmdDDA4.js +154 -227
  32. package/src/core/BleCmdAnalysis/BleCmdFFAA.js +1 -0
  33. package/src/core/BleCmdAnalysis/readAndSetParam.js +45 -17
  34. package/src/core/BleDataProcess.js +15 -11
  35. package/src/core/OtaUpgrade.js +356 -356
  36. package/src/core/mqttServer.js +58 -7
  37. package/src/core/tcpServer.js +76 -38
@@ -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, store = null }) {
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;
@@ -24,9 +32,39 @@ export class MqttServer {
24
32
  this.networkModelUrl = networkModelUrl;
25
33
  this.request = request; // 必须传入的http请求方法
26
34
  this.$store = store;
35
+ this.setPassthroughType = 1; // 1 - 被动回复 0 - 主动回复
36
+ this.currentCommand = "";
37
+ this.verify3LevelPasswordFn = async (deviceObj, ciphertext) => {}
27
38
  // ...existing code...
28
39
  }
29
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
+
30
68
  // 获取参数方法
31
69
  async getParams(item) {
32
70
  try {
@@ -83,13 +121,14 @@ export class MqttServer {
83
121
  handleMqttConnect(client, deviceObj) {
84
122
  console.log('MQTT 连接成功 client', client);
85
123
  console.log('MQTT 连接成功 deviceObj', deviceObj);
86
- this.storeMqttClient(client);
124
+ this.storeMqttClient(client, deviceObj);
87
125
  this.publishInOrder(client, deviceObj).then(() => this.subscribeInOrder(client));
88
126
  }
89
127
 
90
- storeMqttClient(client) {
128
+ storeMqttClient(client, deviceObj) {
91
129
  this.mqttClient[`${client.options.deviceId}_mqtt`] = client;
92
- this.$store?.commit('setMqttClient', this.mqttClient);
130
+ this.mqttClient[`${client.options.deviceId}_mqtt`].deviceObj = deviceObj;
131
+ // this.$store?.commit('setMqttClient', this.mqttClient);
93
132
  console.log('存储的 MQTT 客户端:', this.mqttClient);
94
133
  }
95
134
 
@@ -162,6 +201,8 @@ export class MqttServer {
162
201
  let resultArray = [];
163
202
  let codeValue = [];
164
203
  if (this.cmdCode === 'jbdPWD') {
204
+ const deviceObj = this.mqttClient[`${client.options.deviceId}_mqtt`]?.deviceObj || {};
205
+ await this.verify3LevelPasswordFn(deviceObj, deviceObj.macAddr);
165
206
  const cmdMap = {
166
207
  2: 'FFAA23010125',
167
208
  3: 'FFAA1F010121',
@@ -178,6 +219,7 @@ export class MqttServer {
178
219
  try {
179
220
  if (resultArray[0]?.toUpperCase() === 'FF' && resultArray[1]?.toUpperCase() === 'AA') {
180
221
  } else if (resultArray[1]?.toUpperCase() === '78' || resultArray[1]?.toUpperCase() === '50') {
222
+ this.currentCommand = resultArray.slice(0, 4).map(o => `0x${o}`)
181
223
  } else {
182
224
  }
183
225
  const publishObj = {
@@ -193,6 +235,7 @@ export class MqttServer {
193
235
  if (this.cmdCode.toLowerCase().includes('wifi_')) {
194
236
  await this.publishATHandle(msg.value);
195
237
  } else {
238
+ console.log('resultArray', resultArray)
196
239
  this.writePublishInfoCmd(resultArray);
197
240
  await sleep(this.DELAY_TIME);
198
241
  }
@@ -226,6 +269,7 @@ export class MqttServer {
226
269
 
227
270
  closeMqttByDeviceId(deviceId) {
228
271
  const client = this.mqttClient[`${deviceId}_mqtt`];
272
+ console.log('closeMqttByDeviceId', this.mqttClient, client)
229
273
  if (client) {
230
274
  client.end(() => {
231
275
  this.mqttClient[`${deviceId}_mqtt`] = null;
@@ -238,7 +282,7 @@ export class MqttServer {
238
282
  console.log('MQTT 连接关闭', res, client);
239
283
  client.end(() => {
240
284
  this.mqttClient[`${client.options.deviceId}_mqtt`] = null;
241
- this.$store?.commit('setMqttClient', this.mqttClient);
285
+ // this.$store?.commit('setMqttClient', this.mqttClient);
242
286
  });
243
287
  }
244
288
 
@@ -254,7 +298,10 @@ export class MqttServer {
254
298
  callbackId: this.publishObj.callbackId,
255
299
  });
256
300
  if (client) {
257
- 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
+ });
258
305
  }
259
306
  if (this.publishObj.isLast) {
260
307
  await sleep(300);
@@ -286,6 +333,7 @@ export class MqttServer {
286
333
  this.$store?.commit('setMqttPublish', true);
287
334
  this.mqttCode = values[2];
288
335
  let res = null;
336
+ this.setPassthroughType = 1;
289
337
  // 下发指令逻辑
290
338
  if (values[0]?.toUpperCase() === 'FF' && values[1]?.toUpperCase() === 'AA') {
291
339
  res = await set_PlanCMD_FFAA(this.publishObj.deviceId, values.join(''));
@@ -295,12 +343,13 @@ export class MqttServer {
295
343
  // DD
296
344
  res = await set_PlanCMD_DD(this.publishObj.deviceId, values.join(''));
297
345
  }
346
+ console.log('res', res)
298
347
  const content = res?.data || []
299
348
  let code = Transfer.decimalToHex(content[1]);
300
349
  if (content[0] == 0xff && content[1] == 0xaa) {
301
350
  code = Transfer.decimalToHex(content[2]);
302
351
  }
303
- console.log('code: 回复的code', code);
352
+ console.log('code: 回复的code', code, this.mqttCode);
304
353
  // mqtt连接正常且是平台下发的指令 需上报数据到后端
305
354
  if (code.toUpperCase() == this.mqttCode.toUpperCase()) {
306
355
  this.publishHandle(res?.dataStr);
@@ -312,4 +361,6 @@ export class MqttServer {
312
361
  }
313
362
  }
314
363
 
364
+ export const mqttServer = new MqttServer()
365
+
315
366
  // export default MqttServer;
@@ -40,7 +40,14 @@ export const tcpServer = {
40
40
  tcpSockets: {},
41
41
  isRetryings: {},
42
42
  socketOpens: {},
43
- async initTCPConnection(macAddr, deviceId) {
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
44
51
  console.log('macAddr: init-------tcp', macAddr, deviceId);
45
52
  return new Promise((resolve, reject) => {
46
53
  try {
@@ -237,10 +244,37 @@ export const tcpServer = {
237
244
  if (msgObj.command == 120) {
238
245
  let value = msgObj.data.content
239
246
  if (!value) return
240
-
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
+ }
241
275
  // 使用正则表达式将字符串两两分隔为数组
242
- let resultArray = value.match(/.{1,2}/g);
243
- console.log('resultArray: ', resultArray);
276
+ resultArray = codeValue.match(/.{1,2}/g);
277
+ console.log('resultArray: ', value, resultArray, codeValue);
244
278
  // 给蓝牙设备发送指令
245
279
  this.macAddr = msgObj.data.macAddr
246
280
  let deviceId = null
@@ -252,40 +286,7 @@ export const tcpServer = {
252
286
  console.log('deviceId: ', deviceId);
253
287
  this.txnNo = msgObj.txnNo
254
288
  tcpSend.isLast = msgObj.isLast
255
- tcpSend.isSending = true
256
- tcpSend.code = resultArray[2]
257
- // 向蓝牙设备发送一个0x00的16进制数据,读取指令
258
- let length = resultArray.length
259
- const buffer = new ArrayBuffer(length);
260
- const dataView = new DataView(buffer);
261
- resultArray.forEach((el, index) => {
262
- // 循环,取每一个十六进制值 添加到buffer
263
- dataView.setUint8(index, '0x' + el);
264
- })
265
- console.log(buffer, "读取指令-----------------------");
266
- // store.commit('setPassthroughType', 1);// 设置类型为 被动回复1
267
- // BLE.sendMsgToKey(buffer, deviceId)
268
-
269
- // this.mqttCode = resultArray[2];
270
- let res = null;
271
- // 下发指令逻辑
272
- if (resultArray[0]?.toUpperCase() === 'FF' && resultArray[1]?.toUpperCase() === 'AA') {
273
- res = await set_PlanCMD_FFAA(deviceId, resultArray.join(''));
274
- } else if (resultArray[1]?.toUpperCase() === '78' || resultArray[1]?.toUpperCase() === '50') {
275
- res = await getCMDESInfoAsync(deviceId, resultArray.join(''));
276
- } else {
277
- // DD
278
- res = await set_PlanCMD_DD(deviceId, resultArray.join(''));
279
- }
280
- const content = res?.data || []
281
- let code = Transfer.decimalToHex(content[1]);
282
- if (content[0] == 0xff && content[1] == 0xaa) {
283
- code = Transfer.decimalToHex(content[2]);
284
- }
285
- console.log('code: 回复的code', code);
286
- if (code.toUpperCase() == tcpSend?.code?.toUpperCase()) {
287
- this.sendWriteReportData(content, deviceId)
288
- }
289
+ this.writePublishInfoCmd(resultArray, deviceId)
289
290
  } else if (msgObj.command == 97) {
290
291
  tcpSend.hasHeartbeat = true
291
292
  }
@@ -300,4 +301,41 @@ export const tcpServer = {
300
301
  }
301
302
  });
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
+ },
303
341
  }