Forráskód Böngészése

完善设备信息变化处理;调整设备信息变化返回结构

Frankensteinly 1 éve
szülő
commit
5582f98b8c

+ 26 - 8
app/src/main/java/com/grkj/iscs/activity/ModbusActivity.kt

@@ -29,8 +29,29 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         ModBusController.registerStatusListener(this) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             res.forEach { bytes ->
-                val rst = ModBusController.updateStatus(bytes)
-                println("设备状态变化 : $rst")
+                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 蓝牙通信
+                            }
+                        }
+                    }
+                } 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)
+                            }
+                        }
+                    }
+                }
             }
         }
 
@@ -96,12 +117,9 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
             ModBusController.readDeviceType { res ->
                 LogUtil.i("设备类型数量 : ${res.size}")
                 LogUtil.i("设备类型 : ${res.map { it.toHexStrings()}}")
-//                // TODO 暂时先清空
-//                ModBusController.deviceList.clear()
                 res.forEach { bytes ->
                     if (bytes.size < 5) return@forEach
-                    // TODO 设备具体数据由0x0011寄存器提供
-//                    ModBusController.deviceList.add(DeviceBean(bytes[0], bytes[4], mutableListOf(), mutableListOf()))
+                    // 设备具体数据由0x0011寄存器提供
                     ModBusController.updateDeviceType(bytes[0], bytes[4])
                     val type = when (bytes[4]) {
                         0x00.toByte() -> "钥匙底座"
@@ -207,7 +225,7 @@ 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.controlLockBuckle(false, deviceBean.addr.toInt() - 1, list) {
                     LogUtil.i("关所有锁卡扣 : ${it.toHexStrings()}")
                 }
             }
@@ -216,7 +234,7 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         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.controlLockBuckle(true, deviceBean.addr.toInt() - 1, list) {
                     LogUtil.i("开所有锁卡扣 : ${it.toHexStrings()}")
                 }
             }

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

@@ -11,15 +11,15 @@ import com.grkj.iscs.util.log.LogUtil
  * @param lockList 锁具列表,锁具底座才有
  */
 class DeviceBean(
-    var addr: Byte?,
+    var addr: Byte,
     var type: Byte?,
     var keyList: MutableList<KeyBean>,
     var lockList: MutableList<LockBean>
 ) {
 
-    fun parseStatus(byteArray: ByteArray): Any? {
+    fun parseStatus(byteArray: ByteArray): DeviceBean? {
         if (byteArray.isEmpty()) {
-            return false
+            return null
         }
         type?.let {
             // 因为都是一个寄存器返回的,所以一定能得到2个钥匙的状态或者10把锁具的状态
@@ -64,7 +64,7 @@ class DeviceBean(
                         }
                     }
 
-                    return changeList
+                    return DeviceBean(addr, type, changeList, mutableListOf())
                 }
                 0x01.toByte() -> {
                     val tempList = mutableListOf<Boolean>()
@@ -93,7 +93,7 @@ class DeviceBean(
                     }
 
                     LogUtil.i("锁具刷新状态 : $changeList")
-                    return changeList
+                    return DeviceBean(addr, type, mutableListOf(), changeList)
                 }
                 0x02.toByte() -> {
                     // TODO 临时占位

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

@@ -137,14 +137,14 @@ object ModBusController {
             val noLockIdxList = deviceBean.lockList.filter { !it.hasLock }.map { it.idx } as MutableList<Int>
 
             hasLockIdxList.forEach { idx ->
-                readLockRfid(deviceBean.addr!!.toInt() - 1, idx) { res ->
+                readLockRfid(deviceBean.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(deviceBean.addr.toInt(), idx, rfid)
                 }
             }
 
-            controlLockBuckle(true, deviceBean.addr!!.toInt() - 1, noLockIdxList)
+            controlLockBuckle(true, deviceBean.addr.toInt() - 1, noLockIdxList)
         }
     }
 
@@ -154,14 +154,14 @@ object ModBusController {
     private fun initKey() {
         LogUtil.i("initKey : $deviceList")
         deviceList.filter { it.type == 0x00.toByte() }.forEach { deviceBean ->
-            controlKeyLight(deviceBean.addr!!.toInt() - 1, 2, 2)
+            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 : ${deviceBean.addr.toInt() - 1} : ${key.isLeft}")
+                    readKeyRfid(deviceBean.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(deviceBean.addr.toInt(), isLeft, rfid)
                     }
                 } else {
                     // TODO 关闭钥匙卡扣
@@ -173,7 +173,7 @@ object ModBusController {
     /**
      * 更新状态
      */
-    fun updateStatus(byteArray: ByteArray): Any? {
+    fun updateStatus(byteArray: ByteArray): DeviceBean? {
         if (byteArray.isEmpty()) {
             return null
         }

+ 13 - 8
app/src/main/res/layout/activity_modbus.xml

@@ -35,42 +35,47 @@
             android:layout_margin="5dp"/>
 
         <Button
-            android:id="@+id/keyBuckleStatus"
+            android:id="@+id/deviceType"
             android:layout_width="100dp"
             android:layout_height="40dp"
             android:minWidth="0dp"
             android:minHeight="0dp"
-            android:text="Key Buckle Status"
+            android:text="Device Type"
             android:textSize="8sp"
             android:layout_margin="5dp"/>
+    </LinearLayout>
 
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="horizontal">
         <Button
-            android:id="@+id/lockBuckleStatus"
+            android:id="@+id/keyBuckleStatus"
             android:layout_width="100dp"
             android:layout_height="40dp"
             android:minWidth="0dp"
             android:minHeight="0dp"
-            android:text="Lock Buckle Status"
+            android:text="Key Buckle Status"
             android:textSize="8sp"
             android:layout_margin="5dp"/>
 
         <Button
-            android:id="@+id/keyRfid"
+            android:id="@+id/lockBuckleStatus"
             android:layout_width="100dp"
             android:layout_height="40dp"
             android:minWidth="0dp"
             android:minHeight="0dp"
-            android:text="Key Rfid"
+            android:text="Lock Buckle Status"
             android:textSize="8sp"
             android:layout_margin="5dp"/>
 
         <Button
-            android:id="@+id/deviceType"
+            android:id="@+id/keyRfid"
             android:layout_width="100dp"
             android:layout_height="40dp"
             android:minWidth="0dp"
             android:minHeight="0dp"
-            android:text="Device Type"
+            android:text="Key Rfid"
             android:textSize="8sp"
             android:layout_margin="5dp"/>
     </LinearLayout>