|
|
@@ -100,23 +100,80 @@ object ModBusController {
|
|
|
/**
|
|
|
* 初始化所有设备的状态
|
|
|
*/
|
|
|
+ // TODO 通电后多久执行?App每次重启的执行是什么
|
|
|
fun initDevicesStatus() {
|
|
|
- readDeviceType {
|
|
|
+ readDeviceType { res ->
|
|
|
+ res.forEach { bytes ->
|
|
|
+ if (bytes.size < 5) return@forEach
|
|
|
+ // TODO 设备具体数据由0x0011寄存器提供
|
|
|
+ updateDeviceType(bytes[0], bytes[4])
|
|
|
+ val type = when (bytes[4]) {
|
|
|
+ 0x00.toByte() -> "钥匙底座"
|
|
|
+ 0x01.toByte() -> "锁具底座"
|
|
|
+ 0x02.toByte() -> "电磁锁控制板"
|
|
|
+ else -> "未知"
|
|
|
+ }
|
|
|
+ LogUtil.i("initDevicesStatus 设备(${bytes[0].toInt()})类型:$type")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Executor.delayOnIO({
|
|
|
// TODO 待完善
|
|
|
- // TODO 打开所有无锁的卡扣
|
|
|
- // TODO 打开所有无钥匙的卡扣
|
|
|
- // TODO 关闭所有钥匙灯光
|
|
|
- // TODO 读取所有锁的RFID
|
|
|
- // TODO 读取所有钥匙的RFID
|
|
|
+ initLock() // TODO 打开所有无锁的卡扣、读取所有锁的RFID
|
|
|
+ initKey() // TODO 打开所有无钥匙的卡扣、关闭所有钥匙灯光、读取所有钥匙的RFID
|
|
|
// TODO 设置所有钥匙的模式
|
|
|
+ // TODO 通过HTTP获取所有钥匙的Mac
|
|
|
+ }, 3000)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化锁具——打开所有无锁的卡扣、读取RFID
|
|
|
+ */
|
|
|
+ private fun initLock() {
|
|
|
+ LogUtil.i("initLock : $deviceList")
|
|
|
+ deviceList.filter { it.type == 0x01.toByte() }.forEach { deviceBean ->
|
|
|
+ deviceBean.lockList.forEach { lockBean ->
|
|
|
+ if (lockBean.hasLock) {
|
|
|
+ LogUtil.i("initLock rfid: ${deviceBean.idx!!.toInt() - 1} : ${lockBean.idx}")
|
|
|
+ readLockRfid(deviceBean.idx!!.toInt() - 1, lockBean.idx)
|
|
|
+ } else {
|
|
|
+ LogUtil.i("initLock buckle : ${deviceBean.idx!!.toInt() - 1} : ${lockBean.idx}")
|
|
|
+ controlLockBuckle(false, deviceBean.idx!!.toInt() - 1, lockBean.idx)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- fun checkStatus(byteArray: ByteArray): Any? {
|
|
|
+ /**
|
|
|
+ * 初始化钥匙——关闭所有钥匙灯光
|
|
|
+ */
|
|
|
+ private fun initKey() {
|
|
|
+ LogUtil.i("initKey : $deviceList")
|
|
|
+ deviceList.filter { it.type == 0x00.toByte() }.forEach { deviceBean ->
|
|
|
+ controlKeyLight(deviceBean.idx!!.toInt() - 1, 2, 2)
|
|
|
+ deviceBean.keyList.forEach { key ->
|
|
|
+ if (key.hasKey) {
|
|
|
+ LogUtil.i("initKey : ${deviceBean.idx!!.toInt() - 1} : ${key.isLeft}")
|
|
|
+ readKeyRfid(deviceBean.idx!!.toInt() - 1, key.isLeft)
|
|
|
+ } else {
|
|
|
+ // TODO 关闭钥匙卡扣
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新状态
|
|
|
+ */
|
|
|
+ fun updateStatus(byteArray: ByteArray): Any? {
|
|
|
if (byteArray.isEmpty()) {
|
|
|
return null
|
|
|
}
|
|
|
- return deviceList.find { it.idx == byteArray[0] }?.parseStatus(byteArray)
|
|
|
+ val deviceBean = deviceList.find { it.idx == byteArray[0] }
|
|
|
+ return deviceBean?.parseStatus(byteArray) ?: let {
|
|
|
+ val temp = DeviceBean(byteArray[0], null, mutableListOf(), mutableListOf())
|
|
|
+ deviceList.add(temp)
|
|
|
+ temp.parseStatus(byteArray)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -128,6 +185,13 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新设备类型
|
|
|
+ */
|
|
|
+ fun updateDeviceType(idx: Byte?, type: Byte?) {
|
|
|
+ deviceList.find { it.idx == idx }?.type = type
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 读取卡扣状态
|
|
|
*
|
|
|
@@ -161,6 +225,9 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 读取钥匙RFID
|
|
|
+ */
|
|
|
fun readKeyRfid(slaveIdx: Int?, isLeft: Boolean, done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null) {
|
|
|
slaveIdx?.let {
|
|
|
modBusManager?.generateKeyRfidCmd(isLeft)?.let { cmd ->
|
|
|
@@ -171,6 +238,9 @@ object ModBusController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 读取锁具RFID
|
|
|
+ */
|
|
|
fun readLockRfid(slaveIdx: Int?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null) {
|
|
|
slaveIdx?.let {
|
|
|
modBusManager?.generateLockRfidCmd(lockIdx)?.let { cmd ->
|