| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- package com.grkj.iscs.modbus
- import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
- import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
- 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.util.log.LogUtil
- /**
- * RS-485 设备底座 Bean
- *
- * @param addr 设备地址 0x01-0x16
- * @param type 0x00:钥匙底座 0x01:锁具底座 0x02:电磁锁控制板 0x03:便携式底座
- * @param deviceList 设备列表
- */
- class DockBean(
- var addr: Byte,
- var type: Byte?,
- var deviceList: MutableList<DeviceBean>
- ) {
- fun parseStatus(byteArray: ByteArray): DockBean? {
- if (byteArray.isEmpty()) {
- return null
- }
- type?.let {
- // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
- when (it) {
- DOCK_TYPE_KEY -> {
- // 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 (getKeyList().isEmpty()) {
- deviceList.add(KeyBean(0, leftHasKey, true, isLeftCharging, null, null))
- deviceList.add(KeyBean(1, rightHasKey, false, isRightCharging, null, null))
- return null
- }
- val changeList = mutableListOf<DeviceBean>()
- getKeyList().forEach { keyBean ->
- if (keyBean.isLeft) {
- if (leftHasKey != keyBean.isExist) {
- keyBean.isExist = leftHasKey
- keyBean.isCharging = isLeftCharging
- changeList.add(keyBean)
- // 拿走钥匙,移除钥匙信息
- if (!leftHasKey) {
- keyBean.rfid = null
- keyBean.mac = null
- }
- }
- } else {
- if (rightHasKey != keyBean.isExist) {
- keyBean.isExist = rightHasKey
- keyBean.isCharging = isRightCharging
- changeList.add(keyBean)
- // 拿走钥匙,移除钥匙信息
- if (!rightHasKey) {
- keyBean.rfid = null
- keyBean.mac = null
- }
- }
- }
- }
- return DockBean(addr, type, changeList)
- }
- DOCK_TYPE_LOCK -> {
- 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 (getLockList().isEmpty()) {
- for (i in 0 until tempList.size) {
- deviceList.add(LockBean(i, tempList[i], null))
- }
- }
- val changeList = mutableListOf<DeviceBean>()
- for (i in 0 until getLockList().size) {
- if (getLockList()[i].isExist != tempList[i]) {
- getLockList()[i].isExist = tempList[i]
- changeList.add(getLockList()[i])
- // 拿走锁具,移除锁具信息
- if (!tempList[i]) {
- getLockList()[i].rfid = null
- }
- }
- }
- LogUtil.i("锁具刷新状态 : $changeList")
- return DockBean(addr, type, changeList)
- }
- DOCK_TYPE_ELEC_LOCK_BOARD -> {
- // TODO 临时占位
- return null
- }
- DOCK_TYPE_PORTABLE -> {
- // TODO 便携式底座更新
- val tempList = mutableListOf<Boolean>()
- for (i in 0..2) {
- tempList.add((byteArray[4].toInt() shr i) and 0x1 == 1)
- }
- if (getLockList().isEmpty()) {
- for (i in 0 until tempList.size) {
- deviceList.add(LockBean(i, tempList[i], null))
- }
- }
- val isKeyExist = (byteArray[3].toInt() shr 0) and 0x1 == 1
- val isKeyCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
- if (getKeyList().isEmpty()) {
- deviceList.add(KeyBean(0, isKeyExist, true, isKeyCharging, null, null))
- }
- val isDockCharging = (byteArray[3].toInt() shr 6) and 0x1 == 1
- val isDockFullCharged = (byteArray[3].toInt() shr 7) and 0x1 == 1
- val changeList = mutableListOf<DeviceBean>()
- for (i in 0 until getLockList().size) {
- if (getLockList()[i].isExist != tempList[i]) {
- getLockList()[i].isExist = tempList[i]
- changeList.add(getLockList()[i])
- // 拿走锁具,移除锁具信息
- if (!tempList[i]) {
- getLockList()[i].rfid = null
- }
- }
- }
- if (getKeyList()[0].isExist != isKeyExist) {
- getKeyList()[0].isExist = isKeyExist
- changeList.add(getKeyList()[0])
- // 拿走钥匙,移除钥匙信息
- if (!isKeyExist) {
- getKeyList()[0].rfid = null
- getKeyList()[0].mac = null
- }
- }
- return DockBean(addr, type, changeList)
- }
- else -> return null
- }
- } ?: return null
- }
- fun getKeyList(): MutableList<KeyBean> {
- return deviceList.filter { it.type == 0 } as MutableList<KeyBean>
- }
- fun getLockList() : MutableList<LockBean> {
- return deviceList.filter { it.type == 1 } as MutableList<LockBean>
- }
- 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()
- }
- /**
- * 通用设备信息Bean
- *
- * @param type 0:钥匙 1:锁
- * @param idx 锁具底座时:锁具序号 0-9 钥匙底座时:0-左 1-右 便携式底座:锁具0-2 钥匙0
- * @param isExist true:有设备 false:无设备
- */
- open class DeviceBean(
- var type: Int,
- var idx: Int,
- var isExist: Boolean
- )
- /**
- * 钥匙
- */
- // TODO isLeft待移除,使用idx替代
- class KeyBean(
- idx: Int,
- isExist: Boolean,
- var isLeft: Boolean,
- var isCharging: Boolean,
- var rfid: String?,
- var mac: String?
- ) : DeviceBean(DEVICE_TYPE_KEY, idx, isExist)
- /**
- * 锁具
- *
- * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
- */
- class LockBean(
- idx: Int,
- isExist: Boolean,
- var rfid: String?
- ) : DeviceBean(DEVICE_TYPE_LOCK, idx, isExist)
- }
|