const common = require('../common.js'); const app = getApp(); const readServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB'; const readID = '000036F6-0000-1000-8000-00805F9B34FB'; const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB'; const writeID = '000036F5-0000-1000-8000-00805F9B34FB'; const MTU = 128; var joinPack = []; function acceptDevice(device) { return device.btid ? true : false; } function isSingleBt() { return true; } function isBuzzer() { return true; } function haveBms() { console.log('是单蓝牙并且带bms'); return false; } function isDevice(device, data) { const advertisData = new Uint8Array(data.advertisData); const mac = device.btid .split('') .map((p, i) => parseInt(p + device.btid[i + 1], 16)) .filter((p, i) => i % 2 == 0); if (advertisData.slice(0, 4).toString() == [9, 255, 1, 2].toString() && advertisData.slice(4, 10).toString() == mac.toString()) { return true; } return false; } function sendCommand(cmd, data = []) { data = [cmd, data.length].concat(data); return common.completArrayCRC(data, data.length); } // 鉴权 function alterConnect(device, deviceId) { return [sendCommand(1, [171, 205, 171, 205])]; } function readData(device, value, data) { var value = new Uint8Array(value); for (var i = 0; value.length > i; i++) { console.log(value[i]); } switch (value[0]) { case 72: if (value.length == 7) { data.voltage = value[2] * 256 + value[3] * 1; if (value[4] == 128) { data.temp = -value[5]; } else { data.temp = value[5]; } } console.log('电压来了'); console.log(data); break; case 66: break; } if (value[0] != 72 && value.length != 7) { return; } return data; } function sendQVCommand(data) { data = [6, data.length].concat(data); return common.completArrayCRC(data, data.length); } function sendDataCommand(data) { data = [2, data.length, (data.length & 65280) >> 8, data.length & 255, 0].concat(data); return common.completArrayCRC(data, data.length); } function voltageToEle(device, value) { var commandList = []; var list = hexToList(value); commandList.push(sendQVCommand(list)); return commandList; } function stateUpdate(device, deviceId) { return [[64, 0, 64]]; } function turnOn(device, deviceId) { return [sendCommand(48, [170])]; } function turnOff(device, deviceId) { return [sendCommand(48, [187])]; } function turnOnBuzzer(device, deviceId) { return [sendCommand(80, [170])]; } function turnOffBuzzer(device, deviceId) { return [sendCommand(80, [187])]; } function bmsDischargeMOS(value, device) { if (value == 1) { const bluetoothConfig = app.globalData.bluetoothConfig; if (Object.keys(bluetoothConfig).includes(device.device_type)) { var turnonCommand = bluetoothConfig[device.device_type].turnon; var commandList = []; var list = hexToList(turnonCommand); commandList.push(sendDataCommand(list)); return commandList; } } else { const bluetoothConfig = app.globalData.bluetoothConfig; if (Object.keys(bluetoothConfig).includes(device.device_type)) { var turnoffCommand = bluetoothConfig[device.device_type].turnoff; var commandList = []; var list = hexToList(turnoffCommand); commandList.push(sendDataCommand(list)); return commandList; } } } function bmsChargingMOS(value, device) { if (value == 1) { const bluetoothConfig = app.globalData.bluetoothConfig; if (Object.keys(bluetoothConfig).includes(device.device_type)) { var chargingMosOn = bluetoothConfig[device.device_type].chargingMosOn; var commandList = []; var list = hexToList(chargingMosOn); commandList.push(sendDataCommand(list)); return commandList; } } else { const bluetoothConfig = app.globalData.bluetoothConfig; if (Object.keys(bluetoothConfig).includes(device.device_type)) { var chargingMosOff = bluetoothConfig[device.device_type].chargingMosOff; var commandList = []; var list = hexToList(chargingMosOff); commandList.push(sendDataCommand(list)); return commandList; } } } function hexToList(str) { var val = []; for (var i = 0; i < str.length / 2; i++) { val.push(parseInt(str.substring(0 + i * 2, 2 + i * 2), 16)); } return val; } module.exports = { readServiceID: readServiceID, readID: readID, writeServiceID: writeServiceID, writeID: writeID, MTU: MTU, acceptDevice: acceptDevice, isDevice: isDevice, alterConnect: alterConnect, readData: readData, stateUpdate: stateUpdate, turnOn: turnOn, turnOff: turnOff, turnOnBuzzer: turnOnBuzzer, turnOffBuzzer: turnOffBuzzer, isSingleBt: isSingleBt, haveBms: haveBms, bmsDischargeMOS: bmsDischargeMOS, bmsChargingMOS: bmsChargingMOS, voltageToEle: voltageToEle, isBuzzer: isBuzzer };