Ver código fonte

钥匙初始化需要切成待机模式,放入钥匙的时候需要根据当前模式不同处理;补充打印

Frankensteinly 8 meses atrás
pai
commit
749c393ef9

+ 54 - 9
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -37,10 +37,12 @@ import com.grkj.iscs.model.bo.DeviceTakeUpdateBO
 import com.grkj.iscs.model.bo.WorkTicketGetBO
 import com.grkj.iscs.model.bo.WorkTicketSendBO
 import com.grkj.iscs.model.bo.WorkTicketSendBO.LockListBO
+import com.grkj.iscs.model.eventmsg.CurrentModeMsg
 import com.grkj.iscs.model.eventmsg.DeviceTakeUpdateMsg
 import com.grkj.iscs.model.eventmsg.GetTicketStatusMsg
 import com.grkj.iscs.model.eventmsg.LoadingMsg
 import com.grkj.iscs.model.eventmsg.MsgEvent
+import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_CURRENT_MODE
 import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_DEVICE_TAKE_UPDATE
 import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_GET_TICKET_STATUS
 import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_LOADING
@@ -103,16 +105,23 @@ object BusinessManager {
      */
     fun initMsgEventBus() {
         mEventBus.observeForever {
+            LogUtil.i("msgEvent : $it")
             when (it.code) {
+                // loading消息
                 MSG_EVENT_LOADING -> {
                     Executor.runOnMain {
                         val loadingMsg = it.data as LoadingMsg
                         (ActivityUtils.currentActivity() as BaseActivity<*>).handleLoading(loadingMsg.isShow, loadingMsg.loadingText)
                     }
                 }
+                // 设备取出
                 MSG_EVENT_DEVICE_TAKE_UPDATE -> {
                     handleDeviceTake(it.data as DeviceTakeUpdateMsg)
                 }
+                // 钥匙当前模式
+                MSG_EVENT_CURRENT_MODE -> {
+                    handleCurrentMode(it.data as CurrentModeMsg)
+                }
             }
         }
     }
@@ -244,7 +253,7 @@ object BusinessManager {
                                             NetApi.getKeyInfo(rfid) {
                                                 if (it != null && !it.macAddress.isNullOrEmpty()) {
                                                     ModBusController.updateKeyMac(dockBean.addr, isLeft, it.macAddress)
-                                                    showKeyReturnDialog(it.macAddress, isLeft, dockBean.addr)
+//                                                    showKeyReturnDialog(it.macAddress, isLeft, dockBean.addr)
                                                 } else {
                                                     ToastUtils.tip(R.string.get_key_info_fail)
                                                 }
@@ -304,13 +313,13 @@ object BusinessManager {
      * 钥匙归还提示确认弹框
      */
     private fun showKeyReturnDialog(mac: String, isLeft: Boolean, slaveAddress: Byte) {
-        val dlg = TipDialog(MyApplication.instance!!.applicationContext)
-        dlg.setTip(MyApplication.instance!!.getString(R.string.key_return_tip))
-        dlg.setType(TipDialog.TYPE_CONFIRM)
-        dlg.setConfirmListener {
-            ModBusController.controlKeyBuckle(false, isLeft, slaveAddress)
-            getTicketStatusBusiness(mac, true)
-        }
+//        val dlg = TipDialog(MyApplication.instance!!.applicationContext)
+//        dlg.setTip(MyApplication.instance!!.getString(R.string.key_return_tip))
+//        dlg.setType(TipDialog.TYPE_CONFIRM)
+//        dlg.setConfirmListener {
+//            ModBusController.controlKeyBuckle(false, isLeft, slaveAddress)
+//            getTicketStatusBusiness(mac, true)
+//        }
     }
 
     fun readLockBuckleStatus() {
@@ -422,7 +431,9 @@ object BusinessManager {
         if (deviceList.none { it.bleDevice.mac == mac }) {
             connectListeners.add(ConnectListener(mac, callBack))
             if (connectListeners.size == 1) {
-                connectKey()
+                Executor.runOnMain {
+                    connectKey()
+                }
             }
         } else {
             callBack?.invoke(true, deviceList.find { it.bleDevice.mac == mac })
@@ -864,6 +875,10 @@ object BusinessManager {
             byteArray.startsWith(BleConst.RSP_SWITCH_MODE) -> handleSwitchModeResult(byteArray, isNeedLoading)
             // 工作票下发
             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)))
+            }
             // 获取设备工作票完成情况
             byteArray.startsWith(BleConst.RSP_WORK_TICKET_RESULT) && byteArray[3] == 0x02.toByte() ->
                 handleTicketStatus(bleBean.bleDevice, byteArray, isNeedLoading)
@@ -909,6 +924,21 @@ object BusinessManager {
         }
     }
 
+    fun getCurrentStatus(bleDevice: BleDevice) {
+        BleCmdManager.getCurrentStatus(bleDevice, object : CustomBleWriteCallback() {
+            override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
+                LogUtil.i("getCurrentStatus success")
+            }
+
+            override fun onWriteFailure(exception: BleException?) {
+                LogUtil.i("getCurrentStatus fail")
+                Executor.delayOnMain(500) {
+                    getCurrentStatus(bleDevice)
+                }
+            }
+        })
+    }
+
     private fun switchWorkMode(bleDevice: BleDevice, isNeedLoading: Boolean = false) {
         LogUtil.i("switchWorkMode - ${bleDevice.mac}")
         BleCmdManager.switchMode(STATUS_WORK, bleDevice, object : CustomBleWriteCallback() {
@@ -1046,4 +1076,19 @@ object BusinessManager {
             }
         }
     }
+
+    private fun handleCurrentMode(currentModeMsg: CurrentModeMsg) {
+        when (currentModeMsg.mode) {
+            0x01.toByte() -> {
+                // TODO 读工作票
+                getTicketStatusBusiness(currentModeMsg.bleBean.bleDevice.mac)
+            }
+            0x02.toByte() -> {
+                // 暂无
+            }
+            0x03.toByte() -> {
+                // TODO 上报?
+            }
+        }
+    }
 }

+ 5 - 4
app/src/main/java/com/grkj/iscs/ble/BleCmdManager.kt

@@ -235,20 +235,21 @@ object BleCmdManager {
      * 处理设备当前状态
      * 0x01:工作模式 0x02:待机模式 0x03:故障状态
      */
-    private fun handleCurrentStatus(byteArray: ByteArray) {
+    fun handleCurrentStatus(byteArray: ByteArray, callBack: ((Byte) -> Unit)? = null) {
         LogUtil.i("handleCurrentStatus : ${byteArray.toHexStrings()}")
         val job = byteArray[4]
         when (job) {
             0x01.toByte() -> {
-                println("工作模式")
+                LogUtil.i("handleCurrentStatus : 工作模式")
             }
             0x02.toByte() -> {
-                println("待机模式")
+                LogUtil.i("handleCurrentStatus : 待机模式")
             }
             0x03.toByte() -> {
-                println("故障状态")
+                LogUtil.i("handleCurrentStatus : 故障状态")
             }
         }
+        callBack?.invoke(job)
     }
 
     /**

+ 7 - 1
app/src/main/java/com/grkj/iscs/modbus/ModBusController.kt

@@ -201,7 +201,13 @@ object ModBusController {
                             if (it != null && !it.macAddress.isNullOrEmpty()) {
                                 // 更新mac
                                 updateKeyMac(dockBean.addr, key.isLeft, it.macAddress)
-                                BusinessManager.registerConnectListener(it.macAddress)
+                                BusinessManager.registerConnectListener(it.macAddress) { isDone, bleBean ->
+                                    Executor.delayOnMain(500) {
+                                        bleBean?.let {
+                                            BusinessManager.getCurrentStatus(bleBean.bleDevice)
+                                        }
+                                    }
+                                }
                             } else {
                                 ToastUtils.tip(R.string.get_key_info_fail)
                             }

+ 8 - 0
app/src/main/java/com/grkj/iscs/model/eventmsg/CurrentModeMsg.kt

@@ -0,0 +1,8 @@
+package com.grkj.iscs.model.eventmsg
+
+import com.grkj.iscs.ble.BleBean
+
+data class CurrentModeMsg(
+    val bleBean: BleBean,
+    val mode: Byte
+)

+ 3 - 0
app/src/main/java/com/grkj/iscs/model/eventmsg/MsgEventConstants.kt

@@ -10,4 +10,7 @@ object MsgEventConstants {
 
     // ------------------------------ 作业票 1-003-000 ------------------------------
     const val MSG_EVENT_GET_TICKET_STATUS = 1_003_000   // 获取设备工作票完成情况
+
+    // ------------------------------ 蓝牙 1-004-000 ------------------------------
+    const val MSG_EVENT_CURRENT_MODE = 1_004_000         // 当前模式
 }

+ 1 - 13
app/src/main/java/com/grkj/iscs/view/presenter/HomePresenter.kt

@@ -1,16 +1,8 @@
 package com.grkj.iscs.view.presenter
 
 import com.grkj.iscs.BusinessManager
-import com.grkj.iscs.extentions.removeLeadingZeros
-import com.grkj.iscs.extentions.toHexStrings
-import com.grkj.iscs.modbus.DockBean
-import com.grkj.iscs.modbus.ModBusController
-import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
-import com.grkj.iscs.util.ActivityUtils
 import com.grkj.iscs.util.Executor
-import com.grkj.iscs.util.log.LogUtil
-import com.grkj.iscs.view.base.BaseActivity
 import com.grkj.iscs.view.base.BasePresenter
 import com.grkj.iscs.view.iview.IHomeView
 
@@ -24,7 +16,7 @@ class HomePresenter : BasePresenter<IHomeView>() {
                         if (keyBean.isExist) {
                             Executor.repeatOnMain({
                                 keyBean.mac?.let {
-                                    getTicketNew(it, 3)
+                                    BusinessManager.getCurrentStatus(BusinessManager.getBleDeviceByMac(it)?.bleDevice!!)
                                 }
                                 return@repeatOnMain keyBean.mac == null
                             }, 1000, true)
@@ -35,10 +27,6 @@ class HomePresenter : BasePresenter<IHomeView>() {
         }
     }
 
-    private fun getTicketNew(mac: String, retryCount: Int) {
-        BusinessManager.getTicketStatusBusiness(mac, true)
-    }
-
     fun unregisterListener() {
         BusinessManager.unregisterListener(this)
     }