|
@@ -3,6 +3,7 @@ package com.grkj.iscs.view.viewmodel
|
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.lifecycle.liveData
|
|
import androidx.lifecycle.liveData
|
|
|
|
|
+import com.clj.fastble.BleManager
|
|
|
import com.google.gson.Gson
|
|
import com.google.gson.Gson
|
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.google.gson.reflect.TypeToken
|
|
|
import com.grkj.iscs.BusinessManager
|
|
import com.grkj.iscs.BusinessManager
|
|
@@ -22,6 +23,7 @@ import com.sik.sikcore.thread.ThreadUtils
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger
|
|
|
import kotlin.collections.iterator
|
|
import kotlin.collections.iterator
|
|
|
import kotlin.coroutines.resume
|
|
import kotlin.coroutines.resume
|
|
|
|
|
|
|
@@ -30,13 +32,17 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
val isLoadComplete: MutableLiveData<Boolean> = MutableLiveData(false)
|
|
val isLoadComplete: MutableLiveData<Boolean> = MutableLiveData(false)
|
|
|
var isStartCheckKey: Boolean = false
|
|
var isStartCheckKey: Boolean = false
|
|
|
var isDestroy: Boolean = false
|
|
var isDestroy: Boolean = false
|
|
|
|
|
+ var newHardwareKeySize: Int = 0
|
|
|
|
|
+ var newHardwareLockSize: Int = 0
|
|
|
private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
|
|
|
|
|
|
fun initData(rowList: MutableList<DockStatusBO>) {
|
|
fun initData(rowList: MutableList<DockStatusBO>) {
|
|
|
- val dockConfigJson = SPUtils.getDockConfig(MyApplication.Companion.instance?.applicationContext!!)
|
|
|
|
|
|
|
+ val dockConfigJson =
|
|
|
|
|
+ SPUtils.getDockConfig(MyApplication.Companion.instance?.applicationContext!!)
|
|
|
if (!dockConfigJson.isNullOrEmpty()) {
|
|
if (!dockConfigJson.isNullOrEmpty()) {
|
|
|
val tempList: MutableList<DockTestFragment.DockTestBean> = Gson().fromJson(
|
|
val tempList: MutableList<DockTestFragment.DockTestBean> = Gson().fromJson(
|
|
|
- dockConfigJson, object : TypeToken<MutableList<DockTestFragment.DockTestBean>>() {}.type
|
|
|
|
|
|
|
+ dockConfigJson,
|
|
|
|
|
+ object : TypeToken<MutableList<DockTestFragment.DockTestBean>>() {}.type
|
|
|
)
|
|
)
|
|
|
if (tempList.isNotEmpty()) {
|
|
if (tempList.isNotEmpty()) {
|
|
|
tempList.forEach { dock ->
|
|
tempList.forEach { dock ->
|
|
@@ -65,6 +71,20 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ fun checkNewHardware(device: DockBean.DeviceBean, callback: () -> Unit) {
|
|
|
|
|
+ if (device is DockBean.KeyBean) {
|
|
|
|
|
+ NetApi.getKeyInfo(device.rfid.toString()) {
|
|
|
|
|
+ device.newHardware = it == null
|
|
|
|
|
+ callback()
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (device is DockBean.LockBean) {
|
|
|
|
|
+ NetApi.getLockInfo(device.rfid.toString()) {
|
|
|
|
|
+ device.newHardware = it == null
|
|
|
|
|
+ callback()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
fun registerInitListener(): LiveData<Boolean> {
|
|
fun registerInitListener(): LiveData<Boolean> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
isStartCheckKey = false
|
|
isStartCheckKey = false
|
|
@@ -73,36 +93,59 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
val allDeviceCloseCmdSend =
|
|
val allDeviceCloseCmdSend =
|
|
|
BleConnectionManager.scanOnlineKeyLockMacAndSwitchModeToClose()
|
|
BleConnectionManager.scanOnlineKeyLockMacAndSwitchModeToClose()
|
|
|
LogUtil.i("设备录入-是否所有关闭命令发送成功:${allDeviceCloseCmdSend}")
|
|
LogUtil.i("设备录入-是否所有关闭命令发送成功:${allDeviceCloseCmdSend}")
|
|
|
|
|
+ BleManager.getInstance().disconnectAllDevice()
|
|
|
BusinessManager.registerInitListener {
|
|
BusinessManager.registerInitListener {
|
|
|
if (isStartCheckKey) {
|
|
if (isStartCheckKey) {
|
|
|
return@registerInitListener
|
|
return@registerInitListener
|
|
|
}
|
|
}
|
|
|
isStartCheckKey = true
|
|
isStartCheckKey = true
|
|
|
val dockList = ModBusController.dockList
|
|
val dockList = ModBusController.dockList
|
|
|
-
|
|
|
|
|
- newHardwareKeyBean.putAll(dockList.filter { it.type == DeviceConst.DOCK_TYPE_KEY || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
|
|
|
|
|
- .associate {
|
|
|
|
|
- it.addr to it.deviceList.filterIsInstance<DockBean.KeyBean>()
|
|
|
|
|
- .filter { it.newHardware && it.isExist }
|
|
|
|
|
- .toMutableList()
|
|
|
|
|
- })
|
|
|
|
|
- LogUtil.d("设备录入-新设备:${newHardwareKeyBean}")
|
|
|
|
|
ThreadUtils.runOnIO {
|
|
ThreadUtils.runOnIO {
|
|
|
- for ((addr, keyBeans) in newHardwareKeyBean) {
|
|
|
|
|
- for (keyBean in keyBeans) {
|
|
|
|
|
- if (isDestroy) {
|
|
|
|
|
- return@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)
|
|
|
}
|
|
}
|
|
|
- openChargeAndScanMac(addr, keyBean)
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- isLoadComplete.postValue(true)
|
|
|
|
|
emit(true)
|
|
emit(true)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private fun checkNewHardwareKey(dockList: MutableList<DockBean>) {
|
|
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
|
|
+ LogUtil.i("设备录入-重新检测是否是新设备完成")
|
|
|
|
|
+ newHardwareKeyBean.putAll(dockList.filter { it.type == DeviceConst.DOCK_TYPE_KEY || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
|
|
|
|
|
+ .associate {
|
|
|
|
|
+ it.addr to it.deviceList.filterIsInstance<DockBean.KeyBean>()
|
|
|
|
|
+ .filter { it.newHardware && it.isExist }
|
|
|
|
|
+ .toMutableList()
|
|
|
|
|
+ })
|
|
|
|
|
+ LogUtil.d("设备录入-新设备:${newHardwareKeyBean}")
|
|
|
|
|
+ for ((addr, keyBeans) in newHardwareKeyBean) {
|
|
|
|
|
+ for (keyBean in keyBeans) {
|
|
|
|
|
+ if (isDestroy) {
|
|
|
|
|
+ return@runOnIO
|
|
|
|
|
+ }
|
|
|
|
|
+ openChargeAndScanMac(addr, keyBean)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ newHardwareKeySize = newHardwareKeyBean.map { it.value }.flatten().size
|
|
|
|
|
+ newHardwareLockSize =
|
|
|
|
|
+ ModBusController.dockList.filter { it.type == DeviceConst.DOCK_TYPE_LOCK || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
|
|
|
|
|
+ .map { it.deviceList }.flatten().filterIsInstance<DockBean.LockBean>()
|
|
|
|
|
+ .count { it.newHardware && it.isExist }
|
|
|
|
|
+ isLoadComplete.postValue(true)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 打开充电并扫描蓝牙
|
|
* 打开充电并扫描蓝牙
|
|
|
*/
|
|
*/
|
|
@@ -111,7 +154,7 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
LogUtil.i("设备录入-关闭充电:${addr},${keyBean.idx}")
|
|
LogUtil.i("设备录入-关闭充电:${addr},${keyBean.idx}")
|
|
|
ModBusController.controlKeyCharge(false, keyBean.isLeft, addr) {
|
|
ModBusController.controlKeyCharge(false, keyBean.isLeft, addr) {
|
|
|
ThreadUtils.runOnIO {
|
|
ThreadUtils.runOnIO {
|
|
|
- delay(3000)
|
|
|
|
|
|
|
+ delay(800)
|
|
|
LogUtil.i("设备录入-打开充电:${addr},${keyBean.idx}")
|
|
LogUtil.i("设备录入-打开充电:${addr},${keyBean.idx}")
|
|
|
ModBusController.controlKeyLockAndCharge(true, keyBean.isLeft, addr) {
|
|
ModBusController.controlKeyLockAndCharge(true, keyBean.isLeft, addr) {
|
|
|
ThreadUtils.runOnIO {
|
|
ThreadUtils.runOnIO {
|
|
@@ -125,11 +168,12 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
) { it.mac }
|
|
) { it.mac }
|
|
|
}"
|
|
}"
|
|
|
)
|
|
)
|
|
|
|
|
+ if (isDestroy) {
|
|
|
|
|
+ cont.cancel()
|
|
|
|
|
+ return@scanOnlineKeyLockMac
|
|
|
|
|
+ }
|
|
|
if (bleDevices?.isEmpty() == true) {
|
|
if (bleDevices?.isEmpty() == true) {
|
|
|
ThreadUtils.runOnIO {
|
|
ThreadUtils.runOnIO {
|
|
|
- if (isDestroy) {
|
|
|
|
|
- return@runOnIO
|
|
|
|
|
- }
|
|
|
|
|
openChargeAndScanMac(addr, keyBean)
|
|
openChargeAndScanMac(addr, keyBean)
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
@@ -142,10 +186,23 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
}?.mac
|
|
}?.mac
|
|
|
}"
|
|
}"
|
|
|
)
|
|
)
|
|
|
- keyBean.mac = bleDevices?.find {
|
|
|
|
|
|
|
+ val bleDevice = bleDevices?.find {
|
|
|
it.mac !in newHardwareKeyBean.map { it.value }.flatten()
|
|
it.mac !in newHardwareKeyBean.map { it.value }.flatten()
|
|
|
.map { it.mac }
|
|
.map { it.mac }
|
|
|
- }?.mac
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ keyBean.mac = bleDevice?.mac
|
|
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
|
|
+ keyBean.mac?.let {
|
|
|
|
|
+ val connected =
|
|
|
|
|
+ BleConnectionManager.tryConnectWithOptionalCharge(it)
|
|
|
|
|
+ if (connected) {
|
|
|
|
|
+ bleDevice?.let {
|
|
|
|
|
+ BleConnectionManager.switchReadyMode(it)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
cont.resume(true)
|
|
cont.resume(true)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -198,8 +255,11 @@ class DeviceInputKeyAndLockViewModel : BaseViewModel() {
|
|
|
.filterIsInstance<DockBean.LockBean>().filter { it.newHardware == true }
|
|
.filterIsInstance<DockBean.LockBean>().filter { it.newHardware == true }
|
|
|
val keyDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_KEY }
|
|
val keyDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_KEY }
|
|
|
.filterIsInstance<DockBean.KeyBean>().filter { it.newHardware == true }
|
|
.filterIsInstance<DockBean.KeyBean>().filter { it.newHardware == true }
|
|
|
- lockDevice.filter { it.rfid?.isNotEmpty() == true }.map { it.rfid }.forEach { rfid ->
|
|
|
|
|
- deviceInputLockSuspend(rfid.toString())
|
|
|
|
|
|
|
+ lockDevice.filter { it.rfid?.isNotEmpty() == true }.forEach { lockDevice ->
|
|
|
|
|
+ val isBind = deviceInputLockSuspend(lockDevice.rfid.toString())
|
|
|
|
|
+ if (isBind) {
|
|
|
|
|
+ lockDevice.newHardware = false
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
keyDevice.filter { it.rfid?.isNotEmpty() == true && it.mac?.isNotEmpty() == true }
|
|
keyDevice.filter { it.rfid?.isNotEmpty() == true && it.mac?.isNotEmpty() == true }
|
|
|
.forEach { keyDevice ->
|
|
.forEach { keyDevice ->
|