Selaa lähdekoodia

修改底座和底座设备类结构,方便后续拓展(暂时使用MutableList,用filter过滤,若有性能问题,改成HashMap)

Frankensteinly 1 vuosi sitten
vanhempi
sitoutus
dcf06b9c5f

+ 29 - 25
app/src/main/java/com/grkj/iscs/activity/ModbusActivity.kt

@@ -5,10 +5,8 @@ import com.grkj.iscs.databinding.ActivityModbusBinding
 import com.grkj.iscs.extentions.removeLeadingZeros
 import com.grkj.iscs.extentions.toHexStrings
 import com.grkj.iscs.iview.IModbusView
-import com.grkj.iscs.modbus.DeviceBean
 import com.grkj.iscs.modbus.ModBusController
 import com.grkj.iscs.presenter.ModBusPresenter
-import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.ToastUtils
 import com.grkj.iscs.util.log.LogUtil
 import java.util.stream.Collectors
@@ -29,28 +27,34 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         ModBusController.registerStatusListener(this) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             res.forEach { bytes ->
-                val deviceBean = ModBusController.updateStatus(bytes) ?: return@forEach
-                if (deviceBean.type == 0x00.toByte()) {
-                    deviceBean.keyList.forEach { keyBean ->
-                        if (keyBean.hasKey) {
-                            // 放回钥匙,读取rfid
-                            ModBusController.readKeyRfid(deviceBean.addr.toInt() - 1, keyBean.isLeft) { isLeft, res ->
-                                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                ModBusController.updateKeyRfid(deviceBean.addr.toInt(), keyBean.isLeft, rfid)
-                                // TODO 从HTTP读取Mac
-                                // TODO 蓝牙通信
+                val dockBean = ModBusController.updateStatus(bytes) ?: return@forEach
+                when (dockBean.type) {
+                    0x00.toByte() -> {
+                        dockBean.getKeyList().forEach { keyBean ->
+                            if (keyBean.isExist) {
+                                // 放回钥匙,读取rfid
+                                ModBusController.readKeyRfid(dockBean.addr.toInt() - 1, keyBean.isLeft) { isLeft, res ->
+                                    val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
+                                    ModBusController.updateKeyRfid(dockBean.addr.toInt(), keyBean.isLeft, rfid)
+                                    // TODO 从HTTP读取Mac
+                                    // TODO 蓝牙通信
+                                }
                             }
                         }
                     }
-                } else if (deviceBean.type == 0x01.toByte()) {
-                    deviceBean.lockList.forEach { lockBean ->
-                        if (lockBean.hasLock) {
-                            ModBusController.readLockRfid(deviceBean.addr.toInt() - 1, lockBean.idx) { res ->
-                                val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                ModBusController.updateLockRfid(deviceBean.addr.toInt(), lockBean.idx, rfid)
+                    0x01.toByte() -> {
+                        dockBean.getLockList().forEach { lockBean ->
+                            if (lockBean.isExist) {
+                                ModBusController.readLockRfid(dockBean.addr.toInt() - 1, lockBean.idx) { res ->
+                                    val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
+                                    ModBusController.updateLockRfid(dockBean.addr.toInt(), lockBean.idx, rfid)
+                                }
                             }
                         }
                     }
+                    0x03.toByte() -> {
+                        // TODO 便携式待完善
+                    }
                 }
             }
         }
@@ -224,25 +228,25 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         }
 
         mBinding?.closeAllLockBuckles?.setOnClickListener {
-            ModBusController.deviceList.filter { it.type == 0x01.toByte() }.forEach { deviceBean ->
-                val list = deviceBean.lockList.stream().map { it.idx }.collect(Collectors.toList())
-                ModBusController.controlLockBuckle(false, deviceBean.addr.toInt() - 1, list) {
+            ModBusController.dockList.filter { it.type == 0x01.toByte() }.forEach { dockBean ->
+                val list = dockBean.getLockList().stream().map { it.idx }.collect(Collectors.toList())
+                ModBusController.controlLockBuckle(false, dockBean.addr.toInt() - 1, list) {
                     LogUtil.i("关所有锁卡扣 : ${it.toHexStrings()}")
                 }
             }
         }
 
         mBinding?.openAllLockBuckles?.setOnClickListener {
-            ModBusController.deviceList.filter { it.type == 0x01.toByte() }.forEach { deviceBean ->
-                val list = deviceBean.lockList.stream().map { it.idx }.collect(Collectors.toList())
-                ModBusController.controlLockBuckle(true, deviceBean.addr.toInt() - 1, list) {
+            ModBusController.dockList.filter { it.type == 0x01.toByte() }.forEach { dockBean ->
+                val list = dockBean.getLockList().stream().map { it.idx }.collect(Collectors.toList())
+                ModBusController.controlLockBuckle(true, dockBean.addr.toInt() - 1, list) {
                     LogUtil.i("开所有锁卡扣 : ${it.toHexStrings()}")
                 }
             }
         }
 
         mBinding?.deviceList?.setOnClickListener {
-            println("当前设备列表 : ${ModBusController.deviceList}")
+            println("当前底座列表 : ${ModBusController.dockList}")
         }
 
         mBinding?.exit?.setOnClickListener { finish() }

+ 79 - 33
app/src/main/java/com/grkj/iscs/modbus/DeviceBean.kt → app/src/main/java/com/grkj/iscs/modbus/DockBean.kt

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

+ 31 - 30
app/src/main/java/com/grkj/iscs/modbus/ModBusController.kt

@@ -14,9 +14,9 @@ import java.util.concurrent.Executors
 object ModBusController {
 
     /**
-     * 所有的设备列表
+     * 底座列表
      */
-    var deviceList: MutableList<DeviceBean> = mutableListOf()
+    var dockList: MutableList<DockBean> = mutableListOf()
 
     private const val LISTENER_TYPE_STATUS = 3
 
@@ -113,6 +113,7 @@ object ModBusController {
                     0x00.toByte() -> "钥匙底座"
                     0x01.toByte() -> "锁具底座"
                     0x02.toByte() -> "电磁锁控制板"
+                    0x03.toByte() -> "便携式底座"
                     else -> "未知"
                 }
                 LogUtil.i("initDevicesStatus 设备(${bytes[0].toInt()})类型:$type")
@@ -131,20 +132,20 @@ object ModBusController {
      * 初始化锁具——打开所有无锁的卡扣、读取RFID
      */
     private fun initLock() {
-        LogUtil.i("initLock : $deviceList")
-        deviceList.filter { it.type == 0x01.toByte() }.forEach { deviceBean ->
-            val hasLockIdxList = deviceBean.lockList.filter { it.hasLock }.map { it.idx }
-            val noLockIdxList = deviceBean.lockList.filter { !it.hasLock }.map { it.idx } as MutableList<Int>
+        LogUtil.i("initLock : $dockList")
+        dockList.filter { it.type == 0x01.toByte() }.forEach { dockBean ->
+            val hasLockIdxList = dockBean.getLockList().filter { it.isExist }.map { it.idx }
+            val noLockIdxList = dockBean.getLockList().filter { !it.isExist }.map { it.idx } as MutableList<Int>
 
             hasLockIdxList.forEach { idx ->
-                readLockRfid(deviceBean.addr.toInt() - 1, idx) { res ->
+                readLockRfid(dockBean.addr.toInt() - 1, idx) { res ->
                     val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                     LogUtil.i("初始化锁具 RFID : $rfid")
-                    updateLockRfid(deviceBean.addr.toInt(), idx, rfid)
+                    updateLockRfid(dockBean.addr.toInt(), idx, rfid)
                 }
             }
 
-            controlLockBuckle(true, deviceBean.addr.toInt() - 1, noLockIdxList)
+            controlLockBuckle(true, dockBean.addr.toInt() - 1, noLockIdxList)
         }
     }
 
@@ -152,16 +153,16 @@ object ModBusController {
      * 初始化钥匙——关闭所有钥匙灯光
      */
     private fun initKey() {
-        LogUtil.i("initKey : $deviceList")
-        deviceList.filter { it.type == 0x00.toByte() }.forEach { deviceBean ->
-            controlKeyLight(deviceBean.addr.toInt() - 1, 2, 2)
-            deviceBean.keyList.forEach { key ->
-                if (key.hasKey) {
-                    LogUtil.i("initKey : ${deviceBean.addr.toInt() - 1} : ${key.isLeft}")
-                    readKeyRfid(deviceBean.addr.toInt() - 1, key.isLeft) { isLeft, res ->
+        LogUtil.i("initKey : $dockList")
+        dockList.filter { it.type == 0x00.toByte() }.forEach { dockBean ->
+            controlKeyLight(dockBean.addr.toInt() - 1, 2, 2)
+            dockBean.getKeyList().forEach { key ->
+                if (key.isExist) {
+                    LogUtil.i("initKey : ${dockBean.addr.toInt() - 1} : ${key.isLeft}")
+                    readKeyRfid(dockBean.addr.toInt() - 1, key.isLeft) { isLeft, res ->
                         val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                         LogUtil.i("初始化钥匙 RFID : $rfid")
-                        updateKeyRfid(deviceBean.addr.toInt(), isLeft, rfid)
+                        updateKeyRfid(dockBean.addr.toInt(), isLeft, rfid)
                     }
                 } else {
                     // TODO 关闭钥匙卡扣
@@ -173,14 +174,14 @@ object ModBusController {
     /**
      * 更新状态
      */
-    fun updateStatus(byteArray: ByteArray): DeviceBean? {
+    fun updateStatus(byteArray: ByteArray): DockBean? {
         if (byteArray.isEmpty()) {
             return null
         }
-        val deviceBean = deviceList.find { it.addr == byteArray[0] }
-        return deviceBean?.parseStatus(byteArray) ?: let {
-            val temp = DeviceBean(byteArray[0], null, mutableListOf(), mutableListOf())
-            deviceList.add(temp)
+        val dockB = dockList.find { it.addr == byteArray[0] }
+        return dockB?.parseStatus(byteArray) ?: let {
+            val temp = DockBean(byteArray[0], null, mutableListOf())
+            dockList.add(temp)
             temp.parseStatus(byteArray)
         }
     }
@@ -198,7 +199,7 @@ object ModBusController {
      * 更新设备类型
      */
     fun updateDeviceType(idx: Byte?, type: Byte?) {
-        deviceList.find { it.addr == idx }?.type = type
+        dockList.find { it.addr == idx }?.type = type
     }
 
     /**
@@ -276,21 +277,21 @@ object ModBusController {
      * 更新钥匙RFID
      */
     fun updateKeyRfid(slaveIdx: Int, isLeft: Boolean, rfid: String) {
-        deviceList.find { it.addr.toInt() == slaveIdx }?.keyList?.find { it.isLeft == isLeft }?.rfid = rfid
+        dockList.find { it.addr.toInt() == slaveIdx }?.getKeyList()?.find { it.isLeft == isLeft }?.rfid = rfid
     }
 
     /**
      * 通过RFID更新对应的Mac
      */
     fun updateKeyMacByRfid(rfid: String, mac: String) {
-        deviceList.find { it.type == 0x00.toByte() }?.keyList?.find { it.rfid == rfid }?.mac = mac
+        dockList.find { it.type == 0x00.toByte() }?.getKeyList()?.find { it.rfid == rfid }?.mac = mac
     }
 
     /**
      * 更新锁具RFID
      */
     fun updateLockRfid(slaveIdx: Int, lockIdx: Int, rfid: String) {
-        deviceList.find { it.addr.toInt() == slaveIdx }?.lockList?.find { it.idx == lockIdx }?.rfid = rfid
+        dockList.find { it.addr.toInt() == slaveIdx }?.getLockList()?.find { it.idx == lockIdx }?.rfid = rfid
     }
 
     /**
@@ -327,14 +328,14 @@ object ModBusController {
     /**
      * 根据RFID找钥匙
      */
-    fun getKeyByRfid(rfid: String): DeviceBean.KeyBean? {
-        return deviceList.find { it.type == 0x00.toByte() }?.keyList?.find { it.rfid == rfid }
+    fun getKeyByRfid(rfid: String): DockBean.KeyBean? {
+        return dockList.find { it.type == 0x00.toByte() }?.getKeyList()?.find { it.rfid == rfid }
     }
 
     /**
      * 根据RFID找锁具
      */
-    fun getLockByRfid(rfid: String): DeviceBean.LockBean? {
-        return deviceList.find { it.type == 0x01.toByte() }?.lockList?.find { it.rfid == rfid }
+    fun getLockByRfid(rfid: String): DockBean.LockBean? {
+        return dockList.find { it.type == 0x01.toByte() }?.getLockList()?.find { it.rfid == rfid }
     }
 }