|
|
@@ -37,10 +37,11 @@ import kotlinx.coroutines.launch
|
|
|
import kotlinx.serialization.json.Json
|
|
|
|
|
|
/**
|
|
|
- * 用于管理蓝牙钥匙设备的任务读取操作
|
|
|
+ * 用于管理蓝牙钥匙设备的作业数据读取操作
|
|
|
*
|
|
|
- * 1. 蓝牙扫描任务常开
|
|
|
- * 2. 循环连接蓝牙设备,读取设备任务完成情况
|
|
|
+ * 1. 蓝牙钥匙(keyLock名称过滤搜索)扫描任务常开
|
|
|
+ * 2. 循环连接钥匙设备,读取设备任务完成情况
|
|
|
+ * 3. 使用中的设备享有优先权,被标记为使用中的设备不进行作业任务的读取操作,等待进行作业任务的下发操作
|
|
|
*/
|
|
|
object BleTask {
|
|
|
|
|
|
@@ -97,7 +98,10 @@ object BleTask {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 标记设备为使用中
|
|
|
+ * 标记设备为使用中,主要用于在上锁或解锁任务时即将使用的设备有优先下发任务的权限
|
|
|
+ *
|
|
|
+ * @param mac 蓝牙钥匙的MAC地址
|
|
|
+ * @param value 下发的作业任务JSON
|
|
|
*/
|
|
|
fun markDeviceUsed(mac: String, value: String) {
|
|
|
usedDevices[mac] = value
|
|
|
@@ -105,15 +109,27 @@ object BleTask {
|
|
|
|
|
|
/**
|
|
|
* 从使用列表中移除
|
|
|
+ *
|
|
|
+ * @param mac 移除使用中的设备,从优先队列中移除
|
|
|
*/
|
|
|
fun removeDeviceUsed(mac: String) {
|
|
|
usedDevices.remove(mac)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加蓝牙钥匙数据读取回调,实时更新当前页面点位隔离状态
|
|
|
+ *
|
|
|
+ * @param listener
|
|
|
+ */
|
|
|
fun addOnTaskChangeListener(listener: OnTaskStatusChangeListener) {
|
|
|
this.listener += listener
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 移除一个蓝牙钥匙的监听回调
|
|
|
+ *
|
|
|
+ * @param listener
|
|
|
+ */
|
|
|
fun removeOnTaskChangeListener(listener: OnTaskStatusChangeListener) {
|
|
|
this.listener.remove(listener)
|
|
|
}
|
|
|
@@ -137,7 +153,7 @@ object BleTask {
|
|
|
private fun loop() {
|
|
|
scope.launch {
|
|
|
for (device in deviceQueue) {
|
|
|
- execTask(device)
|
|
|
+ connectDeviceAndDealData(device)
|
|
|
// 设备处理完成后,从列表中清除
|
|
|
devices.remove(device.address)
|
|
|
// 更新设备处理处理时间
|
|
|
@@ -147,9 +163,11 @@ object BleTask {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 执行设备的操作
|
|
|
+ * 循环连接被发现的设备,从设备中读取作业信息
|
|
|
+ *
|
|
|
+ * @param device 蓝牙设备
|
|
|
*/
|
|
|
- private suspend fun execTask(device: BluetoothDevice) {
|
|
|
+ private suspend fun connectDeviceAndDealData(device: BluetoothDevice) {
|
|
|
if (usedDevices.contains(device.address)) {
|
|
|
if (usedDevices[device.address].isNullOrEmpty()) {
|
|
|
LogUtil.w("BleTask", "[${device.address}] 设备即将被使用,不做状态获取处理")
|
|
|
@@ -208,7 +226,7 @@ object BleTask {
|
|
|
}
|
|
|
}
|
|
|
val disRet = bm.writeByResponse(token.buildBLEDisconnectCMD()).getDisconnectResult()
|
|
|
- LogUtil.i("BleTask", "[${device.address}] 设备断开 -> ${if(disRet == 1) "成功" else "失败"}")
|
|
|
+ LogUtil.i("BleTask", "[${device.address}] 设备断开 -> ${if (disRet == 1) "成功" else "失败"}")
|
|
|
}
|
|
|
bm.disconnect()
|
|
|
LogUtil.d("BleTask", "[${device.address}] 连接断开")
|
|
|
@@ -216,6 +234,8 @@ object BleTask {
|
|
|
|
|
|
/**
|
|
|
* 发送作业票
|
|
|
+ *
|
|
|
+ * @param device 使用中的设备执行作业票数据的下发
|
|
|
*/
|
|
|
private suspend fun sendTicket2Device(device: BluetoothDevice) {
|
|
|
val json = usedDevices[device.address] ?: "{}"
|