Эх сурвалжийг харах

下发作业票添加重试机制;绕过因钥匙断连导致的钥匙准备状态异常;登录添加loading框优化体验

Frankensteinly 7 сар өмнө
parent
commit
eb14e1937e

+ 33 - 17
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -25,12 +25,9 @@ import com.grkj.iscs.extentions.startsWith
 import com.grkj.iscs.extentions.toHexStrings
 import com.grkj.iscs.modbus.DockBean
 import com.grkj.iscs.modbus.ModBusController
-import com.grkj.iscs.modbus.ModBusController.controlKeyBuckle
-import com.grkj.iscs.modbus.ModBusController.controlLockBuckle
 import com.grkj.iscs.modbus.ModBusController.dockList
 import com.grkj.iscs.model.Constants.PERMISSION_REQUEST_CODE
 import com.grkj.iscs.model.Constants.USER_TYPE_LOCKER
-import com.grkj.iscs.model.DeviceConst
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
@@ -150,10 +147,10 @@ object BusinessManager {
                                     mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "未找到钥匙信息", false)))
                                     ToastUtils.tip(R.string.key_not_exists)
                                 } else {
-                                    sendLoadingEventMsg(MyApplication.instance?.applicationContext?.getString(R.string.take_out_key_tip))
+                                    sendLoadingEventMsg(CommonUtils.getStr(R.string.take_out_key_tip))
                                     val dock = ModBusController.getDockByKeyMac(it.data.bleBean.bleDevice.mac)
                                     ModBusController.controlKeyBuckle(true, keyBean.isLeft, dock?.addr)
-                                    ModBusController.updateKeyReadyStatus(it.data.bleBean.bleDevice.mac, false)
+                                    ModBusController.updateKeyReadyStatus(it.data.bleBean.bleDevice.mac, false, 1)
                                     ToastUtils.tip(R.string.take_out_key)
                                 }
                             } else {
@@ -166,7 +163,7 @@ object BusinessManager {
                         // 待机模式
                         2 -> {
                             if (it.data.res == 1) {
-                                ModBusController.updateKeyReadyStatus(it.data.bleBean.bleDevice.mac, true)
+                                ModBusController.updateKeyReadyStatus(it.data.bleBean.bleDevice.mac, true, 2)
                                 // 延时再次获取当前状态,触发handleCurrentMode里工作票下发状态检查
                                 Executor.delayOnMain(500) {
                                     getCurrentStatus(1, it.data.bleBean.bleDevice)
@@ -222,7 +219,7 @@ object BusinessManager {
         ModBusController.registerStatusListener(this) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             if (res.isEmpty() || res.any { it.isEmpty() }) {
-                var tipStr = MyApplication.instance?.applicationContext?.getString(R.string.no_response_board_exists) + " : "
+                var tipStr = CommonUtils.getStr(R.string.no_response_board_exists) + " : "
                 val addressList = mutableListOf<String>()
 
                 ModBusController.modBusManager?.mSlaveAddressList?.forEach { itDock ->
@@ -243,7 +240,7 @@ object BusinessManager {
                     DOCK_TYPE_KEY -> {
                         dockBean.getKeyList().forEach { keyBean ->
                             if (keyBean.isExist) {
-                                sendLoadingEventMsg(MyApplication.instance?.applicationContext?.getString(R.string.data_is_uploading))
+                                sendLoadingEventMsg(CommonUtils.getStr(R.string.data_is_uploading))
                                 // 放回钥匙,读取rfid
                                 ModBusController.readKeyRfid(dockBean.addr, if (keyBean.isLeft) 0 else 1) { isLeft, res ->
                                     if (res.size < 11) {
@@ -255,6 +252,7 @@ object BusinessManager {
                                     NetApi.getKeyInfo(rfid) {
                                         if (it != null && !it.macAddress.isNullOrEmpty()) {
                                             ModBusController.updateKeyMac(dockBean.addr, keyBean.isLeft, it.macAddress)
+                                            ModBusController.updateKeyReadyStatus(it.macAddress, false, 5)
                                             // TODO 移动到HomePresenter里,根据业务进行处理
 //                                            showKeyReturnDialog(it.macAddress, isLeft, dockBean.addr.toInt())
 //                                            registerConnectListener(it.macAddress)
@@ -715,7 +713,6 @@ object BusinessManager {
                     bleDevice.mac?.let { itMac ->
                         unregisterConnectListener(itMac)
                     }
-                    ModBusController.updateKeyReadyStatus(bleDevice.mac, false)
                     if (!isActiveDisConnected) {
                         // 测试模式下不重连
                         if (isTestMode) {
@@ -725,6 +722,8 @@ object BusinessManager {
                         Executor.delayOnMain(300) {
                             registerConnectListener(bleDevice.mac)
                         }
+                    } else {
+                        ModBusController.updateKeyReadyStatus(bleDevice.mac, false, 3)
                     }
                 }
             })
@@ -844,6 +843,7 @@ object BusinessManager {
                 return@registerConnectListener
             }
             // 单bleBean json赋值
+            bleBean.retryCount = 0
             bleBean.ticketSend = generateTicketSendJson(isLock, ticketDetail, lockList)
             bleBean.ticketSend?.let { itJson ->
                 sendTicketWithRetry(itJson, bleBean.bleDevice, isNeedLoading)
@@ -922,16 +922,16 @@ object BusinessManager {
     }
 
     private fun sendTicket(jsonStr: String, bleDevice: BleDevice, isNeedLoading: Boolean = false, processCallback: ((Boolean) -> Unit)? = null) {
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始下发工作票...", null)))
+        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, CommonUtils.getStr(R.string.start_to_send_ticket), null)))
         BleCmdManager.sendWorkTicket(jsonStr, bleDevice = bleDevice, callback = object : CustomBleWriteCallback() {
             override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
                 LogUtil.i("sendTicket success")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "工作票下发中...", null)))
+                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, CommonUtils.getStr(R.string.sending_ticket), null)))
             }
 
             override fun onWriteFailure(exception: BleException?) {
                 LogUtil.e("sendTicket fail : ${bleDevice.mac}")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票下发失败", null)))
+                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, CommonUtils.getStr(R.string.send_ticket_fail), null)))
                 processCallback?.invoke(false)
             }
         })
@@ -1066,6 +1066,21 @@ object BusinessManager {
                 Executor.delayOnIO(800) {
                     switchWorkMode(bleBean.bleDevice, isNeedLoading)
                 }
+            } else {
+                sendLoadingEventMsg(null, false)
+                if (bleBean.retryCount < 3) {
+                    Executor.delayOnMain(500) {
+                        bleBean.retryCount++
+                        sendLoadingEventMsg(MyApplication.instance!!.getString(R.string.start_to_send_ticket))
+                        sendTicketWithRetry(bleBean.ticketSend!!, bleBean.bleDevice, isNeedLoading)
+                    }
+                } else {
+                    ToastUtils.tip(R.string.send_ticket_fail)
+                    LogUtil.e("Send ticket fail")
+                    ModBusController.getKeyByMac(bleBean.bleDevice.mac)?.let { itKey ->
+                        mDeviceTakeList.removeIf { it.deviceType == DEVICE_TYPE_KEY && it.nfc == itKey.rfid }
+                    }
+                }
             }
         }
     }
@@ -1239,7 +1254,7 @@ object BusinessManager {
                             mEventBus.postValue(MsgEvent(MSG_EVENT_UPDATE_TICKET_PROGRESS, UpdateTicketProgressMsg(info.ticketId)))
                         }
                     }
-                }
+                } ?: sendLoadingEventMsg(null, false)
             }
             // 挂锁
             1 -> {
@@ -1323,7 +1338,8 @@ object BusinessManager {
                             }
                         }
                     } ?: let {
-                        ModBusController.updateKeyReadyStatus(currentModeMsg.bleBean.bleDevice.mac, true)
+                        ModBusController.updateKeyReadyStatus(currentModeMsg.bleBean.bleDevice.mac, true, 4)
+                        ModBusController.controlKeyBuckle(false, currentModeMsg.bleBean.bleDevice.mac)
                         sendLoadingEventMsg(null, false)
                     }
                 }
