Browse Source

refactor(开关和设备异常)
- 蓝牙处理

周文健 5 months ago
parent
commit
40db642994

+ 71 - 45
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -1,5 +1,5 @@
 package com.grkj.iscs
 package com.grkj.iscs
-
+//todo 所有蓝牙包替换com.clj. -> com.clj.
 import android.bluetooth.BluetoothGatt
 import android.bluetooth.BluetoothGatt
 import android.content.Context
 import android.content.Context
 import android.content.Intent
 import android.content.Intent
@@ -196,10 +196,6 @@ object BusinessManager {
                                         1
                                         1
                                     )
                                     )
                                     ToastUtils.tip(R.string.take_out_key)
                                     ToastUtils.tip(R.string.take_out_key)
-                                    //连接一把
-                                    if (BleManager.getInstance().allConnectedDevice.size < BleConst.MAX_KEY_STAND_BY) {
-                                        connectExistsKey(it.data.bleBean.bleDevice.mac)
-                                    }
                                 }
                                 }
                             } else {
                             } else {
                                 LogUtil.e("切换工作模式失败 : ${it.data.bleBean.bleDevice.mac}")
                                 LogUtil.e("切换工作模式失败 : ${it.data.bleBean.bleDevice.mac}")
@@ -211,10 +207,6 @@ object BusinessManager {
                         // 待机模式
                         // 待机模式
                         2 -> {
                         2 -> {
                             if (it.data.res == 1) {
                             if (it.data.res == 1) {
-                                // 只能在这里断开,不能全部断开
-                                if (BleManager.getInstance().allConnectedDevice.size > BleConst.MAX_KEY_STAND_BY) {
-                                    BleManager.getInstance().disconnect(it.data.bleBean.bleDevice)
-                                }
                                 ModBusController.updateKeyReadyStatus(
                                 ModBusController.updateKeyReadyStatus(
                                     it.data.bleBean.bleDevice.mac,
                                     it.data.bleBean.bleDevice.mac,
                                     true,
                                     true,
@@ -282,7 +274,8 @@ object BusinessManager {
                     (keyPage?.records
                     (keyPage?.records
                         ?.filter { it.exStatus == keyStatusList.find { d -> d.dictLabel == "异常" }?.dictValue }
                         ?.filter { it.exStatus == keyStatusList.find { d -> d.dictLabel == "异常" }?.dictValue }
                         ?.map { it.keyNfc ?: "" }
                         ?.map { it.keyNfc ?: "" }
-                        ?.toMutableList() ?: mutableListOf()).apply { add(exceptKeyMac) }
+                        ?.toMutableList() ?: mutableListOf()),
+                    mutableListOf(exceptKeyMac)
                 )
                 )
             }
             }
         }
         }
@@ -364,7 +357,7 @@ object BusinessManager {
                 DOCK_TYPE_KEY -> {
                 DOCK_TYPE_KEY -> {
                     dockBean.getKeyList().forEach { keyBean ->
                     dockBean.getKeyList().forEach { keyBean ->
                         if (keyBean.isExist) {
                         if (keyBean.isExist) {
-                            sendLoadingEventMsg(CommonUtils.getStr(R.string.ble_connecting))
+//                            sendLoadingEventMsg(CommonUtils.getStr(R.string.ble_connecting))
                             // 放回钥匙,读取rfid
                             // 放回钥匙,读取rfid
                             ModBusController.readKeyRfid(
                             ModBusController.readKeyRfid(
                                 dockBean.addr,
                                 dockBean.addr,
@@ -398,9 +391,7 @@ object BusinessManager {
                                         ToastUtils.tip(R.string.get_key_info_fail)
                                         ToastUtils.tip(R.string.get_key_info_fail)
                                     }
                                     }
                                 }
                                 }
-                                // TODO 蓝牙通信
                             }
                             }
-//                                ModBusController.controlKeyBuckle(false, isLeft = true, dockBean.addr.toInt() - 1)
                         } else {
                         } else {
                             // 移出待连监听集合,防止connectKey循环失败
                             // 移出待连监听集合,防止connectKey循环失败
                             keyBean.mac?.let {
                             keyBean.mac?.let {
@@ -692,6 +683,7 @@ object BusinessManager {
     ) {
     ) {
         // 你可以改成接收 CoroutineScope 或者直接在全局 Scope 启动
         // 你可以改成接收 CoroutineScope 或者直接在全局 Scope 启动
         ThreadUtils.runOnMain {
         ThreadUtils.runOnMain {
+            sendLoadingEventMsg(MyApplication.instance?.applicationContext!!.getString(R.string.check_key_and_lock))
             try {
             try {
                 // —— 串行请求1 & 2 ——
                 // —— 串行请求1 & 2 ——
                 val slotsPage = getSlotsPage()
                 val slotsPage = getSlotsPage()
@@ -760,12 +752,12 @@ object BusinessManager {
                         )
                         )
                     }
                     }
                 }
                 }
-
                 // —— 全部计算完毕,在主线程一次性回调 ——
                 // —— 全部计算完毕,在主线程一次性回调 ——
                 callBack(keyPair, lockMap)
                 callBack(keyPair, lockMap)
 
 
             } catch (e: Exception) {
             } catch (e: Exception) {
                 // 根据需求处理异常,或把异常信息也通过 callback 返回
                 // 根据需求处理异常,或把异常信息也通过 callback 返回
+                sendLoadingEventMsg(null, false)
                 e.printStackTrace()
                 e.printStackTrace()
                 ToastUtils.tip("检查设备异常:${e.message}")
                 ToastUtils.tip("检查设备异常:${e.message}")
             }
             }
@@ -1524,6 +1516,7 @@ object BusinessManager {
                 )
                 )
 
 
                 Executor.delayOnIO(800) {
                 Executor.delayOnIO(800) {
+                    //切换到工作模式
                     switchWorkMode(bleBean.bleDevice, isNeedLoading)
                     switchWorkMode(bleBean.bleDevice, isNeedLoading)
                 }
                 }
             } else {
             } else {
@@ -1846,6 +1839,22 @@ object BusinessManager {
                                         UpdateTicketProgressMsg(info.ticketId)
                                         UpdateTicketProgressMsg(info.ticketId)
                                     )
                                     )
                                 )
                                 )
+                                //钥匙取出之后重新再连一把钥匙待机
+                                connectListeners.removeIf { connectListener ->
+                                    connectListener.mac == ModBusController.getKeyByRfid(
+                                        info.nfc
+                                    )?.mac
+                                }
+                                //待机数不够就再连一把,但不能是原来那把
+                                if (BleManager.getInstance().allConnectedDevice.size < BleConst.MAX_KEY_STAND_BY) {
+                                    ModBusController.getKeyByRfid(
+                                        info.nfc
+                                    )?.mac?.let {
+                                        connectExistsKey(
+                                            it
+                                        )
+                                    }
+                                }
                             }
                             }
                         }
                         }
                     } ?: sendLoadingEventMsg(null, false)
                     } ?: sendLoadingEventMsg(null, false)
