Browse Source

补充硬件状态初始化

Frankensteinly 1 year ago
parent
commit
f0c0c63015

+ 10 - 4
app/src/main/java/com/grkj/iscs/activity/ModbusActivity.kt

@@ -8,6 +8,7 @@ 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
 
@@ -20,10 +21,14 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
 
         presenter?.initModbus()
 
+        Executor.delayOnIO({
+            ModBusController.initDevicesStatus()
+        }, 3000)
+
         ModBusController.registerStatusListener(this) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             res.forEach { bytes ->
-                val rst = ModBusController.checkStatus(bytes)
+                val rst = ModBusController.updateStatus(bytes)
                 println("设备状态变化 : $rst")
             }
         }
@@ -86,12 +91,13 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
             ModBusController.readDeviceType { res ->
                 LogUtil.i("设备类型数量 : ${res.size}")
                 LogUtil.i("设备类型 : ${res.map { it.toHexStrings()}}")
-                // TODO 暂时先清空
-                ModBusController.deviceList.clear()
+//                // 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()))
+//                    ModBusController.deviceList.add(DeviceBean(bytes[0], bytes[4], mutableListOf(), mutableListOf()))
+                    ModBusController.updateDeviceType(bytes[0], bytes[4])
                     val type = when (bytes[4]) {
                         0x00.toByte() -> "钥匙底座"
                         0x01.toByte() -> "锁具底座"

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

@@ -103,11 +103,16 @@ class DeviceBean(
         return sb.toString()
     }
 
+    override fun toString(): String {
+        return "DeviceBean(idx=$idx, type=$type, keyList=$keyList, lockList=$lockList)"
+    }
+
 
     /**
      * 钥匙
      */
     data class KeyBean(var isLeft: Boolean, var hasKey: Boolean, var isCharging: Boolean, var rfid: String?, var mac: String?)
+
     /**
      * 锁具
      *
@@ -115,4 +120,6 @@ class DeviceBean(
      * @param rfid 锁具的RFID(仅有关闭锁扣的时候读取并保存,否则为null)
      */
     data class LockBean(var idx: Int, var hasLock: Boolean, var rfid: String?)
+
+
 }

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

@@ -100,23 +100,80 @@ object ModBusController {
     /**
      * 初始化所有设备的状态
      */
+    // TODO 通电后多久执行?App每次重启的执行是什么
     fun initDevicesStatus() {
-        readDeviceType {
+        readDeviceType { res ->
+            res.forEach { bytes ->
+                if (bytes.size < 5) return@forEach
+                // TODO 设备具体数据由0x0011寄存器提供
+                updateDeviceType(bytes[0], bytes[4])
+                val type = when (bytes[4]) {
+                    0x00.toByte() -> "钥匙底座"
+                    0x01.toByte() -> "锁具底座"
+                    0x02.toByte() -> "电磁锁控制板"
+                    else -> "未知"
+                }
+                LogUtil.i("initDevicesStatus 设备(${bytes[0].toInt()})类型:$type")
+            }
+        }
+        Executor.delayOnIO({
             // TODO 待完善
-            // TODO 打开所有无锁的卡扣
-            // TODO 打开所有无钥匙的卡扣
-            // TODO 关闭所有钥匙灯光
-            // TODO 读取所有锁的RFID
-            // TODO 读取所有钥匙的RFID
+            initLock()    // TODO 打开所有无锁的卡扣、读取所有锁的RFID
+            initKey()     // TODO 打开所有无钥匙的卡扣、关闭所有钥匙灯光、读取所有钥匙的RFID
             // TODO 设置所有钥匙的模式
+            // TODO 通过HTTP获取所有钥匙的Mac
+        }, 3000)
+    }
+
+    /**
+     * 初始化锁具——打开所有无锁的卡扣、读取RFID
+     */
+    private fun initLock() {
+        LogUtil.i("initLock : $deviceList")
+        deviceList.filter { it.type == 0x01.toByte() }.forEach { deviceBean ->
+            deviceBean.lockList.forEach { lockBean ->
+                if (lockBean.hasLock) {
+                    LogUtil.i("initLock rfid: ${deviceBean.idx!!.toInt() - 1} : ${lockBean.idx}")
+                    readLockRfid(deviceBean.idx!!.toInt() - 1, lockBean.idx)
+                } else {
+                    LogUtil.i("initLock buckle : ${deviceBean.idx!!.toInt() - 1} : ${lockBean.idx}")
+                    controlLockBuckle(false, deviceBean.idx!!.toInt() - 1, lockBean.idx)
+                }
+            }
         }
     }
 
-    fun checkStatus(byteArray: ByteArray): Any? {
+    /**
+     * 初始化钥匙——关闭所有钥匙灯光
+     */
+    private fun initKey() {
+        LogUtil.i("initKey : $deviceList")
+        deviceList.filter { it.type == 0x00.toByte() }.forEach { deviceBean ->
+            controlKeyLight(deviceBean.idx!!.toInt() - 1, 2, 2)
+            deviceBean.keyList.forEach { key ->
+                if (key.hasKey) {
+                    LogUtil.i("initKey : ${deviceBean.idx!!.toInt() - 1} : ${key.isLeft}")
+                    readKeyRfid(deviceBean.idx!!.toInt() - 1, key.isLeft)
+                } else {
+                    // TODO 关闭钥匙卡扣
+                }
+            }
+        }
+    }
+
+    /**
+     * 更新状态
+     */
+    fun updateStatus(byteArray: ByteArray): Any? {
         if (byteArray.isEmpty()) {
             return null
         }
-        return deviceList.find { it.idx == byteArray[0] }?.parseStatus(byteArray)
+        val deviceBean = deviceList.find { it.idx == byteArray[0] }
+        return deviceBean?.parseStatus(byteArray) ?: let {
+            val temp = DeviceBean(byteArray[0], null, mutableListOf(), mutableListOf())
+            deviceList.add(temp)
+            temp.parseStatus(byteArray)
+        }
     }
 
     /**
@@ -128,6 +185,13 @@ object ModBusController {
         }
     }
 
+    /**
+     * 更新设备类型
+     */
+    fun updateDeviceType(idx: Byte?, type: Byte?) {
+        deviceList.find { it.idx == idx }?.type = type
+    }
+
     /**
      * 读取卡扣状态
      *
@@ -161,6 +225,9 @@ object ModBusController {
         }
     }
 
+    /**
+     * 读取钥匙RFID
+     */
     fun readKeyRfid(slaveIdx: Int?, isLeft: Boolean, done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null) {
         slaveIdx?.let {
             modBusManager?.generateKeyRfidCmd(isLeft)?.let { cmd ->
@@ -171,6 +238,9 @@ object ModBusController {
         }
     }
 
+    /**
+     * 读取锁具RFID
+     */
     fun readLockRfid(slaveIdx: Int?, lockIdx: Int, done: ((res: ByteArray) -> Unit)? = null) {
         slaveIdx?.let {
             modBusManager?.generateLockRfidCmd(lockIdx)?.let { cmd ->

+ 3 - 0
app/src/main/java/com/grkj/iscs/modbus/ModBusManager.kt

@@ -277,6 +277,9 @@ class ModBusManager(
         )
     }
 
+    /**
+     * 生成读钥匙RFID指令
+     */
     fun generateKeyRfidCmd(isLeft: Boolean): MBFrame {
         return MBFrame(
             FRAME_TYPE_READ,

+ 1 - 1
app/src/main/java/com/grkj/iscs/research/WorkTicketBean.kt → app/src/main/java/com/grkj/iscs/model/WorkTicketBean.kt

@@ -1,4 +1,4 @@
-package com.grkj.iscs.research
+package com.grkj.iscs.model
 
 class WorkTicketBean {
     /**