@@ -1332,7 +1348,7 @@ object BusinessManager {
             0x03.toByte() -> {
                 // TODO 上报?
                 ToastUtils.tip("${currentModeMsg.bleBean.bleDevice.mac} : " +
-                        "${MyApplication.instance?.applicationContext?.getString(R.string.key_is_in_failure_mode)}"
+                        "${CommonUtils.getStr(R.string.key_is_in_failure_mode)}"
                 )
             }
         }
@@ -1385,12 +1401,12 @@ object BusinessManager {
         // 关所有有设备的卡扣
         dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
             val hasLockIdxList = dockBean.getLockList().filter { it.isExist }.map { it.idx } as MutableList<Int>
-            controlLockBuckle(false, dockBean.addr, hasLockIdxList)
+            ModBusController.controlLockBuckle(false, dockBean.addr, hasLockIdxList)
         }
         dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
             dockBean.getKeyList().forEach { key ->
                 if (key.isExist) {
-                    controlKeyBuckle(false, key.isLeft, dockBean.addr)
+                    ModBusController.controlKeyBuckle(false, key.isLeft, dockBean.addr)
                 }
             }
         }

+ 2 - 1
app/src/main/java/com/grkj/iscs/ble/BleBean.kt

@@ -8,5 +8,6 @@ data class BleBean(
     var token: ByteArray? = null,
     var fileSend: File? = null,
     var ticketSend: String? = null,             // 下发的工作票
-    var ticketStatus: ByteArray = byteArrayOf() // 从钥匙拿的工作票
+    var ticketStatus: ByteArray = byteArrayOf(), // 从钥匙拿的工作票
+    var retryCount: Int = 0
 )

+ 1 - 0
app/src/main/java/com/grkj/iscs/ble/BleCmdManager.kt

