const common = require('./common.js');
import SystemInfoUtil from './SystemInfoUtil.js';
const app = getApp();
const bluetoothDevices = {
	ZX16D: require('./bluetooth/ZX16D.js'),
	FMBMS: require('./bluetooth/FMBMS.js'),
	LFBMS: require('./bluetooth/LFBMS.js'),
	JYBMS: require('./bluetooth/LFBMS.js'),
	BLFM: require('./bluetooth/BLFM.js'),
	ZXBT: require('./bluetooth/ZXBT.js'),
	LSBMS: require('./bluetooth/LSBMS.js'),
	LSCabinet: require('./bluetooth/LSCabinet.js'),
	LSBTS: require('./bluetooth/LSBTS.js'),
	ZXBTS: require('./bluetooth/ZXBTS.js'),
	AD3BTS: require('./bluetooth/AD3BTS.js'),
	BWJT: require('./bluetooth/ZXBT_JL.js'),
	JTBMS: require('./bluetooth/ZXBT_JL.js')
};
//初始化蓝牙
function initBluetooth() {
	//监听蓝牙适配器状态变化事件
	uni.onBluetoothAdapterStateChange((res) => {
		console.log(res);
		Object.keys(app.globalData.adapterStateChangeFunc).forEach((n) => app.globalData.adapterStateChangeFunc[
			n](res));
	});
	//监听低功耗蓝牙连接状态的改变事件。包括开发者主动连接或断开连接，设备丢失，连接异常断开等等
	uni.onBLEConnectionStateChange((res) => {
		console.log(res);
		if (app.globalData.connectionState[res.deviceId]) {
			app.globalData.connectionState[res.deviceId].connected = res.connected;
			const device = app.globalData.connectionState[res.deviceId].device;
			if (app.globalData.connectionStateChangeFunc[device.mac_id]) {
				Object.keys(app.globalData.connectionStateChangeFunc[device.mac_id]).forEach((p) => app
					.globalData.connectionStateChangeFunc[device.mac_id][p](res));
			}
		}
	});
	//监听低功耗蓝牙设备的特征值变化事件。必须先启用 notifyBLECharacteristicValueChange 接口才能接收到设备推送的 notification
	uni.onBLECharacteristicValueChange((res) => {
		console.log(res + "监听到数据")
		if (app.globalData.connectionState[res.deviceId]) {
			const device = app.globalData.connectionState[res.deviceId].device;
			if (
				res.serviceId.toUpperCase() == bluetoothDeviceConfig(device).readServiceID.toUpperCase() &&
				res.characteristicId.toUpperCase() == bluetoothDeviceConfig(device).readID.toUpperCase()
			) {
				var data = bluetoothDeviceConfig(device).readData(device, res.value, app.globalData
					.connectionState[res.deviceId].data);
				if (data) {
					app.globalData.connectionState[res.deviceId].data = data;
					if (app.globalData.characteristicStateChangeFunc[device.mac_id]) {
						Object.keys(app.globalData.characteristicStateChangeFunc[device.mac_id]).forEach((p) =>
							app.globalData.characteristicStateChangeFunc[device.mac_id][p](data)
						);
					}
				}
			}
		}
	});
}

function onAdapterStateChange(name, callback) {
	app.globalData.adapterStateChangeFunc[name] = callback;
}

function offAdapterStateChange(name) {
	delete app.globalData.adapterStateChangeFunc[name];
}

function onConnectionStateChange(macid, name, callback) {
	if (!app.globalData.connectionStateChangeFunc[macid]) {
		app.globalData.connectionStateChangeFunc[macid] = {};
	}
	app.globalData.connectionStateChangeFunc[macid][name] = callback;
}

function offConnectionStateChange(macid, name) {
	if (app.globalData.connectionStateChangeFunc[macid]) {
		delete app.globalData.connectionStateChangeFunc[macid][name];
	}
}

function onCharacteristicStateChange(macid, name, callback) {
	if (!app.globalData.characteristicStateChangeFunc[macid]) {
		app.globalData.characteristicStateChangeFunc[macid] = {};
	}
	app.globalData.characteristicStateChangeFunc[macid][name] = callback;
}

function offCharacteristicStateChange(macid, name) {
	if (app.globalData.characteristicStateChangeFunc[macid]) {
		delete app.globalData.characteristicStateChangeFunc[macid][name];
	}
}

