Forráskód Böngészése

refactor(更新) :
- 开启预充电加速作业票的发放
- 当前模式处理时如果有钥匙待发放但是锁还有的,就不处理

周文健 5 hónapja
szülő
commit
5ddacee89d

+ 23 - 12
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -34,6 +34,7 @@ import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
 import com.grkj.iscs.model.DictConstants
+import com.grkj.iscs.model.ISCSDomainData
 import com.grkj.iscs.model.bo.DeviceTakeUpdateBO
 import com.grkj.iscs.model.bo.UpdateKeyReturnBO
 import com.grkj.iscs.model.bo.WorkTicketGetBO
@@ -175,11 +176,6 @@ object BusinessManager {
                                         keyBean.isLeft,
                                         dock?.addr
                                     )
-                                    ModBusController.controlKeyCharge(
-                                        false,
-                                        keyBean.isLeft,
-                                        dock?.addr
-                                    )
                                     ModBusController.updateKeyReadyStatus(
                                         it.data.bleBean.bleDevice.mac,
                                         false,
@@ -357,6 +353,11 @@ object BusinessManager {
                                 dockBean.addr,
                                 if (keyBean.isLeft) 0 else 1
                             ) { isLeft, res ->
+                                ModBusController.controlKeyCharge(
+                                    true,
+                                    keyBean.idx == 1,
+                                    dockBean.addr
+                                )
                                 if (res.size < 11) {
                                     LogUtil.e("Key rfid error")
                                     return@readKeyRfid
@@ -524,6 +525,7 @@ object BusinessManager {
                                 }
                             }
                         } else {
+                            LogUtil.i("挂锁取出-:${lockBean.rfid}")
                             sendEventMsg(
                                 MsgEvent(
                                     MSG_EVENT_DEVICE_TAKE_UPDATE,
@@ -1232,11 +1234,9 @@ object BusinessManager {
                         MSG_EVENT_LOADING, LoadingMsg(true, "切换钥匙为工作模式", null)
                     )
                 )
-                ModBusController.controlKeyCharge(false, bleBean.bleDevice.mac) {
-                    Executor.delayOnIO(800) {
-                        //切换到工作模式
-                        switchWorkMode(bleBean.bleDevice, isNeedLoading)
-                    }
+                Executor.delayOnIO(800) {
+                    //切换到工作模式
+                    switchWorkMode(bleBean.bleDevice, isNeedLoading)
                 }
             } else {
                 sendLoadingEventMsg(null, false)
@@ -1361,8 +1361,15 @@ object BusinessManager {
                     MSG_EVENT_LOADING, LoadingMsg(false, "TD$ticketJson}", true)
                 )
             )
-
-            val workTicketGetBO = Gson().fromJson(ticketJson, WorkTicketGetBO::class.java)
+            val workTicketGetBO = try {
+                Gson().fromJson(ticketJson, WorkTicketGetBO::class.java)
+            } catch (e: Exception) {
+                null
+            }
+            if (workTicketGetBO == null) {
+                ToastUtils.tip(R.string.ticket_data_error)
+                return@handleTicketStatus
+            }
 
             // 判断workTicketGetBO里是否有未完成的
             ThreadUtils.runOnIO {
@@ -1758,6 +1765,10 @@ object BusinessManager {
                     // 判断是否有待取的钥匙
                     val updateBo =
                         mDeviceTakeList.find { it.deviceType == DEVICE_TYPE_KEY && key.rfid == it.nfc }
+                    if (mDeviceTakeList.any { it.deviceType == DEVICE_TYPE_LOCK && it.ticketId == updateBo?.ticketId }) {
+                        //todo 如果有钥匙待取但是对应的作业票的锁还有的,就不发
+                        return
+                    }
                     updateBo?.let { itBO ->
                         NetApi.getStepDetail(itBO.ticketId) {
                             var step = 0

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

@@ -362,6 +362,9 @@ object BleCmdManager {
 
                     override fun onWriteFailure(exception: BleException?) {
                         LogUtil.e("getTicketStatusPart fail")
+                        BusinessManager.getBleDeviceByMac(bleDevice.mac)?.let {
+                            it.ticketStatus = byteArrayOf()
+                        }
                         BusinessManager.sendEventMsg(
                             MsgEvent(
                                 MSG_EVENT_GET_TICKET_STATUS,

+ 13 - 15
app/src/main/java/com/grkj/iscs/ble/BleConnectionManager.kt

@@ -84,15 +84,18 @@ object BleConnectionManager {
             return
         }
         if (connectNow) {
+            LogUtil.w("蓝牙连接-立即连接 mac: $mac")
             unregisterConnectListener(mac)
         }
         // 重复注册检查
         if (connectListeners.any { it.mac == mac } || currentConnectingMac == mac) {
             LogUtil.w("蓝牙连接-忽略重复注册 mac: $mac")
+            callBack?.invoke(false, null)
             return
         }
         // 加入队列并启动连接
         fun checkAndConnect(isDisconnectAll: Boolean = false) {
+            LogUtil.w("蓝牙连接-开始检查连接 mac: $mac")
             if (BleManager.getInstance().allConnectedDevice.size < maxConnectCount) {
                 connectListeners.add(ConnectListener(mac, callBack))
                 connectKey()
@@ -124,7 +127,7 @@ object BleConnectionManager {
      */
     private fun checkProcess(mac: String?, hideLoading: Boolean = true): Boolean {
         val canProcess = connectListeners.any { it.mac == mac }
-        if (!canProcess&&hideLoading) sendLoadingEventMsg(null, false)
+        if (!canProcess && hideLoading) sendLoadingEventMsg(null, false)
         return canProcess
     }
 
@@ -133,8 +136,8 @@ object BleConnectionManager {
      */
     private fun connectKey() {
         if (connectListeners.isEmpty()) return
-        if (isPreparing || BleManager.getInstance().allConnectedDevice.size >= maxStandbyCount) {
-            LogUtil.i("暂时不能连接:${isPreparing},${BleManager.getInstance().allConnectedDevice.size >= maxStandbyCount}")
+        if (isPreparing || BleManager.getInstance().allConnectedDevice.size > maxStandbyCount) {
+            LogUtil.i("暂时不能连接:${isPreparing},${BleManager.getInstance().allConnectedDevice.size > maxStandbyCount}")
             Executor.delayOnMain(1000) { connectKey() }
             return
         }
@@ -158,13 +161,6 @@ object BleConnectionManager {
                     if (checkProcess(listener.mac)) {
                         listener.callBack?.invoke(false, null)
                         unregisterConnectListener(listener.mac)
-//                        Executor.delayOnMain(2000) {
-//                            registerConnectListener(
-//                                listener.mac,
-//                                false,
-//                                listener.callBack
-//                            )
-//                        }
                     }
                     return@runOnMain
                 }
@@ -174,9 +170,6 @@ object BleConnectionManager {
                     listener.callBack?.invoke(true, bleBean)
                     unregisterConnectListener(listener.mac)
                 }
-//                bleBean?.bleDevice?.mac?.let { mac ->
-//                    addToStandby(mac)
-//                }
                 if (connectListeners.isNotEmpty()) connectKey()
             }
         }
@@ -547,8 +540,13 @@ object BleConnectionManager {
             // -------- 第二次尝试:先开电,再连 --------
             // 开电,并等待回调
             suspendCoroutine<Unit> { unitCont ->
-                controlKeyCharge(true, mac) {
-                    unitCont.resume(Unit)
+                controlKeyCharge(false, mac) {
+                    ThreadUtils.runOnIO {
+                        delay(300)
+                        controlKeyCharge(true, mac) {
+                            unitCont.resume(Unit)
+                        }
+                    }
                 }
             }
             LogUtil.i("蓝牙连接-开启充电并等待500ms")

+ 21 - 24
app/src/main/java/com/grkj/iscs/modbus/ModBusController.kt

@@ -11,6 +11,7 @@ import com.grkj.iscs.ble.BleConnectionManager
 import com.grkj.iscs.ble.CustomBleWriteCallback
 import com.grkj.iscs.extentions.removeLeadingZeros
 import com.grkj.iscs.extentions.toHexStrings
+import com.grkj.iscs.model.DeviceConst
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
@@ -20,6 +21,7 @@ import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
+import com.grkj.iscs.model.ISCSDomainData
 import com.grkj.iscs.model.vo.hardware.CabinetSlotsRecord
 import com.grkj.iscs.util.CommonUtils
 import com.grkj.iscs.util.Executor
@@ -196,9 +198,10 @@ object ModBusController {
                         val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                         LogUtil.i("初始化锁具 RFID : $rfid")
                         updateLockRfid(dockBean.addr, idx, rfid)
-                        NetApi.getLockInfo(rfid) {
-                            updateLockNewHardware(dockBean.addr, idx, it == null)
-                        }
+                        //todo 为设备录入增加
+//                        NetApi.getLockInfo(rfid) {
+//                            updateLockNewHardware(dockBean.addr, idx, it == null)
+//                        }
                     }
                 }
                 controlLockBuckle(false, dockBean.addr, hasLockIdxList)
@@ -224,10 +227,6 @@ object ModBusController {
                             val rfid =
                                 res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                             LogUtil.i("初始化钥匙 RFID : $rfid")
-                            controlKeyCharge(false, key.isLeft, dockBean.addr) {
-                                LogUtil.i("关闭充电:${it.toHexStrings()}")
-                                LogUtil.i("关闭充电: $rfid")
-                            }
                             // 更新rfid
                             updateKeyRfid(dockBean.addr, isLeft, rfid)
                             // 蓝牙准备操作
@@ -237,18 +236,19 @@ object ModBusController {
                                 if (keyInfo != null && !keyInfo.macAddress.isNullOrEmpty()) {
                                     // 更新mac
                                     updateKeyMac(dockBean.addr, key.isLeft, keyInfo.macAddress)
-                                    controlKeyCharge(true, keyInfo.macAddress) {
-                                        ThreadUtils.runOnIODelayed(500) {
-                                            BusinessManager.registerConnectListener(
+                                    ThreadUtils.runOnIO {
+                                        val isConnect =
+                                            BleConnectionManager.tryConnectWithOptionalCharge(
                                                 keyInfo.macAddress
-                                            ) { isDone, bleBean ->
-                                                if (isDone && bleBean?.bleDevice != null) {
-                                                    Executor.delayOnMain(500) {
-                                                        BusinessManager.getCurrentStatus(
-                                                            3, bleBean.bleDevice
-                                                        )
-                                                        BusinessManager.getBatteryPower(bleBean.bleDevice)
-                                                    }
+                                            )
+                                        if (isConnect){
+                                            val bleBean = BusinessManager.getBleDeviceByMac(keyInfo.macAddress)
+                                            bleBean?.let {
+                                                Executor.delayOnMain(500) {
+                                                    BusinessManager.getCurrentStatus(
+                                                        3, it.bleDevice
+                                                    )
+                                                    BusinessManager.getBatteryPower(it.bleDevice)
                                                 }
                                             }
                                         }
@@ -261,11 +261,6 @@ object ModBusController {
                         controlKeyBuckle(false, key.isLeft, dockBean.addr)
                     } else {
                         controlKeyBuckle(true, key.isLeft, dockBean.addr)
-                        controlKeyCharge(
-                            false, key.isLeft, dockBean.addr
-                        ) {
-                            LogUtil.i("关闭充电:${it.toHexStrings()}")
-                        }
                     }
                 }
             }
@@ -707,6 +702,9 @@ object ModBusController {
         slaveAddress?.let {
             ModBusCMDHelper.generateKeyBuckleCmd(isOpen, if (isLeft) 0 else 1)?.let { cmd ->
                 modBusManager?.sendTo(it, cmd) { res ->
+                    if (isOpen) {
+                        controlKeyCharge(false, isLeft, slaveAddress)
+                    }
                     done?.invoke(res)
                 }
             }
@@ -1014,7 +1012,6 @@ object ModBusController {
     }
 
 
-
     /**
      * 根据数量获取锁具(基于锁柜和便携柜不存在接一起的情况)
      *

+ 16 - 0
app/src/main/java/com/grkj/iscs/model/ISCSDomainData.kt

@@ -0,0 +1,16 @@
+package com.grkj.iscs.model
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.liveData
+
+/**
+ * 业务数据
+ */
+object ISCSDomainData {
+    /**
+     * 设备是否初始化完成
+     */
+    val isDeviceInit: MutableLiveData<Boolean> = MutableLiveData(false)
+    val deviceInitProgress: MutableLiveData<Int> = MutableLiveData(0)
+}

+ 1 - 0
app/src/main/java/com/grkj/iscs/view/fragment/JobProgressFragment.kt

@@ -215,6 +215,7 @@ class JobProgressFragment(val goBack: () -> Unit, val changePage: (PageChangeBO)
 
     override fun onResume() {
         super.onResume()
+        presenter?.preOpenKeyCharge()
         isVisible = true
     }
 

+ 2 - 0
app/src/main/java/com/grkj/iscs/view/fragment/WorkerFragment.kt

@@ -53,6 +53,8 @@ class WorkerFragment(val goBack: () -> Unit, val changePage: (PageChangeBO) -> U
             if (isColockerChanged) {
                 presenter?.checkColockerChangedStepJump(requireContext(), mStepDetailList, mStep) {
                     presenter?.updateTicketUser(mPageChangeBO?.ticketId!!, mSelectedList) {
+                        //选择人员之后提前打开充电,加速连接
+                        presenter?.preOpenKeyCharge()
                         if (it) {
                             goBack()
                         }

+ 27 - 0
app/src/main/java/com/grkj/iscs/view/presenter/JobProgressPresenter.kt

@@ -6,12 +6,15 @@ import com.grkj.iscs.R
 import com.grkj.iscs.extentions.serialNo
 import com.grkj.iscs.modbus.DockBean
 import com.grkj.iscs.modbus.ModBusController
+import com.grkj.iscs.modbus.ModBusController.dockList
 import com.grkj.iscs.model.Constants.JOB_STATUS_ACQUIRE_KEY
 import com.grkj.iscs.model.Constants.JOB_STATUS_ACQUIRE_LOCK
 import com.grkj.iscs.model.Constants.JOB_STATUS_NOT_STARTED
 import com.grkj.iscs.model.Constants.USER_TYPE_LOCKER
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
 import com.grkj.iscs.model.eventmsg.DeviceExceptionMsg
 import com.grkj.iscs.model.eventmsg.MsgEvent
 import com.grkj.iscs.model.eventmsg.MsgEventConstants.MSG_EVENT_DEVICE_EXCEPTION
@@ -29,6 +32,8 @@ import com.grkj.iscs.view.base.BasePresenter
 import com.grkj.iscs.view.dialog.TipDialog
 import com.grkj.iscs.view.iview.IJobProgressView
 import com.grkj.iscs.view.step_mode.IStepMode
+import com.sik.sikcore.thread.ThreadUtils
+import kotlinx.coroutines.delay
 
 class JobProgressPresenter : BasePresenter<IJobProgressView>() {
     private var tipDialog: TipDialog? = null
@@ -412,4 +417,26 @@ class JobProgressPresenter : BasePresenter<IJobProgressView>() {
             }
         }
     }
+
+
+    /**
+     * 预打开钥匙的充电尝试激活
+     */
+    fun preOpenKeyCharge() {
+        ThreadUtils.runOnIO {
+            val keyDockList =
+                dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }
+                    .sortedBy { it.addr }.onEach { it.deviceList.sortBy { dev -> dev.idx } }
+            keyDockList.forEach { dock ->
+                dock.deviceList.filter { it.isExist }.forEach { key ->
+                    ModBusController.controlKeyCharge(false, key.idx == 1, dock.addr) {
+                        ThreadUtils.runOnIO {
+                            delay(300)
+                            ModBusController.controlKeyCharge(true, key.idx == 1, dock.addr)
+                        }
+                    }
+                }
+            }
+        }
+    }
 }

+ 27 - 0
app/src/main/java/com/grkj/iscs/view/presenter/WorkerPresenter.kt

@@ -2,7 +2,11 @@ package com.grkj.iscs.view.presenter
 
 import android.content.Context
 import com.grkj.iscs.R
+import com.grkj.iscs.modbus.ModBusController
+import com.grkj.iscs.modbus.ModBusController.dockList
 import com.grkj.iscs.model.Constants.USER_ROLE_LOCKER
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
 import com.grkj.iscs.model.vo.ticket.StepDetailRespVO
 import com.grkj.iscs.model.vo.ticket.TicketDetailRespVO
 import com.grkj.iscs.model.vo.ticket.TicketUserReqVO
@@ -13,6 +17,8 @@ import com.grkj.iscs.util.ToastUtils
 import com.grkj.iscs.view.base.BasePresenter
 import com.grkj.iscs.view.iview.IWorkerView
 import com.grkj.iscs.view.step_mode.IStepMode
+import com.sik.sikcore.thread.ThreadUtils
+import kotlinx.coroutines.delay
 
 class WorkerPresenter : BasePresenter<IWorkerView>() {
     /**
@@ -121,4 +127,25 @@ class WorkerPresenter : BasePresenter<IWorkerView>() {
     fun getMinColockerSize(context: Context): Int {
         return IStepMode.getStepMode(context)?.getMinColockerSize() ?: 1
     }
+
+    /**
+     * 预打开钥匙的充电
+     */
+    fun preOpenKeyCharge() {
+        ThreadUtils.runOnIO {
+            val keyDockList =
+                dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }
+                    .sortedBy { it.addr }.onEach { it.deviceList.sortBy { dev -> dev.idx } }
+            keyDockList.forEach { dock ->
+                dock.deviceList.filter { it.isExist }.forEach { key ->
+                    ModBusController.controlKeyCharge(false, key.idx == 1, dock.addr) {
+                        ThreadUtils.runOnIO {
+                            delay(300)
+                            ModBusController.controlKeyCharge(true, key.idx == 1, dock.addr)
+                        }
+                    }
+                }
+            }
+        }
+    }
 }

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

@@ -359,4 +359,5 @@
     <string name="slot_exception_tag">Slot is tag for exception</string>
     <string name="lock_exception_tag">Lock is tag for exception</string>
     <string name="job_already_finished">job already finished</string>
+    <string name="ticket_data_error">job ticket data error</string>
 </resources>

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

@@ -359,4 +359,5 @@
     <string name="slot_exception_tag">该锁仓已被标记异常</string>
     <string name="lock_exception_tag">该挂锁已被标记异常</string>
     <string name="job_already_finished">该作业已被结束</string>
+    <string name="ticket_data_error">工作票数据损坏</string>
 </resources>

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

@@ -359,4 +359,5 @@
     <string name="slot_exception_tag">该锁仓已被标记异常</string>
     <string name="lock_exception_tag">该挂锁已被标记异常</string>
     <string name="job_already_finished">该作业已被结束</string>
+    <string name="ticket_data_error">工作票数据损坏</string>
 </resources>