@@ -219,6 +219,7 @@ object BleCmdManager {
         } else {
             if (res == 0x00.toByte()) {
                 LogUtil.i("Work ticket is done")
+                bleBean.retryCount = 0
                 callBack?.invoke(true, res)
             } else {
                 callBack?.invoke(false, res)

+ 14 - 2
app/src/main/java/com/grkj/iscs/modbus/ModBusController.kt

@@ -399,6 +399,17 @@ object ModBusController {
         }
     }
 
+    /**
+     * 控制钥匙卡扣
+     */
+    fun controlKeyBuckle(isOpen: Boolean, mac: String, done: ((res: ByteArray) -> Unit)? = null) {
+        val dockBean = getDockByKeyMac(mac)
+        dockBean ?: return
+        val key = getKeyByMac(mac)
+        key ?: return
+        controlKeyBuckle(isOpen, key.isLeft, dockBean.addr, done)
+    }
+
     /**
      * 开/关钥匙卡扣
      *
@@ -439,7 +450,8 @@ object ModBusController {
     /**
      * 更新钥匙的准备状态
      */
-    fun updateKeyReadyStatus(mac: String, isReady: Boolean) {
+    fun updateKeyReadyStatus(mac: String, isReady: Boolean, from: Int) {
+        LogUtil.i("mac : $mac - $from")
         dockList.find { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }?.getKeyList()?.find { it.mac == mac }?.isReady = isReady
     }
 
@@ -539,7 +551,7 @@ object ModBusController {
 
         keyList.forEach {
             LogUtil.i("keyStatus : ${it.isExist} - ${it.rfid} - ${it.mac} - ${it.isReady} - " +
-                    "${BusinessManager.getBleDeviceByMac(it.mac!!)?.bleDevice == null} - " +
+                    "${BusinessManager.getBleDeviceByMac(it.mac!!)?.bleDevice != null} - " +
                     "${BleManager.getInstance().isConnected(BusinessManager.getBleDeviceByMac(it.mac!!)?.bleDevice)}")
         }
         val key = keyList.find { it.isExist && it.rfid != null && it.mac != null && it.isReady &&

+ 6 - 0
app/src/main/java/com/grkj/iscs/view/presenter/LoginPresenter.kt

@@ -31,24 +31,28 @@ class LoginPresenter : BasePresenter<ILoginView>() {
             ToastUtils.tip(context.getString(R.string.please_input_password))
             return
         }
+        BusinessManager.sendLoadingEventMsg(mContext?.getString(R.string.doing_login))
         NetApi.login(account, pwd) {
             commonProcess(it, callBack)
         }
     }
 
     fun cardLogin(card: String, callBack: (Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit) {
+        BusinessManager.sendLoadingEventMsg(mContext?.getString(R.string.doing_login))
         NetApi.cardLogin(card) {
             commonProcess(it, callBack)
         }
     }
 
     fun fingerprintLogin(bitmap: Bitmap, callBack: (Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit) {
+        BusinessManager.sendLoadingEventMsg(mContext?.getString(R.string.doing_login))
         NetApi.loginByFingerprint(mutableListOf(FileStreamReqParam("file", BitmapUtil.bitmapToByteArray(bitmap), ".bmp"))) {
             commonProcess(it, callBack)
         }
     }
 
     fun faceLogin(bitmap: Bitmap, callBack: (Boolean, CardInfoRespVO?, UserInfoRespVO?) -> Unit) {
+        BusinessManager.sendLoadingEventMsg(mContext?.getString(R.string.doing_login))
         NetApi.loginByFace(mutableListOf(FileStreamReqParam("file", BitmapUtil.bitmapToByteArray(bitmap), ".bmp"))) {
             commonProcess(it, callBack)
         }
@@ -59,6 +63,7 @@ class LoginPresenter : BasePresenter<ILoginView>() {
             NetApi.getUserInfo { userInfo ->
                 NetApi.getCardInfoByLoginUser { itInfo ->
                     NetApi.getSystemAttributePage(1, -1) { itSys ->
+                        BusinessManager.sendLoadingEventMsg(null, false)
                         itSys?.let { sys ->
                             SPUtils.saveSystemAttribute(mContext!!, sys.records)
                         }
@@ -74,6 +79,7 @@ class LoginPresenter : BasePresenter<ILoginView>() {
             }
         } else {
             Executor.runOnMain {
+                BusinessManager.sendLoadingEventMsg(null, false)
                 callBack.invoke(false, null, null)
             }
         }

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

@@ -273,4 +273,8 @@
     <string name="can_not_change_locker">当前无法更换上锁人</string>
     <string name="can_not_change_colocker">当前无法更换共锁人</string>
     <string name="is_processing_please_wait">正在处理中,请稍后······</string>
+    <string name="doing_login">正在登录······</string>
+    <string name="start_to_send_ticket">开始下发工作票······</string>
+    <string name="sending_ticket">工作票下发中······</string>
+    <string name="send_ticket_fail">作业票下发失败</string>
 </resources>