|
|
@@ -5,9 +5,9 @@ import android.content.Context
|
|
|
import com.grkj.iscs_mars.BusinessManager
|
|
|
import com.grkj.iscs_mars.BusinessManager.CAN_RETURN
|
|
|
import com.grkj.iscs_mars.R
|
|
|
-import com.grkj.iscs_mars.ble.BleConnectionManager
|
|
|
import com.grkj.iscs_mars.ble.BleReturnDispatcher
|
|
|
import com.grkj.iscs_mars.ble.BleSendDispatcher
|
|
|
+import com.grkj.iscs_mars.enums.NoKeyReason
|
|
|
import com.grkj.iscs_mars.extentions.removeLeadingZeros
|
|
|
import com.grkj.iscs_mars.extentions.toHexStrings
|
|
|
import com.grkj.iscs_mars.model.DeviceConst
|
|
|
@@ -20,6 +20,7 @@ import com.grkj.iscs_mars.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
|
|
|
import com.grkj.iscs_mars.model.DeviceConst.DOCK_TYPE_KEY
|
|
|
import com.grkj.iscs_mars.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
import com.grkj.iscs_mars.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
|
+import com.grkj.iscs_mars.model.ISCSDomainData
|
|
|
import com.grkj.iscs_mars.model.eventmsg.MsgEvent
|
|
|
import com.grkj.iscs_mars.model.eventmsg.MsgEventConstants
|
|
|
import com.grkj.iscs_mars.model.vo.hardware.CabinetSlotsRecord
|
|
|
@@ -29,8 +30,6 @@ import com.grkj.iscs_mars.util.NetApi
|
|
|
import com.grkj.iscs_mars.util.ToastUtils
|
|
|
import com.grkj.iscs_mars.util.log.LogUtil
|
|
|
import com.huyuhui.fastble.BleManager
|
|
|
-import com.sik.sikcore.thread.ThreadUtils
|
|
|
-import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.isActive
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
import java.util.stream.Collectors
|
|
|
@@ -1045,6 +1044,7 @@ object ModBusController {
|
|
|
exceptionKeysRfid: List<String>,
|
|
|
exceptionKeysMac: List<String> = mutableListOf(),
|
|
|
): Pair<Byte, DockBean.KeyBean?>? {
|
|
|
+ ISCSDomainData.noKeyReason = NoKeyReason.NONE
|
|
|
// 1. 过滤并准备钥匙列表
|
|
|
val slotCols = exceptionSlots.mapNotNull { it.col?.toInt() }
|
|
|
val keyDockList =
|
|
|
@@ -1068,10 +1068,11 @@ object ModBusController {
|
|
|
!kb.rfid.isNullOrEmpty() && kb.rfid !in exceptionKeysRfid && kb.mac !in exceptionKeysMac && !kb.mac.isNullOrEmpty() && kb.isExist && !BleReturnDispatcher.isConnected(
|
|
|
kb.mac ?: ""
|
|
|
) && !BleReturnDispatcher.isConnecting(kb.mac ?: "")
|
|
|
- }.shuffled()
|
|
|
+ }
|
|
|
|
|
|
LogUtil.i("蓝牙连接-获取到钥匙信息:${keyList}")
|
|
|
if (keyList.isEmpty()) {
|
|
|
+ ISCSDomainData.noKeyReason = NoKeyReason.NO_KEY
|
|
|
return null
|
|
|
}
|
|
|
val sendConnectingAndConnected = BleSendDispatcher.getConnectedAndConnecting()
|
|
|
@@ -1087,57 +1088,78 @@ object ModBusController {
|
|
|
} // 主键:在线优先
|
|
|
.thenByDescending { it.power }
|
|
|
)
|
|
|
+ val powerLowerLimit = ISCSDomainData.powerMin
|
|
|
val connectedKey = keyList.find { BleSendDispatcher.isConnected(it.mac ?: "") }
|
|
|
if (connectedKey != null) {
|
|
|
- val addr =
|
|
|
- keyDockList.firstOrNull {
|
|
|
- it.getKeyList().any { it.rfid == connectedKey.rfid }
|
|
|
- }?.addr
|
|
|
- if (addr != null) {
|
|
|
- return addr to connectedKey
|
|
|
+ if (connectedKey.power < powerLowerLimit) {
|
|
|
+ connectedKey.mac?.let {
|
|
|
+ BleSendDispatcher.scheduleDisconnect(it)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ val addr =
|
|
|
+ keyDockList.firstOrNull {
|
|
|
+ it.getKeyList().any { it.rfid == connectedKey.rfid }
|
|
|
+ }?.addr
|
|
|
+ if (addr != null) {
|
|
|
+ return addr to connectedKey
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
val connectingKey = keyList.find { BleSendDispatcher.isConnecting(it.mac ?: "") }
|
|
|
if (connectingKey != null) {
|
|
|
- val result = suspendCoroutine { cont ->
|
|
|
- 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)
|
|
|
+ if (connectingKey.power < powerLowerLimit) {
|
|
|
+ connectingKey.mac?.let {
|
|
|
+ BleSendDispatcher.scheduleDisconnect(it)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ val result = suspendCoroutine { cont ->
|
|
|
+ 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)
|
|
|
+ } else {
|
|
|
+ cont.resume(null)
|
|
|
+ }
|
|
|
} else {
|
|
|
cont.resume(null)
|
|
|
}
|
|
|
- } else {
|
|
|
- cont.resume(null)
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- if (result != null) {
|
|
|
- return result
|
|
|
+ if (result != null) {
|
|
|
+ return result
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- for (kb in keyList) {
|
|
|
+ for (kb in keyList.filter { it.mac != connectedKey?.mac && it.mac != connectingKey?.mac }) {
|
|
|
val mac = kb.mac ?: continue
|
|
|
val result = suspendCoroutine { cont ->
|
|
|
BleSendDispatcher.submit(mac) { connected ->
|
|
|
if (connected) {
|
|
|
LogUtil.i("蓝牙连接完成 :${mac}")
|
|
|
- // 找到第一个能连的:从 keyDockList 里拿同 rfid 的 addr
|
|
|
- val addr =
|
|
|
- keyDockList.firstOrNull {
|
|
|
- it.getKeyList().any { it.rfid == kb.rfid }
|
|
|
- }?.addr
|
|
|
- if (addr != null) {
|
|
|
+ if ((getKeyByMac(mac)?.power ?: 0) < powerLowerLimit) {
|
|
|
+ LogUtil.i("电量小于阈值,断开继续下一把 :${mac}")
|
|
|
+ BleSendDispatcher.scheduleDisconnect(mac)
|
|
|
if (cont.context.isActive) {
|
|
|
- cont.resume(addr to kb)
|
|
|
+ cont.resume(null)
|
|
|
}
|
|
|
} else {
|
|
|
- if (cont.context.isActive) {
|
|
|
- cont.resume(null)
|
|
|
+ // 找到第一个能连的:从 keyDockList 里拿同 rfid 的 addr
|
|
|
+ val addr =
|
|
|
+ keyDockList.firstOrNull {
|
|
|
+ it.getKeyList().any { it.rfid == kb.rfid }
|
|
|
+ }?.addr
|
|
|
+ if (addr != null) {
|
|
|
+ if (cont.context.isActive) {
|
|
|
+ cont.resume(addr to kb)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (cont.context.isActive) {
|
|
|
+ cont.resume(null)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
@@ -1152,6 +1174,7 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ ISCSDomainData.noKeyReason = NoKeyReason.KEY_POWER_LOW
|
|
|
// 一个都没成功
|
|
|
LogUtil.e("getOneKey : no key match")
|
|
|
return null
|