|
|
@@ -5,7 +5,6 @@ import com.clj.fastble.BleManager
|
|
|
import com.grkj.iscs.BusinessManager
|
|
|
import com.grkj.iscs.BusinessManager.CAN_RETURN
|
|
|
import com.grkj.iscs.R
|
|
|
-import com.grkj.iscs.extentions.crc16
|
|
|
import com.grkj.iscs.extentions.removeLeadingZeros
|
|
|
import com.grkj.iscs.extentions.toHexStrings
|
|
|
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
|
|
|
@@ -17,6 +16,7 @@ import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
|
+import com.grkj.iscs.model.vo.hardware.CabinetSlotsRecord
|
|
|
import com.grkj.iscs.util.CommonUtils
|
|
|
import com.grkj.iscs.util.Executor
|
|
|
import com.grkj.iscs.util.NetApi
|
|
|
@@ -24,7 +24,6 @@ import com.grkj.iscs.util.ToastUtils
|
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
|
|
import java.util.stream.Collectors
|
|
|
-import kotlin.random.Random
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -77,10 +76,9 @@ object ModBusController {
|
|
|
@ExperimentalUnsignedTypes
|
|
|
fun start(ctx: Context) {
|
|
|
modBusManager?.stop()
|
|
|
- PortManager.openCtrlBord(ctx)
|
|
|
- ?.let { pm ->
|
|
|
- return@let ModBusManager(pm, true)
|
|
|
- }
|
|
|
+ PortManager.openCtrlBord(ctx)?.let { pm ->
|
|
|
+ return@let ModBusManager(pm, true)
|
|
|
+ }
|
|
|
// 间隔 1 秒读一遍桶的状态
|
|
|
?.repeatSendToAll(MBFrame.READ_STATUS, {
|
|
|
interruptReadStatus
|
|
|
@@ -93,11 +91,9 @@ object ModBusController {
|
|
|
l.listener(res)
|
|
|
}
|
|
|
}
|
|
|
- }, REPEAT_FREQUENCY)
|
|
|
- ?.also {
|
|
|
+ }, REPEAT_FREQUENCY)?.also {
|
|
|
modBusManager = it
|
|
|
- }
|
|
|
- ?.start()
|
|
|
+ }?.start()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -229,8 +225,7 @@ object ModBusController {
|
|
|
if (isDone && bleBean?.bleDevice != null) {
|
|
|
Executor.delayOnMain(500) {
|
|
|
BusinessManager.getCurrentStatus(
|
|
|
- 3,
|
|
|
- bleBean.bleDevice
|
|
|
+ 3, bleBean.bleDevice
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
@@ -286,6 +281,20 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新开关状态
|
|
|
+ */
|
|
|
+ fun updateSwitchStatus(done: () -> Unit) {
|
|
|
+ modBusManager?.mSlaveAddressList?.find { it == (0xA1).toByte() }?.let {
|
|
|
+ modBusManager?.sendTo(it, MBFrame.READ_BUCKLE_STATUS) { res ->
|
|
|
+ LogUtil.i("****************************************************************************")
|
|
|
+ // 过滤非空的数据,重置slaveCount
|
|
|
+ // 不再使用slaveCount,改用地址池
|
|
|
+ switchStatus(res, done)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 第9,10锁位卡扣状态
|
|
|
*/
|
|
|
@@ -312,9 +321,7 @@ object ModBusController {
|
|
|
DOCK_TYPE_LOCK -> {
|
|
|
dockBean.getLockList().filter { it.idx > 7 }.forEach { lockBean ->
|
|
|
updateLockStatus(
|
|
|
- dockBean.addr,
|
|
|
- lockBean.idx,
|
|
|
- lockBean.lockEnabled
|
|
|
+ dockBean.addr, lockBean.idx, lockBean.lockEnabled
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
@@ -322,6 +329,26 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 开关量更新
|
|
|
+ */
|
|
|
+ private fun switchStatus(res: Any, done: () -> Unit) {
|
|
|
+ LogUtil.i("开关板:${(res as ByteArray).toHexStrings()}")
|
|
|
+ if (res.isEmpty()) {
|
|
|
+ var tipStr = CommonUtils.getStr(R.string.no_response_board_exists) + " : "
|
|
|
+ val addressList = mutableListOf<String>()
|
|
|
+
|
|
|
+ modBusManager?.mSlaveAddressList?.forEach { itDock ->
|
|
|
+ if (res.isNotEmpty() && res == itDock) {
|
|
|
+ addressList.add("0x${String.format("%02X", itDock)}")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tipStr += addressList
|
|
|
+ ToastUtils.tip(tipStr)
|
|
|
+ }
|
|
|
+ updateSwitchStatus(res, done)
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 第1-8锁位卡扣状态和钥匙
|
|
|
*/
|
|
|
@@ -349,9 +376,7 @@ object ModBusController {
|
|
|
DOCK_TYPE_KEY -> {
|
|
|
dockBean.getKeyList().forEach { keyBean ->
|
|
|
updateKeyLockStatus(
|
|
|
- dockBean.addr,
|
|
|
- keyBean.isLeft,
|
|
|
- keyBean.lockEnabled
|
|
|
+ dockBean.addr, keyBean.isLeft, keyBean.lockEnabled
|
|
|
)
|
|
|
//todo 更新锁仓状态
|
|
|
}
|
|
|
@@ -360,9 +385,7 @@ object ModBusController {
|
|
|
DOCK_TYPE_LOCK -> {
|
|
|
dockBean.getLockList().forEach { lockBean ->
|
|
|
updateLockStatus(
|
|
|
- dockBean.addr,
|
|
|
- lockBean.idx,
|
|
|
- lockBean.lockEnabled
|
|
|
+ dockBean.addr, lockBean.idx, lockBean.lockEnabled
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
@@ -378,17 +401,13 @@ object ModBusController {
|
|
|
when (deviceBean.type) {
|
|
|
DEVICE_TYPE_KEY -> {
|
|
|
updateKeyLockStatus(
|
|
|
- dockBean.addr,
|
|
|
- true,
|
|
|
- deviceBean.lockEnabled
|
|
|
+ dockBean.addr, true, deviceBean.lockEnabled
|
|
|
)
|
|
|
}
|
|
|
|
|
|
DEVICE_TYPE_LOCK -> {
|
|
|
updateLockStatus(
|
|
|
- dockBean.addr,
|
|
|
- deviceBean.idx,
|
|
|
- deviceBean.lockEnabled
|
|
|
+ dockBean.addr, deviceBean.idx, deviceBean.lockEnabled
|
|
|
)
|
|
|
}
|
|
|
|
|
|
@@ -426,6 +445,17 @@ object ModBusController {
|
|
|
return dockB?.parseLockStatus(byteArray)
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新开关状态
|
|
|
+ */
|
|
|
+ private fun updateSwitchStatus(byteArray: ByteArray, done: () -> Unit): DockBean? {
|
|
|
+ if (byteArray.isEmpty()) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ val dockB = dockList.find { it.addr == byteArray[0] }
|
|
|
+ return dockB?.parseSwitchStatus(byteArray, done)
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取额外的9,10锁仓数据
|
|
|
*/
|
|
|
@@ -486,10 +516,7 @@ object ModBusController {
|
|
|
* 开/关锁具卡扣 单
|
|
|
*/
|
|
|
fun controlLockBuckle(
|
|
|
- isOpen: Boolean,
|
|
|
- slaveAddress: Byte?,
|
|
|
- lockIdx: Int,
|
|
|
- done: ((res: ByteArray) -> Unit)? = null
|
|
|
+ isOpen: Boolean, slaveAddress: Byte?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
|
slaveAddress?.let {
|
|
|
ModBusCMDHelper.generateLockBuckleCmd(isOpen, lockIdx)?.let { cmd ->
|
|
|
@@ -524,9 +551,7 @@ object ModBusController {
|
|
|
* 读取钥匙RFID
|
|
|
*/
|
|
|
fun readKeyRfid(
|
|
|
- slaveAddress: Byte?,
|
|
|
- idx: Int,
|
|
|
- done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null
|
|
|
+ slaveAddress: Byte?, idx: Int, done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
|
slaveAddress?.let {
|
|
|
ModBusCMDHelper.generateRfidCmd(idx)?.let { cmd ->
|
|
|
@@ -776,8 +801,8 @@ object ModBusController {
|
|
|
*/
|
|
|
fun getDockByKeyMac(mac: String): DockBean? {
|
|
|
return dockList.find {
|
|
|
- (it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE)
|
|
|
- && it.getKeyList().any { it.mac == mac }
|
|
|
+ (it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE) && it.getKeyList()
|
|
|
+ .any { it.mac == mac }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -872,10 +897,20 @@ object ModBusController {
|
|
|
*
|
|
|
* @return 底座地址,钥匙
|
|
|
*/
|
|
|
- fun getOneKey(): Pair<Byte, DockBean.KeyBean?>? {
|
|
|
+ fun getOneKey(
|
|
|
+ exceptionSlots: MutableList<CabinetSlotsRecord>,
|
|
|
+ exceptionKeys: MutableList<String>
|
|
|
+ ): Pair<Byte, DockBean.KeyBean?>? {
|
|
|
val keyDockList =
|
|
|
dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }
|
|
|
- val keyList = keyDockList.flatMap { it.getKeyList() }.filter { it.isExist }
|
|
|
+ keyDockList.sortedBy { it.addr }
|
|
|
+ keyDockList.forEach { it.deviceList.sortedBy { it.idx } }
|
|
|
+ val keyList =
|
|
|
+ keyDockList.map { it.deviceList }.flatten()
|
|
|
+ .filterIsInstance<DockBean.KeyBean>()
|
|
|
+ .filterIndexed { index, _ -> (index + 1) !in exceptionSlots.map { it.col?.toInt() } }
|
|
|
+ .filter { it.rfid !in exceptionKeys }
|
|
|
+ .toMutableList()
|
|
|
LogUtil.i("keyList : $keyList")
|
|
|
if (keyList.isEmpty()) {
|
|
|
ToastUtils.tip(R.string.no_available_key)
|
|
|
@@ -884,22 +919,22 @@ object ModBusController {
|
|
|
|
|
|
keyList.forEach {
|
|
|
LogUtil.i(
|
|
|
- "keyStatus : ${it.isExist} - ${it.rfid} - ${it.mac} - ${it.isReady} - " +
|
|
|
- "${BusinessManager.getBleDeviceByMac(it.mac)?.bleDevice != null} - " +
|
|
|
- "${
|
|
|
- BleManager.getInstance()
|
|
|
- .isConnected(BusinessManager.getBleDeviceByMac(it.mac)?.bleDevice)
|
|
|
- } - " +
|
|
|
- "${!BusinessManager.mExceptionKeyList.contains(it.rfid)}"
|
|
|
+ "keyStatus : ${it.isExist} - ${it.rfid} - ${it.mac} - ${it.isReady} - " + "${
|
|
|
+ BusinessManager.getBleDeviceByMac(
|
|
|
+ it.mac
|
|
|
+ )?.bleDevice != null
|
|
|
+ } - " + "${
|
|
|
+ BleManager.getInstance()
|
|
|
+ .isConnected(BusinessManager.getBleDeviceByMac(it.mac)?.bleDevice)
|
|
|
+ } - " + "${!BusinessManager.mExceptionKeyList.contains(it.rfid)}"
|
|
|
)
|
|
|
}
|
|
|
val key = keyList.filter {
|
|
|
- it.isExist && it.rfid != null && it.mac != null && it.isReady
|
|
|
- && BleManager.getInstance()
|
|
|
- .isConnected(BusinessManager.getBleDeviceByMac(it.mac)?.bleDevice)
|
|
|
- && !BusinessManager.mExceptionKeyList.contains(it.rfid)
|
|
|
- }
|
|
|
- .shuffled().firstOrNull()
|
|
|
+ it.isExist && it.rfid != null && it.mac != null && it.isReady && BleManager.getInstance()
|
|
|
+ .isConnected(BusinessManager.getBleDeviceByMac(it.mac)?.bleDevice) && !BusinessManager.mExceptionKeyList.contains(
|
|
|
+ it.rfid
|
|
|
+ )
|
|
|
+ }.shuffled().firstOrNull()
|
|
|
if (key == null) {
|
|
|
LogUtil.e("getOneKey : no key match")
|
|
|
return null
|
|
|
@@ -920,22 +955,31 @@ object ModBusController {
|
|
|
*
|
|
|
* @return key: dock地址,value: 锁具RFID列表
|
|
|
*/
|
|
|
- fun getLocks(needLockCount: Int): MutableMap<Byte, MutableList<DockBean.LockBean>> {
|
|
|
+ fun getLocks(
|
|
|
+ needLockCount: Int,
|
|
|
+ exceptionSlots: MutableList<CabinetSlotsRecord>,
|
|
|
+ exceptionLocks: MutableList<String>
|
|
|
+ ): MutableMap<Byte, MutableList<DockBean.LockBean>> {
|
|
|
val map = mutableMapOf<Byte, MutableList<DockBean.LockBean>>()
|
|
|
if (needLockCount == 0) {
|
|
|
return map
|
|
|
}
|
|
|
val lockDockList =
|
|
|
dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }
|
|
|
-
|
|
|
+ lockDockList.sortedBy { it.addr }
|
|
|
var provideCount = 0
|
|
|
- for (lockDock in lockDockList) {
|
|
|
+ for (lockDockIndex in lockDockList.indices) {
|
|
|
if (provideCount >= needLockCount) break
|
|
|
|
|
|
- val validLocks = lockDock.getLockList().filter { it.isExist }
|
|
|
+ val validLocks =
|
|
|
+ lockDockList[lockDockIndex].getLockList().filter { it.rfid !in exceptionLocks }
|
|
|
+ .filter {
|
|
|
+ it.isExist && it.idx !in exceptionSlots.filter { it.row?.toInt() == (lockDockIndex + 1) }
|
|
|
+ .map { (it.col?.toInt() ?: 1) - 1 }
|
|
|
+ }
|
|
|
val toTake = (needLockCount - provideCount).coerceAtMost(validLocks.size)
|
|
|
if (toTake > 0) {
|
|
|
- map[lockDock.addr] = validLocks.take(toTake).toMutableList()
|
|
|
+ map[lockDockList[lockDockIndex].addr] = validLocks.take(toTake).toMutableList()
|
|
|
provideCount += toTake
|
|
|
}
|
|
|
}
|
|
|
@@ -946,8 +990,10 @@ object ModBusController {
|
|
|
* 获取开关量数据
|
|
|
*/
|
|
|
fun getSwitchData(): MutableList<DockBean.SwitchBean> {
|
|
|
- return dockList.filter { it.type == DOCK_TYPE_COLLECT }.map { it.getSwitchList() }.flatten()
|
|
|
- .toMutableList()
|
|
|
+ return dockList.filter { it.type == DOCK_TYPE_COLLECT }.sortedBy { it.addr }
|
|
|
+ .flatMap { it.getSwitchList() }.mapIndexed { index, switchBean ->
|
|
|
+ DockBean.SwitchBean(index, switchBean.switchBoardAddr, switchBean.enabled)
|
|
|
+ }.toMutableList()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -958,28 +1004,9 @@ object ModBusController {
|
|
|
) {
|
|
|
slaveAddress?.let {
|
|
|
ModBusCMDHelper.generateSwitchBoardStatusCmd(addr)?.let { cmd ->
|
|
|
- //todo 模拟数据
|
|
|
- val switchData = Random.nextBytes(2)
|
|
|
- val switchDataFrame = ByteArray(5)
|
|
|
- switchDataFrame[0] = slaveAddress
|
|
|
- switchDataFrame[1] = FRAME_TYPE_READ
|
|
|
- switchDataFrame[2] = 0x02
|
|
|
- switchDataFrame[3] = switchData[0]
|
|
|
- switchDataFrame[4] = switchData[1]
|
|
|
- val switchDataFrameCRC = switchDataFrame.crc16(0, 4)
|
|
|
- switchDataFrame[5] = switchDataFrameCRC[0]
|
|
|
- switchDataFrame[6] = switchDataFrameCRC[1]
|
|
|
- done?.invoke(
|
|
|
- byteArrayOf(
|
|
|
- slaveAddress,
|
|
|
- FRAME_TYPE_READ,
|
|
|
- switchData[0],
|
|
|
- switchData[1],
|
|
|
- ).crc16()
|
|
|
- )
|
|
|
-// modBusManager?.sendTo(it, cmd) { res ->
|
|
|
-// done?.invoke(res)
|
|
|
-// }
|
|
|
+ modBusManager?.sendTo(it, cmd) { res ->
|
|
|
+ done?.invoke(res)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|