|
@@ -1,11 +1,12 @@
|
|
|
package com.grkj.iscs.features.init.viewmodel
|
|
package com.grkj.iscs.features.init.viewmodel
|
|
|
|
|
|
|
|
-import android.bluetooth.BluetoothDevice
|
|
|
|
|
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.clj.fastble.BleManager
|
|
|
import com.grkj.data.data.MMKVConstants
|
|
import com.grkj.data.data.MMKVConstants
|
|
|
|
|
+import com.grkj.data.model.dos.IsLockCabinet
|
|
|
|
|
+import com.grkj.data.model.dos.IsLockCabinetSlots
|
|
|
import com.grkj.data.repository.IHardwareRepository
|
|
import com.grkj.data.repository.IHardwareRepository
|
|
|
import com.grkj.ui_base.base.BaseViewModel
|
|
import com.grkj.ui_base.base.BaseViewModel
|
|
|
import com.grkj.ui_base.business.ModbusBusinessManager
|
|
import com.grkj.ui_base.business.ModbusBusinessManager
|
|
@@ -14,9 +15,7 @@ import com.grkj.ui_base.utils.ble.BleConnectionManager
|
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
import com.grkj.ui_base.utils.modbus.DeviceConst
|
|
|
import com.grkj.ui_base.utils.modbus.DockBean
|
|
import com.grkj.ui_base.utils.modbus.DockBean
|
|
|
import com.grkj.ui_base.utils.modbus.ModBusController
|
|
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.extension.getMMKVData
|
|
|
import com.sik.sikcore.thread.ThreadUtils
|
|
import com.sik.sikcore.thread.ThreadUtils
|
|
|
import com.tencent.mmkv.MMKV
|
|
import com.tencent.mmkv.MMKV
|
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
@@ -24,7 +23,6 @@ import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.delay
|
|
import kotlinx.coroutines.delay
|
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
|
|
import kotlinx.coroutines.withContext
|
|
import kotlinx.coroutines.withContext
|
|
|
-import java.util.concurrent.atomic.AtomicInteger
|
|
|
|
|
import javax.inject.Inject
|
|
import javax.inject.Inject
|
|
|
import kotlin.coroutines.resume
|
|
import kotlin.coroutines.resume
|
|
|
|
|
|
|
@@ -39,23 +37,6 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
private val newHardwareKeyBean: MutableMap<Byte, MutableList<DockBean.KeyBean>> = mutableMapOf()
|
|
|
private val alreadyUsedMac: MutableList<String> = mutableListOf()
|
|
private val alreadyUsedMac: MutableList<String> = mutableListOf()
|
|
|
|
|
|
|
|
- fun checkNewHardware(device: DockBean.DeviceBean, callback: () -> Unit) {
|
|
|
|
|
- if (device is DockBean.KeyBean) {
|
|
|
|
|
- device.mac = null
|
|
|
|
|
- hardwareRepository.getKeyInfo(device.rfid.toString()) {
|
|
|
|
|
- device.newHardware =
|
|
|
|
|
- it == null || it.keyNfc?.isEmpty() == true || it.macAddress?.isEmpty() == true
|
|
|
|
|
- device.mac = it?.macAddress
|
|
|
|
|
- callback()
|
|
|
|
|
- }
|
|
|
|
|
- } else if (device is DockBean.LockBean) {
|
|
|
|
|
- hardwareRepository.getLockInfo(device.rfid.toString()) {
|
|
|
|
|
- device.newHardware = it == null || it.lockNfc?.isEmpty() == true
|
|
|
|
|
- callback()
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 注册监听
|
|
* 注册监听
|
|
|
*/
|
|
*/
|
|
@@ -64,7 +45,7 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
newHardwareKeyBean.clear()
|
|
newHardwareKeyBean.clear()
|
|
|
alreadyUsedMac.clear()
|
|
alreadyUsedMac.clear()
|
|
|
isLoadComplete.postValue(false)
|
|
isLoadComplete.postValue(false)
|
|
|
- ModBusController.controlAllKeyBuckleOpen{
|
|
|
|
|
|
|
+ ModBusController.controlAllKeyBuckleOpen {
|
|
|
ModbusBusinessManager.registerInitListener {
|
|
ModbusBusinessManager.registerInitListener {
|
|
|
if (!ISCSConfig.canInitDevice || isInDeviceInit) {
|
|
if (!ISCSConfig.canInitDevice || isInDeviceInit) {
|
|
|
return@registerInitListener
|
|
return@registerInitListener
|
|
@@ -141,7 +122,7 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
ThreadUtils.runOnIO {
|
|
ThreadUtils.runOnIO {
|
|
|
delay(3000)
|
|
delay(3000)
|
|
|
logger.info("设备录入-开始扫描在线蓝牙Mac")
|
|
logger.info("设备录入-开始扫描在线蓝牙Mac")
|
|
|
- withContext(Dispatchers.Main){
|
|
|
|
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
BleConnectionManager.scanOnlineKeyLockMac(alreadyUsedMac) { mac ->
|
|
BleConnectionManager.scanOnlineKeyLockMac(alreadyUsedMac) { mac ->
|
|
|
logger.info(
|
|
logger.info(
|
|
|
"设备录入-在线的蓝牙设备:${keyBean.rfid},${mac}"
|
|
"设备录入-在线的蓝牙设备:${keyBean.rfid},${mac}"
|
|
@@ -216,13 +197,63 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 仓位录入挂起任务
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun deviceInputSlotsSuspend(dockBean: DockBean) {
|
|
|
|
|
+ when (dockBean.type) {
|
|
|
|
|
+ DeviceConst.DOCK_TYPE_KEY -> {
|
|
|
|
|
+ val slots = dockBean.deviceList.mapIndexed { index, device ->
|
|
|
|
|
+ var isLockCabinetSlots = IsLockCabinetSlots()
|
|
|
|
|
+ isLockCabinetSlots.cabinetId = MMKVConstants.KEY_LOCK_CABINET_ID.getMMKVData(0)
|
|
|
|
|
+ isLockCabinetSlots.slotType = DeviceConst.DEVICE_TYPE_KEY.toString()
|
|
|
|
|
+ isLockCabinetSlots.row = dockBean.row.toString()
|
|
|
|
|
+ isLockCabinetSlots.col = (index + 1).toString()
|
|
|
|
|
+ isLockCabinetSlots
|
|
|
|
|
+ }
|
|
|
|
|
+ hardwareRepository.saveCabinetSlots(slots)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DeviceConst.DOCK_TYPE_LOCK -> {
|
|
|
|
|
+ val slots = dockBean.deviceList.mapIndexed { index, device ->
|
|
|
|
|
+ var isLockCabinetSlots = IsLockCabinetSlots()
|
|
|
|
|
+ isLockCabinetSlots.cabinetId = MMKVConstants.KEY_LOCK_CABINET_ID.getMMKVData(0)
|
|
|
|
|
+ isLockCabinetSlots.slotType = DeviceConst.DEVICE_TYPE_LOCK.toString()
|
|
|
|
|
+ isLockCabinetSlots.row = dockBean.row.toString()
|
|
|
|
|
+ isLockCabinetSlots.col = (index + 1).toString()
|
|
|
|
|
+ isLockCabinetSlots
|
|
|
|
|
+ }
|
|
|
|
|
+ hardwareRepository.saveCabinetSlots(slots)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DeviceConst.DOCK_TYPE_PORTABLE -> {
|
|
|
|
|
+ val slots = dockBean.deviceList.mapIndexed { index, device ->
|
|
|
|
|
+ var isLockCabinetSlots = IsLockCabinetSlots()
|
|
|
|
|
+ isLockCabinetSlots.cabinetId = MMKVConstants.KEY_LOCK_CABINET_ID.getMMKVData(0)
|
|
|
|
|
+ isLockCabinetSlots.row = dockBean.row.toString()
|
|
|
|
|
+ isLockCabinetSlots.col = (index + 1).toString()
|
|
|
|
|
+ if (device is DockBean.KeyBean) {
|
|
|
|
|
+ isLockCabinetSlots.slotType = DeviceConst.DEVICE_TYPE_KEY.toString()
|
|
|
|
|
+ } else if (device is DockBean.LockBean) {
|
|
|
|
|
+ isLockCabinetSlots.slotType = DeviceConst.DEVICE_TYPE_LOCK.toString()
|
|
|
|
|
+ }
|
|
|
|
|
+ isLockCabinetSlots
|
|
|
|
|
+ }
|
|
|
|
|
+ hardwareRepository.saveCabinetSlots(slots)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 设备录入(钥匙和挂锁)
|
|
* 设备录入(钥匙和挂锁)
|
|
|
*/
|
|
*/
|
|
|
fun deviceRegistrationData(): LiveData<Triple<Boolean, Int, Int>> {
|
|
fun deviceRegistrationData(): LiveData<Triple<Boolean, Int, Int>> {
|
|
|
return liveData(Dispatchers.IO) {
|
|
return liveData(Dispatchers.IO) {
|
|
|
- val deviceList =
|
|
|
|
|
- ModBusController.dockList.map { it.deviceList }.flatten()
|
|
|
|
|
|
|
+ val dockList = ModBusController.dockList
|
|
|
|
|
+ val deviceList = dockList.map { it.deviceList }.flatten()
|
|
|
|
|
+ dockList.forEach {
|
|
|
|
|
+ deviceInputSlotsSuspend(it)
|
|
|
|
|
+ }
|
|
|
val lockDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_LOCK }
|
|
val lockDevice = deviceList.filter { it.type == DeviceConst.DEVICE_TYPE_LOCK }
|
|
|
.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 }
|
|
@@ -281,4 +312,23 @@ class InitDeviceRegistrationKeyAndLockViewModel @Inject constructor(val hardware
|
|
|
emit(true)
|
|
emit(true)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 清除锁仓数据
|
|
|
|
|
+ */
|
|
|
|
|
+ fun clearLockCabinetSlots(): LiveData<Boolean> {
|
|
|
|
|
+ return liveData(Dispatchers.IO) {
|
|
|
|
|
+ hardwareRepository.clearCabinetSlots()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建锁柜数据
|
|
|
|
|
+ */
|
|
|
|
|
+ fun createLockCabinetData(): LiveData<Boolean> {
|
|
|
|
|
+ return liveData(Dispatchers.IO) {
|
|
|
|
|
+ hardwareRepository.createCabinetData()
|
|
|
|
|
+ emit(true)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|