| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.grkj.iscs.modbus
- import com.grkj.iscs.util.log.LogUtil
- /**
- * RS-485 设备信息 Bean
- *
- * @param addr 设备地址
- * @param type 0x00:钥匙底座 0x01:锁具底座 0x02:电磁锁控制板
- * @param keyList 钥匙列表,钥匙底座才有
- * @param lockList 锁具列表,锁具底座才有
- */
- class DeviceBean(
- var addr: Byte?,
- var type: Byte?,
- var keyList: MutableList<KeyBean>,
- var lockList: MutableList<LockBean>
- ) {
- fun parseStatus(byteArray: ByteArray): Any? {
- if (byteArray.isEmpty()) {
- return false
- }
- type?.let {
- // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
- when (it) {
- 0x00.toByte() -> {
- // TODO 未验证
- val leftHasKey = (byteArray[4].toInt() shr 0) and 0x1 == 1
- val isLeftCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
- val rightHasKey = (byteArray[3].toInt() shr 0) and 0x1 == 1
- val isRightCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
- LogUtil.i("钥匙刷新状态 : $leftHasKey - $isLeftCharging - $rightHasKey - $isRightCharging")
- if (keyList.isEmpty()) {
- keyList.add(KeyBean(true, leftHasKey, isLeftCharging, null, null))
- keyList.add(KeyBean(false, rightHasKey, isRightCharging, null, null))
- return null
- }
- val changeList = mutableListOf<KeyBean>()
- keyList.forEach { keyBean ->
- if (keyBean.isLeft) {
- if (leftHasKey != keyBean.hasKey) {
- keyBean.hasKey = leftHasKey
- keyBean.isCharging = isLeftCharging
- changeList.add(keyBean)
- }
- } else {
- if (rightHasKey != keyBean.hasKey) {
- keyBean.hasKey = rightHasKey
- keyBean.isCharging = isRightCharging
- changeList.add(keyBean)
- }
- }
- }
- return changeList
- }
- 0x01.toByte() -> {
- val tempList = mutableListOf<Boolean>()
- for (i in 0..7) {
- tempList.add((byteArray[4].toInt() shr i) and 0x1 == 1)
- }
- tempList.add((byteArray[3].toInt() shr 0) and 0x1 == 1)
- tempList.add((byteArray[3].toInt() shr 1) and 0x1 == 1)
- if (lockList.isEmpty()) {
- for (i in 0 until tempList.size) {
- lockList.add(LockBean(i, tempList[i], null))
- }
- }
- val changeList = mutableListOf<LockBean>()
- for (i in 0 until lockList.size) {
- if (lockList[i].hasLock != tempList[i]) {
- lockList[i].hasLock = tempList[i]
- changeList.add(lockList[i])
- }
- }
- LogUtil.i("锁具刷新状态 : $changeList")
- return changeList
- }
- 0x02.toByte() -> {
- // TODO 临时占位
- return null
- }
- else -> return null
- }
- } ?: return null
- }
- fun getBit(by: Byte): String {
- val sb = StringBuffer()
- sb.append((by.toInt() shr 7) and 0x1)
- .append((by.toInt() shr 6) and 0x1)
- .append((by.toInt() shr 5) and 0x1)
- .append((by.toInt() shr 4) and 0x1)
- .append((by.toInt() shr 3) and 0x1)
- .append((by.toInt() shr 2) and 0x1)
- .append((by.toInt() shr 1) and 0x1)
- .append((by.toInt() shr 0) and 0x1)
- return sb.toString()
- }
- override fun toString(): String {
- return "DeviceBean(addr=$addr, type=$type, keyList=$keyList, lockList=$lockList)"
- }
- /**
- * 钥匙
- */
- data class KeyBean(var isLeft: Boolean, var hasKey: Boolean, var isCharging: Boolean, var rfid: String?, var mac: String?)
- /**
- * 锁具
- *
- * @param idx 锁具序号 0-9
- * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
- */
- data class LockBean(var idx: Int, var hasLock: Boolean, var rfid: String?)
- }
|