@@ -1894,6 +1903,7 @@ object BusinessManager {
                                 // 检查有无当前工作票的钥匙
                                 // 检查有无当前工作票的钥匙
                                 mDeviceTakeList.find { it.deviceType == DEVICE_TYPE_KEY && it.ticketId == info.ticketId }
                                 mDeviceTakeList.find { it.deviceType == DEVICE_TYPE_KEY && it.ticketId == info.ticketId }
                                     ?.let { itKey ->
                                     ?.let { itKey ->
+                                        sendLoadingEventMsg(MyApplication.instance?.applicationContext!!.getString(R.string.ble_connecting))
                                         handleGiveKey(itKey)
                                         handleGiveKey(itKey)
                                     }
                                     }
                             }
                             }
@@ -1907,41 +1917,57 @@ object BusinessManager {
      * 分配钥匙
      * 分配钥匙
      */
      */
     private fun handleGiveKey(deviceTakeUpdateBO: DeviceTakeUpdateBO) {
     private fun handleGiveKey(deviceTakeUpdateBO: DeviceTakeUpdateBO) {
-        getCurrentStatus(
-            2,
-            getBleDeviceByMac(ModBusController.getKeyByRfid(deviceTakeUpdateBO.nfc)?.mac)!!.bleDevice
-        ) {
-            if (!it) {
-                return@getCurrentStatus
-            }
-            LogUtil.w("handleGiveKey timeout")
-            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)
+        getBleDeviceByMac(ModBusController.getKeyByRfid(deviceTakeUpdateBO.nfc)?.mac)?.let {
+            getCurrentStatus(
+                2,
+                getBleDeviceByMac(ModBusController.getKeyByRfid(deviceTakeUpdateBO.nfc)?.mac)!!.bleDevice
+            ) {
+                if (!it) {
+                    return@getCurrentStatus
+                }
+                LogUtil.w("handleGiveKey timeout")
+                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)
+                                )
                             )
                             )
-                        )
-                    }
-                    tipDialog.show()
-                } else {
-                    addDeviceTake(
-                        DEVICE_TYPE_KEY, deviceTakeUpdateBO.ticketId, keyPair.second?.rfid!!
-                    )
-                    handleGiveKey(
-                        DeviceTakeUpdateBO(
+                        }
+                        tipDialog.show()
+                    } else {
+                        addDeviceTake(
                             DEVICE_TYPE_KEY, deviceTakeUpdateBO.ticketId, keyPair.second?.rfid!!
                             DEVICE_TYPE_KEY, deviceTakeUpdateBO.ticketId, keyPair.second?.rfid!!
                         )
                         )
-                    )
+                        handleGiveKey(
+                            DeviceTakeUpdateBO(
+                                DEVICE_TYPE_KEY, deviceTakeUpdateBO.ticketId, keyPair.second?.rfid!!
+                            )
+                        )
+                    }
                 }
                 }
             }
             }
