|
|
@@ -999,7 +999,7 @@ object ModBusController {
|
|
|
var keyList = keyDockList.flatMap { it.deviceList }.apply {
|
|
|
logger.info("keyStatus:${this}")
|
|
|
}.filterIsInstance<DockBean.KeyBean>()
|
|
|
- //RFID不为空,RFID不在异常列表,mac不在异常列表,mac不为空,存在,不在归还连接列表,不在归还连接中列表
|
|
|
+ // RFID不为空,RFID不在异常列表,mac不在异常列表,mac不为空,存在,不在归还连接列表,不在归还连接中列表
|
|
|
.filterIndexed { idx, _ -> (idx + 1) !in slotCols }.filter { kb ->
|
|
|
!kb.rfid.isNullOrEmpty() && kb.rfid !in exceptionKeysRfid && kb.mac !in exceptionKeysMac && !kb.mac.isNullOrEmpty() && kb.isExist && !BleReturnDispatcher.isConnected(
|
|
|
kb.mac ?: ""
|
|
|
@@ -1011,7 +1011,7 @@ object ModBusController {
|
|
|
return null
|
|
|
}
|
|
|
val sendConnectingAndConnected = BleSendDispatcher.getConnectedAndConnecting()
|
|
|
- //如果已连接和连接中的设备不在钥匙列表中,直接断开连接让路
|
|
|
+ // 如果已连接和连接中的设备不在钥匙列表中,直接断开连接让路
|
|
|
sendConnectingAndConnected.filter { it !in keyList.map { it.mac } }.forEach {
|
|
|
BleSendDispatcher.scheduleDisconnect(it)
|
|
|
}
|
|
|
@@ -1035,20 +1035,20 @@ object ModBusController {
|
|
|
}
|
|
|
val connectingKey = keyList.find { BleSendDispatcher.isConnecting(it.mac ?: "") }
|
|
|
if (connectingKey != null) {
|
|
|
- val result = suspendCoroutine { cont ->
|
|
|
+ val result = suspendCoroutine<Pair<Any, String?>?> { cont ->
|
|
|
+ val once = java.util.concurrent.atomic.AtomicBoolean(false)
|
|
|
+ fun resumeOnce(v: Pair<Any, String?>?) {
|
|
|
+ if (once.compareAndSet(false, true)) cont.resume(v)
|
|
|
+ }
|
|
|
BleSendDispatcher.submit(connectingKey.mac ?: "") { connected ->
|
|
|
if (connected) {
|
|
|
val addr =
|
|
|
keyDockList.firstOrNull {
|
|
|
it.getKeyList().any { it.rfid == connectingKey.rfid }
|
|
|
}?.addr
|
|
|
- if (addr != null) {
|
|
|
- cont.resume(addr to connectingKey.rfid)
|
|
|
- } else {
|
|
|
- cont.resume(null)
|
|
|
- }
|
|
|
+ resumeOnce(if (addr != null) addr to connectingKey.rfid else null)
|
|
|
} else {
|
|
|
- cont.resume(null)
|
|
|
+ resumeOnce(null)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -1058,7 +1058,11 @@ object ModBusController {
|
|
|
}
|
|
|
for (kb in keyList) {
|
|
|
val mac = kb.mac ?: continue
|
|
|
- val result = suspendCoroutine { cont ->
|
|
|
+ val result = suspendCoroutine<Pair<Any, String?>?> { cont ->
|
|
|
+ val once = java.util.concurrent.atomic.AtomicBoolean(false)
|
|
|
+ fun resumeOnce(v: Pair<Any, String?>?) {
|
|
|
+ if (once.compareAndSet(false, true)) cont.resume(v)
|
|
|
+ }
|
|
|
BleSendDispatcher.submit(mac) { connected ->
|
|
|
if (connected) {
|
|
|
logger.info("蓝牙连接完成 :${mac}")
|
|
|
@@ -1068,19 +1072,9 @@ object ModBusController {
|
|
|
it.getKeyList().any { it.rfid == kb.rfid }
|
|
|
}?.addr
|
|
|
logger.info("蓝牙连接-找到的底座地址 :${addr}")
|
|
|
- if (addr != null) {
|
|
|
- if (cont.context.isActive) {
|
|
|
- cont.resume(addr to kb.rfid)
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (cont.context.isActive) {
|
|
|
- cont.resume(null)
|
|
|
- }
|
|
|
- }
|
|
|
+ resumeOnce(if (addr != null) addr to kb.rfid else null)
|
|
|
} else {
|
|
|
- if (cont.context.isActive) {
|
|
|
- cont.resume(null)
|
|
|
- }
|
|
|
+ resumeOnce(null)
|
|
|
}
|
|
|
}
|
|
|
}
|