|
|
@@ -1,5 +1,6 @@
|
|
|
package com.grkj.iscs.features.init.viewmodel
|
|
|
|
|
|
+import android.bluetooth.BluetoothDevice
|
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.liveData
|
|
|
@@ -13,12 +14,16 @@ import com.grkj.ui_base.utils.ble.BleConnectionManager
|
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
|
import com.grkj.ui_base.utils.modbus.DockBean
|
|
|
import com.grkj.ui_base.utils.modbus.ModBusController
|
|
|
+import com.sik.sikcore.bluetooth.BLEScanner
|
|
|
+import com.sik.sikcore.bluetooth.BluetoothManager
|
|
|
+import com.sik.sikcore.bluetooth.IBluetoothScanCallback
|
|
|
import com.sik.sikcore.thread.ThreadUtils
|
|
|
import com.tencent.mmkv.MMKV
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
import javax.inject.Inject
|
|
|
import kotlin.coroutines.resume
|
|
|
@@ -30,6 +35,7 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
var isDestroy: Boolean = false
|
|
|
var newHardwareKeySize: Int = 0
|
|
|
var newHardwareLockSize: Int = 0
|
|
|
+ var isInDeviceInit: Boolean = false
|
|
|
private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
|
private val alreadyUsedMac: MutableList<String> = mutableListOf()
|
|
|
|
|
|
@@ -58,30 +64,21 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
newHardwareKeyBean.clear()
|
|
|
alreadyUsedMac.clear()
|
|
|
isLoadComplete.postValue(false)
|
|
|
- ModbusBusinessManager.registerInitListener {
|
|
|
- if (!ISCSConfig.canInitDevice) {
|
|
|
- return@registerInitListener
|
|
|
- }
|
|
|
- ISCSConfig.canInitDevice = false
|
|
|
- val dockList = ModBusController.dockList
|
|
|
- if (dockList.size < DockBean.dockConfig.size) {
|
|
|
- ISCSConfig.canInitDevice = true
|
|
|
- return@registerInitListener
|
|
|
- }
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- val allDevice = ModBusController.dockList.map { it.deviceList }.flatten()
|
|
|
- .filter { it is DockBean.KeyBean || it is DockBean.LockBean }
|
|
|
- .filter { it.isExist }
|
|
|
- var currentCheckDevices = AtomicInteger(0)
|
|
|
- allDevice.forEach { device ->
|
|
|
- checkNewHardware(device) {
|
|
|
- currentCheckDevices.addAndGet(1)
|
|
|
- if (currentCheckDevices.get() == allDevice.size) {
|
|
|
- checkNewHardwareKey(dockList)
|
|
|
- }
|
|
|
- }
|
|
|
+ ModBusController.controlAllKeyBuckleOpen{
|
|
|
+ ModbusBusinessManager.registerInitListener {
|
|
|
+ if (!ISCSConfig.canInitDevice || isInDeviceInit) {
|
|
|
+ return@registerInitListener
|
|
|
+ }
|
|
|
+ isInDeviceInit = true
|
|
|
+ val dockList = ModBusController.dockList
|
|
|
+ if (dockList.size < DockBean.dockConfig.size) {
|
|
|
+ isInDeviceInit = true
|
|
|
+ return@registerInitListener
|
|
|
+ }
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ checkNewHardwareKey(dockList)
|
|
|
+ emit(true)
|
|
|
}
|
|
|
- emit(true)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -144,32 +141,19 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
ThreadUtils.runOnIO {
|
|
|
delay(3000)
|
|
|
logger.info("设备录入-开始扫描在线蓝牙Mac")
|
|
|
- BleConnectionManager.scanOnlineKeyLockMac(alreadyUsedMac) { bleDevice ->
|
|
|
- logger.info(
|
|
|
- "设备录入-在线的蓝牙设备:${keyBean.rfid},${
|
|
|
- bleDevice?.mac
|
|
|
- }"
|
|
|
- )
|
|
|
- if (isDestroy) {
|
|
|
+ withContext(Dispatchers.Main){
|
|
|
+ BleConnectionManager.scanOnlineKeyLockMac(alreadyUsedMac) { mac ->
|
|
|
logger.info(
|
|
|
- "设备录入-界面已销毁"
|
|
|
+ "设备录入-在线的蓝牙设备:${keyBean.rfid},${mac}"
|
|
|
)
|
|
|
- cont.cancel()
|
|
|
- return@scanOnlineKeyLockMac
|
|
|
- }
|
|
|
- if (bleDevice == null) {
|
|
|
- logger.info(
|
|
|
- "设备录入-设备空:${keyBean.rfid}"
|
|
|
- )
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- if (retryCount > 0) {
|
|
|
- openChargeAndScanMac(addr, keyBean, retryCount - 1)
|
|
|
- } else {
|
|
|
- cont.resume(false)
|
|
|
- }
|
|
|
+ if (isDestroy) {
|
|
|
+ logger.info(
|
|
|
+ "设备录入-界面已销毁"
|
|
|
+ )
|
|
|
+ cont.cancel()
|
|
|
+ return@scanOnlineKeyLockMac
|
|
|
}
|
|
|
- } else {
|
|
|
- if (bleDevice.mac == null) {
|
|
|
+ if (mac == null) {
|
|
|
logger.info(
|
|
|
"设备录入-设备mac空:${keyBean.rfid}"
|
|
|
)
|
|
|
@@ -184,11 +168,11 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
}
|
|
|
logger.info(
|
|
|
"设备录入-没有使用过的mac:${keyBean.rfid},${
|
|
|
- bleDevice.mac
|
|
|
+ mac
|
|
|
}"
|
|
|
)
|
|
|
- keyBean.mac = bleDevice.mac
|
|
|
- alreadyUsedMac.add(bleDevice.mac)
|
|
|
+ keyBean.mac = mac
|
|
|
+ alreadyUsedMac.add(mac)
|
|
|
if (cont.isActive) {
|
|
|
cont.resume(true)
|
|
|
}
|