function getAdapterState(callback = () => {}, fail = () => {}) {
	uni.getBluetoothAdapterState({
		success: (res) => {
			callback(res);
		},
		fail: (res) => {
			console.log(res);
			fail(res);
		}
	});
}

function openBluetoothAdapter(callback = () => {}, fail = () => {}) {
	uni.openBluetoothAdapter({
		success: (res) => {
			console.log(res);
			callback(res);
		},
		fail: (res) => {
			console.log(res);
			fail(res);
		}
	});
}

function closeBluetoothAdapter(callback = () => {}, fail = () => {}) {
	uni.closeBluetoothAdapter({
		success: (res) => {
			app.globalData.adapterStateChangeFunc = {};
			app.globalData.connectionStateChangeFunc = {};
			app.globalData.characteristicStateChangeFunc = {};
			app.globalData.connectionState = {};
			callback(res);
		},
		fail: (res) => {
			console.log(res);
			fail(res);
		}
	});
}

function acceptDevice(device) {
	return bluetoothDevices[device.bt_type] || (bluetoothDevices[device.device_type] && bluetoothDevices[device
		.device_type].acceptDevice(device)) ? true : false;
}

function isSingleBT(device) {
	// if (bluetoothDevices[device_type] && bluetoothDevices[device_type].isSingleBt) {
	//   return bluetoothDevices[device_type].isSingleBt()
	// }
	// return false
	if (bluetoothDeviceConfig(device) && bluetoothDeviceConfig(device).isSingleBt) {
		return bluetoothDeviceConfig(device).isSingleBt();
	}
	return false;
}

function isBuzzer(device) {
	if (bluetoothDeviceConfig(device) && bluetoothDeviceConfig(device).isBuzzer) {
		return bluetoothDeviceConfig(device).isBuzzer();
	}
	return false;
}

function isVoltageToEle(device) {
	if (bluetoothDeviceConfig(device) && bluetoothDeviceConfig(device).voltageToEle) {
		return true;
	}
	return false;
}

function isSginleBtByMacid(macid) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	console.log(app.globalData.connectionState);
	if (deviceId == undefined) {
		return false;
	}
	return isSingleBT(app.globalData.connectionState[deviceId].device);
}

function haveBMSForBT(device) {
	if (bluetoothDeviceConfig(device) && bluetoothDeviceConfig(device).haveBms) {
		return bluetoothDeviceConfig(device).haveBms();
	}
	return false;
}

function findDevice(device, callback = () => {}, fail = () => {}) {
	var deviceId = '';
	console.log('开始查找电池');
	if (!bluetoothDeviceConfig(device)) {
		return;
	}
	console.log('查找到为蓝牙设备');
	uni.startBluetoothDevicesDiscovery({
		success(res) {
			console.log(res);
			uni.getBluetoothDevices({
				success: (res) => {
					console.log(res);
					res.devices.forEach((data) => {
						console.log(data);
						if (bluetoothDeviceConfig(device).isDevice(device, data)) {
							//uni.offBluetoothDeviceFound();
							uni.stopBluetoothDevicesDiscovery();
							deviceId = data.deviceId;
							if (app.globalData.connectionState[deviceId]) {
								app.globalData.connectionState[deviceId].device = device;
							} else {
								app.globalData.connectionState[deviceId] = {
									device: device,
									deviceId: deviceId,
									connected: false,
									data: {}
								};
							}
							callback(deviceId);
						}
					});
					setTimeout(function() {
						if (!deviceId) {
							//uni.offBluetoothDeviceFound();
							uni.stopBluetoothDevicesDiscovery();
							fail();
						}
					}, 5000);
				},
				fail(res) {
					uni.stopBluetoothDevicesDiscovery();
					console.log(res);
					fail(res);
				}
			});
			uni.onBluetoothDeviceFound((res) => {
				console.log(res);
				res.devices.forEach((data) => {
					if (bluetoothDeviceConfig(device).isDevice(device, data)) {
						//uni.offBluetoothDeviceFound();
						uni.stopBluetoothDevicesDiscovery();
						deviceId = data.deviceId;
						if (app.globalData.connectionState[deviceId]) {
							app.globalData.connectionState[deviceId].device = device;
						} else {
							app.globalData.connectionState[deviceId] = {
								device: device,
								deviceId: deviceId,
								connected: false,
								data: {}
							};
						}
						callback(deviceId);
					}
				});
			});
		},
		fail(res) {
			console.log(res);
			fail(res);
		}
	});
}

