Просмотр исходного кода

补充打印;修复livedata的postvalue造成的数据丢失

Frankensteinly 1 год назад
Родитель
Сommit
167f6d782b

+ 44 - 35
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -144,7 +144,7 @@ object BusinessManager {
                                 // 打开钥匙卡扣
                                 val keyBean = ModBusController.getKeyByMac(it.data.bleBean.bleDevice.mac)
                                 if (keyBean == null) {
-                                    mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "未找到钥匙信息", false)))
+                                    sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "未找到钥匙信息", false)))
                                     ToastUtils.tip(R.string.key_not_exists)
                                 } else {
                                     sendLoadingEventMsg(CommonUtils.getStr(R.string.take_out_key_tip))
@@ -269,7 +269,7 @@ object BusinessManager {
                                 keyBean.mac?.let {
                                     unregisterConnectListener(it)
                                 }
-                                mEventBus.postValue(MsgEvent(MSG_EVENT_DEVICE_TAKE_UPDATE, DeviceTakeUpdateMsg(DEVICE_TYPE_KEY, keyBean.rfid)))
+                                sendEventMsg(MsgEvent(MSG_EVENT_DEVICE_TAKE_UPDATE, DeviceTakeUpdateMsg(DEVICE_TYPE_KEY, keyBean.rfid)))
                             }
                         }
                     }
@@ -296,7 +296,7 @@ object BusinessManager {
                                     }
                                 }
                             } else {
-                                mEventBus.postValue(MsgEvent(MSG_EVENT_DEVICE_TAKE_UPDATE, DeviceTakeUpdateMsg(DEVICE_TYPE_LOCK, lockBean.rfid)))
+                                sendEventMsg(MsgEvent(MSG_EVENT_DEVICE_TAKE_UPDATE, DeviceTakeUpdateMsg(DEVICE_TYPE_LOCK, lockBean.rfid)))
                             }
                         }
                     }
