DockBean.kt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.grkj.iscs.modbus
  2. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
  3. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
  4. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
  5. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
  6. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
  7. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
  8. import com.grkj.iscs.util.log.LogUtil
  9. /**
  10. * RS-485 设备底座 Bean
  11. *
  12. * @param addr 设备地址 0x01-0x16
  13. * @param type 0x00:钥匙底座 0x01:锁具底座 0x02:电磁锁控制板 0x03:便携式底座
  14. * @param deviceList 设备列表
  15. */
  16. class DockBean(
  17. var addr: Byte,
  18. var type: Byte?,
  19. var deviceList: MutableList<DeviceBean>
  20. ) {
  21. fun parseStatus(byteArray: ByteArray): DockBean? {
  22. if (byteArray.isEmpty()) {
  23. return null
  24. }
  25. type?.let {
  26. // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
  27. when (it) {
  28. DOCK_TYPE_KEY -> {
  29. // TODO 未验证
  30. val leftHasKey = (byteArray[4].toInt() shr 0) and 0x1 == 1
  31. val isLeftCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
  32. val rightHasKey = (byteArray[3].toInt() shr 0) and 0x1 == 1
  33. val isRightCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
  34. LogUtil.i("钥匙刷新状态 : $leftHasKey - $isLeftCharging - $rightHasKey - $isRightCharging")
  35. if (getKeyList().isEmpty()) {
  36. deviceList.add(KeyBean(0, leftHasKey, true, isLeftCharging, null, null))
  37. deviceList.add(KeyBean(1, rightHasKey, false, isRightCharging, null, null))
  38. return null
  39. }
  40. val changeList = mutableListOf<DeviceBean>()
  41. getKeyList().forEach { keyBean ->
  42. if (keyBean.isLeft) {
  43. if (leftHasKey != keyBean.isExist) {
  44. keyBean.isExist = leftHasKey
  45. keyBean.isCharging = isLeftCharging
  46. changeList.add(keyBean)
  47. // 拿走钥匙,移除钥匙信息
  48. if (!leftHasKey) {
  49. keyBean.rfid = null
  50. keyBean.mac = null
  51. }
  52. }
  53. } else {
  54. if (rightHasKey != keyBean.isExist) {
  55. keyBean.isExist = rightHasKey
  56. keyBean.isCharging = isRightCharging
  57. changeList.add(keyBean)
  58. // 拿走钥匙,移除钥匙信息
  59. if (!rightHasKey) {
  60. keyBean.rfid = null
  61. keyBean.mac = null
  62. }
  63. }
  64. }
  65. }
  66. return DockBean(addr, type, changeList)
  67. }
  68. DOCK_TYPE_LOCK -> {
  69. val tempList = mutableListOf<Boolean>()
  70. for (i in 0..7) {
  71. tempList.add((byteArray[4].toInt() shr i) and 0x1 == 1)
  72. }
  73. tempList.add((byteArray[3].toInt() shr 0) and 0x1 == 1)
  74. tempList.add((byteArray[3].toInt() shr 1) and 0x1 == 1)
  75. if (getLockList().isEmpty()) {
  76. for (i in 0 until tempList.size) {
  77. deviceList.add(LockBean(i, tempList[i], null))
  78. }
  79. }
  80. val changeList = mutableListOf<DeviceBean>()
  81. for (i in 0 until getLockList().size) {
  82. if (getLockList()[i].isExist != tempList[i]) {
  83. getLockList()[i].isExist = tempList[i]
  84. changeList.add(getLockList()[i])
  85. // 拿走锁具,移除锁具信息
  86. if (!tempList[i]) {
  87. getLockList()[i].rfid = null
  88. }
  89. }
  90. }
  91. LogUtil.i("锁具刷新状态 : $changeList")
  92. return DockBean(addr, type, changeList)
  93. }
  94. DOCK_TYPE_ELEC_LOCK_BOARD -> {
  95. // TODO 临时占位
  96. return null
  97. }
  98. DOCK_TYPE_PORTABLE -> {
  99. // TODO 便携式底座更新
  100. val tempList = mutableListOf<Boolean>()
  101. for (i in 0..2) {
  102. tempList.add((byteArray[4].toInt() shr i) and 0x1 == 1)
  103. }
  104. if (getLockList().isEmpty()) {
  105. for (i in 0 until tempList.size) {
  106. deviceList.add(LockBean(i, tempList[i], null))
  107. }
  108. }
  109. val isKeyExist = (byteArray[3].toInt() shr 0) and 0x1 == 1
  110. val isKeyCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
  111. if (getKeyList().isEmpty()) {
  112. deviceList.add(KeyBean(0, isKeyExist, true, isKeyCharging, null, null))
  113. }
  114. val isDockCharging = (byteArray[3].toInt() shr 6) and 0x1 == 1
  115. val isDockFullCharged = (byteArray[3].toInt() shr 7) and 0x1 == 1
  116. val changeList = mutableListOf<DeviceBean>()
  117. for (i in 0 until getLockList().size) {
  118. if (getLockList()[i].isExist != tempList[i]) {
  119. getLockList()[i].isExist = tempList[i]
  120. changeList.add(getLockList()[i])
  121. // 拿走锁具,移除锁具信息
  122. if (!tempList[i]) {
  123. getLockList()[i].rfid = null
  124. }
  125. }
  126. }
  127. if (getKeyList()[0].isExist != isKeyExist) {
  128. getKeyList()[0].isExist = isKeyExist
  129. changeList.add(getKeyList()[0])
  130. // 拿走钥匙,移除钥匙信息
  131. if (!isKeyExist) {
  132. getKeyList()[0].rfid = null
  133. getKeyList()[0].mac = null
  134. }
  135. }
  136. return DockBean(addr, type, changeList)
  137. }
  138. else -> return null
  139. }
  140. } ?: return null
  141. }
  142. fun getKeyList(): MutableList<KeyBean> {
  143. return deviceList.filter { it.type == 0 } as MutableList<KeyBean>
  144. }
  145. fun getLockList() : MutableList<LockBean> {
  146. return deviceList.filter { it.type == 1 } as MutableList<LockBean>
  147. }
  148. fun getBit(by: Byte): String {
  149. val sb = StringBuffer()
  150. sb.append((by.toInt() shr 7) and 0x1)
  151. .append((by.toInt() shr 6) and 0x1)
  152. .append((by.toInt() shr 5) and 0x1)
  153. .append((by.toInt() shr 4) and 0x1)
  154. .append((by.toInt() shr 3) and 0x1)
  155. .append((by.toInt() shr 2) and 0x1)
  156. .append((by.toInt() shr 1) and 0x1)
  157. .append((by.toInt() shr 0) and 0x1)
  158. return sb.toString()
  159. }
  160. /**
  161. * 通用设备信息Bean
  162. *
  163. * @param type 0:钥匙 1:锁
  164. * @param idx 锁具底座时:锁具序号 0-9 钥匙底座时:0-左 1-右 便携式底座:锁具0-2 钥匙0
  165. * @param isExist true:有设备 false:无设备
  166. */
  167. open class DeviceBean(
  168. var type: Int,
  169. var idx: Int,
  170. var isExist: Boolean
  171. )
  172. /**
  173. * 钥匙
  174. */
  175. // TODO isLeft待移除,使用idx替代
  176. class KeyBean(
  177. idx: Int,
  178. isExist: Boolean,
  179. var isLeft: Boolean,
  180. var isCharging: Boolean,
  181. var rfid: String?,
  182. var mac: String?
  183. ) : DeviceBean(DEVICE_TYPE_KEY, idx, isExist)
  184. /**
  185. * 锁具
  186. *
  187. * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
  188. */
  189. class LockBean(
  190. idx: Int,
  191. isExist: Boolean,
  192. var rfid: String?
  193. ) : DeviceBean(DEVICE_TYPE_LOCK, idx, isExist)
  194. }