Эх сурвалжийг харах

修正底座状态变化监听

Frankensteinly 1 жил өмнө
parent
commit
cc063e3a62

+ 3 - 2
app/src/main/java/com/grkj/iscs/activity/ModbusActivity.kt

@@ -22,7 +22,8 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         ModBusController.registerStatusListener(this) { res ->
         ModBusController.registerStatusListener(this) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             res.forEach { bytes ->
             res.forEach { bytes ->
-                ModBusController.checkStatus(bytes)
+                val rst = ModBusController.checkStatus(bytes)
+                println("设备状态变化 : $rst")
             }
             }
         }
         }
 
 
@@ -48,7 +49,7 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
                 res.forEach { bytes ->
                 res.forEach { bytes ->
                     if (bytes.size < 5) return@forEach
                     if (bytes.size < 5) return@forEach
                     // TODO status由0x0011寄存器提供
                     // TODO status由0x0011寄存器提供
-                    ModBusController.deviceList.add(DeviceBean(bytes[0], bytes[4], null))
+                    ModBusController.deviceList.add(DeviceBean(bytes[0], bytes[4], mutableListOf(), mutableListOf()))
                     val type = when (bytes[4]) {
                     val type = when (bytes[4]) {
                         0x00.toByte() -> "钥匙底座"
                         0x00.toByte() -> "钥匙底座"
                         0x01.toByte() -> "锁具底座"
                         0x01.toByte() -> "锁具底座"

+ 71 - 21
app/src/main/java/com/grkj/iscs/modbus/DeviceBean.kt

@@ -1,22 +1,28 @@
 package com.grkj.iscs.modbus
 package com.grkj.iscs.modbus
 
 
 import com.grkj.iscs.util.log.LogUtil
 import com.grkj.iscs.util.log.LogUtil
-import kotlin.experimental.and
 
 
 /**
 /**
  * RS-485 设备信息 Bean
  * RS-485 设备信息 Bean
  *
  *
  * @param idx 设备序号(地址)
  * @param idx 设备序号(地址)
  * @param type 0x00:钥匙底座 0x01:锁具底座  0x02:电磁锁控制板
  * @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()) {
         if (byteArray.isEmpty()) {
             return false
             return false
         }
         }
         type?.let {
         type?.let {
+            // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
             when (it) {
             when (it) {
                 0x00.toByte() -> {
                 0x00.toByte() -> {
                     // TODO 未验证
                     // 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 rightHasKey = (byteArray[4].toInt() shr 0) and 0x1 == 1
                     val isRightCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
                     val isRightCharging = (byteArray[4].toInt() shr 1) and 0x1 == 1
                     LogUtil.i("钥匙刷新状态 : $leftHasKey - $isLeftCharging - $rightHasKey - $isRightCharging")
                     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() -> {
                 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() -> {
                 0x02.toByte() -> {
                     // TODO 临时占位
                     // TODO 临时占位
-                    return false
+                    return null
                 }
                 }
-                else -> return false
+                else -> return null
             }
             }
-        } ?: return false
+        } ?: return null
     }
     }
 
 
     fun getBit(by: Byte): String {
     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)
             .append((by.toInt() shr 0) and 0x1)
         return sb.toString()
         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?)
 }
 }

+ 1 - 2
app/src/main/java/com/grkj/iscs/modbus/ModBusController.kt

@@ -3,7 +3,6 @@ package com.grkj.iscs.modbus
 import android.content.Context
 import android.content.Context
 import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.log.LogUtil
 import com.grkj.iscs.util.log.LogUtil
-import java.util.*
 import java.util.concurrent.Executors
 import java.util.concurrent.Executors
 
 
 
 
@@ -180,7 +179,7 @@ object ModBusController {
 
 
     /*****************************************************************************************/
     /*****************************************************************************************/
 
 
-    fun checkStatus(byteArray: ByteArray): Boolean? {
+    fun checkStatus(byteArray: ByteArray): Any? {
         if (byteArray.isEmpty()) {
         if (byteArray.isEmpty()) {
             return null
             return null
         }
         }