@@ -628,7 +628,7 @@ object BusinessManager {
             LogUtil.e("Prepare is canceled : $mac")
             return
         }
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "正在扫描设备...", null)))
+        if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "正在扫描设备...", null)))
         BleUtil.instance?.scan(object : CustomBleScanCallback() {
             override fun onPrompt(promptStr: String?) {
                 // 蓝牙未启动重试
@@ -639,7 +639,7 @@ object BusinessManager {
             override fun onScanStarted(success: Boolean) {
                 LogUtil.i("onScanStarted:${success}")
                 if (!success) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
                     prepareDoneCallBack?.invoke(false, null)
                 }
             }
@@ -653,7 +653,7 @@ object BusinessManager {
 
             override fun onScanFinished(scanResultList: MutableList<BleDevice>?) {
                 LogUtil.i("onScanFinished: $mac")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
                 // 没有扫描到
                 if (scanResultList?.none { it.mac == mac } == true) {
                     LogUtil.w("$mac is not scanned")
@@ -675,24 +675,24 @@ object BusinessManager {
             LogUtil.e("Prepare is canceled : ${bleDevice.mac}")
             return
         }
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, CommonUtils.getStr(R.string.ble_connecting), null)))
+        if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, CommonUtils.getStr(R.string.ble_connecting), null)))
         BleManager.getInstance().disconnect(bleDevice)
         BleUtil.instance?.connectBySelect(bleDevice,
             object : CustomBleGattCallback() {
                 override fun onPrompt(promptStr: String?) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, promptStr, null)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, promptStr, null)))
                 }
 
                 override fun onStartConnect() {}
 
                 override fun onConnectFail(bleDevice: BleDevice?, exception: BleException?) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, CommonUtils.getStr(R.string.ble_connect_fail), false)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, CommonUtils.getStr(R.string.ble_connect_fail), false)))
                     LogUtil.e("onConnectFail : ${bleDevice?.mac} - ${exception?.description}")
                     prepareDoneCallBack?.invoke(false, null)
                 }
 
                 override fun onConnectSuccess(bleDevice: BleDevice?, gatt: BluetoothGatt?, status: Int) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
                     LogUtil.i("onConnectSuccess : ${bleDevice?.mac}")
                     bleDevice?.let {
                         deviceList.removeIf { it.bleDevice.mac == bleDevice.mac }
@@ -714,7 +714,7 @@ object BusinessManager {
                 }
 
                 override fun onDisConnected(isActiveDisConnected: Boolean, device: BleDevice?, gatt: BluetoothGatt?, status: Int) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
                     LogUtil.i("onDisConnected : ${device?.mac} - $isActiveDisConnected")
                     getBleDeviceByMac(device?.mac)?.let {
                         deviceList.remove(it)
@@ -750,7 +750,7 @@ object BusinessManager {
             LogUtil.e("Prepare is canceled : ${bleBean?.bleDevice?.mac}")
             return
         }
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始监听...", null)))
+        if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始监听...", null)))
         bleBean?.let {
             var isIndicateSuccess = false
             BleUtil.instance?.indicate(it.bleDevice, indicateCallback = object : CustomBleIndicateCallback() {
@@ -775,7 +775,7 @@ object BusinessManager {
                 }
 
                 override fun onIndicateFailure(exception: BleException?) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
                     LogUtil.e("onIndicateFailure : ${bleBean.bleDevice.mac} - ${exception?.description}")
                     Executor.delayOnIO(500) {
                         if (isIndicateSuccess) {
@@ -807,16 +807,16 @@ object BusinessManager {
             LogUtil.e("Prepare is canceled : ${bleBean?.bleDevice?.mac}")
             return
         }
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始获取token...", null)))
+        if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始获取token...", null)))
         bleBean?.let {
             BleCmdManager.getToken(it.bleDevice.mac, object : CustomBleWriteCallback() {
                 override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "token获取成功", null)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "token获取成功", null)))
                     LogUtil.i("getToken success : ${bleBean.bleDevice.mac}")
                 }
 
                 override fun onWriteFailure(exception: BleException?) {
-                    if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "token获取失败", false)))
+                    if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "token获取失败", false)))
                     LogUtil.e("getToken fail : ${bleBean.bleDevice.mac}")
                     prepareDoneCallBack?.invoke(false, null)
                 }
@@ -909,7 +909,7 @@ object BusinessManager {
                     getTicketStatusWithRetry(bleBean!!.bleDevice, isNeedLoading)
                 }
             } else {
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, false)))
             }
         }
     }
@@ -939,16 +939,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, CommonUtils.getStr(R.string.start_to_send_ticket), null)))
+        if (isNeedLoading) sendEventMsg(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, CommonUtils.getStr(R.string.sending_ticket), null)))
+                if (isNeedLoading) sendEventMsg(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, CommonUtils.getStr(R.string.send_ticket_fail), null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, CommonUtils.getStr(R.string.send_ticket_fail), null)))
                 processCallback?.invoke(false)
             }
         })