function connectDevice(device, callback = () => {}, fail = () => {}) {
	console.log(device);
	console.log("连接设备中。。。");
	console.log(app.globalData.connectionState);
	if (!bluetoothDeviceConfig(device) || !bluetoothDeviceConfig(device).acceptDevice(device)) {
		return;
	}
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == device.mac_id);
	if (deviceId == undefined) {
		findDevice(
			device,
			(deviceId) => {
				connectDevice(device, callback, fail);
			},
			(res) => {
				fail(res);
			}
		);
		return;
	} else {
		if (app.globalData.connectionState[deviceId].connected) {
			callback();
			return;
		}
	}
	uni.createBLEConnection({
		deviceId: deviceId,
		success: (res) => {
			app.globalData.connectionState[deviceId] = {
				device: device,
				deviceId: deviceId,
				connected: true,
				data: {}
			};
			alterConnect(
				device,
				deviceId,
				(res) => {
					console.log(res);
					callback(res);
				},
				(res) => {
					console.log(res);
					closeDevice(deviceId);
					fail(res);
				}
			);
		},
		fail(res) {
			console.log(res);
			if (res.errCode == -1) {
				closeDevice(
					deviceId,
					(res) => {
						connectDevice(device, callback, fail);
					},
					(res) => {
						fail(res);
					}
				);
			} else {
				fail(res);
			}
		}
	});
}

function alterConnect(device, deviceId, callback = () => {}, fail = () => {}) {
	console.log("校检连接2");
	if (!bluetoothDeviceConfig(device) || !bluetoothDeviceConfig(device).acceptDevice(device)) {
		return;
	}
	console.log("校检连接1");
	setTimeout(() => {
		uni.getBLEDeviceServices({
			deviceId: deviceId,
			success(res) {
				console.log("校检连接3");
				console.log(res);
				console.log(deviceId + "deviceId");
				console.log(bluetoothDeviceConfig(device).writeServiceID + "deviceId");
				uni.getBLEDeviceCharacteristics({
					deviceId: deviceId,
					serviceId: bluetoothDeviceConfig(device).writeServiceID,
					success(res) {
						console.log("校检连接");
						console.log(res);
						uni.getBLEDeviceCharacteristics({
							deviceId: deviceId,
							serviceId: bluetoothDeviceConfig(device).readServiceID,
							success(res) {
								console.log(res);
								uni.notifyBLECharacteristicValueChange({
									state: true,
									deviceId: deviceId,
									serviceId: bluetoothDeviceConfig(device)
										.readServiceID,
									characteristicId: bluetoothDeviceConfig(
										device).readID,
									success(res) {
										console.log(res);
										if (bluetoothDeviceConfig(device).MTU) {
											uni.setBLEMTU({
												deviceId: deviceId,
												mtu: bluetoothDeviceConfig(
													device).MTU,
												success: (res) => {
													console.log(
														'setBLEMTU success>>',
														res);
													if (bluetoothDeviceConfig(
															device)
														.alterConnect
														) {
														var data =
															bluetoothDeviceConfig(
																device
																)
															.alterConnect(
																device,
																deviceId
																);
														if (data) {
															console
																.log(
																	'来写了'
																	);
															writeData
																(device,
																	deviceId,
																	data,
																	callback,
																	fail
																	);
														} else {
															callback
																(
																	res);
														}
													} else {
														callback(
															res);
													}
												},
												fail: (res) => {
													console.log(
														'setBLEMTU fail>>',
														res);
													var data =
														bluetoothDeviceConfig(
															device)
														.alterConnect(
															device,
															deviceId
															);
													if (data) {
														writeData(
															device,
															deviceId,
															data,
															callback,
															fail
															);
													} else {
														callback(
															res);
													}
												}
											});
										} else {
											if (bluetoothDeviceConfig(device)
												.alterConnect) {
												var data =
													bluetoothDeviceConfig(
														device).alterConnect(
														device, deviceId);
												if (data) {
													console.log('来写了99');
													writeData(device, deviceId,
														data, callback, fail
														);
												} else {
													callback(res);
												}
											} else {
												callback(res);
											}
										}
									},
									fail(res) {
										fail(res);
									}
								});
							},
							fail(res) {
								fail(res);
							}
						});
					},
					fail(res) {
						fail(res);
					}
				});
			},
			fail(res) {
				fail(res);
			}
		});
	}, 2000);
}

