Răsfoiți Sursa

refactor(更新) :
- 增加钥匙电量低于阈值时,不再派发,并提示用户
- 增加最低派发电量系统参数
- 优化选择钥匙策略,已连接或连接中的钥匙电量低时,断开连接并选择其他钥匙

周文健 2 luni în urmă
părinte
comite
b5170d1f55

+ 1 - 0
app/src/main/java/com/grkj/iscs_mars/BusinessManager.kt

@@ -16,6 +16,7 @@ import com.grkj.iscs_mars.ble.BleReturnDispatcher
 import com.grkj.iscs_mars.ble.BleSendDispatcher
 import com.grkj.iscs_mars.ble.BleUtil
 import com.grkj.iscs_mars.ble.CustomBleWriteCallback
+import com.grkj.iscs_mars.enums.NoKeyReason
 import com.grkj.iscs_mars.extentions.removeLeadingZeros
 import com.grkj.iscs_mars.extentions.serialNo
 import com.grkj.iscs_mars.extentions.startsWith

+ 21 - 0
app/src/main/java/com/grkj/iscs_mars/enums/NoKeyReason.kt

@@ -0,0 +1,21 @@
+package com.grkj.iscs_mars.enums
+
+/**
+ * 无钥匙原因
+ */
+enum class NoKeyReason {
+    /**
+     * 无原因
+     */
+    NONE,
+
+    /**
+     * 无钥匙
+     */
+    NO_KEY,
+
+    /**
+     * 钥匙电量低
+     */
+    KEY_POWER_LOW
+}

+ 57 - 34
app/src/main/java/com/grkj/iscs_mars/modbus/ModBusController.kt

@@ -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

+ 5 - 0
app/src/main/java/com/grkj/iscs_mars/model/DictAndSystemConstants.kt

@@ -63,4 +63,9 @@ object DictAndSystemConstants {
      * 是否允许跨机柜取设备
      */
     const val KEY_ALLOW_CROSS_CABINET = "Allow_cross_cabinet"
+
+    /**
+     * 最低电池派发电量
+     */
+    const val KEY_SYS_POWER_MIN = "sys.power.min"
 }

+ 11 - 0
app/src/main/java/com/grkj/iscs_mars/model/ISCSDomainData.kt

@@ -1,6 +1,7 @@
 package com.grkj.iscs_mars.model
 
 import com.grkj.iscs_mars.enums.DeviceInputTypeEnum
+import com.grkj.iscs_mars.enums.NoKeyReason
 
 /**
  * 业务数据
@@ -20,4 +21,14 @@ object ISCSDomainData {
      * 是否允许跨机柜取设备
      */
     var isAllowCrossCabinet: Boolean = true
+
+    /**
+     * 电池最低电量
+     */
+    var powerMin: Int = 30
+
+    /**
+     * 无钥匙原因
+     */
+    var noKeyReason: NoKeyReason = NoKeyReason.NONE
 }

+ 3 - 0
app/src/main/java/com/grkj/iscs_mars/view/presenter/HomePresenter.kt

@@ -148,5 +148,8 @@ class HomePresenter : BasePresenter<IHomeView>() {
         NetApi.getIsSystemAttributeByKey(DictAndSystemConstants.KEY_ALLOW_CROSS_CABINET) {
             ISCSDomainData.isAllowCrossCabinet = it?.sysAttrValue == "1"
         }
+        NetApi.getIsSystemAttributeByKey(DictAndSystemConstants.KEY_SYS_POWER_MIN) {
+            ISCSDomainData.powerMin = it?.sysAttrValue?.toInt() ?: 30
+        }
     }
 }

+ 27 - 12
app/src/main/java/com/grkj/iscs_mars/view/presenter/JobProgressPresenter.kt

@@ -3,6 +3,7 @@ package com.grkj.iscs_mars.view.presenter
 import android.content.Context
 import com.grkj.iscs_mars.BusinessManager
 import com.grkj.iscs_mars.R
+import com.grkj.iscs_mars.enums.NoKeyReason
 import com.grkj.iscs_mars.extentions.serialNo
 import com.grkj.iscs_mars.modbus.DockBean
 import com.grkj.iscs_mars.modbus.ModBusController
