|
@@ -1,13 +1,14 @@
|
|
|
package com.grkj.iscs.features.main.viewmodel.hardware_manage
|
|
package com.grkj.iscs.features.main.viewmodel.hardware_manage
|
|
|
|
|
|
|
|
|
|
+import androidx.core.view.isVisible
|
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.LiveData
|
|
|
import androidx.lifecycle.liveData
|
|
import androidx.lifecycle.liveData
|
|
|
import com.grkj.data.model.dos.IsKey
|
|
import com.grkj.data.model.dos.IsKey
|
|
|
import com.grkj.data.model.dos.IsLock
|
|
import com.grkj.data.model.dos.IsLock
|
|
|
import com.grkj.data.model.dos.IsLockCabinetSlots
|
|
import com.grkj.data.model.dos.IsLockCabinetSlots
|
|
|
import com.grkj.data.repository.IHardwareRepository
|
|
import com.grkj.data.repository.IHardwareRepository
|
|
|
|
|
+import com.grkj.iscs.features.init.model.DockData
|
|
|
import com.grkj.ui_base.base.BaseViewModel
|
|
import com.grkj.ui_base.base.BaseViewModel
|
|
|
-import com.grkj.ui_base.business.BleBusinessManager
|
|
|
|
|
import com.grkj.ui_base.business.ModbusBusinessManager
|
|
import com.grkj.ui_base.business.ModbusBusinessManager
|
|
|
import com.grkj.ui_base.utils.ble.BleConnectionManager
|
|
import com.grkj.ui_base.utils.ble.BleConnectionManager
|
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
@@ -22,7 +23,6 @@ import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import kotlinx.coroutines.withContext
|
|
import kotlinx.coroutines.withContext
|
|
|
import javax.inject.Inject
|
|
import javax.inject.Inject
|
|
|
import kotlin.coroutines.resume
|
|
import kotlin.coroutines.resume
|
|
|
-import kotlin.coroutines.suspendCoroutine
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 仓位管理数据模型
|
|
* 仓位管理数据模型
|
|
@@ -36,6 +36,11 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
*/
|
|
*/
|
|
|
var exceptionSlotsData: List<IsLockCabinetSlots> = listOf()
|
|
var exceptionSlotsData: List<IsLockCabinetSlots> = listOf()
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 缓存的dock数据
|
|
|
|
|
+ */
|
|
|
|
|
+ var lastDockSnapshot: MutableList<DockData> = mutableListOf()
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 异常挂锁
|
|
* 异常挂锁
|
|
|
*/
|
|
*/
|
|
@@ -54,9 +59,9 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
/**
|
|
/**
|
|
|
* 注册监听
|
|
* 注册监听
|
|
|
*/
|
|
*/
|
|
|
- fun registerStatusListener(statusListener: () -> Unit) {
|
|
|
|
|
|
|
+ fun registerStatusListener(statusListener: (DockBean) -> Unit) {
|
|
|
ModbusBusinessManager.registerStatusListener(this) {
|
|
ModbusBusinessManager.registerStatusListener(this) {
|
|
|
- statusListener()
|
|
|
|
|
|
|
+ statusListener(it)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -124,7 +129,11 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
/**
|
|
/**
|
|
|
* 标记硬件异常
|
|
* 标记硬件异常
|
|
|
*/
|
|
*/
|
|
|
- fun tagDeviceException(deviceType: Int, deviceRfid: String?, remark: String): LiveData<Boolean> {
|
|
|
|
|
|
|
+ fun tagDeviceException(
|
|
|
|
|
+ deviceType: Int,
|
|
|
|
|
+ deviceRfid: String?,
|
|
|
|
|
+ remark: String
|
|
|
|
|
+ ): LiveData<Boolean> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
when (deviceType) {
|
|
when (deviceType) {
|
|
|
DeviceConst.DEVICE_TYPE_KEY -> {
|
|
DeviceConst.DEVICE_TYPE_KEY -> {
|
|
@@ -143,7 +152,8 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
*/
|
|
*/
|
|
|
fun checkLockRfidIsNewDevice(lockRfid: String): LiveData<Boolean> {
|
|
fun checkLockRfidIsNewDevice(lockRfid: String): LiveData<Boolean> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
- emit(hardwareRepository.getLockInfo(lockRfid) == null)
|
|
|
|
|
|
|
+ val newDevices = hardwareRepository.getLockInfo(lockRfid) == null
|
|
|
|
|
+ emit(newDevices)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -172,7 +182,8 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
*/
|
|
*/
|
|
|
fun checkKeyRfidIsNewDevice(keyRfid: String): LiveData<Boolean> {
|
|
fun checkKeyRfidIsNewDevice(keyRfid: String): LiveData<Boolean> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
- emit(hardwareRepository.getKeyInfo(keyRfid) == null)
|
|
|
|
|
|
|
+ val newDevices = hardwareRepository.getKeyInfo(keyRfid) == null
|
|
|
|
|
+ emit(newDevices)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -181,7 +192,8 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
*/
|
|
*/
|
|
|
fun detectKeyMac(keyBean: DockBean.KeyBean): LiveData<String> {
|
|
fun detectKeyMac(keyBean: DockBean.KeyBean): LiveData<String> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
- emit(detectNewKeyMac(keyBean))
|
|
|
|
|
|
|
+ val mac = detectNewKeyMac(keyBean)
|
|
|
|
|
+ emit(mac)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -189,50 +201,72 @@ class SlotsManageViewModel @Inject constructor(
|
|
|
* 检测新钥匙mac地址
|
|
* 检测新钥匙mac地址
|
|
|
*/
|
|
*/
|
|
|
private suspend fun detectNewKeyMac(keyBean: DockBean.KeyBean): String {
|
|
private suspend fun detectNewKeyMac(keyBean: DockBean.KeyBean): String {
|
|
|
- return suspendCancellableCoroutine<String> { cont ->
|
|
|
|
|
|
|
+ val mac = suspendCancellableCoroutine { cont ->
|
|
|
val existsKeyMac =
|
|
val existsKeyMac =
|
|
|
hardwareRepository.getAllKeyInfo().mapNotNull { it.macAddress }.toList()
|
|
hardwareRepository.getAllKeyInfo().mapNotNull { it.macAddress }.toList()
|
|
|
- logger.info("设备检测-关闭充电:${keyBean.addr},${keyBean.idx}")
|
|
|
|
|
- ModBusController.controlKeyCharge(false, keyBean.idx, keyBean.addr) {
|
|
|
|
|
- ThreadUtils.runOnIO {
|
|
|
|
|
- delay(800)
|
|
|
|
|
- logger.info("设备检测-打开充电:${keyBean.addr},${keyBean.idx}")
|
|
|
|
|
- ModBusController.controlKeyLockAndCharge(true, keyBean.idx, keyBean.addr) {
|
|
|
|
|
- ThreadUtils.runOnIO {
|
|
|
|
|
- delay(3000)
|
|
|
|
|
- logger.info("设备检测-开始扫描在线蓝牙Mac")
|
|
|
|
|
- withContext(Dispatchers.Main) {
|
|
|
|
|
- BleConnectionManager.scanOnlineKeyLockMac(existsKeyMac) { mac ->
|
|
|
|
|
- logger.info(
|
|
|
|
|
- "设备检测-在线的蓝牙设备:${keyBean.rfid},${mac}"
|
|
|
|
|
- )
|
|
|
|
|
- if (isDestroy) {
|
|
|
|
|
- logger.info(
|
|
|
|
|
- "设备检测-界面已销毁"
|
|
|
|
|
- )
|
|
|
|
|
- return@scanOnlineKeyLockMac
|
|
|
|
|
- }
|
|
|
|
|
- if (mac == null) {
|
|
|
|
|
- logger.info(
|
|
|
|
|
- "设备检测-设备mac空:${keyBean.rfid}"
|
|
|
|
|
- )
|
|
|
|
|
- return@scanOnlineKeyLockMac
|
|
|
|
|
- }
|
|
|
|
|
- logger.info(
|
|
|
|
|
- "设备检测-没有使用过的mac:${keyBean.rfid},${
|
|
|
|
|
- mac
|
|
|
|
|
- }"
|
|
|
|
|
- )
|
|
|
|
|
- keyBean.mac = mac
|
|
|
|
|
- if (cont.isActive) {
|
|
|
|
|
- cont.resume(mac)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ logger.info("设备检测-打开充电:${keyBean.addr},${keyBean.idx}")
|
|
|
|
|
+ ModBusController.controlKeyLockAndCharge(true, keyBean.idx, keyBean.addr) {
|
|
|
|
|
+ logger.info("设备检测-开始扫描在线蓝牙Mac")
|
|
|
|
|
+ BleConnectionManager.scanOnlineKeyLockMac(existsKeyMac) { mac ->
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "设备检测-在线的蓝牙设备:${keyBean.rfid},${mac}"
|
|
|
|
|
+ )
|
|
|
|
|
+ if (isDestroy) {
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "设备检测-界面已销毁"
|
|
|
|
|
+ )
|
|
|
|
|
+ return@scanOnlineKeyLockMac
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mac == null) {
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "设备检测-设备mac空:${keyBean.rfid}"
|
|
|
|
|
+ )
|
|
|
|
|
+ return@scanOnlineKeyLockMac
|
|
|
|
|
+ }
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "设备检测-没有使用过的mac:${keyBean.rfid},${
|
|
|
|
|
+ mac
|
|
|
|
|
+ }"
|
|
|
|
|
+ )
|
|
|
|
|
+ keyBean.mac = mac
|
|
|
|
|
+ if (cont.isActive) {
|
|
|
|
|
+ cont.resume(mac)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ return mac
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fun getData(): LiveData<Boolean> {
|
|
|
|
|
+ return liveData(Dispatchers.IO) {
|
|
|
|
|
+ val dockData =
|
|
|
|
|
+ ModBusController.dockList.filter { it.type == DeviceConst.DOCK_TYPE_LOCK || it.type == DeviceConst.DOCK_TYPE_KEY || it.type == DeviceConst.DOCK_TYPE_PORTABLE }
|
|
|
|
|
+ val keyDock = dockData.filter { it.type == DeviceConst.DOCK_TYPE_KEY }.map {
|
|
|
|
|
+ DockData.KeyDock().apply {
|
|
|
|
|
+ keyData.addAll(it.deviceList.filterIsInstance<DockBean.KeyBean>())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ val keyDatas = keyDock.map { it.keyData }.flatten()
|
|
|
|
|
+ val newKeyDock = DockData.KeyDock()
|
|
|
|
|
+ newKeyDock.keyData.addAll(keyDatas)
|
|
|
|
|
+ val newKeyDockList = newKeyDock.keyData.chunked(4).map { subList ->
|
|
|
|
|
+ DockData.KeyDock().apply { keyData.addAll(subList) }
|
|
|
|
|
+ }
|
|
|
|
|
+ val lockDock = dockData.filter { it.type == DeviceConst.DOCK_TYPE_LOCK }.map {
|
|
|
|
|
+ DockData.LockDock().apply {
|
|
|
|
|
+ lockData.addAll(it.deviceList.filterIsInstance<DockBean.LockBean>())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ val portableDock =
|
|
|
|
|
+ dockData.filter { it.type == DeviceConst.DOCK_TYPE_PORTABLE }.map {
|
|
|
|
|
+ DockData.PortableDock().apply {
|
|
|
|
|
+ deviceData.addAll(it.deviceList.toList())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ lastDockSnapshot.clear()
|
|
|
|
|
+ lastDockSnapshot.addAll(newKeyDockList + portableDock + lockDock)
|
|
|
|
|
+ emit(true)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|