瀏覽代碼

refactor(更新) :
- 修改初始化时的钥匙连接策略,检查状态发现是待机模式并且没有作业票要下发的时候,先看还有没有设备要连接的,有的话直接断开,否则保持连接
- 提取检查待办事项以处理钥匙连接的逻辑

周文健 3 月之前
父節點
當前提交
0d6ea93584

+ 49 - 36
app/src/main/java/com/grkj/iscs_mars/BusinessManager.kt

@@ -81,6 +81,7 @@ import com.sik.sikcore.date.TimeUtils
 import com.sik.sikcore.thread.ThreadUtils
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.async
+import kotlinx.coroutines.coroutineScope
 import kotlinx.coroutines.suspendCancellableCoroutine
 import kotlinx.coroutines.withContext
 import kotlin.coroutines.resume
@@ -192,6 +193,9 @@ object BusinessManager {
                                         it.data.bleBean.bleDevice.mac, false, 1
                                     )
                                     ToastUtils.tip(R.string.take_out_key)
+                                    ThreadUtils.runOnIO {
+                                        checkMyTodoForHandleKey(it.data.bleBean.bleDevice.mac)
+                                    }
                                 }
                             } else {
                                 LogUtil.e("切换工作模式失败 : ${it.data.bleBean.bleDevice.mac}")
@@ -240,6 +244,38 @@ object BusinessManager {
         }
     }
 
+    /**
+     * 检查我的待办
+     */
+    suspend fun checkMyTodoForHandleKey(mac: String? = null): Boolean {
+        if (!CAN_RETURN) {
+            return false
+        }
+        val result = suspendCancellableCoroutine<Boolean> { cont ->
+            NetApi.getMySelfState {
+                if (it) {
+                    val maxPowerMac = getMaxPowerKey().mac
+                    if (maxPowerMac != mac) {
+                        connectExistsKey(listOf(mac ?: ""))
+                        launchDisconnectJob(mac ?: "")
+                    } else {
+                        connectExistsKey()
+                    }
+                } else {
+                    mac?.let {
+                        launchDisconnectJob(it)
+                    } ?: let {
+                        launchDisconnectAllJob()
+                    }
+                }
+                if (cont.isActive) {
+                    cont.resume(it)
+                }
+            }
+        }
+        return result
+    }
+
     /**
      * 连接一把存在的可连接的钥匙
      */