function closeDevice(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return;
	} else {
		if (!app.globalData.connectionState[deviceId].connected) {
			callback();
			return;
		}
	}
	const device = app.globalData.connectionState[deviceId].device;
	uni.closeBLEConnection({
		deviceId: deviceId,
		success: (res) => {
			console.log(res);
			if (app.globalData.connectionState[deviceId]) {
				app.globalData.connectionState[deviceId].connected = false;
			}
			callback(res);
		},
		fail(res) {
			console.log(res);
			fail(res);
		}
	});
}

function bmsInfo(macid) {
	console.log(macid);
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).bmsInfo) {
		return false;
	}
	return bluetoothDeviceConfig(device).bmsInfo(device, deviceId, app.globalData.connectionState[deviceId].data);
}

function bmsSet(deviceInfo, callback = () => {}, fail = () => {}) {
	console.log(deviceInfo.mac_id);
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == deviceInfo.mac_id);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	console.log('3333');
	if (!bluetoothDeviceConfig(device).bmsSet) {
		console.log('4444');
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).bmsSet(deviceInfo);
	console.log('555');
	if (data) {
		console.log('6666');
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	console.log('777');
	fail();
	return false;
}

// function bmsSet(macid, name, vars, callback = () => { }, fail = () => { }) {
//   const deviceId = Object.keys(app.globalData.connectionState).find(i => app.globalData.connectionState[i].device.mac_id == macid)
//   if (deviceId == undefined) {
//     fail()
//     return false
//   }
//   const device = app.globalData.connectionState[deviceId].device

//   if (!bluetoothDevices[device.device_type].bmsSet) {
//     fail()
//     return false
//   }

//   var data = bluetoothDevices[device.device_type].bmsSet(device, deviceId, name, vars)
//   if (data) {
//     writeData(device, deviceId, data, callback, fail)
//     return true
//   }

//   fail()
//   return false
// }

function writeData(device, deviceId, data, callback = () => {}, fail = () => {}) {
	if (data.length == 0) {
		return;
	}
	var buffer;
	console.log(data + "data");
	buffer = common.toArrayBuffer(data.shift());
	console.log(buffer);
	console.log(deviceId)
	console.log(bluetoothDeviceConfig(device).writeServiceID)
	console.log(bluetoothDeviceConfig(device).writeID)
	uni.writeBLECharacteristicValue({
		deviceId: deviceId,
		serviceId: bluetoothDeviceConfig(device).writeServiceID,
		characteristicId: bluetoothDeviceConfig(device).writeID,
		value: buffer,
		success(res) {
			console.log(res);
			console.log(data.length);
			if (data.length == 0) {
				callback(res);
			} else {
				setTimeout(() => {
					console.log('来写了91');
					writeData(device, deviceId, data, callback, fail);
				}, 500);
			}
		},
		fail(res) {
			console.log(res);
			fail(res);
		}
	});
}

function stateUpdate(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).stateUpdate) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).stateUpdate(device, deviceId);
	console.log("电池" + data)
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function voltageToEle(macid, value, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).voltageToEle) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).voltageToEle(device, value);
	if (data) {
		console.log('来写配置文件');
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function turnOn(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).turnOn) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).turnOn(device, deviceId);
	if (data) {
		console.log('来写了');
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function turnOnBuzzer(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).turnOnBuzzer) {
		console.log('找不到turnOnBuzzer');
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).turnOnBuzzer(device, deviceId);
	if (data) {
		console.log('来写了');
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function turnOffBuzzer(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).turnOffBuzzer) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).turnOffBuzzer(device, deviceId);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function turnOff(macid, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	console.log(deviceId);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).turnOff) {
		console.log(deviceId + '失败2');
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).turnOff(device, deviceId);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	console.log(data + '失败3');
	fail();
	return false;
}

function bmsChargingMOS(macid, value, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).bmsChargingMOS) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).bmsChargingMOS(value - 0, device);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function bmsDischargeMOS(macid, value, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).bmsDischargeMOS) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).bmsDischargeMOS(value - 0, device);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

//查询波特率
function queryBaud(macid, callback = () => {}, fail = () => {}) {
	console.log(macid);
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).queryBaud) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).queryBaud(device, deviceId);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

