|
|
@@ -1,22 +1,28 @@
|
|
|
package com.grkj.iscs.modbus
|
|
|
|
|
|
import com.grkj.iscs.util.log.LogUtil
|
|
|
-import kotlin.experimental.and
|
|
|
|
|
|
/**
|
|
|
* RS-485 设备信息 Bean
|
|
|
*
|
|
|
* @param idx 设备序号(地址)
|
|
|
* @param type 0x00:钥匙底座 0x01:锁具底座 0x02:电磁锁控制板
|
|
|
- * @param status 底座当前的状态
|
|
|
+ * @param keyList 钥匙列表,钥匙底座才有
|
|
|
+ * @param lockList 锁具列表,锁具底座才有
|
|
|
*/
|
|
|
-class DeviceBean(var idx: Byte?, var type: Byte?, var status: ByteArray?) {
|
|
|
+class DeviceBean(
|
|
|
+ var idx: Byte?,
|
|
|
+ private var type: Byte?,
|
|
|
+ var keyList: MutableList<KeyBean>,
|
|
|
+ var lockList: MutableList<LockBean>
|
|
|
+) {
|
|
|
|
|
|
- fun parseStatus(byteArray: ByteArray): Boolean {
|
|
|
+ fun parseStatus(byteArray: ByteArray): Any? {
|
|
|
if (byteArray.isEmpty()) {
|
|
|
return false
|
|
|
}
|
|
|
type?.let {
|
|
|
+ // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
|
|
|
when (it) {
|
|
|
0x00.toByte() -> {
|
|
|
// TODO 未验证
|
|
|
@@ -25,32 +31,63 @@ class DeviceBean(var idx: Byte?, var type: Byte?, var status: ByteArray?) {
|
|
|
val rightHasKey = (byteArray[4].toInt() shr 0) and 0x1 == 1
|
|
|
val isRightCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
|
|
|
LogUtil.i("钥匙刷新状态 : $leftHasKey - $isLeftCharging - $rightHasKey - $isRightCharging")
|
|
|
- if (status == null) {
|
|
|
- return false
|
|
|
+ if (keyList.isEmpty()) {
|
|
|
+ keyList.add(KeyBean(true, leftHasKey, isLeftCharging))
|
|
|
+ keyList.add(KeyBean(false, rightHasKey, isRightCharging))
|
|
|
+ return null
|
|
|
}
|
|
|
- return status!![4] and 0x01.toByte() == 0x01.toByte() == leftHasKey
|
|
|
+
|
|
|
+ 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 is1HasLock = (byteArray[4].toInt() shr 0) and 0x1 == 1
|
|
|
- val is2HasLock = (byteArray[4].toInt() shr 1) and 0x1 == 1
|
|
|
- val is3HasLock = (byteArray[4].toInt() shr 2) and 0x1 == 1
|
|
|
- val is4HasLock = (byteArray[4].toInt() shr 3) and 0x1 == 1
|
|
|
- val is5HasLock = (byteArray[4].toInt() shr 4) and 0x1 == 1
|
|
|
- val is6HasLock = (byteArray[4].toInt() shr 5) and 0x1 == 1
|
|
|
- val is7HasLock = (byteArray[4].toInt() shr 6) and 0x1 == 1
|
|
|
- val is8HasLock = (byteArray[4].toInt() shr 7) and 0x1 == 1
|
|
|
- val is9HasLock = (byteArray[3].toInt() shr 0) and 0x1 == 1
|
|
|
+ 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 + 1, 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("锁具刷新状态 : $is1HasLock - $is2HasLock - $is3HasLock - $is4HasLock - $is5HasLock - $is6HasLock - $is7HasLock - $is8HasLock - $is9HasLock")
|
|
|
- return false
|
|
|
+ LogUtil.i("锁具刷新状态 : $changeList")
|
|
|
+ return changeList
|
|
|
}
|
|
|
0x02.toByte() -> {
|
|
|
// TODO 临时占位
|
|
|
- return false
|
|
|
+ return null
|
|
|
}
|
|
|
- else -> return false
|
|
|
+ else -> return null
|
|
|
}
|
|
|
- } ?: return false
|
|
|
+ } ?: return null
|
|
|
}
|
|
|
|
|
|
fun getBit(by: Byte): String {
|
|
|
@@ -65,4 +102,17 @@ class DeviceBean(var idx: Byte?, var type: Byte?, var status: ByteArray?) {
|
|
|
.append((by.toInt() shr 0) and 0x1)
|
|
|
return sb.toString()
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 钥匙
|
|
|
+ */
|
|
|
+ data class KeyBean(var isLeft: Boolean, var hasKey: Boolean, var isCharging: Boolean)
|
|
|
+ /**
|
|
|
+ * 锁具
|
|
|
+ *
|
|
|
+ * @param idx 锁具序号 1-10
|
|
|
+ * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
|
|
|
+ */
|
|
|
+ data class LockBean(var idx: Int, var hasLock: Boolean, var rfid: String?)
|
|
|
}
|