Преглед изворни кода

refactor(更新) :
- 硬件识别

周文健 пре 5 месеци
родитељ
комит
c9ce7d5928

+ 27 - 22
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -1761,19 +1761,22 @@ object BusinessManager {
                 removeDeviceTake(DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc)
                 checkEquipCount(0, true) { keyPair, lockMap ->
                     if (keyPair == null) {
-                        val ctx = MyApplication.instance?.applicationContext!!
-                        val tipDialog = TipDialog(ctx)
-                        tipDialog.setTip(ctx.getString(R.string.key_take_error_tip))
-                        tipDialog.setConfirmListener {
-                            tipDialog.dismiss()
-                            sendEventMsg(
-                                MsgEvent(
-                                    MSG_EVENT_DEVICE_EXCEPTION,
-                                    DeviceExceptionMsg(DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc)
+                        ThreadUtils.runOnMain {
+
+                            val ctx = MyApplication.instance?.applicationContext!!
+                            val tipDialog = TipDialog(ctx)
+                            tipDialog.setTip(ctx.getString(R.string.key_take_error_tip))
+                            tipDialog.setConfirmListener {
+                                tipDialog.dismiss()
+                                sendEventMsg(
+                                    MsgEvent(
+                                        MSG_EVENT_DEVICE_EXCEPTION,
+                                        DeviceExceptionMsg(DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc)
+                                    )
                                 )
-                            )
+                            }
+                            tipDialog.show()
                         }
-                        tipDialog.show()
                     } else {
                         addDeviceTake(
                             DEVICE_TYPE_KEY, deviceTakeUpdateBO.ticketId, keyPair.second?.rfid!!
@@ -1787,19 +1790,21 @@ object BusinessManager {
                 }
             }
         } ?: run {
-            val ctx = MyApplication.instance?.applicationContext!!
-            val tipDialog = TipDialog(ctx)
-            tipDialog.setTip(ctx.getString(R.string.key_take_error_tip))
-            tipDialog.setConfirmListener {
-                tipDialog.dismiss()
-                sendEventMsg(
-                    MsgEvent(
-                        MSG_EVENT_DEVICE_EXCEPTION,
-                        DeviceExceptionMsg(DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc)
+            ThreadUtils.runOnMain {
+                val ctx = MyApplication.instance?.applicationContext!!
+                val tipDialog = TipDialog(ctx)
+                tipDialog.setTip(ctx.getString(R.string.key_take_error_tip))
+                tipDialog.setConfirmListener {
+                    tipDialog.dismiss()
+                    sendEventMsg(
+                        MsgEvent(
+                            MSG_EVENT_DEVICE_EXCEPTION,
+                            DeviceExceptionMsg(DEVICE_TYPE_KEY, deviceTakeUpdateBO.nfc)
+                        )
                     )
-                )
+                }
+                tipDialog.show()
             }
-            tipDialog.show()
         }
     }
 

+ 8 - 8
app/src/main/java/com/grkj/iscs/ble/BleConnectionManager.kt

@@ -161,7 +161,7 @@ object BleConnectionManager {
                 currentConnectingMac = null
                 if (!isDone) {
                     // 判断是否仍然待连,防止拿走;移到末尾,防止循环影响
-                    if (checkProcess(listener.mac)) {
+                    if (checkProcess(listener.mac, false)) {
                         listener.callBack?.invoke(false, null)
                         unregisterConnectListener(listener.mac)
                     }
@@ -188,7 +188,7 @@ object BleConnectionManager {
         isNeedLoading: Boolean = false,
         prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
     ) {
-        if (!checkProcess(mac)) {
+        if (!checkProcess(mac, false)) {
             LogUtil.e("蓝牙连接-Prepare is canceled : $mac")
             return
         }
@@ -206,7 +206,7 @@ object BleConnectionManager {
         prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
     ) {
         LogUtil.i("蓝牙连接-doScanBle:$mac")
-        if (!checkProcess(mac)) {
+        if (!checkProcess(mac, false)) {
             LogUtil.e("蓝牙连接-Prepare is canceled : $mac")
             return
         }
@@ -272,7 +272,7 @@ object BleConnectionManager {
         prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
     ) {
         LogUtil.i("蓝牙连接-doConnect : ${bleDevice.mac}")
-        if (!checkProcess(bleDevice.mac)) {
+        if (!checkProcess(bleDevice.mac, false)) {
             LogUtil.e("蓝牙连接-Prepare is canceled : ${bleDevice.mac}")
             return
         }
@@ -328,7 +328,7 @@ object BleConnectionManager {
                             removeExceptionKey(it.mac)
                             // 设置MTU
                             Executor.delayOnMain(200) {
-                                if (!checkProcess(bleDevice.mac)) {
+                                if (!checkProcess(bleDevice.mac, false)) {
                                     LogUtil.e("Prepare is canceled : ${bleDevice.mac}")
                                     return@delayOnMain
                                 }
@@ -391,7 +391,7 @@ object BleConnectionManager {
         isNeedLoading: Boolean = false,
         prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
     ) {
-        if (!checkProcess(bleBean?.bleDevice?.mac)) {
+        if (!checkProcess(bleBean?.bleDevice?.mac, false)) {
             LogUtil.e("蓝牙连接-Prepare is canceled : ${bleBean?.bleDevice?.mac}")
             return
         }
@@ -693,7 +693,7 @@ object BleConnectionManager {
     /**
      * 扫描在线的蓝牙
      */
-    fun scanOnlineKeyLockMac(callback: (MutableList<BleDevice>?) -> Unit) {
+    fun scanOnlineKeyLockMac(callback: (List<BleDevice>?) -> Unit) {
         BleUtil.instance?.scan(object : CustomBleScanCallback() {
             override fun onPrompt(promptStr: String?) {
                 // 蓝牙未启动重试
@@ -720,7 +720,7 @@ object BleConnectionManager {
 
             override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
                 LogUtil.d("设备录入-扫描完成:$scanResultList")
-                callback(scanResultList)
+                callback(scanResultList?.toList())
             }
         })
     }

+ 0 - 1
app/src/main/java/com/grkj/iscs/view/activity/LoginActivity.kt

@@ -88,7 +88,6 @@ class LoginActivity : BaseMvpActivity<ILoginView, LoginPresenter, ActivityLoginB
             BusinessManager.connectDock(true)
         }
         presenter?.registerListener()
-
         FingerprintUtil.init(this)
         FingerprintUtil.start()
         FingerprintUtil.setScanListener(object : FingerprintUtil.OnScanListener {

+ 0 - 2
app/src/main/java/com/grkj/iscs/view/fragment/DeviceRegistrationKeyAndLockFragment.kt

@@ -49,8 +49,6 @@ class DeviceRegistrationKeyAndLockFragment :
     override fun initView() {
         viewModel.loginUser = SPUtils.getLoginUser(requireContext())
         //打开所有钥匙仓并关闭充电
-        LogUtil.d("设备录入-打开所有钥匙仓并关闭充电")
-        ModBusController.controlAllKeyBuckleOpen()
         viewModel.registerInitListener().observe(this) {
             LogUtil.d("设备录入-初始化检测任务分发完成")
         }

+ 4 - 0
app/src/main/java/com/grkj/iscs/view/fragment/DeviceRegistrationTypeSelectFragment.kt

@@ -3,7 +3,9 @@ package com.grkj.iscs.view.fragment
 import com.grkj.iscs.R
 import com.grkj.iscs.databinding.FragmentDeviceRegistrationTypeSelectBinding
 import com.grkj.iscs.enums.DeviceInputTypeEnum
+import com.grkj.iscs.modbus.ModBusController
 import com.grkj.iscs.model.ISCSDomainData
+import com.grkj.iscs.util.log.LogUtil
 import com.grkj.iscs.view.activity.HomeActivity
 import com.grkj.iscs.view.base.BaseNavFragment
 import com.sik.sikcore.extension.setDebouncedClickListener
@@ -36,5 +38,7 @@ class DeviceRegistrationTypeSelectFragment :
     override fun onResume() {
         super.onResume()
         ISCSDomainData.isDeviceRegistration = true
+        LogUtil.d("设备录入-打开所有钥匙仓并关闭充电")
+        ModBusController.controlAllKeyBuckleOpen()
     }
 }

+ 21 - 10
app/src/main/java/com/grkj/iscs/view/viewmodel/DeviceRegistrationKeyAndLockViewModel.kt

@@ -35,6 +35,7 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
     var newHardwareKeySize: Int = 0
     var newHardwareLockSize: Int = 0
     private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
+    private val alreadyUsedMac: MutableList<String> = mutableListOf()
 
     fun initData(rowList: MutableList<DockStatusBO>) {
         val dockConfigJson =
@@ -73,8 +74,10 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
 
     fun checkNewHardware(device: DockBean.DeviceBean, callback: () -> Unit) {
         if (device is DockBean.KeyBean) {
+            device.mac = null
             NetApi.getKeyInfo(device.rfid.toString()) {
                 device.newHardware = it == null
+                device.mac = it?.macAddress
                 callback()
             }
         } else if (device is DockBean.LockBean) {
@@ -89,6 +92,7 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
         return liveData(Dispatchers.IO) {
             isStartCheckKey = false
             newHardwareKeyBean.clear()
+            alreadyUsedMac.clear()
             isLoadComplete.postValue(false)
             val allDeviceCloseCmdSend =
                 BleConnectionManager.scanOnlineKeyLockMacAndSwitchModeToClose()
@@ -128,6 +132,11 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
                         .filter { it.newHardware && it.isExist }
                         .toMutableList()
                 })
+            alreadyUsedMac.addAll(
+                dockList.filter { it.type == DeviceConst.DOCK_TYPE_KEY || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
+                    .map { it.deviceList }.flatten().filterIsInstance<DockBean.KeyBean>()
+                    .filter { it.mac?.isNotEmpty() == true }.mapNotNull { it.mac }
+            )
             LogUtil.d("设备录入-新设备:${newHardwareKeyBean}")
             for ((addr, keyBeans) in newHardwareKeyBean) {
                 for (keyBean in keyBeans) {
@@ -177,20 +186,22 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
                                         openChargeAndScanMac(addr, keyBean)
                                     }
                                 } else {
+                                    val bleDevice = bleDevices?.find {
+                                        it.mac !in alreadyUsedMac
+                                    }
+                                    if (bleDevice?.mac?.isEmpty() == true) {
+                                        ThreadUtils.runOnIO {
+                                            openChargeAndScanMac(addr, keyBean)
+                                        }
+                                        return@scanOnlineKeyLockMac
+                                    }
                                     LogUtil.i(
                                         "设备录入-没有使用过的mac:${keyBean.rfid},${
-                                            bleDevices?.find {
-                                                it.mac !in newHardwareKeyBean.map { it.value }
-                                                    .flatten()
-                                                    .map { it.mac }
-                                            }?.mac
+                                            bleDevice?.mac
                                         }"
                                     )
-                                    val bleDevice = bleDevices?.find {
-                                        it.mac !in newHardwareKeyBean.map { it.value }.flatten()
-                                            .map { it.mac }
-                                    }
-                                    keyBean.mac = bleDevice?.mac
+                                    keyBean.mac = bleDevice?.mac!!
+                                    alreadyUsedMac.add(bleDevice.mac)
                                     ThreadUtils.runOnIO {
                                         keyBean.mac?.let {
                                             val connected =