|
@@ -3,28 +3,37 @@ package com.grkj.iscs.modbus
|
|
|
import android.content.Context
|
|
import android.content.Context
|
|
|
import com.clj.fastble.BleManager
|
|
import com.clj.fastble.BleManager
|
|
|
import com.grkj.iscs.BusinessManager
|
|
import com.grkj.iscs.BusinessManager
|
|
|
|
|
+import com.grkj.iscs.BusinessManager.CAN_RETURN
|
|
|
import com.grkj.iscs.R
|
|
import com.grkj.iscs.R
|
|
|
|
|
+import com.grkj.iscs.extentions.crc16
|
|
|
import com.grkj.iscs.extentions.removeLeadingZeros
|
|
import com.grkj.iscs.extentions.removeLeadingZeros
|
|
|
import com.grkj.iscs.extentions.toHexStrings
|
|
import com.grkj.iscs.extentions.toHexStrings
|
|
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
|
|
|
|
|
+import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
|
|
|
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
|
|
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
|
|
|
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
|
|
import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
|
|
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_COLLECT
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
|
|
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_KEY
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
|
|
|
+import com.grkj.iscs.util.CommonUtils
|
|
|
import com.grkj.iscs.util.Executor
|
|
import com.grkj.iscs.util.Executor
|
|
|
import com.grkj.iscs.util.NetApi
|
|
import com.grkj.iscs.util.NetApi
|
|
|
import com.grkj.iscs.util.ToastUtils
|
|
import com.grkj.iscs.util.ToastUtils
|
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
|
-import java.util.concurrent.Executors
|
|
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicInteger
|
|
|
import java.util.stream.Collectors
|
|
import java.util.stream.Collectors
|
|
|
|
|
+import kotlin.random.Random
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* ModBus 主控板控制器
|
|
* ModBus 主控板控制器
|
|
|
*/
|
|
*/
|
|
|
object ModBusController {
|
|
object ModBusController {
|
|
|
-
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 是否初始化完成
|
|
|
|
|
+ */
|
|
|
var isInitReady = false
|
|
var isInitReady = false
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -32,43 +41,50 @@ object ModBusController {
|
|
|
*/
|
|
*/
|
|
|
var dockList: MutableList<DockBean> = mutableListOf()
|
|
var dockList: MutableList<DockBean> = mutableListOf()
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 监听状态类型
|
|
|
|
|
+ */
|
|
|
private const val LISTENER_TYPE_STATUS = 3
|
|
private const val LISTENER_TYPE_STATUS = 3
|
|
|
|
|
|
|
|
// 主控板管理器
|
|
// 主控板管理器
|
|
|
var modBusManager: ModBusManager? = null
|
|
var modBusManager: ModBusManager? = null
|
|
|
- private var slaveCount: Int = 0
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 监听器
|
|
|
|
|
+ */
|
|
|
private val listeners = ArrayList<StatusListener>()
|
|
private val listeners = ArrayList<StatusListener>()
|
|
|
|
|
|
|
|
// 是否中断读取状态
|
|
// 是否中断读取状态
|
|
|
private var interruptReadStatus: ArrayList<Boolean> = ArrayList()
|
|
private var interruptReadStatus: ArrayList<Boolean> = ArrayList()
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 重复时间
|
|
|
|
|
+ */
|
|
|
// TODO 临时改成5s
|
|
// TODO 临时改成5s
|
|
|
const val REPEAT_FREQUENCY = 800L
|
|
const val REPEAT_FREQUENCY = 800L
|
|
|
|
|
|
|
|
- class StatusListener(
|
|
|
|
|
- val key: Any,
|
|
|
|
|
- val listener: (Any) -> Unit,
|
|
|
|
|
- val type: Int
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 中断读取
|
|
|
|
|
+ */
|
|
|
fun interruptReadTrashBinStatus(interrupt: Boolean) {
|
|
fun interruptReadTrashBinStatus(interrupt: Boolean) {
|
|
|
interruptReadStatus.clear()
|
|
interruptReadStatus.clear()
|
|
|
interruptReadStatus.add(interrupt)
|
|
interruptReadStatus.add(interrupt)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 启动通信引擎
|
|
|
|
|
+ */
|
|
|
@ExperimentalUnsignedTypes
|
|
@ExperimentalUnsignedTypes
|
|
|
fun start(ctx: Context) {
|
|
fun start(ctx: Context) {
|
|
|
modBusManager?.stop()
|
|
modBusManager?.stop()
|
|
|
PortManager.openCtrlBord(ctx)
|
|
PortManager.openCtrlBord(ctx)
|
|
|
?.let { pm ->
|
|
?.let { pm ->
|
|
|
- return@let ModBusManager(slaveCount, pm, true)
|
|
|
|
|
|
|
+ return@let ModBusManager(pm, true)
|
|
|
}
|
|
}
|
|
|
// 间隔 1 秒读一遍桶的状态
|
|
// 间隔 1 秒读一遍桶的状态
|
|
|
?.repeatSendToAll(MBFrame.READ_STATUS, {
|
|
?.repeatSendToAll(MBFrame.READ_STATUS, {
|
|
|
interruptReadStatus
|
|
interruptReadStatus
|
|
|
}, { res ->
|
|
}, { res ->
|
|
|
-// // Logger.d("ModbusController", "res: ${res.map { it.toHexString() }}")
|
|
|
|
|
LogUtil.i("****************************************************************************")
|
|
LogUtil.i("****************************************************************************")
|
|
|
// 过滤非空的数据,重置slaveCount
|
|
// 过滤非空的数据,重置slaveCount
|
|
|
// 不再使用slaveCount,改用地址池
|
|
// 不再使用slaveCount,改用地址池
|
|
@@ -84,10 +100,16 @@ object ModBusController {
|
|
|
?.start()
|
|
?.start()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 注册监听器
|
|
|
|
|
+ */
|
|
|
fun registerStatusListener(key: Any, listener: (Any) -> Unit) {
|
|
fun registerStatusListener(key: Any, listener: (Any) -> Unit) {
|
|
|
listeners.add(StatusListener(key, listener, LISTENER_TYPE_STATUS))
|
|
listeners.add(StatusListener(key, listener, LISTENER_TYPE_STATUS))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 取消注册监听器
|
|
|
|
|
+ */
|
|
|
fun unregisterListener(key: Any) {
|
|
fun unregisterListener(key: Any) {
|
|
|
val it = listeners.iterator()
|
|
val it = listeners.iterator()
|
|
|
while (it.hasNext()) {
|
|
while (it.hasNext()) {
|
|
@@ -97,10 +119,16 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 停止引擎
|
|
|
|
|
+ */
|
|
|
fun stop() {
|
|
fun stop() {
|
|
|
modBusManager?.stop()
|
|
modBusManager?.stop()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 引擎是否运行中
|
|
|
|
|
+ */
|
|
|
fun isRunning(): Boolean? {
|
|
fun isRunning(): Boolean? {
|
|
|
return modBusManager?.isRunning()
|
|
return modBusManager?.isRunning()
|
|
|
}
|
|
}
|
|
@@ -121,6 +149,7 @@ object ModBusController {
|
|
|
DOCK_TYPE_LOCK -> "锁具底座"
|
|
DOCK_TYPE_LOCK -> "锁具底座"
|
|
|
DOCK_TYPE_ELEC_LOCK_BOARD -> "电磁锁控制板"
|
|
DOCK_TYPE_ELEC_LOCK_BOARD -> "电磁锁控制板"
|
|
|
DOCK_TYPE_PORTABLE -> "便携式底座"
|
|
DOCK_TYPE_PORTABLE -> "便携式底座"
|
|
|
|
|
+ DOCK_TYPE_COLLECT -> "开关量采集板"
|
|
|
else -> "未知"
|
|
else -> "未知"
|
|
|
}
|
|
}
|
|
|
LogUtil.i("initDevicesStatus 设备(${bytes[0].toInt()})类型:$type")
|
|
LogUtil.i("initDevicesStatus 设备(${bytes[0].toInt()})类型:$type")
|
|
@@ -159,6 +188,9 @@ object ModBusController {
|
|
|
val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
|
|
|
LogUtil.i("初始化锁具 RFID : $rfid")
|
|
LogUtil.i("初始化锁具 RFID : $rfid")
|
|
|
updateLockRfid(dockBean.addr, idx, rfid)
|
|
updateLockRfid(dockBean.addr, idx, rfid)
|
|
|
|
|
+ NetApi.getLockInfo(rfid) {
|
|
|
|
|
+ updateLockNewHardware(dockBean.addr, idx, it == null)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
controlLockBuckle(false, dockBean.addr, hasLockIdxList)
|
|
controlLockBuckle(false, dockBean.addr, hasLockIdxList)
|
|
@@ -189,6 +221,7 @@ object ModBusController {
|
|
|
// 蓝牙准备操作
|
|
// 蓝牙准备操作
|
|
|
NetApi.getKeyInfo(rfid) {
|
|
NetApi.getKeyInfo(rfid) {
|
|
|
LogUtil.i("getKeyInfo : $rfid - ${it?.macAddress}")
|
|
LogUtil.i("getKeyInfo : $rfid - ${it?.macAddress}")
|
|
|
|
|
+ updateKeyNewHardware(dockBean.addr, isLeft, it == null)
|
|
|
if (it != null && !it.macAddress.isNullOrEmpty()) {
|
|
if (it != null && !it.macAddress.isNullOrEmpty()) {
|
|
|
// 更新mac
|
|
// 更新mac
|
|
|
updateKeyMac(dockBean.addr, key.isLeft, it.macAddress)
|
|
updateKeyMac(dockBean.addr, key.isLeft, it.macAddress)
|
|
@@ -216,7 +249,7 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 更新状态
|
|
|
|
|
|
|
+ * 获取最新状态
|
|
|
*/
|
|
*/
|
|
|
fun updateStatus(byteArray: ByteArray): DockBean? {
|
|
fun updateStatus(byteArray: ByteArray): DockBean? {
|
|
|
if (byteArray.isEmpty()) {
|
|
if (byteArray.isEmpty()) {
|
|
@@ -226,6 +259,184 @@ object ModBusController {
|
|
|
return dockB?.parseStatus(byteArray)
|
|
return dockB?.parseStatus(byteArray)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新所有锁仓状态
|
|
|
|
|
+ */
|
|
|
|
|
+ fun updateAllBuckleStatus(done: () -> Unit) {
|
|
|
|
|
+ val remaining = AtomicInteger(2)
|
|
|
|
|
+ modBusManager?.sendToAll(MBFrame.READ_BUCKLE_STATUS) { res ->
|
|
|
|
|
+ LogUtil.i("****************************************************************************")
|
|
|
|
|
+ // 过滤非空的数据,重置slaveCount
|
|
|
|
|
+ // 不再使用slaveCount,改用地址池
|
|
|
|
|
+ lockBuckleStatus(res)
|
|
|
|
|
+ // 每完成一个就把计数减一,减到 0 就触发 done()
|
|
|
|
|
+ if (remaining.decrementAndGet() == 0) {
|
|
|
|
|
+ done()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ modBusManager?.sendToAll(MBFrame.READ_LOCK_BUCKLE_EXTRA_STATUS) { res ->
|
|
|
|
|
+ LogUtil.i("****************************************************************************")
|
|
|
|
|
+ // 过滤非空的数据,重置slaveCount
|
|
|
|
|
+ // 不再使用slaveCount,改用地址池
|
|
|
|
|
+ lockBuckleExtraStatus(res)
|
|
|
|
|
+ // 每完成一个就把计数减一,减到 0 就触发 done()
|
|
|
|
|
+ if (remaining.decrementAndGet() == 0) {
|
|
|
|
|
+ done()
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 第9,10锁位卡扣状态
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun lockBuckleExtraStatus(res: Any) {
|
|
|
|
|
+ LogUtil.i("硬件状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
|
|
|
|
|
+ if (res.isEmpty() || res.any { it.isEmpty() }) {
|
|
|
|
|
+ var tipStr = CommonUtils.getStr(R.string.no_response_board_exists) + " : "
|
|
|
|
|
+ val addressList = mutableListOf<String>()
|
|
|
|
|
+
|
|
|
|
|
+ modBusManager?.mSlaveAddressList?.forEach { itDock ->
|
|
|
|
|
+ if (res.none { it.isNotEmpty() && it[0] == itDock }) {
|
|
|
|
|
+ addressList.add("0x${String.format("%02X", itDock)}")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ tipStr += addressList
|
|
|
|
|
+ ToastUtils.tip(tipStr)
|
|
|
|
|
+ }
|
|
|
|
|
+ res.forEach { bytes ->
|
|
|
|
|
+ val dockBean = updateExtraLockStatus(bytes) ?: return@forEach
|
|
|
|
|
+ if (!CAN_RETURN) {
|
|
|
|
|
+ return@forEach
|
|
|
|
|
+ }
|
|
|
|
|
+ when (dockBean.type) {
|
|
|
|
|
+ DOCK_TYPE_LOCK -> {
|
|
|
|
|
+ dockBean.getLockList().filter { it.idx > 7 }.forEach { lockBean ->
|
|
|
|
|
+ updateLockStatus(
|
|
|
|
|
+ dockBean.addr,
|
|
|
|
|
+ lockBean.idx,
|
|
|
|
|
+ lockBean.lockEnabled
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 第1-8锁位卡扣状态和钥匙
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun lockBuckleStatus(res: Any) {
|
|
|
|
|
+ LogUtil.i("硬件状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
|
|
|
|
|
+ if (res.isEmpty() || res.any { it.isEmpty() }) {
|
|
|
|
|
+ var tipStr = CommonUtils.getStr(R.string.no_response_board_exists) + " : "
|
|
|
|
|
+ val addressList = mutableListOf<String>()
|
|
|
|
|
+
|
|
|
|
|
+ modBusManager?.mSlaveAddressList?.forEach { itDock ->
|
|
|
|
|
+ if (res.none { it.isNotEmpty() && it[0] == itDock }) {
|
|
|
|
|
+ addressList.add("0x${String.format("%02X", itDock)}")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ tipStr += addressList
|
|
|
|
|
+ ToastUtils.tip(tipStr)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ res.forEach { bytes ->
|
|
|
|
|
+ val dockBean = updateLockStatus(bytes) ?: return@forEach
|
|
|
|
|
+ if (!CAN_RETURN) {
|
|
|
|
|
+ return@forEach
|
|
|
|
|
+ }
|
|
|
|
|
+ when (dockBean.type) {
|
|
|
|
|
+ DOCK_TYPE_KEY -> {
|
|
|
|
|
+ dockBean.getKeyList().forEach { keyBean ->
|
|
|
|
|
+ updateKeyLockStatus(
|
|
|
|
|
+ dockBean.addr,
|
|
|
|
|
+ keyBean.isLeft,
|
|
|
|
|
+ keyBean.lockEnabled
|
|
|
|
|
+ )
|
|
|
|
|
+ //todo 更新锁仓状态
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DOCK_TYPE_LOCK -> {
|
|
|
|
|
+ dockBean.getLockList().forEach { lockBean ->
|
|
|
|
|
+ updateLockStatus(
|
|
|
|
|
+ dockBean.addr,
|
|
|
|
|
+ lockBean.idx,
|
|
|
|
|
+ lockBean.lockEnabled
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DOCK_TYPE_ELEC_LOCK_BOARD -> {
|
|
|
|
|
+ // TODO 占位
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DOCK_TYPE_PORTABLE -> {
|
|
|
|
|
+ // TODO 便携式待完善
|
|
|
|
|
+ dockBean.deviceList.forEach { deviceBean ->
|
|
|
|
|
+ if (deviceBean.isExist) {
|
|
|
|
|
+ when (deviceBean.type) {
|
|
|
|
|
+ DEVICE_TYPE_KEY -> {
|
|
|
|
|
+ updateKeyLockStatus(
|
|
|
|
|
+ dockBean.addr,
|
|
|
|
|
+ true,
|
|
|
|
|
+ deviceBean.lockEnabled
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DEVICE_TYPE_LOCK -> {
|
|
|
|
|
+ updateLockStatus(
|
|
|
|
|
+ dockBean.addr,
|
|
|
|
|
+ deviceBean.idx,
|
|
|
|
|
+ deviceBean.lockEnabled
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DEVICE_TYPE_CARD -> {
|
|
|
|
|
+ readPortalCaseCardRfid(dockBean.addr) { res ->
|
|
|
|
|
+ if (res.size < 11) {
|
|
|
|
|
+ LogUtil.e("Portal Case card rfid error")
|
|
|
|
|
+ return@readPortalCaseCardRfid
|
|
|
|
|
+ }
|
|
|
|
|
+ val rfid = res.copyOfRange(3, 11).toHexStrings(false)
|
|
|
|
|
+ .removeLeadingZeros()
|
|
|
|
|
+ LogUtil.i("卡片RFID : $rfid")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DEVICE_TYPE_FINGERPRINT -> {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取第1-8的锁仓数据和左右钥匙数据
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun updateLockStatus(byteArray: ByteArray): DockBean? {
|
|
|
|
|
+ if (byteArray.isEmpty()) {
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
|
|
+ val dockB = dockList.find { it.addr == byteArray[0] }
|
|
|
|
|
+ return dockB?.parseLockStatus(byteArray)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取额外的9,10锁仓数据
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun updateExtraLockStatus(byteArray: ByteArray): DockBean? {
|
|
|
|
|
+ if (byteArray.isEmpty()) {
|
|
|
|
|
+ return null
|
|
|
|
|
+ }
|
|
|
|
|
+ val dockB = dockList.find { it.addr == byteArray[0] && it.type == DOCK_TYPE_LOCK }
|
|
|
|
|
+ return dockB?.parseExtraLockStatus(byteArray)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 读取设备类型
|
|
* 读取设备类型
|
|
|
*/
|
|
*/
|
|
@@ -243,7 +454,7 @@ object ModBusController {
|
|
|
dock?.let {
|
|
dock?.let {
|
|
|
it.type = type
|
|
it.type = type
|
|
|
} ?: let {
|
|
} ?: let {
|
|
|
- dockList.add(DockBean(idx, type, mutableListOf()))
|
|
|
|
|
|
|
+ dockList.add(DockBean(idx, type, true, mutableListOf()))
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -272,7 +483,7 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 开/关锁具卡扣
|
|
|
|
|
|
|
+ * 开/关锁具卡扣 单
|
|
|
*/
|
|
*/
|
|
|
fun controlLockBuckle(
|
|
fun controlLockBuckle(
|
|
|
isOpen: Boolean,
|
|
isOpen: Boolean,
|
|
@@ -281,7 +492,7 @@ object ModBusController {
|
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateLockBuckleCmd(isOpen, lockIdx)?.let { cmd ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateLockBuckleCmd(isOpen, lockIdx)?.let { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
|
done?.invoke(res)
|
|
done?.invoke(res)
|
|
|
}
|
|
}
|
|
@@ -289,6 +500,9 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 开/关锁具卡扣 多
|
|
|
|
|
+ */
|
|
|
fun controlLockBuckle(
|
|
fun controlLockBuckle(
|
|
|
isOpen: Boolean,
|
|
isOpen: Boolean,
|
|
|
slaveAddress: Byte?,
|
|
slaveAddress: Byte?,
|
|
@@ -296,7 +510,7 @@ object ModBusController {
|
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateLockBuckleCmd(isOpen, lockIdxList)?.let { cmdList ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateLockBuckleCmd(isOpen, lockIdxList)?.let { cmdList ->
|
|
|
cmdList.forEach { cmd ->
|
|
cmdList.forEach { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
|
done?.invoke(res)
|
|
done?.invoke(res)
|
|
@@ -315,7 +529,7 @@ object ModBusController {
|
|
|
done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null
|
|
done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateRfidCmd(idx)?.let { cmd ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateRfidCmd(idx)?.let { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) {
|
|
modBusManager?.sendTo(it, cmd) {
|
|
|
done?.invoke(idx == 0, it)
|
|
done?.invoke(idx == 0, it)
|
|
|
}
|
|
}
|
|
@@ -328,7 +542,7 @@ object ModBusController {
|
|
|
*/
|
|
*/
|
|
|
fun readLockRfid(slaveAddress: Byte?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null) {
|
|
fun readLockRfid(slaveAddress: Byte?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateRfidCmd(lockIdx)?.let { cmd ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateRfidCmd(lockIdx)?.let { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
|
done?.invoke(res)
|
|
done?.invoke(res)
|
|
|
}
|
|
}
|
|
@@ -341,7 +555,7 @@ object ModBusController {
|
|
|
*/
|
|
*/
|
|
|
fun readPortalCaseCardRfid(slaveAddress: Byte?, done: ((res: ByteArray) -> Unit)? = null) {
|
|
fun readPortalCaseCardRfid(slaveAddress: Byte?, done: ((res: ByteArray) -> Unit)? = null) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateRfidCmd(8)?.let { cmd ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateRfidCmd(8)?.let { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
|
done?.invoke(res)
|
|
done?.invoke(res)
|
|
|
}
|
|
}
|
|
@@ -357,6 +571,14 @@ object ModBusController {
|
|
|
?.find { it.isLeft == isLeft }?.rfid = rfid
|
|
?.find { it.isLeft == isLeft }?.rfid = rfid
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新钥匙锁仓状态
|
|
|
|
|
+ */
|
|
|
|
|
+ private fun updateKeyLockStatus(slaveAddress: Byte, isLeft: Boolean, lockEnabled: Boolean) {
|
|
|
|
|
+ dockList.find { it.addr == slaveAddress }?.getKeyList()
|
|
|
|
|
+ ?.find { it.isLeft == isLeft }?.lockEnabled = lockEnabled
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 更新钥匙MAC
|
|
* 更新钥匙MAC
|
|
|
*/
|
|
*/
|
|
@@ -366,11 +588,11 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 通过RFID更新对应的Mac
|
|
|
|
|
|
|
+ * 更新钥匙时候为新设备
|
|
|
*/
|
|
*/
|
|
|
- fun updateKeyMacByRfid(rfid: String, mac: String) {
|
|
|
|
|
- dockList.find { it.type == DOCK_TYPE_KEY }?.getKeyList()?.find { it.rfid == rfid }?.mac =
|
|
|
|
|
- mac
|
|
|
|
|
|
|
+ fun updateKeyNewHardware(slaveAddress: Byte, isLeft: Boolean, newHardware: Boolean) {
|
|
|
|
|
+ dockList.find { it.addr == slaveAddress }?.getKeyList()
|
|
|
|
|
+ ?.find { it.isLeft == isLeft }?.newHardware = newHardware
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -382,46 +604,19 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 设备是否存在,加入deviceType防止有重复的但是不同类型的
|
|
|
|
|
- *
|
|
|
|
|
- * @param deviceType {@link [com.grkj.iscs.model.bo.DeviceTakeUpdateBO]<class>#[deviceType]}
|
|
|
|
|
|
|
+ * 更新锁是不是新设备,就是后台没有数据的
|
|
|
*/
|
|
*/
|
|
|
- fun isDeviceExist(rfid: String, deviceType: Int): Boolean {
|
|
|
|
|
- return when (deviceType) {
|
|
|
|
|
- DEVICE_TYPE_KEY -> {
|
|
|
|
|
- dockList.find { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }
|
|
|
|
|
- ?.getKeyList()?.find { it.rfid == rfid } != null
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- DEVICE_TYPE_LOCK -> {
|
|
|
|
|
- dockList.find { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }
|
|
|
|
|
- ?.getLockList()?.find { it.rfid == rfid } != null
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- else -> {
|
|
|
|
|
- false
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ fun updateLockNewHardware(slaveAddress: Byte, lockIdx: Int, newHardware: Boolean) {
|
|
|
|
|
+ dockList.find { it.addr == slaveAddress }?.getLockList()
|
|
|
|
|
+ ?.find { it.idx == lockIdx }?.newHardware = newHardware
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 操作钥匙灯
|
|
|
|
|
- *
|
|
|
|
|
- * @param leftAction、rightAction 0:保持当前状态 1:点亮 2:熄灭 默认0
|
|
|
|
|
|
|
+ * 更新锁仓状态
|
|
|
*/
|
|
*/
|
|
|
- fun controlKeyLight(
|
|
|
|
|
- slaveAddress: Byte?,
|
|
|
|
|
- leftAction: Int = 0,
|
|
|
|
|
- rightAction: Int = 0,
|
|
|
|
|
- done: ((res: ByteArray) -> Unit)? = null
|
|
|
|
|
- ) {
|
|
|
|
|
- slaveAddress?.let {
|
|
|
|
|
- modBusManager?.generateKeyLightCmd(leftAction, rightAction)?.let { cmd ->
|
|
|
|
|
- modBusManager?.sendTo(it, cmd) {
|
|
|
|
|
- done?.invoke(it)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ fun updateLockStatus(slaveAddress: Byte, lockIdx: Int, lockEnabled: Boolean) {
|
|
|
|
|
+ dockList.find { it.addr == slaveAddress }?.getLockList()
|
|
|
|
|
+ ?.find { it.idx == lockIdx }?.lockEnabled = lockEnabled
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -448,7 +643,7 @@ object ModBusController {
|
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
done: ((res: ByteArray) -> Unit)? = null
|
|
|
) {
|
|
) {
|
|
|
slaveAddress?.let {
|
|
slaveAddress?.let {
|
|
|
- modBusManager?.generateKeyBuckleCmd(isOpen, if (isLeft) 0 else 1)?.let { cmd ->
|
|
|
|
|
|
|
+ ModBusCMDHelper.generateKeyBuckleCmd(isOpen, if (isLeft) 0 else 1)?.let { cmd ->
|
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
modBusManager?.sendTo(it, cmd) { res ->
|
|
|
done?.invoke(res)
|
|
done?.invoke(res)
|
|
|
}
|
|
}
|
|
@@ -472,25 +667,44 @@ object ModBusController {
|
|
|
.flatMap { it.getKeyList() }.find { it.mac == mac }
|
|
.flatMap { it.getKeyList() }.find { it.mac == mac }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据底座获取钥匙
|
|
|
|
|
+ */
|
|
|
fun getKeyByDock(dockAddr: Byte?, isLeft: Boolean): DockBean.KeyBean? {
|
|
fun getKeyByDock(dockAddr: Byte?, isLeft: Boolean): DockBean.KeyBean? {
|
|
|
dockAddr ?: return null
|
|
dockAddr ?: return null
|
|
|
return dockList.find { it.addr == dockAddr }?.getKeyList()?.find { it.isLeft == isLeft }
|
|
return dockList.find { it.addr == dockAddr }?.getKeyList()?.find { it.isLeft == isLeft }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 根据RFID找锁具
|
|
|
|
|
|
|
+ * 钥匙是否存在
|
|
|
*/
|
|
*/
|
|
|
- fun getLockByRfid(rfid: String): DockBean.LockBean? {
|
|
|
|
|
- return dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }
|
|
|
|
|
- .flatMap { it.getLockList() }.find { it.rfid == rfid }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
fun isKeyExist(dockAddr: Byte?, isLeft: Boolean): Boolean {
|
|
fun isKeyExist(dockAddr: Byte?, isLeft: Boolean): Boolean {
|
|
|
dockAddr ?: return false
|
|
dockAddr ?: return false
|
|
|
return dockList.find { it.addr == dockAddr }?.getKeyList()
|
|
return dockList.find { it.addr == dockAddr }?.getKeyList()
|
|
|
?.find { it.isLeft == isLeft && it.isExist } != null
|
|
?.find { it.isLeft == isLeft && it.isExist } != null
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取钥匙锁仓的锁定状态
|
|
|
|
|
+ */
|
|
|
|
|
+ fun getKeyBuckleLockEnabled(dockAddr: Byte?, isLeft: Boolean): Boolean {
|
|
|
|
|
+ dockAddr ?: return false
|
|
|
|
|
+ return dockList.find { it.addr == dockAddr }?.getKeyList()
|
|
|
|
|
+ ?.find { it.isLeft == isLeft }?.lockEnabled == true
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取挂锁锁仓的锁定状态
|
|
|
|
|
+ */
|
|
|
|
|
+ fun getLockBuckleLockEnabled(dockAddr: Byte?, lockIdx: Int): Boolean {
|
|
|
|
|
+ dockAddr ?: return false
|
|
|
|
|
+ return dockList.find { it.addr == dockAddr }?.getLockList()
|
|
|
|
|
+ ?.find { it.idx == lockIdx }?.lockEnabled == true
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取挂锁是否存在
|
|
|
|
|
+ */
|
|
|
fun isLockExist(dockAddr: Byte?, lockIdx: Int): Boolean {
|
|
fun isLockExist(dockAddr: Byte?, lockIdx: Int): Boolean {
|
|
|
dockAddr ?: return false
|
|
dockAddr ?: return false
|
|
|
return dockList.find { it.addr == dockAddr }?.getLockList()
|
|
return dockList.find { it.addr == dockAddr }?.getLockList()
|
|
@@ -567,13 +781,6 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- fun getDockByLockNfc(nfc: String): DockBean? {
|
|
|
|
|
- return dockList.find {
|
|
|
|
|
- (it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE)
|
|
|
|
|
- && it.getLockList().any { it.rfid == nfc }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 根据类型获取底座列表
|
|
* 根据类型获取底座列表
|
|
|
*/
|
|
*/
|
|
@@ -581,12 +788,18 @@ object ModBusController {
|
|
|
return dockList.filter { it.type == type }
|
|
return dockList.filter { it.type == type }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据底座类型获取钥匙列表
|
|
|
|
|
+ */
|
|
|
fun getKeyByDockType(type: Byte): MutableList<DockBean.KeyBean>? {
|
|
fun getKeyByDockType(type: Byte): MutableList<DockBean.KeyBean>? {
|
|
|
return dockList.find { it.type == type }?.let {
|
|
return dockList.find { it.type == type }?.let {
|
|
|
it.getKeyList()
|
|
it.getKeyList()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 全部锁扣的开关
|
|
|
|
|
+ */
|
|
|
fun controlAllLockBuckles(isOpen: Boolean) {
|
|
fun controlAllLockBuckles(isOpen: Boolean) {
|
|
|
dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }
|
|
dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }
|
|
|
.forEach { dockBean ->
|
|
.forEach { dockBean ->
|
|
@@ -598,6 +811,9 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 打印全部底座信息
|
|
|
|
|
+ */
|
|
|
fun printDockInfo() {
|
|
fun printDockInfo() {
|
|
|
LogUtil.i("当前底座列表 : $dockList")
|
|
LogUtil.i("当前底座列表 : $dockList")
|
|
|
dockList.forEach { dockBean ->
|
|
dockList.forEach { dockBean ->
|
|
@@ -626,6 +842,9 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新设备类型
|
|
|
|
|
+ */
|
|
|
fun updateDeviceType() {
|
|
fun updateDeviceType() {
|
|
|
LogUtil.i("____________________________________")
|
|
LogUtil.i("____________________________________")
|
|
|
readDeviceType { res ->
|
|
readDeviceType { res ->
|
|
@@ -722,4 +941,46 @@ object ModBusController {
|
|
|
}
|
|
}
|
|
|
return map
|
|
return map
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取开关量数据
|
|
|
|
|
+ */
|
|
|
|
|
+ fun getSwitchData(): MutableList<DockBean.SwitchBean> {
|
|
|
|
|
+ return dockList.filter { it.type == DOCK_TYPE_COLLECT }.map { it.getSwitchList() }.flatten()
|
|
|
|
|
+ .toMutableList()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 读取开关采集板数据
|
|
|
|
|
+ */
|
|
|
|
|
+ fun readSwitchStatus(
|
|
|
|
|
+ slaveAddress: Byte?, addr: ByteArray, done: ((res: ByteArray) -> Unit)? = null
|
|
|
|
|
+ ) {
|
|
|
|
|
+ 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)
|
|
|
|
|
+// }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|