@@ -1032,14 +1032,14 @@ object BusinessManager {
             // 工作模式切换
             byteArray.startsWith(BleConst.RSP_SWITCH_MODE) -> {
                 handleSwitchModeResult(byteArray, isNeedLoading) { res, job ->
-                    mEventBus.postValue(MsgEvent(MSG_EVENT_SWITCH_MODE, SwitchModeMsg(job.toInt(), res.toInt(), bleBean)))
+                    sendEventMsg(MsgEvent(MSG_EVENT_SWITCH_MODE, SwitchModeMsg(job.toInt(), res.toInt(), bleBean)))
                 }
             }
             // 工作票下发
             byteArray.startsWith(BleConst.RSP_SEND_WORK_TICKET) -> handleWorkTicketResult(bleBean, byteArray, isNeedLoading)
             // 获取设备当前状态
             byteArray.startsWith(BleConst.RSP_CURRENT_STATUS) -> BleCmdManager.handleCurrentStatus(byteArray) {
-                mEventBus.postValue(MsgEvent(MSG_EVENT_CURRENT_MODE, CurrentModeMsg(bleBean, it)))
+                sendEventMsg(MsgEvent(MSG_EVENT_CURRENT_MODE, CurrentModeMsg(bleBean, it)))
             }
             // 获取设备工作票完成情况
             byteArray.startsWith(BleConst.RSP_WORK_TICKET_RESULT) && byteArray[3] == 0x02.toByte() ->
@@ -1056,13 +1056,13 @@ object BusinessManager {
         BleCmdManager.handleSwitchModeResult(byteArray) { job, res ->
             if (res == 0x01.toByte() && job == 0x01.toByte()) {
                 LogUtil.i("切换工作模式成功")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "切换工作模式成功", null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "切换工作模式成功", null)))
             } else if (res == 0x01.toByte() && job == 0x02.toByte()) {
                 LogUtil.i("切换待机模式成功")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "切换待机模式成功", null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "切换待机模式成功", null)))
             } else {
                 LogUtil.e("切换模式失败 : ${job.toInt()} - ${res.toInt()}")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
             }
             callBack?.invoke(res, job)
         }
@@ -1074,11 +1074,11 @@ object BusinessManager {
      */
     private fun handleWorkTicketResult(bleBean: BleBean, byteArray: ByteArray, isNeedLoading: Boolean = false) {
         BleCmdManager.handleWorkTicketResult(bleBean, byteArray) { isSuccess, rst ->
-            if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
+            if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, null, null)))
             if (isSuccess) {
                 // 下发完毕,切换工作模式
                 LogUtil.i("工作票下发完毕")
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "切换钥匙为工作模式", null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "切换钥匙为工作模式", null)))
 
                 Executor.delayOnIO(800) {
                     switchWorkMode(bleBean.bleDevice, isNeedLoading)
@@ -1141,10 +1141,10 @@ object BusinessManager {
             if (ticketJson.isNullOrEmpty()) {
                 return@handleTicketStatus
             }
-            if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票完成状态读取完成", null)))
+            if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票完成状态读取完成", null)))
             LogUtil.i("Get ticket status complete : ${bleDevice.mac}")
             // TD:Ticket Done
-            if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "TD$ticketJson}", true)))
+            if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "TD$ticketJson}", true)))
 
             val workTicketGetBO = Gson().fromJson(ticketJson, WorkTicketGetBO::class.java)
 
@@ -1201,7 +1201,7 @@ object BusinessManager {
                 NetApi.updateLockPointBatch(updateList) { isSuccess ->
                     if (isSuccess) {
                         data.taskCode?.toLong()?.let {
-                            mEventBus.postValue(MsgEvent(MSG_EVENT_UPDATE_TICKET_PROGRESS, UpdateTicketProgressMsg(it)))
+                            sendEventMsg(MsgEvent(MSG_EVENT_UPDATE_TICKET_PROGRESS, UpdateTicketProgressMsg(it)))
                         }
                         // 确认归还,切换为待机模式
                         switchReadyMode(bleDevice)
@@ -1244,13 +1244,13 @@ object BusinessManager {
      * 获取工作票完成情况
      */
     private fun getTicketStatus(bleDevice: BleDevice, isNeedLoading: Boolean = false, processCallback: ((Boolean) -> Unit)? = null) {
-        if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始获取工作票", null)))
+        if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(true, "开始获取工作票", null)))
         BleCmdManager.getTicketStatus(bleDevice, object : CustomBleWriteCallback() {
             override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票获取成功", null)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票获取成功", null)))
                 LogUtil.i("getTicketStatus success")}
             override fun onWriteFailure(exception: BleException?) {
-                if (isNeedLoading) mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票获取失败", false)))
+                if (isNeedLoading) sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(false, "工作票获取失败", false)))
                 processCallback?.invoke(false)
                 LogUtil.e("getTicketStatus fail")}
         })
