const common = require('../common.js');

// const readServiceID = '00001811-0000-1000-8000-00805F9B34FB'
// const readID = '000036F6-0000-1000-8000-00805F9B34FB'
// const writeServiceID = '0000FEE7-0000-1000-8000-00805F9B34FB'
// const writeID = '0000FEE7-0000-1000-8000-00805F9B34FB'

const readServiceID = '00001811-0000-1000-8000-00805F9B34FB';
const readID = '000036F6-0000-1000-8000-00805F9B34FB';
const writeServiceID = '00001811-0000-1000-8000-00805F9B34FB';
const writeID = '0000FEE7-0000-1000-8000-00805F9B34FB';
const MTU = 115;
function acceptDevice(device) {
    return device.btid ? true : false;
}
function isSingleBt() {
    return true;
}
function isDevice(device, data) {
    console.log('是否是当前设备');
    console.log(data);
    console.log(device);
    var reg = new RegExp(':', 'g');
    var daeviceBtId = data.deviceId.replace(reg, '');
    if (daeviceBtId === device.btid) {
        console.log('是当前设备' + device.btid);
        return true;
    }
    return false;
    // const advertisData = new Uint8Array(data.advertisData)
    // const mac = device.btid.split('').map((p, i) => parseInt(p + device.btid[i + 1], 15)).filter((p, i) => i % 2 == 0)
    // if (advertisData.slice(0, 4).toString() == [0x0A, 0xFF, 0x01, 0x02].toString() &&
    //   advertisData.slice(4, 10).toString() == mac.toString()) {
    // return true
    // }
    // return false
}
function readData(device, value, data) {
    var value = new Uint8Array(value);
    console.log(value);
    if (value.length > 17) {
        switch (value[5]) {
            case 15:
                {
                    data.quantity = value[12];
                    data.voltage = Math.round(value[14] * 256 + value[15]) / 100;
                }
                break;
        }
    }
    return data;
}
function stateUpdate(device, deviceId) {
    console.log('获取电池信息');
    var data = [[90, 88, 58, 0, 0, 15, 251]];
    return data;
}
function turnOn(device, deviceId) {
    console.log('恢复通电');
    var data = [[90, 88, 58, 0, 0, 0, 236]];
    return data;
}
function turnOff(device, deviceId) {
    console.log('断电');
    var data = [[90, 88, 58, 0, 0, 1, 237]];
    return data;
}
module.exports = {
    readServiceID: readServiceID,
    readID: readID,
    writeServiceID: writeServiceID,
    writeID: writeID,
    MTU: MTU,
    acceptDevice: acceptDevice,
    isDevice: isDevice,
    readData: readData,
    stateUpdate: stateUpdate,
    turnOn: turnOn,
    turnOff: turnOff,
    isSingleBt: isSingleBt
};