+        } ?: 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)
+                    )
+                )
+            }
+            tipDialog.show()
         }
         }
     }
     }
 
 
@@ -2007,7 +2033,7 @@ object BusinessManager {
                             false, currentModeMsg.bleBean.bleDevice.mac
                             false, currentModeMsg.bleBean.bleDevice.mac
                         )
                         )
                         sendLoadingEventMsg(null, false)
                         sendLoadingEventMsg(null, false)
-                        //连上之后没有工作票要下发就断开,不要占位
+                        //连上之后没有工作票要下发就断开
                         BleManager.getInstance().disconnect(currentModeMsg.bleBean.bleDevice)
                         BleManager.getInstance().disconnect(currentModeMsg.bleBean.bleDevice)
                     }
                     }
                 }
                 }

+ 141 - 35
app/src/main/java/com/grkj/iscs/ble/BleCmdManager.kt

@@ -39,7 +39,7 @@ object BleCmdManager {
     /**
     /**
      * 拼接时间戳 + token
      * 拼接时间戳 + token
      */
      */
-     private fun assembleData(bleBean: BleBean, byteArray: ByteArray): ByteArray? {
+    private fun assembleData(bleBean: BleBean, byteArray: ByteArray): ByteArray? {
         bleBean.token?.let {
         bleBean.token?.let {
             return assembleTimeStamp(byteArray) + it
             return assembleTimeStamp(byteArray) + it
         } ?: run {
         } ?: run {
@@ -80,17 +80,26 @@ object BleCmdManager {
 //        }
 //        }
         when {
         when {
             // 获取令牌
             // 获取令牌
-            byteArray.startsWith(BleConst.RSP_GET_TOKEN) -> handleToken(bleBean.bleDevice, byteArray)
+            byteArray.startsWith(BleConst.RSP_GET_TOKEN) -> handleToken(
+                bleBean.bleDevice,
+                byteArray
+            )
             // 工作模式切换
             // 工作模式切换
             byteArray.startsWith(BleConst.RSP_SWITCH_MODE) -> handleSwitchModeResult(byteArray)
             byteArray.startsWith(BleConst.RSP_SWITCH_MODE) -> handleSwitchModeResult(byteArray)
             // 工作票下发
             // 工作票下发
-            byteArray.startsWith(BleConst.RSP_SEND_WORK_TICKET) -> handleWorkTicketResult(bleBean, byteArray)
+            byteArray.startsWith(BleConst.RSP_SEND_WORK_TICKET) -> handleWorkTicketResult(
+                bleBean,
+                byteArray
+            )
             // 获取设备当前状态
             // 获取设备当前状态
             byteArray.startsWith(BleConst.RSP_CURRENT_STATUS) -> handleCurrentStatus(byteArray)
             byteArray.startsWith(BleConst.RSP_CURRENT_STATUS) -> handleCurrentStatus(byteArray)
             // 获取钥匙电量
             // 获取钥匙电量
             byteArray.startsWith(BleConst.RSP_POWER_STATUS) -> handlePowerStatus(byteArray)
             byteArray.startsWith(BleConst.RSP_POWER_STATUS) -> handlePowerStatus(byteArray)
             // 传输文件
             // 传输文件
-            byteArray.startsWith(BleConst.RSP_TRANSFER_FILE) && byteArray[3] == 0x01.toByte() -> handleFileRsp(bleBean, byteArray)
+            byteArray.startsWith(BleConst.RSP_TRANSFER_FILE) && byteArray[3] == 0x01.toByte() -> handleFileRsp(
+                bleBean,
+                byteArray
+            )
             // 获取固件版本号
             // 获取固件版本号
             byteArray.startsWith(BleConst.RSP_GET_VERSION) -> handleVersion(byteArray)
             byteArray.startsWith(BleConst.RSP_GET_VERSION) -> handleVersion(byteArray)
             // 获取设备工作票完成情况
             // 获取设备工作票完成情况
@@ -106,7 +115,11 @@ object BleCmdManager {
         LogUtil.i("$mac")
         LogUtil.i("$mac")
         BusinessManager.getBleDeviceByMac(mac)?.bleDevice?.let {
         BusinessManager.getBleDeviceByMac(mac)?.bleDevice?.let {
             LogUtil.i("Get token : $mac")
             LogUtil.i("Get token : $mac")
-            BleUtil.instance?.write(it, cmd = assembleTimeStamp(REQ_GET_TOKEN), writeCallback = callback)
+            BleUtil.instance?.write(
+                it,
+                cmd = assembleTimeStamp(REQ_GET_TOKEN),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -115,7 +128,11 @@ object BleCmdManager {
      *
      *
      * @param callBack 是否成功
      * @param callBack 是否成功
      */
      */
-    fun handleToken(bleDevice: BleDevice, byteArray: ByteArray, callBack: ((Boolean) -> Unit)? = null) {
+    fun handleToken(
+        bleDevice: BleDevice,
+        byteArray: ByteArray,
+        callBack: ((Boolean) -> Unit)? = null
+    ) {
         LogUtil.i("handleToken : ${byteArray.toHexStrings()}")
         LogUtil.i("handleToken : ${byteArray.toHexStrings()}")
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
             it.token = byteArrayOf(byteArray[11], byteArray[12], byteArray[13], byteArray[14])
             it.token = byteArrayOf(byteArray[11], byteArray[12], byteArray[13], byteArray[14])
@@ -133,7 +150,11 @@ object BleCmdManager {
      */
      */
     fun switchMode(mode: ByteArray, bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
     fun switchMode(mode: ByteArray, bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_SWITCH_MODE + mode), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_SWITCH_MODE + mode),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -152,7 +173,12 @@ object BleCmdManager {
     /**
     /**
      * 工作票下发
      * 工作票下发
      */
      */
-    fun sendWorkTicket(json: String, idx: Int = 0, bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
+    fun sendWorkTicket(
+        json: String,
+        idx: Int = 0,
+        bleDevice: BleDevice,
+        callback: CustomBleWriteCallback?
+    ) {
         LogUtil.i("sendWorkTicket : $idx")
         LogUtil.i("sendWorkTicket : $idx")
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
             it.ticketSend = json
             it.ticketSend = json
@@ -167,16 +193,30 @@ object BleCmdManager {
             json.toByteArray().copyOfRange(idx * 128, (idx + 1) * 128)
             json.toByteArray().copyOfRange(idx * 128, (idx + 1) * 128)
         }
         }
 //        val jsonInfo = total + idx.toByteArray() + CRC16.crc16(data, 0, data.size - 1).toByteArray() + data.size.toByteArray() + data
 //        val jsonInfo = total + idx.toByteArray() + CRC16.crc16(data, 0, data.size - 1).toByteArray() + data.size.toByteArray() + data
-        val jsonInfo = total + idx.toByteArray() + data.crc16(0, data.size) + data.size.toByteArray() + data
-        LogUtil.d("debug1 : ${total.size} : ${idx.toByteArray().size} : ${data.crc16(0, data.size).size} : ${data.size.toByteArray().size} : ${data.size}")
+        val jsonInfo =
+            total + idx.toByteArray() + data.crc16(0, data.size) + data.size.toByteArray() + data
+        LogUtil.d(
+            "debug1 : ${total.size} : ${idx.toByteArray().size} : ${
+                data.crc16(
+                    0,
+                    data.size
+                ).size
+            } : ${data.size.toByteArray().size} : ${data.size}"
+        )
         LogUtil.d("debug2 : ${(jsonInfo.size + 1).toByteArray(1).size} : ${0x02.toByteArray(1).size} : ${jsonInfo.size}")
         LogUtil.d("debug2 : ${(jsonInfo.size + 1).toByteArray(1).size} : ${0x02.toByteArray(1).size} : ${jsonInfo.size}")
 
 
-        val cmd = REQ_SEND_WORK_TICKET + (jsonInfo.size + 1).toByteArray(1) + 0x02.toByteArray(1) + jsonInfo
+        val cmd =
+            REQ_SEND_WORK_TICKET + (jsonInfo.size + 1).toByteArray(1) + 0x02.toByteArray(1) + jsonInfo
 
 
         LogUtil.d("debug3 : ${cmd.toHexStrings()}")
         LogUtil.d("debug3 : ${cmd.toHexStrings()}")
 
 
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, writeUUID = WRITE_UUID, cmd = assembleData(it, cmd), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                writeUUID = WRITE_UUID,
+                cmd = assembleData(it, cmd),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -186,7 +226,11 @@ object BleCmdManager {
      *
      *
      * @param callBack 是否成功、结果
      * @param callBack 是否成功、结果
      */
      */
-    fun handleWorkTicketResult(bleBean: BleBean, byteArray: ByteArray, callBack: ((Boolean, Byte?) -> Unit)? = null) {
+    fun handleWorkTicketResult(
+        bleBean: BleBean,
+        byteArray: ByteArray,
+        callBack: ((Boolean, Byte?) -> Unit)? = null
+    ) {
         LogUtil.i("handleWorkTicketResult : ${byteArray.toHexStrings()}")
         LogUtil.i("handleWorkTicketResult : ${byteArray.toHexStrings()}")
         val idx = byteArray[4] + byteArray[5]
         val idx = byteArray[4] + byteArray[5]
         val total = byteArray[6] + byteArray[7]
         val total = byteArray[6] + byteArray[7]
@@ -208,7 +252,12 @@ object BleCmdManager {
                     if (res == 0x00.toByte()) idx + 1 else idx,
                     if (res == 0x00.toByte()) idx + 1 else idx,
                     bleBean.bleDevice,
                     bleBean.bleDevice,
                     object : CustomBleWriteCallback() {
                     object : CustomBleWriteCallback() {
-                        override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {}
+                        override fun onWriteSuccess(
+                            current: Int,
+                            total: Int,
+                            justWrite: ByteArray?
+                        ) {
+                        }
 
 
                         override fun onWriteFailure(exception: BleException?) {}
                         override fun onWriteFailure(exception: BleException?) {}
                     })
                     })
@@ -231,7 +280,11 @@ object BleCmdManager {
      */
      */
     fun getCurrentStatus(bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
     fun getCurrentStatus(bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_CURRENT_STATUS), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_CURRENT_STATUS),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -246,9 +299,11 @@ object BleCmdManager {
             0x01.toByte() -> {
             0x01.toByte() -> {
                 LogUtil.i("handleCurrentStatus : 工作模式")
                 LogUtil.i("handleCurrentStatus : 工作模式")
             }
             }
+
             0x02.toByte() -> {
             0x02.toByte() -> {
                 LogUtil.i("handleCurrentStatus : 待机模式")
                 LogUtil.i("handleCurrentStatus : 待机模式")
             }
             }
+
             0x03.toByte() -> {
             0x03.toByte() -> {
                 LogUtil.i("handleCurrentStatus : 故障状态")
                 LogUtil.i("handleCurrentStatus : 故障状态")
             }
             }
@@ -261,14 +316,22 @@ object BleCmdManager {
      */
      */
     fun getTicketStatus(bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
     fun getTicketStatus(bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_WORK_TICKET_RESULT), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_WORK_TICKET_RESULT),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
     /**
     /**
      * 处理工作票完成情况
      * 处理工作票完成情况
      */
      */
-    fun handleTicketStatus(bleDevice: BleDevice, byteArray: ByteArray, callBack: ((String?) -> Unit)? = null) {
+    fun handleTicketStatus(
+        bleDevice: BleDevice,
+        byteArray: ByteArray,
+        callBack: ((String?) -> Unit)? = null
+    ) {
         // TODO 需要有超时重传机制
         // TODO 需要有超时重传机制
         LogUtil.i("handleTicketStatus : ${byteArray.toHexStrings()}")
         LogUtil.i("handleTicketStatus : ${byteArray.toHexStrings()}")
 
 
@@ -283,16 +346,26 @@ object BleCmdManager {
         }
         }
         // TODO 缺少res处理
         // TODO 缺少res处理
         if (idx != total - 1) {
         if (idx != total - 1) {
-            getTicketStatusPart(idx.toByteArray(), total.toByteArray(), byteArrayOf(0x00.toByte()), bleDevice, object : CustomBleWriteCallback() {
-                override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
-                    LogUtil.i("getTicketStatusPart success")
-                }
-
-                override fun onWriteFailure(exception: BleException?) {
-                    LogUtil.e("getTicketStatusPart fail")
-                    BusinessManager.sendEventMsg(MsgEvent(MSG_EVENT_GET_TICKET_STATUS, GetTicketStatusMsg(false, bleDevice)))
-                }
-            })
+            getTicketStatusPart(
+                idx.toByteArray(),
+                total.toByteArray(),
+                byteArrayOf(0x00.toByte()),
+                bleDevice,
+                object : CustomBleWriteCallback() {
+                    override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
+                        LogUtil.i("getTicketStatusPart success")
+                    }
+
+                    override fun onWriteFailure(exception: BleException?) {
+                        LogUtil.e("getTicketStatusPart fail")
+                        BusinessManager.sendEventMsg(
+                            MsgEvent(
+                                MSG_EVENT_GET_TICKET_STATUS,
+                                GetTicketStatusMsg(false, bleDevice)
+                            )
+                        )
+                    }
+                })
         } else {
         } else {
             BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
             BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
                 LogUtil.i("工作票完成接收 : ${String(it.ticketStatus)}")
                 LogUtil.i("工作票完成接收 : ${String(it.ticketStatus)}")
@@ -308,9 +381,19 @@ object BleCmdManager {
     /**
     /**
      * 获取工作票完成情况分包
      * 获取工作票完成情况分包
      */
      */
-    private fun getTicketStatusPart(idx: ByteArray, total: ByteArray, res: ByteArray, bleDevice: BleDevice, callback: CustomBleWriteCallback?) {
+    private fun getTicketStatusPart(
+        idx: ByteArray,
+        total: ByteArray,
+        res: ByteArray,
+        bleDevice: BleDevice,
+        callback: CustomBleWriteCallback?
+    ) {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
         BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_WORK_TICKET_RESULT_PART + idx + total + res), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_WORK_TICKET_RESULT_PART + idx + total + res),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -319,7 +402,11 @@ object BleCmdManager {
      */
      */
     fun getPower(mac: String?, callback: CustomBleWriteCallback?) {
     fun getPower(mac: String?, callback: CustomBleWriteCallback?) {
         BusinessManager.getBleDeviceByMac(mac)?.let {
         BusinessManager.getBleDeviceByMac(mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_POWER_STATUS), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_POWER_STATUS),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 
@@ -337,9 +424,11 @@ object BleCmdManager {
             0x01.toByte() -> {
             0x01.toByte() -> {
                 LogUtil.i("充电状态:未充电")
                 LogUtil.i("充电状态:未充电")
             }
             }
+
             0x02.toByte() -> {
             0x02.toByte() -> {
                 LogUtil.i("充电状态:充电中")
                 LogUtil.i("充电状态:充电中")
             }
             }
+
             0x03.toByte() -> {
             0x03.toByte() -> {
                 LogUtil.i("充电状态:充满")
                 LogUtil.i("充电状态:充满")
             }
             }
@@ -358,7 +447,13 @@ object BleCmdManager {
      * PGSZ:当前包长度(字节)
      * PGSZ:当前包长度(字节)
      * PGDATA:当前包数据
      * PGDATA:当前包数据
      */
      */
-    fun sendFile(type: Int, file: File, idx: Int = 0, mac: String?, callback: CustomBleWriteCallback?) {
+    fun sendFile(
+        type: Int,
+        file: File,
+        idx: Int = 0,
+        mac: String?,
+        callback: CustomBleWriteCallback?
+    ) {
         Executor.runOnIO {
         Executor.runOnIO {
             LogUtil.i("sendFile : $idx")
             LogUtil.i("sendFile : $idx")
 
 
@@ -383,17 +478,24 @@ object BleCmdManager {
             val pgsz = pgdata.size.toByteArray()
             val pgsz = pgdata.size.toByteArray()
             val pgcrc = pgdata.crc16()
             val pgcrc = pgdata.crc16()
 
 
-            val fileInfo = byteArrayOf(type.toByte()) + flnm + flsz + flcrc + pgtotal.toByteArray() + idx.toByteArray() + pgcrc + pgsz + pgdata
+            val fileInfo =
+                byteArrayOf(type.toByte()) + flnm + flsz + flcrc + pgtotal.toByteArray() + idx.toByteArray() + pgcrc + pgsz + pgdata
 //        println("______________________________________________________________________________")
 //        println("______________________________________________________________________________")
 //        println("${file.readBytes().size}")
 //        println("${file.readBytes().size}")
 //        println("${byteArrayOf(type.toByte()).size} : ${flnm.size} : ${flsz.size} : ${flcrc.size} : ${pgtotal.toByteArray().size} : ${idx.toByteArray().size} : ${pgcrc.size} : ${pgsz.size} : ${pgdata.size}")
 //        println("${byteArrayOf(type.toByte()).size} : ${flnm.size} : ${flsz.size} : ${flcrc.size} : ${pgtotal.toByteArray().size} : ${idx.toByteArray().size} : ${pgcrc.size} : ${pgsz.size} : ${pgdata.size}")
 //        println("______________________________________________________________________________")
 //        println("______________________________________________________________________________")
 //        println("file info size : ${fileInfo.size}")
 //        println("file info size : ${fileInfo.size}")
-            val cmd = REQ_TRANSFER_FILE + (fileInfo.size + 1).toByteArray(1) + 0x01.toByteArray(1) + fileInfo
+            val cmd =
+                REQ_TRANSFER_FILE + (fileInfo.size + 1).toByteArray(1) + 0x01.toByteArray(1) + fileInfo
 //        println("cmd size : ${REQ_TRANSFER_FILE.size} : ${(fileInfo.size + 1).toByteArray(1).size} : ${0x01.toByteArray(1).size} : ${fileInfo.size}")
 //        println("cmd size : ${REQ_TRANSFER_FILE.size} : ${(fileInfo.size + 1).toByteArray(1).size} : ${0x01.toByteArray(1).size} : ${fileInfo.size}")
 
 
             BusinessManager.getBleDeviceByMac(mac)?.let {
             BusinessManager.getBleDeviceByMac(mac)?.let {
-                BleUtil.instance?.write(it.bleDevice, writeUUID = WRITE_UUID, cmd = assembleData(it, cmd), writeCallback = callback)
+                BleUtil.instance?.write(
+                    it.bleDevice,
+                    writeUUID = WRITE_UUID,
+                    cmd = assembleData(it, cmd),
+                    writeCallback = callback
+                )
             }
             }
             Thread.sleep(50)
             Thread.sleep(50)
             sendFile(type, file, idx + 1, mac, callback)
             sendFile(type, file, idx + 1, mac, callback)
@@ -420,7 +522,11 @@ object BleCmdManager {
      */
      */
     fun getVersion(mac: String?, callback: CustomBleWriteCallback?) {
     fun getVersion(mac: String?, callback: CustomBleWriteCallback?) {
         BusinessManager.getBleDeviceByMac(mac)?.let {
         BusinessManager.getBleDeviceByMac(mac)?.let {
-            BleUtil.instance?.write(it.bleDevice, cmd = assembleData(it, REQ_GET_VERSION), writeCallback = callback)
+            BleUtil.instance?.write(
+                it.bleDevice,
+                cmd = assembleData(it, REQ_GET_VERSION),
+                writeCallback = callback
+            )
         }
         }
     }
     }
 
 

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

@@ -903,7 +903,8 @@ object ModBusController {
      */
      */
     suspend fun getOneKey(
     suspend fun getOneKey(
         exceptionSlots: List<CabinetSlotsRecord>,
         exceptionSlots: List<CabinetSlotsRecord>,
-        exceptionKeys: List<String>
+        exceptionKeysRfid: List<String>,
+        exceptionKeysMac: List<String> = mutableListOf(),
     ): Pair<Byte, DockBean.KeyBean?>? {
     ): Pair<Byte, DockBean.KeyBean?>? {
         // 1. 过滤并准备钥匙列表
         // 1. 过滤并准备钥匙列表
         val slotCols = exceptionSlots.mapNotNull { it.col?.toInt() }
         val slotCols = exceptionSlots.mapNotNull { it.col?.toInt() }
@@ -918,7 +919,8 @@ object ModBusController {
             .filterIndexed { idx, _ -> (idx + 1) !in slotCols }
             .filterIndexed { idx, _ -> (idx + 1) !in slotCols }
             .filter { kb ->
             .filter { kb ->
                 !kb.rfid.isNullOrEmpty()
                 !kb.rfid.isNullOrEmpty()
-                        && kb.rfid !in exceptionKeys
+                        && kb.rfid !in exceptionKeysRfid
+                        && kb.mac !in exceptionKeysMac
                         && !kb.mac.isNullOrEmpty()
                         && !kb.mac.isNullOrEmpty()
                         && kb.isExist
                         && kb.isExist
             }
             }

+ 26 - 66
app/src/main/java/com/grkj/iscs/view/fragment/DeviceStatusFragment.kt

@@ -255,19 +255,11 @@ class DeviceStatusFragment :
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                 }
                                 }
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_1) {
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_1) {
-                                    if (presenter?.loginUser?.roleKeyList?.any { it == USER_ROLE_ADMHDWTESTER } == true) {
-                                        presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
-                                            presenter?.mExceptionRecovery?.invoke(
-                                                (row.row + 1),
-                                                1,
-                                                it.toInt()
-                                            )
-                                        }
-                                    } else {
-                                        ToastUtils.tip(
-                                            MyApplication.instance?.applicationContext!!.getString(
-                                                R.string.no_permission
-                                            )
+                                    presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
+                                        presenter?.mExceptionRecovery?.invoke(
+                                            row.row,
+                                            1,
+                                            it.toInt()
                                         )
                                         )
                                     }
                                     }
                                     true
                                     true
@@ -286,19 +278,11 @@ class DeviceStatusFragment :
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                 }
                                 }
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_2) {
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_2) {
-                                    if (presenter?.loginUser?.roleKeyList?.any { it == USER_ROLE_ADMHDWTESTER } == true) {
-                                        presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
-                                            presenter?.mExceptionRecovery?.invoke(
-                                                (row.row + 1),
-                                                2,
-                                                it.toInt()
-                                            )
-                                        }
-                                    } else {
-                                        ToastUtils.tip(
-                                            MyApplication.instance?.applicationContext!!.getString(
-                                                R.string.no_permission
-                                            )
+                                    presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
+                                        presenter?.mExceptionRecovery?.invoke(
+                                            row.row,
+                                            2,
+                                            it.toInt()
                                         )
                                         )
                                     }
                                     }
                                     true
                                     true
@@ -317,19 +301,11 @@ class DeviceStatusFragment :
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                 }
                                 }
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_3) {
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_3) {
-                                    if (presenter?.loginUser?.roleKeyList?.any { it == USER_ROLE_ADMHDWTESTER } == true) {
-                                        presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
-                                            presenter?.mExceptionRecovery?.invoke(
-                                                (row.row + 1),
-                                                3,
-                                                it.toInt()
-                                            )
-                                        }
-                                    } else {
-                                        ToastUtils.tip(
-                                            MyApplication.instance?.applicationContext!!.getString(
-                                                R.string.no_permission
-                                            )
+                                    presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
+                                        presenter?.mExceptionRecovery?.invoke(
+                                            row.row,
+                                            3,
+                                            it.toInt()
                                         )
                                         )
                                     }
                                     }
                                     true
                                     true
@@ -348,19 +324,11 @@ class DeviceStatusFragment :
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                 }
                                 }
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_4) {
                                 holder?.setOnLongClickListener(R.id.iv_key_exception_4) {
-                                    if (presenter?.loginUser?.roleKeyList?.any { it == USER_ROLE_ADMHDWTESTER } == true) {
-                                        presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
-                                            presenter?.mExceptionRecovery?.invoke(
-                                                (row.row + 1),
-                                                4,
-                                                it.toInt()
-                                            )
-                                        }
-                                    } else {
-                                        ToastUtils.tip(
-                                            MyApplication.instance?.applicationContext!!.getString(
-                                                R.string.no_permission
-                                            )
+                                    presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
+                                        presenter?.mExceptionRecovery?.invoke(
+                                            row.row,
+                                            4,
+                                            it.toInt()
                                         )
                                         )
                                     }
                                     }
                                     true
                                     true
@@ -372,7 +340,7 @@ class DeviceStatusFragment :
             holder?.setOnLongClickListener(R.id.iv_key_1) {
             holder?.setOnLongClickListener(R.id.iv_key_1) {
                 presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
                 presenter?.slotType?.find { it.dictLabel == "钥匙" }?.dictValue?.let {
                     presenter?.mExceptionReporter?.invoke(
                     presenter?.mExceptionReporter?.invoke(
-                        (row.row + 1),
+                        row.row,
                         1,
                         1,
                         it.toInt()
                         it.toInt()
                     )
                     )
@@ -472,19 +440,11 @@ class DeviceStatusFragment :
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                     presenter?.exceptionHint(cabinetSlotsRecord.remark ?: "")
                                 }
                                 }
                                 holder?.setOnLongClickListener(R.id.iv_lock_exception) {
                                 holder?.setOnLongClickListener(R.id.iv_lock_exception) {
-                                    if (presenter?.loginUser?.roleKeyList?.any { it == USER_ROLE_ADMHDWTESTER } == true) {
-                                        presenter?.slotType?.find { it.dictLabel == "锁" }?.dictValue?.let {
-                                            presenter?.mExceptionRecovery?.invoke(
-                                                row.row,
-                                                (lockIdx + 1),
-                                                it.toInt()
-                                            )
-                                        }
-                                    } else {
-                                        ToastUtils.tip(
-                                            MyApplication.instance?.applicationContext!!.getString(
-                                                R.string.no_permission
-                                            )
+                                    presenter?.slotType?.find { it.dictLabel == "锁" }?.dictValue?.let {
+                                        presenter?.mExceptionRecovery?.invoke(
+                                            row.row,
+                                            (lockIdx + 1),
+                                            it.toInt()
                                         )
                                         )
                                     }
                                     }
                                     true
                                     true

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

@@ -352,4 +352,5 @@
     <string name="recovery_failed">recovery failed</string>
     <string name="recovery_failed">recovery failed</string>
     <string name="no_permission">no permission</string>
     <string name="no_permission">no permission</string>
     <string name="exception_select_hardware_tip">Please select hardware</string>
     <string name="exception_select_hardware_tip">Please select hardware</string>
+    <string name="check_key_and_lock">check key and lock</string>
 </resources>
 </resources>

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

@@ -352,4 +352,5 @@
     <string name="recovery_failed">恢复失败</string>
     <string name="recovery_failed">恢复失败</string>
     <string name="no_permission">权限不足</string>
     <string name="no_permission">权限不足</string>
     <string name="exception_select_hardware_tip">请选择硬件</string>
     <string name="exception_select_hardware_tip">请选择硬件</string>
+    <string name="check_key_and_lock">正在检查钥匙和挂锁</string>
 </resources>
 </resources>

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

@@ -352,4 +352,5 @@
     <string name="recovery_failed">恢复失败</string>
     <string name="recovery_failed">恢复失败</string>
     <string name="no_permission">权限不足</string>
     <string name="no_permission">权限不足</string>
     <string name="exception_select_hardware_tip">请选择硬件</string>
     <string name="exception_select_hardware_tip">请选择硬件</string>
+    <string name="check_key_and_lock">正在检查钥匙和挂锁</string>
 </resources>
 </resources>