|
|
@@ -14,11 +14,13 @@ import com.grkj.data.model.req.LockPointUpdateReq
|
|
|
import com.grkj.data.model.res.TicketDetailRes
|
|
|
import com.grkj.shared.config.Constants
|
|
|
import com.grkj.ui_base.R
|
|
|
-import com.grkj.ui_base.data.DictConstants
|
|
|
+import com.grkj.data.data.DictConstants
|
|
|
+import com.grkj.data.enums.LockStepEnum
|
|
|
import com.grkj.ui_base.dialog.TipDialog
|
|
|
import com.grkj.ui_base.utils.CommonUtils
|
|
|
import com.grkj.ui_base.utils.Executor
|
|
|
import com.grkj.ui_base.utils.SPUtils
|
|
|
+import com.grkj.ui_base.utils.ble.BleBean
|
|
|
import com.grkj.ui_base.utils.ble.BleCmdManager
|
|
|
import com.grkj.ui_base.utils.ble.BleConnectionManager
|
|
|
import com.grkj.ui_base.utils.ble.BleConst
|
|
|
@@ -26,12 +28,15 @@ import com.grkj.ui_base.utils.ble.CustomBleWriteCallback
|
|
|
import com.grkj.ui_base.utils.event.CurrentModeEvent
|
|
|
import com.grkj.ui_base.utils.event.DeviceExceptionEvent
|
|
|
import com.grkj.ui_base.utils.event.LoadingEvent
|
|
|
+import com.grkj.ui_base.utils.event.TicketFinishedEvent
|
|
|
import com.grkj.ui_base.utils.event.UpdateTicketProgressEvent
|
|
|
import com.grkj.ui_base.utils.extension.serialNo
|
|
|
+import com.grkj.ui_base.utils.extension.startsWith
|
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
|
import com.grkj.ui_base.utils.modbus.ModBusController
|
|
|
import com.kongzue.dialogx.dialogs.PopTip
|
|
|
import com.sik.sikcore.SIKCore
|
|
|
+import com.sik.sikcore.activity.ActivityTracker
|
|
|
import com.sik.sikcore.thread.ThreadUtils
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.async
|
|
|
@@ -45,152 +50,174 @@ object BleBusinessManager {
|
|
|
private val logger = LoggerFactory.getLogger(BleBusinessManager::class.java)
|
|
|
|
|
|
/**
|
|
|
- * 处理工作票完成情况
|
|
|
+ * 处理蓝牙返回
|
|
|
*/
|
|
|
- fun handleTicketStatus(
|
|
|
- bleDevice: BleDevice, byteArray: ByteArray, isNeedLoading: Boolean = false
|
|
|
+ fun handleRsp(
|
|
|
+ bleBean: BleBean,
|
|
|
+ byteArray: ByteArray,
|
|
|
+ isNeedLoading: Boolean = false,
|
|
|
+ prepareDoneCallBack: ((Boolean, BleBean?) -> Unit)?
|
|
|
) {
|
|
|
- BleCmdManager.handleTicketStatus(bleDevice, byteArray) { ticketJson ->
|
|
|
- if (ticketJson.isNullOrEmpty()) {
|
|
|
- return@handleTicketStatus
|
|
|
- }
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票完成状态读取完成", true)
|
|
|
- logger.info("Get ticket status complete : ${bleDevice.mac}")
|
|
|
- // TD:Ticket Done
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent("TD$ticketJson}", true)
|
|
|
-
|
|
|
- val workTicketGet = Gson().fromJson(ticketJson, WorkTicketGet::class.java)
|
|
|
-
|
|
|
- // 判断WorkTicketGet里是否有未完成的
|
|
|
- if (workTicketGet.hasFinished()) {
|
|
|
- Executor.delayOnMain(500) {
|
|
|
- handleKeyReturn(bleDevice, workTicketGet)
|
|
|
+ when {
|
|
|
+ // 获取令牌
|
|
|
+ byteArray.startsWith(BleConst.RSP_GET_TOKEN) -> BleCmdManager.handleToken(
|
|
|
+ bleBean.bleDevice, byteArray
|
|
|
+ ) { isSuccess ->
|
|
|
+ if (isSuccess) {
|
|
|
+ prepareDoneCallBack?.invoke(true, bleBean)
|
|
|
}
|
|
|
- } else {
|
|
|
- // 当前策略:作业票未完成禁止归还钥匙
|
|
|
- TipDialog.show(
|
|
|
- msg = CommonUtils.getStr(R.string.key_return_tip)!!,
|
|
|
- onConfirmClick = {
|
|
|
- LoadingEvent.sendLoadingEvent()
|
|
|
- PopTip.tip(CommonUtils.getStr(R.string.continue_the_ticket))
|
|
|
- BleManager.getInstance().disconnect(bleDevice)
|
|
|
- // 打开卡扣,防止初始化的时候选择不处理钥匙导致无法使用
|
|
|
- val dock = ModBusController.getDockByKeyMac(bleDevice.mac)
|
|
|
- val keyBean = dock?.getKeyList()?.find { it.mac == bleDevice.mac }
|
|
|
- keyBean?.let {
|
|
|
- ModBusController.controlKeyBuckle(true, keyBean.idx, dock.addr)
|
|
|
- }
|
|
|
- })
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理钥匙归还
|
|
|
- */
|
|
|
- private fun handleKeyReturn(bleDevice: BleDevice, workTicketGet: WorkTicketGet?) {
|
|
|
- val dock = ModBusController.getDockByKeyMac(bleDevice.mac)
|
|
|
- val keyBean = dock?.getKeyList()?.find { it.mac == bleDevice.mac }
|
|
|
- keyBean?.let {
|
|
|
- ModBusController.controlKeyBuckle(false, keyBean.idx, dock.addr)
|
|
|
- }
|
|
|
- // 上报隔离点状态
|
|
|
- val keyNfc = ModBusController.getKeyByMac(bleDevice.mac)?.rfid
|
|
|
- workTicketGet?.data?.forEach { data ->
|
|
|
- val updateList = mutableListOf<LockPointUpdateReq>()
|
|
|
- data.dataList?.forEach { dataListDTO ->
|
|
|
- data.taskCode?.toLong()?.let {
|
|
|
- SPUtils.returnKey(it)
|
|
|
+ // 工作模式切换
|
|
|
+ byteArray.startsWith(BleConst.RSP_SWITCH_MODE) -> {
|
|
|
+ handleSwitchModeResult(byteArray, isNeedLoading) { res, job ->
|
|
|
+ switchModeResultDeal(job.toInt(), res.toInt(), bleBean)
|
|
|
}
|
|
|
- val updateVO = LockPointUpdateReq(
|
|
|
- data.taskCode?.toLong(),
|
|
|
- dataListDTO.infoRfidNo,
|
|
|
- dataListDTO.equipRfidNo,
|
|
|
- keyNfc!!,
|
|
|
- dataListDTO.target,
|
|
|
- dataListDTO.status
|
|
|
- )
|
|
|
- updateList.add(updateVO)
|
|
|
}
|
|
|
+ // 工作票下发
|
|
|
+ byteArray.startsWith(BleConst.RSP_SEND_WORK_TICKET) -> handleWorkTicketResult(
|
|
|
+ bleBean, byteArray, isNeedLoading
|
|
|
+ )
|
|
|
+ // 获取设备当前状态
|
|
|
+ byteArray.startsWith(BleConst.RSP_CURRENT_STATUS) -> BleCmdManager.handleCurrentStatus(
|
|
|
+ byteArray
|
|
|
+ ) {
|
|
|
+ handleCurrentMode(CurrentModeEvent(bleBean, it))
|
|
|
+ }
|
|
|
+ // 获取设备工作票完成情况
|
|
|
+ byteArray.startsWith(BleConst.RSP_WORK_TICKET_RESULT) && byteArray[3] == 0x02.toByte() -> handleTicketStatus(
|
|
|
+ bleBean.bleDevice, byteArray, isNeedLoading
|
|
|
+ )
|
|
|
|
|
|
- LoadingEvent.sendLoadingEvent()
|
|
|
- PopTip.tip(R.string.key_return_success)
|
|
|
- if (RepositoryManager.hardwareRepo.canReturn()) {
|
|
|
- // 上报点位钥匙绑定
|
|
|
- RepositoryManager.hardwareRepo.updateLockPointBatch(updateList) { isSuccess, msg ->
|
|
|
- if (isSuccess || msg == CommonUtils.getStr(R.string.lock_nfc_lost)) {
|
|
|
- data.taskCode?.toLong()?.let {
|
|
|
- UpdateTicketProgressEvent.sendUpdateTicketProgressEvent(it)
|
|
|
- }
|
|
|
- // 确认归还,切换为待机模式
|
|
|
- switchReadyMode(bleDevice)
|
|
|
- } else if (msg != CommonUtils.getStr(R.string.lock_nfc_lost)) {
|
|
|
- SPUtils.saveUpdateLockPoint(SIKCore.getApplication(), updateList)
|
|
|
+ byteArray.startsWith(BleConst.RSP_POWER_STATUS) -> {
|
|
|
+ val power = byteArray[4].toInt()
|
|
|
+ if (power < 50) {//如果电量小于50就打开仓位充电
|
|
|
+ ModBusController.controlKeyCharge(true, bleBean.bleDevice.mac) {
|
|
|
+ logger.info("钥匙: ${bleBean.bleDevice.mac} 开始充电")
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- // 上报钥匙归还
|
|
|
- RepositoryManager.hardwareRepo.updateKeyReturn(
|
|
|
- data.taskCode?.toLong()!!, keyNfc!!, SIKCore.getApplication().serialNo()
|
|
|
- ) { isSuccess, msg ->
|
|
|
- if (!isSuccess && msg != CommonUtils.getStr(R.string.ticket_lost)) {
|
|
|
- SPUtils.saveUpdateKeyReturn(
|
|
|
- SIKCore.getApplication(),
|
|
|
- UpdateKeyReturn(data.taskCode?.toLong()!!, keyNfc!!)
|
|
|
- )
|
|
|
+ } else {
|
|
|
+ ModBusController.controlKeyCharge(false, bleBean.bleDevice.mac) {
|
|
|
+ logger.info("钥匙: ${bleBean.bleDevice.mac} 关闭充电")
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- SPUtils.saveUpdateLockPoint(SIKCore.getApplication(), updateList)
|
|
|
- SPUtils.saveUpdateKeyReturn(
|
|
|
- SIKCore.getApplication(), UpdateKeyReturn(data.taskCode?.toLong()!!, keyNfc!!)
|
|
|
- )
|
|
|
- // 保存待发数据,切换为待机模式
|
|
|
- switchReadyMode(bleDevice)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 处理虚拟钥匙取出,如果作业的全部点位已经上锁更新钥匙的状态使用
|
|
|
+ * 工作模式切换结果
|
|
|
+ * job : 0x01:工作模式 0x02:待机模式
|
|
|
+ * res : 0x01:成功 0x02:失败
|
|
|
*/
|
|
|
- fun handleVirtualKeyGive(ticketId: Long, keyNfc: String, done: () -> Unit) {
|
|
|
- // 上报钥匙取出
|
|
|
- RepositoryManager.hardwareRepo.updateKeyTake(
|
|
|
- ticketId,
|
|
|
- keyNfc,
|
|
|
- SIKCore.getApplication().serialNo()
|
|
|
- ) { isSuccess ->
|
|
|
- if (isSuccess) {
|
|
|
- done()
|
|
|
+ private fun handleSwitchModeResult(
|
|
|
+ byteArray: ByteArray,
|
|
|
+ isNeedLoading: Boolean = false,
|
|
|
+ callBack: ((Byte, Byte) -> Unit)? = null
|
|
|
+ ) {
|
|
|
+ BleCmdManager.handleSwitchModeResult(byteArray) { job, res ->
|
|
|
+ if (res == 0x01.toByte() && job == 0x01.toByte()) {
|
|
|
+ logger.info("切换工作模式成功")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("切换工作模式成功", true)
|
|
|
+ } else if (res == 0x01.toByte() && job == 0x02.toByte()) {
|
|
|
+ logger.info("切换待机模式成功")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("切换待机模式成功", true)
|
|
|
+ } else {
|
|
|
+ logger.error("切换模式失败 : ${job.toInt()} - ${res.toInt()}")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("切换模式失败", true)
|
|
|
}
|
|
|
+ callBack?.invoke(res, job)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 处理虚拟钥匙归还,如果作业的全部点位已经上锁更新钥匙的状态使用
|
|
|
+ * 切换模式结果处理
|
|
|
*/
|
|
|
- fun handleVirtualKeyReturn(ticketId: Long, keyNfc: String, done: () -> Unit) {
|
|
|
- // 上报钥匙归还
|
|
|
- RepositoryManager.hardwareRepo.updateKeyReturn(
|
|
|
- ticketId, keyNfc, SIKCore.getApplication().serialNo()
|
|
|
- ) { isSuccess, msg ->
|
|
|
- if (!isSuccess && msg != CommonUtils.getStr(R.string.ticket_lost)
|
|
|
- ) {
|
|
|
- SPUtils.saveUpdateKeyReturn(
|
|
|
- SIKCore.getApplication(), UpdateKeyReturn(ticketId, keyNfc)
|
|
|
- )
|
|
|
- } else {
|
|
|
- done()
|
|
|
- UpdateTicketProgressEvent.sendUpdateTicketProgressEvent(ticketId)
|
|
|
+ private fun switchModeResultDeal(
|
|
|
+ job: Int,
|
|
|
+ res: Int,
|
|
|
+ bleBean: BleBean
|
|
|
+ ) {
|
|
|
+ when (job) {
|
|
|
+ // 工作模式
|
|
|
+ 1 -> {
|
|
|
+ if (res == 1) {
|
|
|
+ // 只能在这里断开,不能全部断开
|
|
|
+ BleManager.getInstance().disconnect(bleBean.bleDevice)
|
|
|
+
|
|
|
+ // 打开钥匙卡扣
|
|
|
+ val keyBean =
|
|
|
+ ModBusController.getKeyByMac(bleBean.bleDevice.mac)
|
|
|
+ if (keyBean == null) {
|
|
|
+ LoadingEvent.sendLoadingEvent("未找到钥匙信息", true)
|
|
|
+ PopTip.tip(R.string.key_not_exists)
|
|
|
+ } else {
|
|
|
+ LoadingEvent.sendLoadingEvent(
|
|
|
+ CommonUtils.getStr(R.string.take_out_key_tip),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ val dock =
|
|
|
+ ModBusController.getDockByKeyMac(bleBean.bleDevice.mac)
|
|
|
+ keyBean.isReady = false
|
|
|
+ ModBusController.controlKeyBuckle(
|
|
|
+ true, keyBean.idx, dock?.addr
|
|
|
+ )
|
|
|
+ ModBusController.updateKeyReadyStatus(
|
|
|
+ bleBean.bleDevice.mac, false, 1
|
|
|
+ )
|
|
|
+ PopTip.tip(R.string.take_out_key)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("切换工作模式失败 : ${bleBean.bleDevice.mac}")
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ switchWorkMode(bleBean.bleDevice, false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 待机模式
|
|
|
+ 2 -> {
|
|
|
+ if (res == 1) {
|
|
|
+ ModBusController.updateKeyReadyStatus(
|
|
|
+ bleBean.bleDevice.mac, true, 2
|
|
|
+ )
|
|
|
+ // 延时再次获取当前状态,触发handleCurrentMode里工作票下发状态检查
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ BleConnectionManager.getCurrentStatus(1, bleBean.bleDevice)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ logger.error("切换待机模式失败 : ${bleBean.bleDevice.mac}")
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ switchReadyMode(bleBean.bleDevice)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 切换工作模式
|
|
|
+ */
|
|
|
+ private fun switchWorkMode(bleDevice: BleDevice, isNeedLoading: Boolean = false) {
|
|
|
+ logger.info("switchWorkMode - ${bleDevice.mac}")
|
|
|
+ BleCmdManager.switchMode(
|
|
|
+ BleConst.STATUS_WORK,
|
|
|
+ bleDevice,
|
|
|
+ object : CustomBleWriteCallback() {
|
|
|
+ override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
+ logger.info("switch mode work success : ${bleDevice.mac}")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onWriteFailure(exception: BleException?) {
|
|
|
+ logger.error("switch mode work fail : ${exception?.code} - ${exception?.description}")
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ switchWorkMode(bleDevice, isNeedLoading)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 切换待机模式
|
|
|
*/
|
|
|
- fun switchReadyMode(bleDevice: BleDevice) {
|
|
|
+ private fun switchReadyMode(bleDevice: BleDevice) {
|
|
|
BleCmdManager.switchMode(
|
|
|
BleConst.STATUS_READY,
|
|
|
bleDevice,
|
|
|
@@ -209,26 +236,101 @@ object BleBusinessManager {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取工作票完成情况
|
|
|
+ * 工作票下发结果
|
|
|
+ * res:0x00:成功 0x01:失败 0x02:传输超时 0x0D:当前IDX超出范围 0x0E:当前数据CRC校验失败 0x14:JSON结构错误 0x63:未知错误
|
|
|
*/
|
|
|
- private fun getTicketStatus(
|
|
|
+ private fun handleWorkTicketResult(
|
|
|
+ bleBean: BleBean, byteArray: ByteArray, isNeedLoading: Boolean = false
|
|
|
+ ) {
|
|
|
+ BleCmdManager.handleWorkTicketResult(bleBean, byteArray) { isSuccess, rst ->
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent()
|
|
|
+ if (isSuccess) {
|
|
|
+ // 下发完毕,切换工作模式
|
|
|
+ logger.info("工作票下发完毕")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("切换钥匙为工作模式", true)
|
|
|
+ Executor.delayOnIO(800) {
|
|
|
+ //切换到工作模式
|
|
|
+ switchWorkMode(bleBean.bleDevice, isNeedLoading)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ LoadingEvent.sendLoadingEvent()
|
|
|
+ if (bleBean.retryCount < 3) {
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ bleBean.retryCount++
|
|
|
+ LoadingEvent.sendLoadingEvent(
|
|
|
+ CommonUtils.getStr(R.string.start_to_send_ticket),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ sendTicketWithRetry(bleBean.ticketSend!!, bleBean.bleDevice, isNeedLoading)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ PopTip.tip(R.string.send_ticket_fail)
|
|
|
+ logger.error("Send ticket fail")
|
|
|
+ ModBusController.getKeyByMac(bleBean.bleDevice.mac)?.let { itKey ->
|
|
|
+ ModbusBusinessManager.mDeviceTakeList.removeIf { it.deviceType == DeviceConst.DEVICE_TYPE_KEY && it.nfc == itKey.rfid }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 带重试的下发工作票,重试次数3,间隔500ms
|
|
|
+ */
|
|
|
+ private fun sendTicketWithRetry(
|
|
|
+ json: String,
|
|
|
bleDevice: BleDevice,
|
|
|
isNeedLoading: Boolean = false,
|
|
|
- processCallback: ((Boolean) -> Unit)? = null
|
|
|
+ maxRetries: Int = 3,
|
|
|
+ delayMillis: Long = 500
|
|
|
) {
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent("开始获取工作票", true)
|
|
|
- BleCmdManager.getTicketStatus(bleDevice, object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票获取成功", true)
|
|
|
- logger.info("getTicketStatus success")
|
|
|
- }
|
|
|
+ var retryCount = 0
|
|
|
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票获取失败", true)
|
|
|
- processCallback?.invoke(false)
|
|
|
- logger.error("getTicketStatus fail")
|
|
|
+ fun attemptSend() {
|
|
|
+ sendTicket(json, bleDevice, isNeedLoading) { sendRst ->
|
|
|
+ if (!sendRst && retryCount < maxRetries) {
|
|
|
+ retryCount++
|
|
|
+ // 等待一段时间后再次尝试
|
|
|
+ Executor.delayOnMain(delayMillis) {
|
|
|
+ logger.info("Retry attempt, mac : ${bleDevice.mac}, retryCount : $retryCount")
|
|
|
+ attemptSend()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- })
|
|
|
+ }
|
|
|
+
|
|
|
+ attemptSend()
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun sendTicket(
|
|
|
+ jsonStr: String,
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ isNeedLoading: Boolean = false,
|
|
|
+ processCallback: ((Boolean) -> Unit)? = null
|
|
|
+ ) {
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
+ CommonUtils.getStr(R.string.start_to_send_ticket),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ BleCmdManager.sendWorkTicket(
|
|
|
+ jsonStr, bleDevice = bleDevice, callback = object : CustomBleWriteCallback() {
|
|
|
+ override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
+ logger.info("sendTicket success")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
+ CommonUtils.getStr(R.string.sending_ticket),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onWriteFailure(exception: BleException?) {
|
|
|
+ logger.error("sendTicket fail : ${bleDevice.mac}")
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
+ CommonUtils.getStr(R.string.send_ticket_fail),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ processCallback?.invoke(false)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -306,6 +408,287 @@ object BleBusinessManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 读取工作票完成情况
|
|
|
+ */
|
|
|
+ private fun getTicketStatusBusiness(
|
|
|
+ mac: String, isNeedLoading: Boolean = false
|
|
|
+ ) {
|
|
|
+ BleConnectionManager.registerConnectListener(mac) { isDone, bleBean ->
|
|
|
+ if (isDone) {
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ getTicketStatusWithRetry(bleBean!!.bleDevice, isNeedLoading)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun getTicketStatusWithRetry(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ isNeedLoading: Boolean = false,
|
|
|
+ maxRetries: Int = 3,
|
|
|
+ delayMillis: Long = 500
|
|
|
+ ) {
|
|
|
+ var retryCount = 0
|
|
|
+
|
|
|
+ fun attemptSend() {
|
|
|
+ getTicketStatus(bleDevice, isNeedLoading) { sendRst ->
|
|
|
+ if (!sendRst && retryCount < maxRetries) {
|
|
|
+ retryCount++
|
|
|
+ // 等待一段时间后再次尝试
|
|
|
+ Executor.delayOnMain(delayMillis) {
|
|
|
+ logger.info("Retry attempt, mac : ${bleDevice.mac}, retryCount : $retryCount")
|
|
|
+ attemptSend()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ attemptSend()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取工作票完成情况
|
|
|
+ */
|
|
|
+ private fun getTicketStatus(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ isNeedLoading: Boolean = false,
|
|
|
+ processCallback: ((Boolean) -> Unit)? = null
|
|
|
+ ) {
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("开始获取工作票", true)
|
|
|
+ BleCmdManager.getTicketStatus(bleDevice, object : CustomBleWriteCallback() {
|
|
|
+ override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票获取成功", true)
|
|
|
+ logger.info("getTicketStatus success")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onWriteFailure(exception: BleException?) {
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票获取失败", true)
|
|
|
+ processCallback?.invoke(false)
|
|
|
+ logger.error("getTicketStatus fail")
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理工作票完成情况
|
|
|
+ */
|
|
|
+ private fun handleTicketStatus(
|
|
|
+ bleDevice: BleDevice, byteArray: ByteArray, isNeedLoading: Boolean = false
|
|
|
+ ) {
|
|
|
+ BleCmdManager.handleTicketStatus(bleDevice, byteArray) { ticketJson ->
|
|
|
+ if (ticketJson.isNullOrEmpty()) {
|
|
|
+ return@handleTicketStatus
|
|
|
+ }
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("工作票完成状态读取完成", true)
|
|
|
+ logger.info("Get ticket status complete : ${bleDevice.mac}")
|
|
|
+ // TD:Ticket Done
|
|
|
+ if (isNeedLoading) LoadingEvent.sendLoadingEvent("TD$ticketJson}", true)
|
|
|
+
|
|
|
+ val workTicketGet = try {
|
|
|
+ Gson().fromJson(ticketJson, WorkTicketGet::class.java)
|
|
|
+ } catch (e: Exception) {
|
|
|
+ null
|
|
|
+ }
|
|
|
+ if (workTicketGet == null) {
|
|
|
+ PopTip.tip(R.string.ticket_data_error)
|
|
|
+ return@handleTicketStatus
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 判断WorkTicketGet里是否有未完成的
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ val finishedStatus = workTicketGet.hasFinished()
|
|
|
+ logger.info("作业票结束情况:${finishedStatus}")
|
|
|
+ if (finishedStatus.first) {
|
|
|
+ Executor.delayOnMain(500) {
|
|
|
+ handleKeyReturn(bleDevice, workTicketGet, finishedStatus.second)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 当前策略:作业票未完成禁止归还钥匙
|
|
|
+ TipDialog.show(
|
|
|
+ msg = CommonUtils.getStr(R.string.key_return_tip)!!,
|
|
|
+ onConfirmClick = {
|
|
|
+ LoadingEvent.sendLoadingEvent()
|
|
|
+ PopTip.tip(CommonUtils.getStr(R.string.continue_the_ticket))
|
|
|
+ BleManager.getInstance().disconnect(bleDevice)
|
|
|
+ // 打开卡扣,防止初始化的时候选择不处理钥匙导致无法使用
|
|
|
+ val dock = ModBusController.getDockByKeyMac(bleDevice.mac)
|
|
|
+ val keyBean = dock?.getKeyList()?.find { it.mac == bleDevice.mac }
|
|
|
+ keyBean?.let {
|
|
|
+ ModBusController.controlKeyBuckle(true, keyBean.idx, dock.addr)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理钥匙归还
|
|
|
+ */
|
|
|
+ private fun handleKeyReturn(
|
|
|
+ bleDevice: BleDevice,
|
|
|
+ workTicketGet: WorkTicketGet?,
|
|
|
+ ticketFinished: Boolean
|
|
|
+ ) {
|
|
|
+ val dock = ModBusController.getDockByKeyMac(bleDevice.mac)
|
|
|
+ val keyBean = dock?.getKeyList()?.find { it.mac == bleDevice.mac }
|
|
|
+ keyBean?.let {
|
|
|
+ ModBusController.controlKeyBuckle(false, keyBean.idx, dock.addr)
|
|
|
+ }
|
|
|
+ if (ticketFinished) {
|
|
|
+ switchReadyMode(bleDevice)
|
|
|
+ } else {
|
|
|
+ // 上报隔离点状态
|
|
|
+ val keyNfc = ModBusController.getKeyByMac(bleDevice.mac)?.rfid
|
|
|
+ workTicketGet?.data?.forEach { data ->
|
|
|
+ val updateList = mutableListOf<LockPointUpdateReq>()
|
|
|
+ data.dataList?.forEach { dataListDTO ->
|
|
|
+ data.taskCode?.toLong()?.let {
|
|
|
+ SPUtils.returnKey(it)
|
|
|
+ }
|
|
|
+ val updateVO = LockPointUpdateReq(
|
|
|
+ data.taskCode?.toLong(),
|
|
|
+ dataListDTO.infoRfidNo,
|
|
|
+ dataListDTO.equipRfidNo,
|
|
|
+ keyNfc!!,
|
|
|
+ dataListDTO.target,
|
|
|
+ dataListDTO.status
|
|
|
+ )
|
|
|
+ updateList.add(updateVO)
|
|
|
+ }
|
|
|
+
|
|
|
+ LoadingEvent.sendLoadingEvent()
|
|
|
+ // 上报点位钥匙绑定
|
|
|
+ RepositoryManager.jobTicketRepo.updateLockPointBatch(updateList) { isSuccess, msg, code ->
|
|
|
+ logger.info("还锁操作:${isSuccess},${msg},${code}")
|
|
|
+ if (isSuccess) {
|
|
|
+ // 上报钥匙归还
|
|
|
+ RepositoryManager.jobTicketRepo.updateKeyReturn(
|
|
|
+ data.taskCode?.toLong()!!,
|
|
|
+ keyNfc!!,
|
|
|
+ SIKCore.getApplication().serialNo()
|
|
|
+ ) { isSuccess, msg, code ->
|
|
|
+ if (!isSuccess && msg != SIKCore.getApplication().getString(
|
|
|
+ R.string.ticket_lost
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ SPUtils.saveUpdateKeyReturn(
|
|
|
+ SIKCore.getApplication(),
|
|
|
+ UpdateKeyReturn(data.taskCode?.toLong()!!, keyNfc!!)
|
|
|
+ )
|
|
|
+ if (msg == CommonUtils.getStr(R.string.ticket_lost)) {
|
|
|
+ data.taskCode?.let {
|
|
|
+ TicketFinishedEvent.sendTicketFinishedEvent(it.toLong())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ PopTip.tip(R.string.key_return_success)
|
|
|
+ } else {
|
|
|
+ //更新作业票的状态,如果是上锁就更新到共锁,如果是解锁就更新到解锁之后的步骤
|
|
|
+ RepositoryManager.jobTicketRepo.updateTicketDataStatus(
|
|
|
+ data.taskCode?.toLong()!!,
|
|
|
+ if (data.dataList?.any { it.status == 0 } == true) LockStepEnum.COLOCK.type else LockStepEnum.UNLOCKED.type
|
|
|
+ )
|
|
|
+ PopTip.tip(R.string.key_return_success)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data.taskCode?.toLong()?.let {
|
|
|
+ UpdateTicketProgressEvent.sendUpdateTicketProgressEvent(it)
|
|
|
+ }
|
|
|
+ // 确认归还,切换为待机模式
|
|
|
+ switchReadyMode(bleDevice)
|
|
|
+ } else {
|
|
|
+ ThreadUtils.runOnMain {
|
|
|
+ // 当前策略:作业票未完成禁止归还钥匙
|
|
|
+ fun keyReturnErrorConfirm() {
|
|
|
+ LoadingEvent.sendLoadingEvent()
|
|
|
+ PopTip.tip(R.string.continue_the_ticket)
|
|
|
+ BleManager.getInstance().disconnect(bleDevice)
|
|
|
+ // 打开卡扣,防止初始化的时候选择不处理钥匙导致无法使用
|
|
|
+ if (workTicketGet.data?.all { it.dataList?.all { it.closed == 1 } == true } == true) {
|
|
|
+ workTicketGet.data?.firstOrNull()?.taskCode?.toLong()
|
|
|
+ ?.let {
|
|
|
+ checkStepAndTicketDetailThenSendTicket(
|
|
|
+ it,
|
|
|
+ bleDevice.mac
|
|
|
+ )
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ val dock = ModBusController.getDockByKeyMac(bleDevice.mac)
|
|
|
+ val keyBean =
|
|
|
+ dock?.getKeyList()?.find { it.mac == bleDevice.mac }
|
|
|
+ keyBean?.let {
|
|
|
+ ModBusController.controlKeyBuckle(
|
|
|
+ true, keyBean.idx, dock.addr
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ TipDialog.show(
|
|
|
+ msg = CommonUtils.getStr(R.string.key_return_tip).toString(),
|
|
|
+ onCancelClick = {
|
|
|
+ keyReturnErrorConfirm()
|
|
|
+ },
|
|
|
+ onConfirmClick = {
|
|
|
+ keyReturnErrorConfirm()
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+ SPUtils.clearUpdateKeyReturn(SIKCore.getApplication())
|
|
|
+ SPUtils.clearUpdateLockPoint(SIKCore.getApplication())
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查步骤和作业票详情并且下发作业票
|
|
|
+ */
|
|
|
+ private fun checkStepAndTicketDetailThenSendTicket(ticketId: Long, mac: String) {
|
|
|
+ RepositoryManager.jobTicketRepo.getStepDetail(ticketId) {
|
|
|
+ var step = 0
|
|
|
+ it?.filter { it.stepStatus == "1" }
|
|
|
+ ?.maxByOrNull { it.stepIndex!! }?.stepIndex?.let {
|
|
|
+ step = it
|
|
|
+ }
|
|
|
+ RepositoryManager.jobTicketRepo.getTicketDetail(ticketId) { ticketDetail ->
|
|
|
+ if (ticketDetail == null) {
|
|
|
+ return@getTicketDetail
|
|
|
+ }
|
|
|
+ val role = ticketDetail?.ticketUserVOList?.find {
|
|
|
+ it.userId == SPUtils.getLoginUser(SIKCore.getApplication())?.userId && it.userType == Constants.USER_TYPE_LOCKER
|
|
|
+ }
|
|
|
+ if (role == null) {
|
|
|
+ PopTip.tip(R.string.you_are_not_locker_tip)
|
|
|
+ return@getTicketDetail
|
|
|
+ }
|
|
|
+ if (step == 4) { // 上锁工作票
|
|
|
+ sendTicketBusiness(
|
|
|
+ true,
|
|
|
+ mac,
|
|
|
+ ticketDetail,
|
|
|
+ ticketDetail.ticketLockVOList?.filter { it.lockStatus != "2" }
|
|
|
+ ?.map { it.lockNfc }?.toMutableList(),
|
|
|
+ true
|
|
|
+ )
|
|
|
+ } else if (step == 7) { // 解锁工作票
|
|
|
+ sendTicketBusiness(
|
|
|
+ false,
|
|
|
+ mac,
|
|
|
+ ticketDetail,
|
|
|
+ null,
|
|
|
+ true
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 分配钥匙
|
|
|
*/
|
|
|
@@ -366,47 +749,6 @@ object BleBusinessManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 读取工作票完成情况
|
|
|
- */
|
|
|
- private fun getTicketStatusBusiness(
|
|
|
- mac: String, isNeedLoading: Boolean = false
|
|
|
- ) {
|
|
|
- BleConnectionManager.registerConnectListener(mac) { isDone, bleBean ->
|
|
|
- if (isDone) {
|
|
|
- Executor.delayOnMain(500) {
|
|
|
- getTicketStatusWithRetry(bleBean!!.bleDevice, isNeedLoading)
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private fun getTicketStatusWithRetry(
|
|
|
- bleDevice: BleDevice,
|
|
|
- isNeedLoading: Boolean = false,
|
|
|
- maxRetries: Int = 3,
|
|
|
- delayMillis: Long = 500
|
|
|
- ) {
|
|
|
- var retryCount = 0
|
|
|
-
|
|
|
- fun attemptSend() {
|
|
|
- getTicketStatus(bleDevice, isNeedLoading) { sendRst ->
|
|
|
- if (!sendRst && retryCount < maxRetries) {
|
|
|
- retryCount++
|
|
|
- // 等待一段时间后再次尝试
|
|
|
- Executor.delayOnMain(delayMillis) {
|
|
|
- logger.info("Retry attempt, mac : ${bleDevice.mac}, retryCount : $retryCount")
|
|
|
- attemptSend()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- attemptSend()
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 连接一把存在的可连接的钥匙
|
|
|
*/
|
|
|
@@ -536,63 +878,4 @@ object BleBusinessManager {
|
|
|
logger.info("json : $jsonStr")
|
|
|
return jsonStr
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 带重试的下发工作票,重试次数3,间隔500ms
|
|
|
- */
|
|
|
- private fun sendTicketWithRetry(
|
|
|
- json: String,
|
|
|
- bleDevice: BleDevice,
|
|
|
- isNeedLoading: Boolean = false,
|
|
|
- maxRetries: Int = 3,
|
|
|
- delayMillis: Long = 500
|
|
|
- ) {
|
|
|
- var retryCount = 0
|
|
|
-
|
|
|
- fun attemptSend() {
|
|
|
- sendTicket(json, bleDevice, isNeedLoading) { sendRst ->
|
|
|
- if (!sendRst && retryCount < maxRetries) {
|
|
|
- retryCount++
|
|
|
- // 等待一段时间后再次尝试
|
|
|
- Executor.delayOnMain(delayMillis) {
|
|
|
- logger.info("Retry attempt, mac : ${bleDevice.mac}, retryCount : $retryCount")
|
|
|
- attemptSend()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- attemptSend()
|
|
|
- }
|
|
|
-
|
|
|
- private fun sendTicket(
|
|
|
- jsonStr: String,
|
|
|
- bleDevice: BleDevice,
|
|
|
- isNeedLoading: Boolean = false,
|
|
|
- processCallback: ((Boolean) -> Unit)? = null
|
|
|
- ) {
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
- CommonUtils.getStr(R.string.start_to_send_ticket),
|
|
|
- true
|
|
|
- )
|
|
|
- BleCmdManager.sendWorkTicket(
|
|
|
- jsonStr, bleDevice = bleDevice, callback = object : CustomBleWriteCallback() {
|
|
|
- override fun onWriteSuccess(current: Int, total: Int, justWrite: ByteArray?) {
|
|
|
- logger.info("sendTicket success")
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
- CommonUtils.getStr(R.string.sending_ticket),
|
|
|
- true
|
|
|
- )
|
|
|
- }
|
|
|
-
|
|
|
- override fun onWriteFailure(exception: BleException?) {
|
|
|
- logger.error("sendTicket fail : ${bleDevice.mac}")
|
|
|
- if (isNeedLoading) LoadingEvent.sendLoadingEvent(
|
|
|
- CommonUtils.getStr(R.string.send_ticket_fail),
|
|
|
- true
|
|
|
- )
|
|
|
- processCallback?.invoke(false)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
}
|