@@ -447,8 +483,7 @@ object BusinessManager {
      * 挂锁处理
      */
     private fun deviceLockHandler(
-        dockBean: DockBean,
-        lockBean: DockBean.LockBean
+        dockBean: DockBean, lockBean: DockBean.LockBean
     ) {
         if (lockBean.isExist) {
             ModBusController.readLockRfid(dockBean.addr, lockBean.idx) { res ->
@@ -456,8 +491,7 @@ object BusinessManager {
                     LogUtil.e("Lock rfid error")
                     return@readLockRfid
                 }
-                val rfid =
-                    res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
+                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                 ModBusController.updateLockRfid(
                     dockBean.addr, lockBean.idx, rfid
                 )
@@ -476,9 +510,7 @@ object BusinessManager {
                     NetApi.getIsLockPage { lockData ->
                         //锁rfid未异常正常请求锁数据,关锁
                         if (rfid in (lockData?.records?.filter { it.exStatus == lockStatus.find { it.dictLabel == "异常" }?.dictValue }
-                                ?.map { it.lockNfc }?.toMutableList()
-                                ?: mutableListOf())
-                        ) {
+                                ?.map { it.lockNfc }?.toMutableList() ?: mutableListOf())) {
                             ToastUtils.tip(
                                 MyApplication.instance?.applicationContext!!.getString(
                                     R.string.lock_exception_tag
@@ -503,8 +535,7 @@ object BusinessManager {
                                         if (itRst.isNotEmpty()) {
                                             // 上报锁具信息
                                             NetApi.updateLockReturn(
-                                                rfid,
-                                                MyApplication.instance!!.serialNo()
+                                                rfid, MyApplication.instance!!.serialNo()
                                             ) {}
                                         }
                                     }
@@ -540,8 +571,7 @@ object BusinessManager {
                     LogUtil.e("Key rfid error")
                     return@readKeyRfid
                 }
-                val rfid =
-                    res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
+                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                 ThreadUtils.runOnIO {
                     val slotStatus =
                         async { fetchDict<CommonDictRespVO>(DictAndSystemConstants.KEY_SLOT_STATUS) }
@@ -558,9 +588,7 @@ object BusinessManager {
                     val slotTypeList = slotType.await()
                     //锁钥匙未异常正常请求锁数据,关锁
                     if (rfid in (keyData?.records?.filter { it.exStatus == keyStatus.find { it.dictLabel == "异常" }?.dictValue }
-                            ?.map { it.keyNfc }?.toMutableList()
-                            ?: mutableListOf())
-                    ) {
+                            ?.map { it.keyNfc }?.toMutableList() ?: mutableListOf())) {
                         ToastUtils.tip(
                             MyApplication.instance?.applicationContext!!.getString(
                                 R.string.key_exception_tag
@@ -615,8 +643,7 @@ object BusinessManager {
             }
             sendEventMsg(
                 MsgEvent(
-                    MSG_EVENT_DEVICE_TAKE_UPDATE,
-                    DeviceTakeUpdateMsg(DEVICE_TYPE_KEY, keyBean.rfid)
+                    MSG_EVENT_DEVICE_TAKE_UPDATE, DeviceTakeUpdateMsg(DEVICE_TYPE_KEY, keyBean.rfid)
                 )
             )
         }
@@ -1532,8 +1559,7 @@ object BusinessManager {
                                         workTicketGetBO.data?.firstOrNull()?.taskCode?.toLong()
                                             ?.let {
                                                 checkStepAndTicketDetailThenSendTicket(
-                                                    it,
-                                                    bleDevice.mac
+                                                    it, bleDevice.mac
                                                 )
                                             }
                                     } else {
@@ -1909,20 +1935,8 @@ object BusinessManager {
                         sendLoadingEventMsg(null, false)
                         //连上之后没有工作票要下发就断开 看是否还有设备等待连接,没有就不断开,有就让路,一般是初始化的时候
                         launchDisconnectJob(currentModeMsg.bleBean.bleDevice.mac)
-                        NetApi.getMySelfState {
-                            if (it) {
-                                val maxPowerMac = getMaxPowerKey().mac
-                                if (maxPowerMac != currentModeMsg.bleBean.bleDevice.mac) {
-                                    connectExistsKey(
-                                        listOf(
-                                            currentModeMsg.bleBean.bleDevice.mac ?: ""
-                                        )
-                                    )
-                                    launchDisconnectJob(currentModeMsg.bleBean.bleDevice.mac ?: "")
-                                }
-                            } else {
-                                launchDisconnectJob(currentModeMsg.bleBean.bleDevice.mac ?: "")
-                            }
+                        ThreadUtils.runOnIO {
+                            checkMyTodoForHandleKey(currentModeMsg.bleBean.bleDevice.mac)
                         }
                     }
                 }
@@ -1943,10 +1957,9 @@ object BusinessManager {
     private fun checkStepAndTicketDetailThenSendTicket(ticketId: Long, mac: String) {
         NetApi.getStepDetail(ticketId) {
             var step = 0
-            it?.filter { it.stepStatus == "1" }
-                ?.maxByOrNull { it.stepIndex!! }?.stepIndex?.let {
-                    step = it
-                }
+            it?.filter { it.stepStatus == "1" }?.maxByOrNull { it.stepIndex!! }?.stepIndex?.let {
+                step = it
+            }
             NetApi.getTicketDetail(ticketId) { ticketDetail, _ ->
                 if (ticketDetail == null) {
                     return@getTicketDetail

+ 1 - 1
app/src/main/java/com/grkj/iscs_mars/view/activity/HomeActivity.kt

@@ -59,7 +59,7 @@ class HomeActivity : BaseMvpActivity<IHomeView, HomePresenter, ActivityHomeBindi
         presenter?.registerStatusListener()
         presenter?.updateKeyData()
         presenter?.getAndSaveCabinetId()
-        presenter?.getMySelfState()
+        presenter?.checkMyTodoForHandleKey()
         val userInfo = intent.getSerializableExtra("userInfo")
 
         BusinessManager.isTestMode = false

+ 2 - 2
app/src/main/java/com/grkj/iscs_mars/view/fragment/JobProgressFragment.kt

@@ -190,7 +190,7 @@ class JobProgressFragment(val goBack: () -> Unit, val changePage: (PageChangeBO)
                     val ticketId = (newData.data as UpdateTicketProgressMsg).ticketId
                     LogUtil.i("Update progress msg, ticketId : $ticketId")
                     if (ticketId == mPageChangeBO?.ticketId) {
-                        presenter?.getMySelfState()
+                        presenter?.checkMyTodoForHandleKey()
                         mPageChangeBO?.let {
                             refreshPage(it)
                         }
@@ -320,7 +320,7 @@ class JobProgressFragment(val goBack: () -> Unit, val changePage: (PageChangeBO)
                             cardNo,
                             "5"
                         ) {
-                            presenter?.getMySelfState()
+                            presenter?.checkMyTodoForHandleKey()
                             refreshPage(mPageChangeBO!!)
                         }
                     }

+ 2 - 2
app/src/main/java/com/grkj/iscs_mars/view/fragment/StepFragment.kt

@@ -118,7 +118,7 @@ class StepFragment(val goBack: () -> Unit, val changePage: (PageChangeBO) -> Uni
             if (mStep in 1..5) {
                 presenter?.cancelTicket(mChangePage?.ticketId!!) {
                     if (it) {
-                        presenter?.getMySelfState()
+                        presenter?.checkMyTodoForHandleKey()
                         changePage(PageChangeBO(-1))
                     }
                 }
@@ -434,7 +434,7 @@ class StepFragment(val goBack: () -> Unit, val changePage: (PageChangeBO) -> Uni
                                 presenter?.updateStep(
                                     mStepList.find { it.index == step }?.stepDetail?.stepId!!, "1"
                                 ) {
-                                    presenter?.getMySelfState()
+                                    presenter?.checkMyTodoForHandleKey()
                                     mChangePage?.let {
                                         refreshPage(it)
                                     }

+ 1 - 1
app/src/main/java/com/grkj/iscs_mars/view/fragment/TechnologySopFragment.kt

@@ -92,7 +92,7 @@ class TechnologySopFragment(val changePage: (PageChangeBO) -> Unit) :
         mBinding?.cbStart?.setOnClickListener {
             presenter?.createTicket(mMachineryIdx, mSopTypeIdx, mMachineryList) {
                 it?.let {
-                    presenter?.getMySelfState()
+                    presenter?.checkMyTodoForHandleKey()
                     changePage(
                         PageChangeBO(
                             2,

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

@@ -127,11 +127,9 @@ class HomePresenter : BasePresenter<IHomeView>() {
     /**
      * 获取是否需要我连接钥匙
      */
-    fun getMySelfState() {
-        NetApi.getMySelfState {
-            if (it){
-                BusinessManager.connectExistsKey()
-            }
+    fun checkMyTodoForHandleKey() {
+        ThreadUtils.runOnIO {
+            BusinessManager.checkMyTodoForHandleKey()
         }
     }
 }

+ 4 - 5
app/src/main/java/com/grkj/iscs_mars/view/presenter/JobProgressPresenter.kt

@@ -29,6 +29,7 @@ import com.grkj.iscs_mars.view.base.BasePresenter
 import com.grkj.iscs_mars.view.dialog.TipDialog
 import com.grkj.iscs_mars.view.iview.IJobProgressView
 import com.grkj.iscs_mars.view.step_mode.IStepMode
+import com.sik.sikcore.thread.ThreadUtils
 
 class JobProgressPresenter : BasePresenter<IJobProgressView>() {
     private var tipDialog: TipDialog? = null
@@ -420,11 +421,9 @@ class JobProgressPresenter : BasePresenter<IJobProgressView>() {
     /**
      * 获取是否需要我连接钥匙
      */
-    fun getMySelfState() {
-        NetApi.getMySelfState {
-            if (it) {
-                BusinessManager.connectExistsKey()
-            }
+    fun checkMyTodoForHandleKey() {
+        ThreadUtils.runOnIO {
+            BusinessManager.checkMyTodoForHandleKey()
         }
     }
 }

+ 3 - 5
app/src/main/java/com/grkj/iscs_mars/view/presenter/StepPresenter.kt

@@ -185,11 +185,9 @@ class StepPresenter : BasePresenter<IStepView>() {
     /**
      * 获取是否需要我连接钥匙
      */
-    fun getMySelfState() {
-        NetApi.getMySelfState {
-            if (it) {
-                BusinessManager.connectExistsKey()
-            }
+    fun checkMyTodoForHandleKey() {
+        ThreadUtils.runOnIO {
+            BusinessManager.checkMyTodoForHandleKey()
         }
     }
 }

+ 5 - 6
app/src/main/java/com/grkj/iscs_mars/view/presenter/TechnologySopPresenter.kt

@@ -8,6 +8,7 @@ import com.grkj.iscs_mars.util.NetApi
 import com.grkj.iscs_mars.util.ToastUtils
 import com.grkj.iscs_mars.view.base.BasePresenter
 import com.grkj.iscs_mars.view.iview.ITechnologySopView
+import com.sik.sikcore.thread.ThreadUtils
 
 class TechnologySopPresenter : BasePresenter<ITechnologySopView>() {
 
@@ -51,15 +52,13 @@ class TechnologySopPresenter : BasePresenter<ITechnologySopView>() {
             }
         }
     }
-    
+
     /**
      * 获取是否需要我连接钥匙
      */
-    fun getMySelfState() {
-        NetApi.getMySelfState {
-            if (it){
-                BusinessManager.connectExistsKey()
-            }
+    fun checkMyTodoForHandleKey() {
+        ThreadUtils.runOnIO {
+            BusinessManager.checkMyTodoForHandleKey()
         }
     }
 }