//设置波特率
function setBaud(macid, value, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDeviceConfig(device).setBaud) {
		fail();
		return false;
	}
	var data = bluetoothDeviceConfig(device).setBaud(value - 0, device);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function isConnected(macid) {
	console.log(app.globalData.connectionState);
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	console.log(app.globalData.connectionState);
	if (deviceId == undefined) {
		return false;
	}
	return app.globalData.connectionState[deviceId].connected;
}

function getData(macid) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		return false;
	}
	return app.globalData.connectionState[deviceId].data;
}

function getConnectionState(macid) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		return false;
	}
	return app.globalData.connectionState[deviceId];
}

function bluetoothDeviceConfig(device) {
	if (bluetoothDevices[device.bt_type]) {
		return bluetoothDevices[device.bt_type];
	} else if (bluetoothDevices[device.device_type]) {
		return bluetoothDevices[device.device_type];
	} else {
		return false;
	}
}

function isUniversalBluetoothPlugin(device) {
	if (bluetoothDevices[device.bt_type]) {
		return bluetoothDevices[device.bt_type];
	} else {
		return false;
	}
}

function sendHireCommand(macid, info, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendHireCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendHireCommand(info);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function sendBackCommand(macid, info, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendBackCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendBackCommand(info);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function sendExchangeCommand(macid, info, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendExchangeCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendExchangeCommand(info);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function sendGetCabinetInfoCommand(macid, info, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendGetCabinetInfoCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendGetCabinetInfoCommand(info);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function sendConfirmCommand(macid, value, serialNum, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendConfirmCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendConfirmCommand(value, serialNum);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}

function sendCancelCommand(macid, serialNum, callback = () => {}, fail = () => {}) {
	const deviceId = Object.keys(app.globalData.connectionState).find((i) => app.globalData.connectionState[i].device
		.mac_id == macid);
	if (deviceId == undefined) {
		fail();
		return false;
	}
	const device = app.globalData.connectionState[deviceId].device;
	if (!bluetoothDevices[device.device_type].sendCancelCommand) {
		fail();
		return false;
	}
	var data = bluetoothDevices[device.device_type].sendCancelCommand(serialNum);
	if (data) {
		writeData(device, deviceId, data, callback, fail);
		return true;
	}
	fail();
	return false;
}
module.exports = {
	initBluetooth: initBluetooth,
	onAdapterStateChange: onAdapterStateChange,
	offAdapterStateChange: offAdapterStateChange,
	onConnectionStateChange: onConnectionStateChange,
	offConnectionStateChange: offConnectionStateChange,
	onCharacteristicStateChange: onCharacteristicStateChange,
	offCharacteristicStateChange: offCharacteristicStateChange,
	getAdapterState: getAdapterState,
	openBluetoothAdapter: openBluetoothAdapter,
	closeBluetoothAdapter: closeBluetoothAdapter,
	acceptDevice: acceptDevice,
	findDevice: findDevice,
	connectDevice: connectDevice,
	closeDevice: closeDevice,
	stateUpdate: stateUpdate,
	turnOn: turnOn,
	turnOff: turnOff,
	turnOnBuzzer: turnOnBuzzer,
	turnOffBuzzer: turnOffBuzzer,
	bmsInfo: bmsInfo,
	bmsSet: bmsSet,
	isConnected: isConnected,
	getData: getData,
	isSingleBT: isSingleBT,
	isBuzzer: isBuzzer,
	haveBMSForBT: haveBMSForBT,
	isSginleBtByMacid: isSginleBtByMacid,
	getConnectionState: getConnectionState,
	bmsChargingMOS: bmsChargingMOS,
	bmsDischargeMOS: bmsDischargeMOS,
	bluetoothDeviceConfig: bluetoothDeviceConfig,
	voltageToEle: voltageToEle,
	isVoltageToEle: isVoltageToEle,
	setBaud: setBaud,
	queryBaud: queryBaud,
	isUniversalBluetoothPlugin: isUniversalBluetoothPlugin,
	//机柜协议增加
	sendHireCommand: sendHireCommand,
	sendBackCommand: sendBackCommand,
	sendExchangeCommand: sendExchangeCommand,
	sendGetCabinetInfoCommand: sendGetCabinetInfoCommand,
	sendConfirmCommand: sendConfirmCommand,
	sendCancelCommand: sendCancelCommand
};