DeviceBean.kt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.grkj.iscs.modbus
  2. import com.grkj.iscs.util.log.LogUtil
  3. /**
  4. * RS-485 设备信息 Bean
  5. *
  6. * @param addr 设备地址
  7. * @param type 0x00:钥匙底座 0x01:锁具底座 0x02:电磁锁控制板
  8. * @param keyList 钥匙列表,钥匙底座才有
  9. * @param lockList 锁具列表,锁具底座才有
  10. */
  11. class DeviceBean(
  12. var addr: Byte?,
  13. var type: Byte?,
  14. var keyList: MutableList<KeyBean>,
  15. var lockList: MutableList<LockBean>
  16. ) {
  17. fun parseStatus(byteArray: ByteArray): Any? {
  18. if (byteArray.isEmpty()) {
  19. return false
  20. }
  21. type?.let {
  22. // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
  23. when (it) {
  24. 0x00.toByte() -> {
  25. // TODO 未验证
  26. val leftHasKey = (byteArray[4].toInt() shr 0) and 0x1 == 1
  27. val isLeftCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
  28. val rightHasKey = (byteArray[3].toInt() shr 0) and 0x1 == 1
  29. val isRightCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
  30. LogUtil.i("钥匙刷新状态 : $leftHasKey - $isLeftCharging - $rightHasKey - $isRightCharging")
  31. if (keyList.isEmpty()) {
  32. keyList.add(KeyBean(true, leftHasKey, isLeftCharging, null, null))
  33. keyList.add(KeyBean(false, rightHasKey, isRightCharging, null, null))
  34. return null
  35. }
  36. val changeList = mutableListOf<KeyBean>()
  37. keyList.forEach { keyBean ->
  38. if (keyBean.isLeft) {
  39. if (leftHasKey != keyBean.hasKey) {
  40. keyBean.hasKey = leftHasKey
  41. keyBean.isCharging = isLeftCharging
  42. changeList.add(keyBean)
  43. }
  44. } else {
  45. if (rightHasKey != keyBean.hasKey) {
  46. keyBean.hasKey = rightHasKey
  47. keyBean.isCharging = isRightCharging
  48. changeList.add(keyBean)
  49. }
  50. }
  51. }
  52. return changeList
  53. }
  54. 0x01.toByte() -> {
  55. val tempList = mutableListOf<Boolean>()
  56. for (i in 0..7) {
  57. tempList.add((byteArray[4].toInt() shr i) and 0x1 == 1)
  58. }
  59. tempList.add((byteArray[3].toInt() shr 0) and 0x1 == 1)
  60. tempList.add((byteArray[3].toInt() shr 1) and 0x1 == 1)
  61. if (lockList.isEmpty()) {
  62. for (i in 0 until tempList.size) {
  63. lockList.add(LockBean(i, tempList[i], null))
  64. }
  65. }
  66. val changeList = mutableListOf<LockBean>()
  67. for (i in 0 until lockList.size) {
  68. if (lockList[i].hasLock != tempList[i]) {
  69. lockList[i].hasLock = tempList[i]
  70. changeList.add(lockList[i])
  71. }
  72. }
  73. LogUtil.i("锁具刷新状态 : $changeList")
  74. return changeList
  75. }
  76. 0x02.toByte() -> {
  77. // TODO 临时占位
  78. return null
  79. }
  80. else -> return null
  81. }
  82. } ?: return null
  83. }
  84. fun getBit(by: Byte): String {
  85. val sb = StringBuffer()
  86. sb.append((by.toInt() shr 7) and 0x1)
  87. .append((by.toInt() shr 6) and 0x1)
  88. .append((by.toInt() shr 5) and 0x1)
  89. .append((by.toInt() shr 4) and 0x1)
  90. .append((by.toInt() shr 3) and 0x1)
  91. .append((by.toInt() shr 2) and 0x1)
  92. .append((by.toInt() shr 1) and 0x1)
  93. .append((by.toInt() shr 0) and 0x1)
  94. return sb.toString()
  95. }
  96. override fun toString(): String {
  97. return "DeviceBean(addr=$addr, type=$type, keyList=$keyList, lockList=$lockList)"
  98. }
  99. /**
  100. * 钥匙
  101. */
  102. data class KeyBean(var isLeft: Boolean, var hasKey: Boolean, var isCharging: Boolean, var rfid: String?, var mac: String?)
  103. /**
  104. * 锁具
  105. *
  106. * @param idx 锁具序号 0-9
  107. * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
  108. */
  109. data class LockBean(var idx: Int, var hasLock: Boolean, var rfid: String?)
  110. }