@@ -196,19 +197,33 @@ class JobProgressPresenter : BasePresenter<IJobProgressView>() {
                                 Executor.runOnMain {
                                     if (ISCSDomainData.isAllowCrossCabinet) {
                                         if (realCount == 0) {
-                                            showTip(
-                                                mContext!!.getString(R.string.key_not_enough_wait_or_take_on_another),
-                                                TipDialog.TYPE_HINT
-                                            )
+                                            if (ISCSDomainData.noKeyReason == NoKeyReason.NO_KEY) {
+                                                showTip(
+                                                    mContext!!.getString(R.string.key_not_enough_wait_or_take_on_another),
+                                                    TipDialog.TYPE_HINT
+                                                )
+                                            } else if (ISCSDomainData.noKeyReason == NoKeyReason.KEY_POWER_LOW) {
+                                                showTip(
+                                                    mContext!!.getString(R.string.key_power_low_wait_or_take_on_another),
+                                                    TipDialog.TYPE_HINT
+                                                )
+                                            }
                                         } else {
-                                            showTip(
-                                                mContext!!.getString(
-                                                    R.string.lock_enough_but_key_not_enough,
-                                                    lockCount
-                                                ),
-                                                TipDialog.TYPE_ALL, {
-                                                    handleGiveKey(ticketDetail, null, lockMap)
-                                                })
+                                            if (ISCSDomainData.noKeyReason == NoKeyReason.NO_KEY) {
+                                                showTip(
+                                                    mContext!!.getString(
+                                                        R.string.lock_enough_but_key_not_enough,
+                                                        lockCount
+                                                    ),
+                                                    TipDialog.TYPE_ALL, {
+                                                        handleGiveKey(ticketDetail, null, lockMap)
+                                                    })
+                                            } else if (ISCSDomainData.noKeyReason == NoKeyReason.KEY_POWER_LOW) {
+                                                showTip(
+                                                    mContext!!.getString(R.string.lock_enough_but_key_enough_but_key_power_down),
+                                                    TipDialog.TYPE_HINT
+                                                )
+                                            }
                                         }
                                     } else {
                                         showTip(

+ 4 - 2
app/src/main/res/values-en/strings.xml

@@ -400,8 +400,10 @@
     <string name="operation">Operation</string>
     <string name="ticket_get_failed">Ticket get failed,Please reposition the key</string>
     <string name="ticket_not_finish_can_not_return_lock">The padlock is not allowed to be returned if the work ticket is not completed</string>
-    <string name="lock_is_not_enough_check_issue_ticket">%1$d padlocks can be distributed to the current cabinet. Please obtain the remaining padlocks from other cabinets or wait for other operations to return Padlock, are you sure to perform operation?</string>
+    <string name="lock_is_not_enough_check_issue_ticket">%1$d padlocks can be distributed to the current cabinet. Please obtain the remaining padlocks from other cabinets or wait for other operations to return Padlock, \nAre you sure to perform operation?</string>
     <string name="lock_is_empty_stop_issue_ticket">There is no padlock available for the current cabinet. Please obtain the padlock from other cabinets or wait for other jobs to return the padlock.</string>
-    <string name="lock_enough_but_key_not_enough">The current cabinet can distribute %1$d padlocks. Please obtain keys from other cabinets or wait for other jobs to return the keys. Are you sure you want to perform the operation?</string>
+    <string name="lock_enough_but_key_not_enough">The current cabinet can distribute %1$d padlocks. Please obtain keys from other cabinets or wait for other jobs to return the keys. \nAre you sure you want to perform the operation?</string>
     <string name="key_not_enough_wait_or_take_on_another">There is no key available for distribution in the current cabinet. Please obtain the key from other cabinets or wait for other operations to return the key.</string>
+    <string name="key_power_low_wait_or_take_on_another">The current power of the cabinet key is low. Please wait for 15 minutes or get it from another cabinet.</string>
+    <string name="lock_enough_but_key_enough_but_key_power_down">The current cabinet can distribute %1$d padlocks but The current power of the cabinet key is low. Please obtain keys from other cabinets or wait for other jobs to return the keys. \nAre you sure you want to perform the operation?</string>
 </resources>

+ 2 - 0
app/src/main/res/values-zh/strings.xml

@@ -404,4 +404,6 @@
     <string name="lock_is_empty_stop_issue_ticket">当前机柜无可派发挂锁,请从其他机柜获取挂锁或等待其他作业归还挂锁。</string>
     <string name="lock_enough_but_key_not_enough">当前机柜可派发%1$d把挂锁,请从其他机柜获取钥匙或等待其他作业归还钥匙。\n确定要执行操作吗?</string>
     <string name="key_not_enough_wait_or_take_on_another">当前机柜无可派发钥匙,请从其他机柜获取或等待其他作业归还钥匙。</string>
+    <string name="key_power_low_wait_or_take_on_another">当前机柜钥匙电量较低,请等待15分钟或从其他机柜获取。</string>
+    <string name="lock_enough_but_key_enough_but_key_power_down">当前机柜可派发%1$d把挂锁,但是当前机柜钥匙电量较低,请从其他机柜获取钥匙或等待其他作业归还钥匙。\\n确定要执行操作吗?</string>
 </resources>

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -404,4 +404,6 @@
     <string name="lock_is_empty_stop_issue_ticket">当前机柜无可派发挂锁,请从其他机柜获取挂锁或等待其他作业归还挂锁。</string>
     <string name="lock_enough_but_key_not_enough">当前机柜可派发%1$d把挂锁,请从其他机柜获取钥匙或等待其他作业归还钥匙。\n确定要执行操作吗?</string>
     <string name="key_not_enough_wait_or_take_on_another">当前机柜无可派发钥匙,请从其他机柜获取或等待其他作业归还钥匙。</string>
+    <string name="key_power_low_wait_or_take_on_another">当前机柜钥匙电量较低,请等待15分钟或从其他机柜获取。</string>
+    <string name="lock_enough_but_key_enough_but_key_power_down">当前机柜可派发%1$d把挂锁,但是当前机柜钥匙电量较低,请从其他机柜获取钥匙或等待其他作业归还钥匙。\\n确定要执行操作吗?</string>
 </resources>