|
|
@@ -43,6 +43,8 @@ object ModbusBusinessManager {
|
|
|
|
|
|
private val listeners = ArrayList<DeviceListener>()
|
|
|
|
|
|
+ private var initListener: (() -> Unit)? = null
|
|
|
+
|
|
|
/**
|
|
|
* 注册状态监听
|
|
|
*/
|
|
|
@@ -62,6 +64,20 @@ object ModbusBusinessManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 注册初始化监听
|
|
|
+ */
|
|
|
+ fun registerInitListener(listener: () -> Unit) {
|
|
|
+ this.initListener = listener
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 取消注册初始化监听
|
|
|
+ */
|
|
|
+ fun unRegisterInitListener() {
|
|
|
+ this.initListener = null
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加待更新取出状态的设备
|
|
|
*/
|
|
|
@@ -207,6 +223,7 @@ object ModbusBusinessManager {
|
|
|
|
|
|
// —— 在 Default 线程做计算密集操作 ——
|
|
|
val lockMap = withContext(Dispatchers.Default) {
|
|
|
+ logger.info("获取锁具数量:${needLockCount}")
|
|
|
ModBusController.getLocks(
|
|
|
needLockCount,
|
|
|
slotsPage?.records?.filter {
|
|
|
@@ -287,156 +304,13 @@ object ModbusBusinessManager {
|
|
|
when (dockBean.type) {
|
|
|
DeviceConst.DOCK_TYPE_KEY -> {
|
|
|
dockBean.getKeyList().forEach { keyBean ->
|
|
|
- if (keyBean.isExist) {
|
|
|
- // 放回钥匙,读取rfid
|
|
|
- ModBusController.readKeyRfid(
|
|
|
- dockBean.addr, keyBean.idx
|
|
|
- ) { idx, res ->
|
|
|
- if (ISCSConfig.isInit){
|
|
|
- ModBusController.controlKeyCharge(
|
|
|
- true, keyBean.idx, dockBean.addr
|
|
|
- )
|
|
|
- }
|
|
|
- if (res.size < 11) {
|
|
|
- logger.error("Key rfid error")
|
|
|
- return@readKeyRfid
|
|
|
- }
|
|
|
- val rfid =
|
|
|
- res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
- ModBusController.updateKeyRfid(
|
|
|
- dockBean.addr, keyBean.idx, rfid
|
|
|
- )
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- val slotStatus =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_STATUS) }
|
|
|
- val slotType =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_TYPE) }
|
|
|
- val slotsPageReq = async { DataBusiness.getSlotsPage() }
|
|
|
- val keyStatusReq =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_KEY_STATUS) }
|
|
|
- val keyPageReq = async { DataBusiness.getKeyPage() }
|
|
|
- var keyStatus = keyStatusReq.await()
|
|
|
- var keyData = keyPageReq.await()
|
|
|
- val slotsPage = slotsPageReq.await()
|
|
|
- val slotStatusList = slotStatus.await()
|
|
|
- val slotTypeList = slotType.await()
|
|
|
- //锁钥匙未异常正常请求锁数据,关锁
|
|
|
- if (rfid in (keyData?.records?.filter { it.exStatus == keyStatus.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.map { it.keyNfc }?.toMutableList()
|
|
|
- ?: mutableListOf())
|
|
|
- ) {
|
|
|
- PopTip.tip(
|
|
|
- CommonUtils.getStr(R.string.key_exception_tag)
|
|
|
- )
|
|
|
- } else if (slotsPage?.records?.filter {
|
|
|
- it.slotType == slotTypeList.find { d -> d.dictLabel == "钥匙" }?.dictValue && it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
- }
|
|
|
- ?.find { it.row?.toInt() == dockBean.row && it.col?.toInt() == (dockBean.col + (keyBean.idx) * 2) } != null) {
|
|
|
- PopTip.tip(
|
|
|
- CommonUtils.getStr(R.string.slot_exception_tag)
|
|
|
- )
|
|
|
- } else {
|
|
|
- // 放回钥匙,上锁
|
|
|
- ModBusController.controlKeyBuckle(
|
|
|
- false, keyBean.idx, dockBean.addr
|
|
|
- ) {
|
|
|
- RepositoryManager.hardwareRepo.getKeyInfo(rfid) {
|
|
|
- if (it != null && !it.macAddress.isNullOrEmpty()) {
|
|
|
- ModBusController.updateKeyMac(
|
|
|
- dockBean.addr,
|
|
|
- keyBean.idx,
|
|
|
- it.macAddress ?: ""
|
|
|
- )
|
|
|
- ModBusController.updateKeyReadyStatus(
|
|
|
- it.macAddress ?: "", false, 5
|
|
|
- )
|
|
|
- } else {
|
|
|
- logger.error("Get key info fail : $rfid")
|
|
|
- PopTip.tip(R.string.get_key_info_fail)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else if (!keyBean.isCharging) {//增加充电判断,防止无线充电干扰锁仓状态导致判断为取出
|
|
|
- // 移出待连监听集合,防止connectKey循环失败
|
|
|
- keyBean.mac?.let {
|
|
|
- BleConnectionManager.unregisterConnectListener(it)
|
|
|
- }
|
|
|
- DeviceTakeUpdateEvent.sendDeviceTakeEvent(
|
|
|
- DeviceConst.DEVICE_TYPE_KEY,
|
|
|
- keyBean.rfid
|
|
|
- )
|
|
|
- }
|
|
|
+ deviceKeyHandler(dockBean, keyBean)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
DeviceConst.DOCK_TYPE_LOCK -> {
|
|
|
dockBean.getLockList().forEach { lockBean ->
|
|
|
- if (lockBean.isExist) {
|
|
|
- ModBusController.readLockRfid(dockBean.addr, lockBean.idx) { res ->
|
|
|
- if (res.size < 11) {
|
|
|
- logger.error("Lock rfid error")
|
|
|
- return@readLockRfid
|
|
|
- }
|
|
|
- val rfid =
|
|
|
- res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
- ModBusController.updateLockRfid(
|
|
|
- dockBean.addr, lockBean.idx, rfid
|
|
|
- )
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- val lockStatusReq =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_PAD_LOCK_STATUS) }
|
|
|
- val slotStatus =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_STATUS) }
|
|
|
- val slotType =
|
|
|
- async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_TYPE) }
|
|
|
- val slotsPageReq = async { DataBusiness.getSlotsPage() }
|
|
|
- var lockStatus = lockStatusReq.await()
|
|
|
- val slotsPage = slotsPageReq.await()
|
|
|
- val slotStatusList = slotStatus.await()
|
|
|
- val slotTypeList = slotType.await()
|
|
|
- RepositoryManager.hardwareRepo.getIsLockPage { lockData ->
|
|
|
- //锁rfid未异常正常请求锁数据,关锁
|
|
|
- if (rfid in (lockData?.records?.filter { it.exStatus == lockStatus.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.map { it.lockNfc }?.toMutableList()
|
|
|
- ?: mutableListOf())
|
|
|
- ) {
|
|
|
- PopTip.tip(R.string.lock_exception_tag)
|
|
|
- } else if (slotsPage?.records?.filter {
|
|
|
- it.slotType == slotTypeList.find { d -> d.dictLabel == "锁" }?.dictValue && it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
- }
|
|
|
- ?.find { it.row?.toInt() == dockBean.row && (lockBean.idx + 1) == it.col?.toInt() } != null) {
|
|
|
- PopTip.tip(R.string.slot_exception_tag)
|
|
|
- } else {
|
|
|
- RepositoryManager.hardwareRepo.getLockInfo(rfid) {
|
|
|
- if (it != null) {
|
|
|
- // TODO 考虑快速拿取
|
|
|
- ModBusController.controlLockBuckle(
|
|
|
- false, dockBean.addr, lockBean.idx
|
|
|
- ) { itRst ->
|
|
|
- if (itRst.isNotEmpty()) {
|
|
|
- // 上报锁具信息
|
|
|
- RepositoryManager.jobTicketRepo.updateLockReturn(
|
|
|
- rfid,
|
|
|
- SIKCore.getApplication().serialNo()
|
|
|
- ) {}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- logger.info("挂锁取出-:${lockBean.rfid}")
|
|
|
- DeviceTakeUpdateEvent.sendDeviceTakeEvent(
|
|
|
- DeviceConst.DEVICE_TYPE_LOCK,
|
|
|
- lockBean.rfid
|
|
|
- )
|
|
|
- }
|
|
|
+ deviceLockHandler(dockBean, lockBean)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -450,89 +324,11 @@ object ModbusBusinessManager {
|
|
|
if (deviceBean.isExist) {
|
|
|
when (deviceBean.type) {
|
|
|
DeviceConst.DEVICE_TYPE_KEY -> {
|
|
|
- ModBusController.readKeyRfid(
|
|
|
- dockBean.addr, deviceBean.idx
|
|
|
- ) { idx, res ->
|
|
|
- if (res.size < 11) {
|
|
|
- logger.error("Key rfid error")
|
|
|
- return@readKeyRfid
|
|
|
- }
|
|
|
- val rfid = res.copyOfRange(3, 11).toHexStrings(false)
|
|
|
- .removeLeadingZeros()
|
|
|
- ModBusController.updateKeyRfid(
|
|
|
- dockBean.addr, deviceBean.idx, rfid
|
|
|
- )
|
|
|
- RepositoryManager.hardwareRepo.getKeyInfo(rfid) {
|
|
|
- ModBusController.updateKeyNewHardware(
|
|
|
- dockBean.addr, deviceBean.idx, it == null
|
|
|
- )
|
|
|
- if (it != null && !it.macAddress.isNullOrEmpty()) {
|
|
|
- ModBusController.updateKeyMac(
|
|
|
- dockBean.addr,
|
|
|
- deviceBean.idx,
|
|
|
- it.macAddress ?: ""
|
|
|
- )
|
|
|
-// showKeyReturnDialog(it.macAddress, isLeft, dockBean.addr)
|
|
|
- } else {
|
|
|
- PopTip.tip(R.string.get_key_info_fail)
|
|
|
- }
|
|
|
- }
|
|
|
- // TODO 蓝牙通信
|
|
|
- }
|
|
|
+ deviceKeyHandler(dockBean, deviceBean as DockBean.KeyBean)
|
|
|
}
|
|
|
|
|
|
DeviceConst.DEVICE_TYPE_LOCK -> {
|
|
|
- ModBusController.readLockRfid(
|
|
|
- dockBean.addr, deviceBean.idx
|
|
|
- ) { res ->
|
|
|
- if (res.size < 11) {
|
|
|
- logger.error("Lock rfid error")
|
|
|
- return@readLockRfid
|
|
|
- }
|
|
|
- val rfid = res.copyOfRange(3, 11).toHexStrings(false)
|
|
|
- .removeLeadingZeros()
|
|
|
- ModBusController.updateLockRfid(
|
|
|
- dockBean.addr, deviceBean.idx, rfid
|
|
|
- )
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- val lockStatusReq = async {
|
|
|
- DataBusiness.fetchDict(
|
|
|
- DictConstants.KEY_PAD_LOCK_STATUS
|
|
|
- )
|
|
|
- }
|
|
|
- var lockStatus = lockStatusReq.await()
|
|
|
- RepositoryManager.hardwareRepo.getIsLockPage { lockData ->
|
|
|
- //锁rfid未异常正常请求锁数据,关锁
|
|
|
- if (rfid !in (lockData?.records?.filter { it.exStatus == lockStatus.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
- ?.map { it.lockNfc }?.toMutableList()
|
|
|
- ?: mutableListOf())
|
|
|
- ) {
|
|
|
- RepositoryManager.hardwareRepo.getLockInfo(rfid) {
|
|
|
- ModBusController.updateLockNewHardware(
|
|
|
- dockBean.addr,
|
|
|
- deviceBean.idx,
|
|
|
- it == null
|
|
|
- )
|
|
|
- if (it != null) {
|
|
|
- // TODO 考虑快速拿取
|
|
|
- ModBusController.controlLockBuckle(
|
|
|
- false, dockBean.addr, deviceBean.idx
|
|
|
- ) { itRst ->
|
|
|
- if (itRst.isNotEmpty()) {
|
|
|
- // 上报锁具信息
|
|
|
- RepositoryManager.jobTicketRepo.updateLockReturn(
|
|
|
- rfid,
|
|
|
- SIKCore.getApplication()
|
|
|
- .serialNo()
|
|
|
- ) {}
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ deviceLockHandler(dockBean, deviceBean as DockBean.LockBean)
|
|
|
}
|
|
|
|
|
|
DeviceConst.DEVICE_TYPE_CARD -> {
|
|
|
@@ -559,6 +355,174 @@ object ModbusBusinessManager {
|
|
|
listeners.forEach { it.callBack(dockBean) }
|
|
|
}
|
|
|
}
|
|
|
+ Executor.delayOnMain(200) {
|
|
|
+ if (!ISCSConfig.isInit) {
|
|
|
+ initListener?.invoke()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设备挂锁处理
|
|
|
+ */
|
|
|
+ private fun deviceLockHandler(
|
|
|
+ dockBean: DockBean,
|
|
|
+ lockBean: DockBean.LockBean
|
|
|
+ ) {
|
|
|
+ if (lockBean.isExist) {
|
|
|
+ ModBusController.readLockRfid(dockBean.addr, lockBean.idx) { res ->
|
|
|
+ if (res.size < 11) {
|
|
|
+ logger.error("Lock rfid error")
|
|
|
+ return@readLockRfid
|
|
|
+ }
|
|
|
+ val rfid =
|
|
|
+ res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ ModBusController.updateLockRfid(
|
|
|
+ dockBean.addr, lockBean.idx, rfid
|
|
|
+ )
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ val lockStatusReq =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_PAD_LOCK_STATUS) }
|
|
|
+ val slotStatus =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_STATUS) }
|
|
|
+ val slotType =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_TYPE) }
|
|
|
+ val slotsPageReq = async { DataBusiness.getSlotsPage() }
|
|
|
+ var lockStatus = lockStatusReq.await()
|
|
|
+ val slotsPage = slotsPageReq.await()
|
|
|
+ val slotStatusList = slotStatus.await()
|
|
|
+ val slotTypeList = slotType.await()
|
|
|
+ RepositoryManager.hardwareRepo.getIsLockPage { lockData ->
|
|
|
+ //锁rfid未异常正常请求锁数据,关锁
|
|
|
+ if (rfid in (lockData?.records?.filter { it.exStatus == lockStatus.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
+ ?.map { it.lockNfc }?.toMutableList()
|
|
|
+ ?: mutableListOf())
|
|
|
+ ) {
|
|
|
+ PopTip.tip(R.string.lock_exception_tag)
|
|
|
+ } else if (slotsPage?.records?.filter {
|
|
|
+ it.slotType == slotTypeList.find { d -> d.dictLabel == "锁" }?.dictValue && it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
+ }
|
|
|
+ ?.find { it.row?.toInt() == dockBean.row && (lockBean.idx + 1) == it.col?.toInt() } != null) {
|
|
|
+ PopTip.tip(R.string.slot_exception_tag)
|
|
|
+ } else {
|
|
|
+ RepositoryManager.hardwareRepo.getLockInfo(rfid) {
|
|
|
+ if (it != null && it?.lockNfc?.isNotEmpty() == true) {
|
|
|
+ // TODO 考虑快速拿取
|
|
|
+ ModBusController.controlLockBuckle(
|
|
|
+ false, dockBean.addr, lockBean.idx
|
|
|
+ ) { itRst ->
|
|
|
+ if (itRst.isNotEmpty()) {
|
|
|
+ // 上报锁具信息
|
|
|
+ RepositoryManager.jobTicketRepo.updateLockReturn(
|
|
|
+ rfid,
|
|
|
+ SIKCore.getApplication().serialNo()
|
|
|
+ ) {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.info("挂锁取出-:${lockBean.rfid}")
|
|
|
+ handleDeviceTake(DeviceTakeUpdateEvent(DeviceConst.DEVICE_TYPE_LOCK, lockBean.rfid),lockBean.rfid)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钥匙设备处理
|
|
|
+ */
|
|
|
+ private fun deviceKeyHandler(
|
|
|
+ dockBean: DockBean,
|
|
|
+ keyBean: DockBean.KeyBean
|
|
|
+ ) {
|
|
|
+ if (keyBean.isExist) {
|
|
|
+ // 放回钥匙,读取rfid
|
|
|
+ ModBusController.readKeyRfid(
|
|
|
+ dockBean.addr, keyBean.idx
|
|
|
+ ) { idx, res ->
|
|
|
+ if (ISCSConfig.isInit) {
|
|
|
+ ModBusController.controlKeyCharge(
|
|
|
+ true, keyBean.idx, dockBean.addr
|
|
|
+ )
|
|
|
+ }
|
|
|
+ if (res.size < 11) {
|
|
|
+ logger.error("Key rfid error")
|
|
|
+ return@readKeyRfid
|
|
|
+ }
|
|
|
+ val rfid =
|
|
|
+ res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
+ ModBusController.updateKeyRfid(
|
|
|
+ dockBean.addr, keyBean.idx, rfid
|
|
|
+ )
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ val slotStatus =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_STATUS) }
|
|
|
+ val slotType =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_SLOT_TYPE) }
|
|
|
+ val slotsPageReq = async { DataBusiness.getSlotsPage() }
|
|
|
+ val keyStatusReq =
|
|
|
+ async { DataBusiness.fetchDict(DictConstants.KEY_KEY_STATUS) }
|
|
|
+ val keyPageReq = async { DataBusiness.getKeyPage() }
|
|
|
+ var keyStatus = keyStatusReq.await()
|
|
|
+ var keyData = keyPageReq.await()
|
|
|
+ val slotsPage = slotsPageReq.await()
|
|
|
+ val slotStatusList = slotStatus.await()
|
|
|
+ val slotTypeList = slotType.await()
|
|
|
+ //锁钥匙未异常正常请求锁数据,关锁
|
|
|
+ if (rfid in (keyData?.records?.filter { it.exStatus == keyStatus.find { it.dictLabel == "异常" }?.dictValue }
|
|
|
+ ?.map { it.keyNfc }?.toMutableList()
|
|
|
+ ?: mutableListOf())
|
|
|
+ ) {
|
|
|
+ PopTip.tip(
|
|
|
+ CommonUtils.getStr(R.string.key_exception_tag)
|
|
|
+ )
|
|
|
+ } else if (slotsPage?.records?.filter {
|
|
|
+ it.slotType == slotTypeList.find { d -> d.dictLabel == "钥匙" }?.dictValue && it.status == slotStatusList.find { d -> d.dictLabel == "异常" }?.dictValue
|
|
|
+ }
|
|
|
+ ?.find { it.row?.toInt() == dockBean.row && it.col?.toInt() == (dockBean.col + (keyBean.idx) * 2) } != null) {
|
|
|
+ PopTip.tip(
|
|
|
+ CommonUtils.getStr(R.string.slot_exception_tag)
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ // 放回钥匙,上锁
|
|
|
+ ModBusController.controlKeyBuckle(
|
|
|
+ false, keyBean.idx, dockBean.addr
|
|
|
+ ) {
|
|
|
+ RepositoryManager.hardwareRepo.getKeyInfo(rfid) {
|
|
|
+ logger.info("钥匙:${rfid},${it}")
|
|
|
+ if (it != null && !it.macAddress.isNullOrEmpty()) {
|
|
|
+ ModBusController.updateKeyMac(
|
|
|
+ dockBean.addr,
|
|
|
+ keyBean.idx,
|
|
|
+ it.macAddress ?: ""
|
|
|
+ )
|
|
|
+ ModBusController.updateKeyReadyStatus(
|
|
|
+ it.macAddress ?: "", false, 5
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ logger.error("Get key info fail : $rfid")
|
|
|
+ if (!ISCSConfig.isInit) {
|
|
|
+ PopTip.tip(R.string.get_key_info_fail)
|
|
|
+ }
|
|
|
+ ModBusController.controlKeyBuckle(
|
|
|
+ true, keyBean.idx, dockBean.addr
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (!keyBean.isCharging) {//增加充电判断,防止无线充电干扰锁仓状态导致判断为取出
|
|
|
+ // 移出待连监听集合,防止connectKey循环失败
|
|
|
+ keyBean.mac?.let {
|
|
|
+ BleConnectionManager.unregisterConnectListener(it)
|
|
|
+ }
|
|
|
+ handleDeviceTake(DeviceTakeUpdateEvent(DeviceConst.DEVICE_TYPE_KEY, keyBean.rfid),keyBean.rfid)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -569,8 +533,4 @@ object ModbusBusinessManager {
|
|
|
deviceStatusHandle(res)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- fun unregisterMainListener() {
|
|
|
- ModBusController.unregisterListener(this)
|
|
|
- }
|
|
|
}
|