|
|
@@ -56,7 +56,11 @@ import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_SWITCH_MODE
|
|
|
import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_UPDATE_TICKET_PROGRESS
|
|
|
import com.grkj.iscs.model.eventmsg.SwitchModeMsg
|
|
|
import com.grkj.iscs.model.eventmsg.UpdateTicketProgressMsg
|
|
|
+import com.grkj.iscs.model.vo.dict.CommonDictRespVO
|
|
|
+import com.grkj.iscs.model.vo.hardware.CabinetSlotsRespVo
|
|
|
import com.grkj.iscs.model.vo.hardware.SwitchListReqVO
|
|
|
+import com.grkj.iscs.model.vo.key.KeyPageRespVO
|
|
|
+import com.grkj.iscs.model.vo.lock.LockPageRespVO
|
|
|
import com.grkj.iscs.model.vo.lock.LockTakeUpdateReqVO
|
|
|
import com.grkj.iscs.model.vo.ticket.LockPointUpdateReqVO
|
|
|
import com.grkj.iscs.model.vo.ticket.TicketDetailRespVO
|
|
|
@@ -74,9 +78,12 @@ import com.sik.sikcore.activity.ActivityTracker
|
|
|
import com.sik.sikcore.date.TimeUtils
|
|
|
import com.sik.sikcore.thread.ThreadUtils
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.async
|
|
|
import kotlinx.coroutines.delay
|
|
|
+import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import kotlinx.coroutines.withContext
|
|
|
import pub.devrel.easypermissions.AfterPermissionGranted
|
|
|
+import kotlin.coroutines.resume
|
|
|
|
|
|
/**
|
|
|
* 业务层管理
|
|
|
@@ -200,6 +207,8 @@ object BusinessManager {
|
|
|
// 待机模式
|
|
|
2 -> {
|
|
|
if (it.data.res == 1) {
|
|
|
+ // 只能在这里断开,不能全部断开
|
|
|
+ BleManager.getInstance().disconnect(it.data.bleBean.bleDevice)
|
|
|
ModBusController.updateKeyReadyStatus(
|
|
|
it.data.bleBean.bleDevice.mac,
|
|
|
true,
|
|
|
@@ -608,79 +617,121 @@ object BusinessManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 检查钥匙和锁具数量
|
|
|
- *
|
|
|
- * @param needLockCount 需要的锁具的数量(可能在别的机柜取过)
|
|
|
- * @param isNeedKey 是否需要钥匙(可能存在前一次拿了部分锁和钥匙,有锁取出失败)
|
|
|
- */
|
|
|
+ // 1. 把 NetApi.get…Page 包成 suspend 函数
|
|
|
+ private suspend fun getSlotsPage(): CabinetSlotsRespVo? = suspendCancellableCoroutine { cont ->
|
|
|
+ NetApi.getIsLockCabinetSlotsPage { slots ->
|
|
|
+ cont.resume(slots)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private suspend fun getLocksPage(): LockPageRespVO? = suspendCancellableCoroutine { cont ->
|
|
|
+ NetApi.getIsLockPage { locks ->
|
|
|
+ cont.resume(locks)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private suspend fun getKeyPage(): KeyPageRespVO? = suspendCancellableCoroutine { cont ->
|
|
|
+ NetApi.getIsKeyPage { keys ->
|
|
|
+ cont.resume(keys)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 把原本同步的字典查询留在 IO 线程
|
|
|
+ private suspend fun <T> fetchDict(key: String): List<T> =
|
|
|
+ withContext(Dispatchers.IO) {
|
|
|
+ @Suppress("UNCHECKED_CAST")
|
|
|
+ NetApi.getDictData(key) as List<T>
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 重写 checkEquipCount
|
|
|
fun checkEquipCount(
|
|
|
needLockCount: Int,
|
|
|
isNeedKey: Boolean,
|
|
|
callBack: (Pair<Byte, DockBean.KeyBean?>?, MutableMap<Byte, MutableList<DockBean.LockBean>>) -> Unit
|
|
|
) {
|
|
|
- var lockCount = 0
|
|
|
- NetApi.getIsLockCabinetSlotsPage { slots ->
|
|
|
- NetApi.getIsLockPage { locks ->
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- val lockStatus = NetApi.getDictData(DictConstants.KEY_PAD_LOCK_STATUS)
|
|
|
- val slotsStatus = NetApi.getDictData(DictConstants.KEY_SLOT_STATUS)
|
|
|
- val keyStatus = NetApi.getDictData(DictConstants.KEY_KEY_STATUS)
|
|
|
- val slotsType = NetApi.getDictData(DictConstants.KEY_SLOT_TYPE)
|
|
|
- val lockMap = ModBusController.getLocks(
|
|
|
+ // 你可以改成接收 CoroutineScope 或者直接在全局 Scope 启动
|
|
|
+ ThreadUtils.runOnMain {
|
|
|
+ try {
|
|
|
+ // —— 串行请求1 & 2 ——
|
|
|
+ val slotsPage = getSlotsPage()
|
|
|
+ val locksPage = getLocksPage()
|
|
|
+
|
|
|
+ // —— 并行加载字典(或按需串行也行) ——
|
|
|
+ val lockStatus =
|
|
|
+ async { fetchDict<CommonDictRespVO>(DictConstants.KEY_PAD_LOCK_STATUS) }
|
|
|
+ val slotStatus =
|
|
|
+ async { fetchDict<CommonDictRespVO>(DictConstants.KEY_SLOT_STATUS) }
|
|
|
+ val keyStatus = async { fetchDict<CommonDictRespVO>(DictConstants.KEY_KEY_STATUS) }
|
|
|
+ val slotType = async { fetchDict<CommonDictRespVO>(DictConstants.KEY_SLOT_TYPE) }
|
|
|
+
|
|
|
+ // 等待字典加载完成
|
|
|
+ val lockStatusList = lockStatus.await()
|
|
|
+ val slotStatusList = slotStatus.await()
|
|
|
+ val keyStatusList = keyStatus.await()
|
|
|
+ val slotTypeList = slotType.await()
|
|
|
+
|
|
|
+ // —— 在 Default 线程做计算密集操作 ——
|
|
|
+ val lockMap = withContext(Dispatchers.Default) {
|
|
|
+ ModBusController.getLocks(
|
|
|
needLockCount,
|
|
|
- slots?.records?.filter { it.slotType == slotsType?.find { it.dictLabel == "锁" }?.dictValue && it.status == slotsStatus?.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.toMutableList() ?: mutableListOf(),
|
|
|
- locks?.records?.filter { it.exStatus == lockStatus?.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.map { it.lockNfc }?.toMutableList() ?: mutableListOf()
|
|
|
+ slotsPage?.records
|
|
|
+ ?.filter {
|
|
|
+ it.slotType == slotTypeList.find { d -> d.dictLabel == "锁" }?.dictValue &&
|
|
|
+ it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
+ }?.toMutableList() ?: mutableListOf(),
|
|
|
+ locksPage?.records
|
|
|
+ ?.filter { it.exStatus == lockStatusList.find { d -> d.dictLabel == "异常" }?.dictValue }
|
|
|
+ ?.map { it.lockNfc }
|
|
|
+ ?.toMutableList() ?: mutableListOf()
|
|
|
)
|
|
|
- lockMap.forEach { (_, rfidList) ->
|
|
|
- lockCount += rfidList.size
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- var tipStr = ""
|
|
|
- if (lockCount < needLockCount) {
|
|
|
- val msg =
|
|
|
- MyApplication.instance!!.applicationContext.resources.getString(R.string.lock_is_not_enough)
|
|
|
- LogUtil.w(msg)
|
|
|
- lockMap.clear()
|
|
|
- }
|
|
|
+ val actualLockCount = lockMap.values.sumBy { it.size }
|
|
|
+ // 如果锁不够,提前清空并立刻返回
|
|
|
+ if (actualLockCount < needLockCount) {
|
|
|
+ ToastUtils.tip(
|
|
|
+ MyApplication.instance!!.getString(R.string.lock_is_not_enough)
|
|
|
+ )
|
|
|
+ callBack(null, mutableMapOf())
|
|
|
+ return@runOnMain
|
|
|
+ }
|
|
|
|
|
|
- var key: Pair<Byte, DockBean.KeyBean?>? = null
|
|
|
- if (isNeedKey && lockCount >= needLockCount) {
|
|
|
- NetApi.getIsKeyPage { keyList ->
|
|
|
- key =
|
|
|
- ModBusController.getOneKey(slots?.records?.filter { it.slotType == slotsType?.find { it.dictLabel == "钥匙" }?.dictValue && it.status == slotsStatus?.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.toMutableList() ?: mutableListOf(),
|
|
|
- keyList?.records?.filter { it.exStatus == keyStatus?.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.map { it.keyNfc }?.toMutableList() ?: mutableListOf())
|
|
|
- if (key == null) {
|
|
|
- val msg =
|
|
|
- MyApplication.instance!!.applicationContext.resources.getString(
|
|
|
- R.string.no_available_key
|
|
|
- )
|
|
|
- LogUtil.w(msg)
|
|
|
- tipStr = if (tipStr.isEmpty()) {
|
|
|
- msg
|
|
|
- } else {
|
|
|
- tipStr + "\n" + msg
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ // —— 如果需钥匙,再请求并计算 ——
|
|
|
+ var keyPair: Pair<Byte, DockBean.KeyBean?>? = null
|
|
|
+ if (isNeedKey) {
|
|
|
+ val keyPage = withContext(Dispatchers.IO) { getKeyPage() }
|
|
|
+ keyPair = withContext(Dispatchers.Default) {
|
|
|
+ ModBusController.getOneKey(
|
|
|
+ slotsPage?.records
|
|
|
+ ?.filter {
|
|
|
+ it.slotType == slotTypeList.find { d -> d.dictLabel == "钥匙" }?.dictValue &&
|
|
|
+ it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
+ }?.toMutableList() ?: mutableListOf(),
|
|
|
+ keyPage?.records
|
|
|
+ ?.filter { it.exStatus == keyStatusList.find { d -> d.dictLabel == "异常" }?.dictValue }
|
|
|
+ ?.map { it.keyNfc }
|
|
|
+ ?.toMutableList() ?: mutableListOf()
|
|
|
+ )
|
|
|
}
|
|
|
-
|
|
|
- withContext(Dispatchers.Main) {
|
|
|
- if (tipStr.isNotEmpty()) {
|
|
|
- ToastUtils.tip(tipStr)
|
|
|
- }
|
|
|
- LogUtil.i("checkEquipCount : key = $key, lockMap = $lockMap")
|
|
|
- callBack.invoke(key, lockMap)
|
|
|
+ if (keyPair == null) {
|
|
|
+ ToastUtils.tip(
|
|
|
+ MyApplication.instance!!.getString(R.string.no_available_key)
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // —— 全部计算完毕,在主线程一次性回调 ——
|
|
|
+ callBack(keyPair, lockMap)
|
|
|
+
|
|
|
+ } catch (e: Exception) {
|
|
|
+ // 根据需求处理异常,或把异常信息也通过 callback 返回
|
|
|
+ e.printStackTrace()
|
|
|
+ ToastUtils.tip("检查设备异常:${e.message}")
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 获取开关量数据
|
|
|
*/
|
|
|
@@ -754,7 +805,7 @@ object BusinessManager {
|
|
|
if (connectListeners.isEmpty()) {
|
|
|
return
|
|
|
}
|
|
|
- if (isPreparing) {
|
|
|
+ if (isPreparing || BleManager.getInstance().allConnectedDevice.size >= BleConst.MAX_CONNECTED_DEVICE_SIZE) {
|
|
|
Executor.delayOnMain(1000) {
|
|
|
connectKey()
|
|
|
}
|
|
|
@@ -1915,6 +1966,8 @@ object BusinessManager {
|
|
|
false, currentModeMsg.bleBean.bleDevice.mac
|
|
|
)
|
|
|
sendLoadingEventMsg(null, false)
|
|
|
+ //连上之后没有工作票要下发就断开,不要占位
|
|
|
+ BleManager.getInstance().disconnect(currentModeMsg.bleBean.bleDevice)
|
|
|
}
|
|
|
}
|
|
|
}
|