|
|
@@ -10,8 +10,8 @@ import com.grkj.iscs_mars.MyApplication
|
|
|
import com.grkj.iscs_mars.R
|
|
|
import com.grkj.iscs_mars.ble.BleConnectionManager
|
|
|
import com.grkj.iscs_mars.ble.BleSendDispatcher
|
|
|
-import com.grkj.iscs_mars.modbus.DockBean
|
|
|
-import com.grkj.iscs_mars.modbus.ModBusController
|
|
|
+import com.grkj.iscs_mars.can.DockData
|
|
|
+import com.grkj.iscs_mars.enums.HardwareMode
|
|
|
import com.grkj.iscs_mars.model.DeviceConst
|
|
|
import com.grkj.iscs_mars.model.bo.DockStatusBO
|
|
|
import com.grkj.iscs_mars.model.bo.LoginUserBO
|
|
|
@@ -27,7 +27,6 @@ import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
-import kotlin.collections.iterator
|
|
|
import kotlin.coroutines.resume
|
|
|
|
|
|
class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
@@ -38,23 +37,21 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
var newHardwareKeySize: Int = 0
|
|
|
var newHardwareLockSize: Int = 0
|
|
|
private var isInputting: Boolean = false
|
|
|
- private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
|
+ private val newHardwareKeyBean: MutableMap<Int, MutableList<DockData.KeyDock.KeyBean>> = mutableMapOf()
|
|
|
private val alreadyUsedMac: MutableList<String> = mutableListOf()
|
|
|
|
|
|
+ // 硬件操作工具
|
|
|
+ private val hwOpt = HardwareMode.getCurrentHardwareMode()
|
|
|
+
|
|
|
fun initData(rowList: MutableList<DockStatusBO>) {
|
|
|
- val dockConfigJson =
|
|
|
- SPUtils.getDockConfig(MyApplication.Companion.instance?.applicationContext!!)
|
|
|
+ val dockConfigJson = SPUtils.getDockConfig(MyApplication.Companion.instance?.applicationContext!!)
|
|
|
if (!dockConfigJson.isNullOrEmpty()) {
|
|
|
- val tempList: MutableList<DockTestFragment.DockTestBean> = Gson().fromJson(
|
|
|
- dockConfigJson,
|
|
|
- object : TypeToken<MutableList<DockTestFragment.DockTestBean>>() {}.type
|
|
|
- )
|
|
|
+ val tempList: MutableList<DockTestFragment.DockTestBean> =
|
|
|
+ Gson().fromJson(dockConfigJson, object : TypeToken<MutableList<DockTestFragment.DockTestBean>>() {}.type)
|
|
|
if (tempList.isNotEmpty()) {
|
|
|
tempList.forEach { dock ->
|
|
|
try {
|
|
|
- if (dock.type == DeviceConst.DOCK_TYPE_LOCK) {
|
|
|
- dock.deviceList = mutableListOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
|
|
- }
|
|
|
+ if (dock.type == DeviceConst.DOCK_TYPE_LOCK) dock.deviceList = mutableListOf(0, 1, 2, 3, 4)
|
|
|
rowList.find { it.row == dock.row.toInt() }?.let {
|
|
|
it.dockList.add(dock)
|
|
|
} ?: let {
|
|
|
@@ -76,17 +73,19 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun checkNewHardware(device: DockBean.DeviceBean, callback: () -> Unit) {
|
|
|
- if (device is DockBean.KeyBean) {
|
|
|
- device.mac = null
|
|
|
- NetApi.getKeyInfo(device.rfid.toString()) {
|
|
|
+ fun <T : DockData.DeviceBean> checkNewHardware(device: T, callback: () -> Unit) {
|
|
|
+ if (device is DockData.KeyDock.KeyBean) {
|
|
|
+ device.mac = ""
|
|
|
+ NetApi.getKeyInfo(device.rfid) {
|
|
|
device.newHardware = it == null
|
|
|
- device.mac = it?.macAddress
|
|
|
+ device.mac = it?.macAddress ?: ""
|
|
|
+ HardwareMode.getCurrentHardwareMode().updateKeyNewHardware(device.addr, device.idx, it == null)
|
|
|
callback()
|
|
|
}
|
|
|
- } else if (device is DockBean.LockBean) {
|
|
|
- NetApi.getLockInfo(device.rfid.toString()) {
|
|
|
+ } else if (device is DockData.LockDock.LockBean) {
|
|
|
+ NetApi.getLockInfo(device.rfid) {
|
|
|
device.newHardware = it == null
|
|
|
+ HardwareMode.getCurrentHardwareMode().updateLockNewHardware(device.addr, device.idx, it == null)
|
|
|
callback()
|
|
|
}
|
|
|
}
|
|
|
@@ -103,21 +102,23 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
// LogUtil.i("设备录入-是否所有关闭命令发送成功:${allDeviceCloseCmdSend}")
|
|
|
BleSendDispatcher.disconnectAll()
|
|
|
BusinessManager.registerInitListener {
|
|
|
- if (isStartCheckKey) {
|
|
|
- return@registerInitListener
|
|
|
- }
|
|
|
+ if (isStartCheckKey) return@registerInitListener
|
|
|
isStartCheckKey = true
|
|
|
- val dockList = ModBusController.dockList
|
|
|
+ val keyList = hwOpt.getKeyDockData()
|
|
|
+ val lockList = hwOpt.getLockDockData()
|
|
|
+ val allDevices: ArrayList<DockData> = ArrayList()
|
|
|
+ allDevices.addAll(keyList)
|
|
|
+ allDevices.addAll(lockList)
|
|
|
ThreadUtils.runOnIO {
|
|
|
- val allDevice = ModBusController.dockList.map { it.deviceList }.flatten()
|
|
|
- .filter { it is DockBean.KeyBean || it is DockBean.LockBean }
|
|
|
+ val devices = allDevices.map { if (it is DockData.KeyDock) it.keyData else (it as DockData.LockDock).lockData }.flatten()
|
|
|
.filter { it.isExist }
|
|
|
- var currentCheckDevices = AtomicInteger(0)
|
|
|
- allDevice.forEach { device ->
|
|
|
+ LogUtil.i("设备录入-检测到设备:${devices.size}")
|
|
|
+ val currentCheckDevices = AtomicInteger(0)
|
|
|
+ devices.forEach { device ->
|
|
|
checkNewHardware(device) {
|
|
|
currentCheckDevices.addAndGet(1)
|
|
|
- if (currentCheckDevices.get() == allDevice.size) {
|
|
|
- checkNewHardwareKey(dockList)
|
|
|
+ if (currentCheckDevices.get() == devices.size) {
|
|
|
+ checkNewHardwareKey(ArrayList(allDevices))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -127,34 +128,25 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun checkNewHardwareKey(dockList: MutableList<DockBean>) {
|
|
|
+ @Suppress("UNCHECKED_CAST")
|
|
|
+ private fun checkNewHardwareKey(dockList: MutableList<DockData>) {
|
|
|
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()
|
|
|
- })
|
|
|
- alreadyUsedMac.addAll(
|
|
|
- dockList.filter { it.type == DeviceConst.DOCK_TYPE_KEY || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
|
|
|
- .map { it.deviceList }.flatten().filterIsInstance<DockBean.KeyBean>()
|
|
|
- .filter { it.mac?.isNotEmpty() == true }.mapNotNull { it.mac }
|
|
|
- )
|
|
|
+ val keyList: List<DockData.KeyDock> = dockList.filter { it is DockData.KeyDock } as List<DockData.KeyDock>
|
|
|
+ val map = keyList.associate { it.addr to it.keyData.filter { key -> key.newHardware && key.isExist }.toMutableList() }
|
|
|
+ newHardwareKeyBean.putAll(map)
|
|
|
+ alreadyUsedMac.addAll(keyList.map { it.keyData }.flatten().filter { it.mac.isNotEmpty() }.map { it.mac })
|
|
|
LogUtil.d("设备录入-新设备:${newHardwareKeyBean}")
|
|
|
for ((addr, keyBeans) in newHardwareKeyBean) {
|
|
|
for (keyBean in keyBeans) {
|
|
|
- if (isDestroy) {
|
|
|
- return@runOnIO
|
|
|
- }
|
|
|
+ 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 }
|
|
|
+ newHardwareLockSize = dockList.filter { it is DockData.LockDock }.map { (it as DockData.LockDock).lockData }.flatten()
|
|
|
+ .count { it.newHardware && it.isExist }
|
|
|
+ LogUtil.i("设备录入 - 钥匙数:$newHardwareKeySize 挂锁数:$newHardwareLockSize")
|
|
|
isLoadComplete.postValue(true)
|
|
|
}
|
|
|
}
|
|
|
@@ -162,23 +154,19 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
/**
|
|
|
* 打开充电并扫描蓝牙
|
|
|
*/
|
|
|
- suspend fun openChargeAndScanMac(addr: Byte, keyBean: DockBean.KeyBean): Boolean {
|
|
|
- return suspendCancellableCoroutine<Boolean> { cont ->
|
|
|
+ suspend fun openChargeAndScanMac(addr: Int, keyBean: DockData.KeyDock.KeyBean): Boolean {
|
|
|
+ return suspendCancellableCoroutine { cont ->
|
|
|
LogUtil.i("设备录入-关闭充电:${addr},${keyBean.idx}")
|
|
|
- ModBusController.controlKeyCharge(false, keyBean.isLeft, addr) {
|
|
|
+ hwOpt.controlKeyCharge(false, keyBean.idx, keyBean.addr) {
|
|
|
ThreadUtils.runOnIO {
|
|
|
- delay(800)
|
|
|
+ delay(1000)
|
|
|
LogUtil.i("设备录入-打开充电:${addr},${keyBean.idx}")
|
|
|
- ModBusController.controlKeyLockAndCharge(true, keyBean.isLeft, addr) {
|
|
|
+ hwOpt.controlKeyCharge(true, keyBean.idx, addr) {
|
|
|
ThreadUtils.runOnIO {
|
|
|
delay(3000)
|
|
|
LogUtil.i("设备录入-开始扫描在线蓝牙Mac")
|
|
|
BleConnectionManager.scanOnlineKeyLockMac(alreadyUsedMac) { bleDevice ->
|
|
|
- LogUtil.i(
|
|
|
- "设备录入-在线的蓝牙设备:${keyBean.rfid},${
|
|
|
- bleDevice?.mac
|
|
|
- }"
|
|
|
- )
|
|
|
+ LogUtil.i("设备录入-在线的蓝牙设备:${keyBean.rfid},${bleDevice?.mac}")
|
|
|
if (isDestroy) {
|
|
|
cont.cancel()
|
|
|
return@scanOnlineKeyLockMac
|
|
|
@@ -188,18 +176,9 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
openChargeAndScanMac(addr, keyBean)
|
|
|
}
|
|
|
} else {
|
|
|
- if (bleDevice.mac == null) {
|
|
|
- ThreadUtils.runOnIO {
|
|
|
- openChargeAndScanMac(addr, keyBean)
|
|
|
- }
|
|
|
- return@scanOnlineKeyLockMac
|
|
|
- }
|
|
|
- LogUtil.i(
|
|
|
- "设备录入-没有使用过的mac:${keyBean.rfid},${
|
|
|
- bleDevice.mac
|
|
|
- }"
|
|
|
- )
|
|
|
+ LogUtil.i("设备录入-没有使用过的mac:${keyBean.rfid},${bleDevice.mac}")
|
|
|
keyBean.mac = bleDevice.mac
|
|
|
+ HardwareMode.getCurrentHardwareMode().updateKeyMac(keyBean.rfid, bleDevice.mac)
|
|
|
alreadyUsedMac.add(bleDevice.mac)
|
|
|
cont.resume(true)
|
|
|
}
|
|
|
@@ -251,34 +230,22 @@ class DeviceRegistrationKeyAndLockViewModel : BaseViewModel() {
|
|
|
}
|
|
|
isInputting = true
|
|
|
val dockList = dockData.map { it.dockList }.flatten()
|
|
|
- val deviceList =
|
|
|
- ModBusController.dockList.filter { it.addr in dockList.map { it.address } }
|
|
|
- .map { it.deviceList }.flatten()
|
|
|
- val lockDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_LOCK }
|
|
|
- .filterIsInstance<DockBean.LockBean>().filter { it.newHardware == true }
|
|
|
- val keyDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_KEY }
|
|
|
- .filterIsInstance<DockBean.KeyBean>().filter { it.newHardware == true }
|
|
|
- lockDevice.filter { it.rfid?.isNotEmpty() == true }.forEach { lockDevice ->
|
|
|
- val isBind = deviceInputLockSuspend(lockDevice.rfid.toString())
|
|
|
- if (isBind) {
|
|
|
- lockDevice.newHardware = false
|
|
|
- }
|
|
|
+ val dockKeys = hwOpt.getKeyDockData().filter { it.addr.toByte() in dockList.map { dock -> dock.address } }.map { it.keyData }.flatten()
|
|
|
+ val dockLocks = hwOpt.getLockDockData().filter { it.addr.toByte() in dockList.map { dock -> dock.address } }.map { it.lockData }.flatten()
|
|
|
+ val lockDevice = dockLocks.filter { it.newHardware }
|
|
|
+ val keyDevice = dockKeys.filter { it.newHardware }
|
|
|
+ lockDevice.filter { it.rfid.isNotEmpty() }.forEach { lock ->
|
|
|
+ val isBind = deviceInputLockSuspend(lock.rfid)
|
|
|
+ LogUtil.i("设备录入 - 录入挂锁${lock.rfid}:$isBind")
|
|
|
+ if (isBind) lock.newHardware = false
|
|
|
+ }
|
|
|
+ keyDevice.filter { it.rfid.isNotEmpty() && it.mac.isNotEmpty() }.forEach { key ->
|
|
|
+ val isBind = deviceInputKeySuspend(key.rfid, key.mac)
|
|
|
+ LogUtil.i("设备录入 - 录入钥匙${key.rfid}:$isBind")
|
|
|
+ if (isBind) key.newHardware = false
|
|
|
}
|
|
|
- keyDevice.filter { it.rfid?.isNotEmpty() == true && it.mac?.isNotEmpty() == true }
|
|
|
- .forEach { keyDevice ->
|
|
|
- val isBind =
|
|
|
- deviceInputKeySuspend(keyDevice.rfid.toString(), keyDevice.mac.toString())
|
|
|
- if (isBind) {
|
|
|
- keyDevice.newHardware = false
|
|
|
- }
|
|
|
- }
|
|
|
isInputting = false
|
|
|
- emit(
|
|
|
- Triple(
|
|
|
- true,
|
|
|
- keyDevice.count { it.rfid?.isNotEmpty() == true && it.mac?.isNotEmpty() == true },
|
|
|
- lockDevice.count { it.rfid?.isNotEmpty() == true })
|
|
|
- )
|
|
|
+ emit(Triple(true, keyDevice.count { it.rfid.isNotEmpty() && it.mac.isNotEmpty() }, lockDevice.count { it.rfid.isNotEmpty() }))
|
|
|
}
|
|
|
}
|
|
|
}
|