@@ -1275,7 +1275,7 @@ object BusinessManager {
                     NetApi.updateKeyTake(info.ticketId, info.nfc, MyApplication.instance?.serialNo()!!) { isSuccess ->
                         if (isSuccess) {
                             mDeviceTakeList.removeIf { it.deviceType == DEVICE_TYPE_KEY && it.nfc == info.nfc }
-                            mEventBus.postValue(MsgEvent(MSG_EVENT_UPDATE_TICKET_PROGRESS, UpdateTicketProgressMsg(info.ticketId)))
+                            sendEventMsg(MsgEvent(MSG_EVENT_UPDATE_TICKET_PROGRESS, UpdateTicketProgressMsg(info.ticketId)))
                         }
                     }
                 } ?: sendLoadingEventMsg(null, false)
@@ -1433,7 +1433,7 @@ object BusinessManager {
     }
 
     fun sendLoadingEventMsg(str: String?, isShow: Boolean = true) {
-        mEventBus.postValue(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(isShow, str, false)))
+        sendEventMsg(MsgEvent(MSG_EVENT_LOADING, LoadingMsg(isShow, str, false)))
     }
 
     fun logout(context: Context) {
@@ -1485,4 +1485,13 @@ object BusinessManager {
             }
         }
     }
+
+    /**
+     * 强制使用setValue,防止postValue造成数据丢失
+     */
+    fun sendEventMsg(msgEvent: MsgEvent) {
+        Executor.runOnMain {
+            mEventBus.value = msgEvent
+        }
+    }
 }

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

@@ -3,7 +3,6 @@ package com.grkj.iscs.ble
 import com.clj.fastble.data.BleDevice
 import com.clj.fastble.exception.BleException
 import com.grkj.iscs.BusinessManager
-import com.grkj.iscs.BusinessManager.mEventBus
 import com.grkj.iscs.ble.BleConst.REQ_CURRENT_STATUS
 import com.grkj.iscs.ble.BleConst.REQ_GET_TOKEN
 import com.grkj.iscs.ble.BleConst.REQ_GET_VERSION
@@ -291,7 +290,7 @@ object BleCmdManager {
 
                 override fun onWriteFailure(exception: BleException?) {
                     LogUtil.e("getTicketStatusPart fail")
-                    mEventBus.postValue(MsgEvent(MSG_EVENT_GET_TICKET_STATUS, GetTicketStatusMsg(false, bleDevice)))
+                    BusinessManager.sendEventMsg(MsgEvent(MSG_EVENT_GET_TICKET_STATUS, GetTicketStatusMsg(false, bleDevice)))
                 }
             })
         } else {

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

@@ -252,7 +252,7 @@ class DockBean(
         var isReady: Boolean = false    // 钥匙是否准备好(连接上且为待机模式)
     ) : DeviceBean(DEVICE_TYPE_KEY, idx, isExist) {
         override fun toString(): String {
-            return "KeyBean(isLeft=$isLeft, isCharging=$isCharging, rfid=$rfid, mac=$mac, isReady=$isReady)"
+            return "KeyBean(isLeft=$isLeft, isCharging=$isCharging, rfid=$rfid, mac=$mac, isReady=$isReady, idx=$idx, isExist=$isExist)"
         }
 
         fun clone(): KeyBean {
@@ -271,7 +271,7 @@ class DockBean(
         var rfid: String?
     ) : DeviceBean(DEVICE_TYPE_LOCK, idx, isExist) {
         override fun toString(): String {
-            return "LockBean(rfid=$rfid)"
+            return "LockBean(rfid=$rfid, idx=$idx, isExist=$isExist)"
         }
 
         fun clone(): LockBean {