|
@@ -9,6 +9,8 @@ const writeID = '000036F5-0000-1000-8000-00805F9B34FB';
|
|
|
const MTU = 115;
|
|
|
const app = getApp();
|
|
|
let subIndex = -1
|
|
|
+let commands = [];
|
|
|
+let currentCommandIndex = 1;
|
|
|
|
|
|
function acceptDevice(device) {
|
|
|
return device.btid ? true : false;
|
|
@@ -27,7 +29,7 @@ function haveBms() {
|
|
|
function isDevice(device, data) {
|
|
|
|
|
|
const advertisData = new Uint8Array(data.advertisData);
|
|
|
- console.log(advertisData,'判断返回第几个字段是正常模式还是升级模式');
|
|
|
+
|
|
|
|
|
|
|
|
|
const mac = device.btid
|
|
@@ -49,16 +51,23 @@ function isDevice(device, data) {
|
|
|
function alterConnect(device, deviceId) {
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
- const data=[ 0x1F, 0x0F,0x38, 0x36, 0x35, 0x34, 0x31, 0x36, 0x30, 0x33, 0x38, 0x30, 0x30, 0x32, 0x32, 0x30, 0x31]
|
|
|
- const crc_data = crc8Ieee8023(data)
|
|
|
- console.log(crc_data, 111);
|
|
|
+ const data=[ 0x1F, 0x0F,0x38, 0x36, 0x35, 0x34, 0x31, 0x36, 0x30, 0x33, 0x38, 0x30, 0x30, 0x32, 0x32, 0x30, 0x31,0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00]
|
|
|
+
|
|
|
+ const crc_data = crc8IEEE8023(data)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
return [
|
|
|
[0x22, 0x22, 0x01, 0x1F, 0x0F, 0x38, 0x36, 0x35, 0x34, 0x31, 0x36, 0x30, 0x33, 0x38, 0x30, 0x30, 0x32, 0x32, 0x30, 0x31, 0x08, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x67, 0xAC, 0x53, 0xE7, 0x08, 0x00,
|
|
|
crc_data, 0xAA, 0xAA
|
|
@@ -67,56 +76,56 @@ function alterConnect(device, deviceId) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+function crc8IEEE8023(data) {
|
|
|
+ const poly = 0x07;
|
|
|
+ let crc = 0x00;
|
|
|
|
|
|
-function crc8Ieee8023(data) {
|
|
|
- let crc = 0x00;
|
|
|
- const polynomial = 0x07;
|
|
|
-
|
|
|
- for (let byte of data) {
|
|
|
- crc ^= byte;
|
|
|
-
|
|
|
- for (let i = 0; i < 8; i++) {
|
|
|
- if (crc & 0x80) {
|
|
|
- crc = (crc << 1) ^ polynomial;
|
|
|
- } else {
|
|
|
- crc <<= 1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- crc &= 0xFF;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return '0x' + crc.toString(16).toUpperCase().padStart(2, '0');
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ crc ^= data[i];
|
|
|
+ for (let j = 0; j < 8; j++) {
|
|
|
+ if (crc & 0x80) {
|
|
|
+ crc = ((crc << 1) ^ poly) & 0xFF;
|
|
|
+ } else {
|
|
|
+ crc = (crc << 1) & 0xFF;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return crc;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
function readData(device, value, data) {
|
|
|
|
|
|
var value = new Uint8Array(value);
|
|
|
- console.log(value, 'value**************************');
|
|
|
- switch (value[0]) {
|
|
|
- case 0:
|
|
|
-
|
|
|
- switchFactory(device)
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ const normalArray = Array.from(value);
|
|
|
+ console.log(normalArray, 'value**************************');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ switch(normalArray[3]){
|
|
|
+ case 0x00:
|
|
|
+ sendNextCommand(device.mac_id)
|
|
|
+ break;
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ case 0x01:
|
|
|
+ test()
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
return data;
|
|
|
}
|
|
|
-
|
|
|
-function parseOTAData(){
|
|
|
-
|
|
|
-
|
|
|
+function test(){
|
|
|
+ currentCommandIndex=1
|
|
|
+ console.log(commands[currentCommandIndex],'test');
|
|
|
}
|
|
|
|
|
|
+
|
|
|
function sendCommand(cmd, data = []) {
|
|
|
const startCmd = [0x02, 0x02]
|
|
|
const endCmd = [0xAA, 0xAA]
|
|
@@ -145,55 +154,166 @@ function switchFactory(device, deviceId) {
|
|
|
return [[0x22,0x22,0x54,0x01,0x01,0x12,0xAA,0xAA]]
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+function otaUpgrade(device){
|
|
|
+
|
|
|
+ const data = readBinBinarayToCommand()
|
|
|
+ data.then(result=>{commands=result})
|
|
|
+
|
|
|
+
|
|
|
+ return data
|
|
|
+}
|
|
|
+async function readBinBinarayToCommand() {
|
|
|
+ let res =await uni.request({
|
|
|
+ url: 'https://opt.bms16.com/ota.BIN',
|
|
|
+ method: 'GET',
|
|
|
+ responseType: 'arraybuffer',
|
|
|
+ });
|
|
|
+ const arrayBuffer=res[1].data
|
|
|
+ const uint8Array = new Uint8Array(arrayBuffer);
|
|
|
+ const test = splitArrayIntoChunks(uint8Array, 80);
|
|
|
+ const data =makeArr(test)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return data
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
-async function otaUpgrade(device, deviceId) {
|
|
|
-
|
|
|
-
|
|
|
- const subPackage = await readBinBinarayToCommand()
|
|
|
-
|
|
|
- return dataRecursion(subPackage)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+function sendNextCommand(mac_id) {
|
|
|
+ console.log(commands[currentCommandIndex],'command');
|
|
|
+ if (currentCommandIndex >= commands.length) {
|
|
|
+ console.log('所有指令发送完成');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device.mac_id == mac_id);
|
|
|
+ if (deviceId == undefined) {
|
|
|
+ console.log('deviceId == undefined');
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ const command = [commands[currentCommandIndex]];
|
|
|
+
|
|
|
+ if(command.length==0){
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var buffer;
|
|
|
+ buffer = common.toArrayBuffer(command.shift());
|
|
|
+
|
|
|
+ uni.writeBLECharacteristicValue({
|
|
|
+ deviceId: deviceId,
|
|
|
+ serviceId: writeServiceID,
|
|
|
+ characteristicId: writeID,
|
|
|
+ value: buffer,
|
|
|
+ success() {
|
|
|
+
|
|
|
+ },
|
|
|
+ fail(err) {
|
|
|
+ console.error('指令发送失败', err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ currentCommandIndex++;
|
|
|
}
|
|
|
|
|
|
+function splitArrayIntoChunks(array, chunkSize) {
|
|
|
+ const numChunks = Math.ceil(array.length / chunkSize);
|
|
|
+ return Array.from({ length: numChunks }, (_, i) => {
|
|
|
+ return array.slice(i * chunkSize, (i + 1) * chunkSize);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function makeArr(array) {
|
|
|
+ const result = [];
|
|
|
+ for (let i = 0; i < array.length; i ++) {
|
|
|
+
|
|
|
+ let endPackage = 0x00
|
|
|
+ if((i + 1) >= array.length){
|
|
|
+
|
|
|
+ endPackage = 0x01
|
|
|
+ }
|
|
|
+
|
|
|
+ let startArray = []
|
|
|
+ const packageLen=parseInt((array[i].length+8).toString(16).toUpperCase(), 16)
|
|
|
+
|
|
|
+
|
|
|
+ const totalPackageSize=[0x00,0x00,0x01,0xE9,0xac]
|
|
|
+ startArray.push(packageLen,...totalPackageSize)
|
|
|
+
|
|
|
+ const ids=splitNumber(i)
|
|
|
+ startArray.push(...ids)
|
|
|
+
|
|
|
+ startArray.push(endPackage)
|
|
|
+
|
|
|
+ const formattedChunk = array[i].map(byte => `0x${byte.toString(16).padStart(2, '0')}`);
|
|
|
+ startArray.push(...formattedChunk)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ const crcStr='0x'+crc8IEEE8023(startArray).toString(16).toUpperCase()
|
|
|
+ const testCrc=parseInt(crcStr,16)
|
|
|
+ startArray.push(crcStr)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ startArray.push(0xAA,0xAA)
|
|
|
+ startArray.unshift(0x22,0x22,0x30)
|
|
|
+ result.push(startArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function splitNumber(num) {
|
|
|
+
|
|
|
+ const numStr = num.toString();
|
|
|
+
|
|
|
+
|
|
|
+ const parts = numStr.match(/.{1,2}/g) || [];
|
|
|
+
|
|
|
+
|
|
|
+ let result = parts.map(part => parseInt(part, 10));
|
|
|
+
|
|
|
+
|
|
|
+ if (result.length === 1) {
|
|
|
+ result = [0, ...result];
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
function dataRecursion(subPackage){
|
|
|
subIndex += 1
|
|
|
let endPackage = 0x00
|
|
|
if(subPackage.groupedArray.length - 1 == subIndex){
|
|
|
endPackage = 0x01
|
|
|
}
|
|
|
- let startArray = [0x22,0x22,0x30,0x30,0x00,0x58,0x00]
|
|
|
+
|
|
|
+ let startArray = [0x00,0x58,0x00]
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
startArray = startArray.concat(decimalToHexBytes(subPackage.size,4))
|
|
|
- console.log(222222)
|
|
|
+
|
|
|
startArray = startArray.concat(decimalToHexBytes(subIndex + 1,2))
|
|
|
+
|
|
|
startArray.push(endPackage)
|
|
|
+
|
|
|
startArray = startArray.concat(subPackage.groupedArray[subIndex])
|
|
|
- startArray.push(0xCB)
|
|
|
- console.log(subPackage.groupedArray[subIndex])
|
|
|
+
|
|
|
+ const crcStr=crc8IEEE8023(startArray)
|
|
|
+ startArray.push(crcStr)
|
|
|
+ startArray.unshift(0x22,0x22,0x30)
|
|
|
startArray = startArray.concat([0xAA,0xAA])
|
|
|
-
|
|
|
- console.log(startArray)
|
|
|
- return startArray
|
|
|
+
|
|
|
+ return [startArray]
|
|
|
}
|
|
|
|
|
|
-async function readBinBinarayToCommand() {
|
|
|
- let res = await uni.request({
|
|
|
- url: 'https://opt.bms16.com/ota.BIN',
|
|
|
- method: 'GET',
|
|
|
- responseType: 'arraybuffer',
|
|
|
- });
|
|
|
-
|
|
|
- return arrayBufferToHexWithPrefix(res[1].data)
|
|
|
-}
|
|
|
function arrayBufferToHexWithPrefix(buffer,groupSize = 80) {
|
|
|
const uint8Array = new Uint8Array(buffer);
|
|
|
const groupedArray = [];
|