BusinessManager.kt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.grkj.iscs
  2. import com.grkj.iscs.extentions.removeLeadingZeros
  3. import com.grkj.iscs.extentions.toHexStrings
  4. import com.grkj.iscs.modbus.ModBusController
  5. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
  6. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
  7. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
  8. import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
  9. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
  10. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
  11. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
  12. import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
  13. import com.grkj.iscs.util.ToastUtils
  14. import com.grkj.iscs.util.log.LogUtil
  15. import java.util.stream.Collectors
  16. /**
  17. * 业务层管理
  18. */
  19. object BusinessManager {
  20. fun connectDock(count: Int, isNeedInit: Boolean = false) {
  21. ModBusController.setSlaveCount(count)
  22. ModBusController.interruptReadTrashBinStatus(false)
  23. ModBusController.start(MyApplication.instance!!.applicationContext)
  24. ModBusController.unregisterListener(MyApplication.instance!!.applicationContext)
  25. if (isNeedInit) {
  26. ModBusController.initDevicesStatus()
  27. }
  28. }
  29. fun disconnectDock() {
  30. ModBusController.stop()
  31. }
  32. /**
  33. * @param key 可null,null时用applicationContext做全局监听
  34. */
  35. fun registerStatusListener(key: Any?) {
  36. ModBusController.registerStatusListener(key ?: MyApplication.instance!!.applicationContext) { res ->
  37. LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
  38. res.forEach { bytes ->
  39. val dockBean = ModBusController.updateStatus(bytes) ?: return@forEach
  40. when (dockBean.type) {
  41. DOCK_TYPE_KEY -> {
  42. dockBean.getKeyList().forEach { keyBean ->
  43. if (keyBean.isExist) {
  44. // 放回钥匙,读取rfid
  45. ModBusController.readKeyRfid(dockBean.addr.toInt() - 1, if (keyBean.isLeft) 0 else 1) { isLeft, res ->
  46. val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
  47. ModBusController.updateKeyRfid(dockBean.addr.toInt(), keyBean.isLeft, rfid)
  48. // TODO 从HTTP读取Mac
  49. // TODO 蓝牙通信
  50. // TODO 关卡扣
  51. }
  52. }
  53. }
  54. }
  55. DOCK_TYPE_LOCK -> {
  56. dockBean.getLockList().forEach { lockBean ->
  57. if (lockBean.isExist) {
  58. ModBusController.readLockRfid(dockBean.addr.toInt() - 1, lockBean.idx) { res ->
  59. val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
  60. ModBusController.updateLockRfid(dockBean.addr.toInt(), lockBean.idx, rfid)
  61. }
  62. }
  63. }
  64. }
  65. DOCK_TYPE_PORTABLE -> {
  66. // TODO 便携式待完善
  67. dockBean.deviceList.forEach { deviceBean ->
  68. if (deviceBean.isExist) {
  69. when (deviceBean.type) {
  70. DEVICE_TYPE_KEY -> {
  71. }
  72. DEVICE_TYPE_LOCK -> {
  73. ModBusController.readLockRfid(dockBean.addr.toInt() - 1, deviceBean.idx) { res ->
  74. val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
  75. ModBusController.updateLockRfid(dockBean.addr.toInt(), deviceBean.idx, rfid)
  76. }
  77. }
  78. DEVICE_TYPE_CARD -> {
  79. ModBusController.readPortalCaseCardRfid(dockBean.addr.toInt() - 1) { res ->
  80. val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
  81. println("卡片RFID : $rfid")
  82. }
  83. }
  84. DEVICE_TYPE_FINGERPRINT -> {
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. fun readLockBuckleStatus() {
  95. // TODO slaveIdx暂时写死,调试用
  96. ModBusController.readBuckleStatus(true, 0) { type, res ->
  97. LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
  98. when (type) {
  99. 0 -> {
  100. val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
  101. val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
  102. println("锁具底座卡扣状态 : $isLeftLock - $isRightLock")
  103. }
  104. 1 -> {
  105. val tempList = mutableListOf<Boolean>()
  106. for (i in 0..7) {
  107. tempList.add((res[4].toInt() shr i) and 0x1 == 1)
  108. }
  109. println("锁具底座卡扣1-8状态 : $tempList")
  110. }
  111. 2 -> {
  112. val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
  113. val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
  114. println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
  115. }
  116. }
  117. }
  118. }
  119. fun readKeyBuckleStatus() {
  120. // TODO slaveIdx暂时写死,调试用
  121. ModBusController.readBuckleStatus(false, 1) { type, res ->
  122. LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
  123. // TODO 待验证
  124. when (type) {
  125. 0 -> {
  126. val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
  127. val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
  128. println("钥匙底座卡扣状态 : $isLeftLock - $isRightLock")
  129. }
  130. 1 -> {
  131. val tempList = mutableListOf<Boolean>()
  132. for (i in 0..7) {
  133. tempList.add((res[4].toInt() shr i) and 0x1 == 1)
  134. }
  135. println("锁具底座卡扣1-8状态 : $tempList")
  136. }
  137. 2 -> {
  138. val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
  139. val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
  140. println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
  141. }
  142. }
  143. }
  144. }
  145. }