瀏覽代碼

各manager结构调整

Frankensteinly 1 年之前
父節點
當前提交
544833bc41

+ 61 - 3
app/src/main/java/com/grkj/iscs/BusinessManager.kt

@@ -7,11 +7,13 @@ import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_CARD
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_FINGERPRINT
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DEVICE_TYPE_LOCK
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
 import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
 import com.grkj.iscs.util.ToastUtils
 import com.grkj.iscs.util.log.LogUtil
+import java.util.stream.Collectors
 
 /**
  * 业务层管理
@@ -33,8 +35,11 @@ object BusinessManager {
         ModBusController.stop()
     }
 
-    fun registerStatusListener() {
-        ModBusController.registerStatusListener(this) { res ->
+    /**
+     * @param key 可null,null时用applicationContext做全局监听
+     */
+    fun registerStatusListener(key: Any?) {
+        ModBusController.registerStatusListener(key ?: MyApplication.instance!!.applicationContext) { res ->
             LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
             res.forEach { bytes ->
                 val dockBean = ModBusController.updateStatus(bytes) ?: return@forEach
@@ -43,7 +48,7 @@ object BusinessManager {
                         dockBean.getKeyList().forEach { keyBean ->
                             if (keyBean.isExist) {
                                 // 放回钥匙,读取rfid
-                                ModBusController.readKeyRfid(dockBean.addr.toInt() - 1, keyBean.isLeft) { isLeft, res ->
+                                ModBusController.readKeyRfid(dockBean.addr.toInt() - 1, if (keyBean.isLeft) 0 else 1) { isLeft, res ->
                                     val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                                     ModBusController.updateKeyRfid(dockBean.addr.toInt(), keyBean.isLeft, rfid)
                                     // TODO 从HTTP读取Mac
@@ -95,4 +100,57 @@ object BusinessManager {
             }
         }
     }
+
+    fun readLockBuckleStatus() {
+        // TODO slaveIdx暂时写死,调试用
+        ModBusController.readBuckleStatus(true, 0) { type, res ->
+            LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
+            when (type) {
+                0 -> {
+                    val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
+                    val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
+                    println("锁具底座卡扣状态 : $isLeftLock - $isRightLock")
+                }
+                1 -> {
+                    val tempList = mutableListOf<Boolean>()
+                    for (i in 0..7) {
+                        tempList.add((res[4].toInt() shr i) and 0x1 == 1)
+                    }
+                    println("锁具底座卡扣1-8状态 : $tempList")
+                }
+                2 -> {
+                    val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
+                    val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
+                    println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
+                }
+            }
+        }
+    }
+
+    fun readKeyBuckleStatus() {
+        // TODO slaveIdx暂时写死,调试用
+        ModBusController.readBuckleStatus(false, 1) { type, res ->
+            LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
+            // TODO 待验证
+            when (type) {
+                0 -> {
+                    val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
+                    val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
+                    println("钥匙底座卡扣状态 : $isLeftLock - $isRightLock")
+                }
+                1 -> {
+                    val tempList = mutableListOf<Boolean>()
+                    for (i in 0..7) {
+                        tempList.add((res[4].toInt() shr i) and 0x1 == 1)
+                    }
+                    println("锁具底座卡扣1-8状态 : $tempList")
+                }
+                2 -> {
+                    val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
+                    val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
+                    println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
+                }
+            }
+        }
+    }
 }

+ 14 - 149
app/src/main/java/com/grkj/iscs/activity/ModbusActivity.kt

@@ -1,5 +1,6 @@
 package com.grkj.iscs.activity
 
+import com.grkj.iscs.BusinessManager
 import com.grkj.iscs.base.BaseMvpActivity
 import com.grkj.iscs.databinding.ActivityModbusBinding
 import com.grkj.iscs.extentions.removeLeadingZeros
@@ -25,151 +26,24 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         get() = ActivityModbusBinding.inflate(layoutInflater)
 
     override fun initView() {
+        BusinessManager.connectDock(1)
 
-        presenter?.initModbus()
-
-//        Executor.delayOnMain({
-//            ModBusController.initDevicesStatus()
-//        }, 3000)
-
-        ModBusController.registerStatusListener(this) { res ->
-            LogUtil.i("设备状态:${(res as List<ByteArray>).map { it.toHexStrings() }}")
-            res.forEach { bytes ->
-                val dockBean = ModBusController.updateStatus(bytes) ?: return@forEach
-                when (dockBean.type) {
-                    DOCK_TYPE_KEY -> {
-                        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 蓝牙通信
-                                }
-                            }
-                        }
-                    }
-                    DOCK_TYPE_LOCK -> {
-                        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)
-                                }
-                            }
-                        }
-                    }
-                    DOCK_TYPE_PORTABLE -> {
-                        // TODO 便携式待完善
-                        dockBean.deviceList.forEach { deviceBean ->
-                            if (deviceBean.isExist) {
-                                when (deviceBean.type) {
-                                    DEVICE_TYPE_KEY -> {
-
-                                    }
-                                    DEVICE_TYPE_LOCK -> {
-                                        ModBusController.readLockRfid(dockBean.addr.toInt() - 1, deviceBean.idx) { res ->
-                                            val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                            ModBusController.updateLockRfid(dockBean.addr.toInt(), deviceBean.idx, rfid)
-                                        }
-                                    }
-                                    DEVICE_TYPE_CARD -> {
-                                        ModBusController.readPortalCaseCardRfid(dockBean.addr.toInt() - 1) { res ->
-                                            val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
-                                            println("卡片RFID : $rfid")
-                                            ToastUtils.tip("卡片RFID : $rfid")
-                                        }
-                                    }
-                                    DEVICE_TYPE_FINGERPRINT -> {
-
-                                    }
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-        }
+        BusinessManager.registerStatusListener(this)
 
         mBinding?.init?.setOnClickListener {
             ModBusController.initDevicesStatus()
         }
 
         mBinding?.keyBuckleStatus?.setOnClickListener {
-            // TODO slaveIdx暂时写死,调试用
-            ModBusController.readBuckleStatus(false, 1) { type, res ->
-                LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
-                // TODO 待验证
-                when (type) {
-                    0 -> {
-                        val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
-                        val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
-                        println("钥匙底座卡扣状态 : $isLeftLock - $isRightLock")
-                    }
-                    1 -> {
-                        val tempList = mutableListOf<Boolean>()
-                        for (i in 0..7) {
-                            tempList.add((res[4].toInt() shr i) and 0x1 == 1)
-                        }
-                        println("锁具底座卡扣1-8状态 : $tempList")
-                    }
-                    2 -> {
-                        val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
-                        val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
-                        println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
-                    }
-                }
-            }
+            BusinessManager.readKeyBuckleStatus()
         }
 
         mBinding?.lockBuckleStatus?.setOnClickListener {
-            // TODO slaveIdx暂时写死,调试用
-            ModBusController.readBuckleStatus(true, 0) { type, res ->
-                LogUtil.i("单slave卡扣状态 : $type - ${res.toHexStrings()}")
-                when (type) {
-                    0 -> {
-                        val isLeftLock = (res[4].toInt() shr 0) and 0x1 == 1
-                        val isRightLock = (res[4].toInt() shr 4) and 0x1 == 1
-                        println("锁具底座卡扣状态 : $isLeftLock - $isRightLock")
-                    }
-                    1 -> {
-                        val tempList = mutableListOf<Boolean>()
-                        for (i in 0..7) {
-                            tempList.add((res[4].toInt() shr i) and 0x1 == 1)
-                        }
-                        println("锁具底座卡扣1-8状态 : $tempList")
-                    }
-                    2 -> {
-                        val lock9Status = (res[4].toInt() shr 0) and 0x1 == 1
-                        val lock10Status = (res[4].toInt() shr 1) and 0x1 == 1
-                        println("锁具底座卡扣9、10状态 : $lock9Status - $lock10Status")
-                    }
-                }
-            }
+            BusinessManager.readLockBuckleStatus()
         }
 
         mBinding?.deviceType?.setOnClickListener {
-            println("____________________________________")
-            ModBusController.readDeviceType { res ->
-                LogUtil.i("设备类型数量 : ${res.size}")
-                LogUtil.i("设备类型 : ${res.map { it.toHexStrings()}}")
-                res.forEach { bytes ->
-                    if (bytes.size < 5) return@forEach
-                    // 设备具体数据由0x0011寄存器提供
-                    ModBusController.updateDeviceType(bytes[0], bytes[4])
-                    val type = when (bytes[4]) {
-                        DOCK_TYPE_KEY -> "钥匙底座"
-                        DOCK_TYPE_LOCK -> "锁具底座"
-                        DOCK_TYPE_ELEC_LOCK_BOARD -> "电磁锁控制板"
-                        DOCK_TYPE_PORTABLE -> "便携式底座"
-                        else -> "未知"
-                    }
-                    LogUtil.i("设备(${bytes[0].toInt()})类型:$type")
-                }
-                println("____________________________________")
-            }
-
+            ModBusController.updateDeviceType()
         }
 
         mBinding?.openLockBuckle?.setOnClickListener {
@@ -218,7 +92,7 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
 
         mBinding?.keyRfid?.setOnClickListener {
             // TODO 暂时写死,调试用
-            ModBusController.readKeyRfid(1, true) { left, res ->
+            ModBusController.readKeyRfid(1, 1) { left, res ->
                 val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                 println("钥匙RFID : $rfid")
                 ModBusController.updateKeyRfid(1, left, rfid)
@@ -245,7 +119,7 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         mBinding?.openKeyBuckle?.setOnClickListener {
             println("____________________________________")
             // TODO 暂时写死,调试用
-            ModBusController.controlKeyBuckle(isOpen = true, false, 1) {
+            ModBusController.controlKeyBuckle(isOpen = true, true, 0) {
                 LogUtil.i("开锁钥匙卡扣 : ${it.toHexStrings()}")
                 println("____________________________________")
             }
@@ -254,32 +128,22 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
         mBinding?.closeKeyBuckle?.setOnClickListener {
             println("____________________________________")
             // TODO 暂时写死,调试用
-            ModBusController.controlKeyBuckle(isOpen = false, false, 1) {
+            ModBusController.controlKeyBuckle(isOpen = false, true, 0) {
                 LogUtil.i("关锁钥匙卡扣 : ${it.toHexStrings()}")
                 println("____________________________________")
             }
         }
 
         mBinding?.closeAllLockBuckles?.setOnClickListener {
-            ModBusController.dockList.filter { it.type == DOCK_TYPE_LOCK }.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()}")
-                }
-            }
+            ModBusController.closeAllLockBuckles()
         }
 
         mBinding?.openAllLockBuckles?.setOnClickListener {
-            ModBusController.dockList.filter { it.type == DOCK_TYPE_LOCK }.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()}")
-                }
-            }
+            ModBusController.openAllLockBuckles()
         }
 
         mBinding?.deviceList?.setOnClickListener {
-            println("当前底座列表 : ${ModBusController.dockList}")
+            ModBusController.printDockInfo()
         }
 
         mBinding?.exit?.setOnClickListener { finish() }
@@ -291,6 +155,7 @@ class ModbusActivity : BaseMvpActivity<IModbusView, ModBusPresenter, ActivityMod
 
     override fun onDestroy() {
         super.onDestroy()
-        ModBusController.stop()
+        ModBusController.unregisterListener(this)
+        BusinessManager.disconnectDock()
     }
 }

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

@@ -121,7 +121,7 @@ class DockBean(
                     val isKeyCharging = (byteArray[3].toInt() shr 1) and 0x1 == 1
 
                     if (getKeyList().isEmpty()) {
-                        deviceList.add(KeyBean(0, isKeyExist, true, isKeyCharging, null, null))
+                        deviceList.add(KeyBean(4, isKeyExist, true, isKeyCharging, null, null))
                     }
 
                     val isCardExist = (byteArray[3].toInt() shr 4) and 0x1 == 1

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

@@ -10,6 +10,7 @@ import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
 import com.grkj.iscs.util.Executor
 import com.grkj.iscs.util.log.LogUtil
 import java.util.concurrent.Executors
+import java.util.stream.Collectors
 
 
 /**
@@ -137,8 +138,8 @@ object ModBusController {
      */
     private fun initLock() {
         LogUtil.i("initLock : $dockList")
-        dockList.filter { it.type == DOCK_TYPE_LOCK }.forEach { dockBean ->
-            val hasLockIdxList = dockBean.getLockList().filter { it.isExist }.map { it.idx }
+        dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
+            val hasLockIdxList = dockBean.getLockList().filter { it.isExist }.map { it.idx } as MutableList<Int>
             val noLockIdxList = dockBean.getLockList().filter { !it.isExist }.map { it.idx } as MutableList<Int>
 
             hasLockIdxList.forEach { idx ->
@@ -148,7 +149,7 @@ object ModBusController {
                     updateLockRfid(dockBean.addr.toInt(), idx, rfid)
                 }
             }
-
+            controlLockBuckle(false, dockBean.addr.toInt() - 1, hasLockIdxList)
             controlLockBuckle(true, dockBean.addr.toInt() - 1, noLockIdxList)
         }
     }
@@ -158,18 +159,20 @@ object ModBusController {
      */
     private fun initKey() {
         LogUtil.i("initKey : $dockList")
-        dockList.filter { it.type == DOCK_TYPE_KEY }.forEach { dockBean ->
+        dockList.filter { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_PORTABLE }.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 ->
+                    readKeyRfid(dockBean.addr.toInt() - 1, if (key.isLeft) 0 else 1) { isLeft, res ->
                         val rfid = res.copyOfRange(3, 11).toHexStrings(false).removeLeadingZeros()
                         LogUtil.i("初始化钥匙 RFID : $rfid")
                         updateKeyRfid(dockBean.addr.toInt(), isLeft, rfid)
                     }
+                    controlKeyBuckle(false, key.isLeft, dockBean.addr.toInt() - 1)
                 } else {
-                    // TODO 关闭钥匙卡扣
+                    // TODO 开钥匙卡扣
+                    controlKeyBuckle(true, key.isLeft, dockBean.addr.toInt() - 1)
                 }
             }
         }
@@ -254,11 +257,11 @@ object ModBusController {
     /**
      * 读取钥匙RFID
      */
-    fun readKeyRfid(slaveIdx: Int?, isLeft: Boolean, done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null) {
+    fun readKeyRfid(slaveIdx: Int?, idx: Int, done: ((isLeft: Boolean, res: ByteArray) -> Unit)? = null) {
         slaveIdx?.let {
-            modBusManager?.generateRfidCmd(if (isLeft) 0 else 1)?.let { cmd ->
+            modBusManager?.generateRfidCmd(idx)?.let { cmd ->
                 modBusManager?.sendTo(it, cmd) {
-                    done?.invoke(isLeft, it)
+                    done?.invoke(idx == 0, it)
                 }
             }
         }
@@ -355,4 +358,70 @@ object ModBusController {
     fun getLockByRfid(rfid: String): DockBean.LockBean? {
         return dockList.find { it.type == DOCK_TYPE_LOCK }?.getLockList()?.find { it.rfid == rfid }
     }
+
+    fun openAllLockBuckles() {
+        dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
+            val list = dockBean.getLockList().stream().map { it.idx }.collect(Collectors.toList())
+            controlLockBuckle(true, dockBean.addr.toInt() - 1, list) {
+                LogUtil.i("开所有锁卡扣 : ${it.toHexStrings()}")
+            }
+        }
+    }
+
+    fun closeAllLockBuckles() {
+        dockList.filter { it.type == DOCK_TYPE_LOCK || it.type == DOCK_TYPE_PORTABLE }.forEach { dockBean ->
+            val list = dockBean.getLockList().stream().map { it.idx }.collect(Collectors.toList())
+            controlLockBuckle(false, dockBean.addr.toInt() - 1, list) {
+                LogUtil.i("关所有锁卡扣 : ${it.toHexStrings()}")
+            }
+        }
+    }
+
+    fun printDockInfo() {
+        println("当前底座列表 : $dockList")
+        dockList.forEach { dockBean ->
+            when (dockBean.type) {
+                DOCK_TYPE_LOCK -> {
+                    dockBean.getLockList().forEach { lockBean ->
+                        println("${dockBean.addr}锁${lockBean.idx} : ${lockBean.rfid}")
+                    }
+                }
+                DOCK_TYPE_KEY -> {
+                    dockBean.getKeyList().forEach { keyBean ->
+                        println("${dockBean.addr}钥${keyBean.idx} : ${keyBean.rfid}")
+                    }
+                }
+                DOCK_TYPE_PORTABLE -> {
+                    dockBean.getLockList().forEach { lockBean ->
+                        println("${dockBean.addr}柜锁${lockBean.idx} : ${lockBean.rfid}")
+                    }
+                    dockBean.getKeyList().forEach { keyBean ->
+                        println("${dockBean.addr}柜钥${keyBean.idx} : ${keyBean.rfid}")
+                    }
+                }
+            }
+        }
+    }
+
+    fun updateDeviceType() {
+        println("____________________________________")
+        readDeviceType { res ->
+            LogUtil.i("设备类型数量 : ${res.size}")
+            LogUtil.i("设备类型 : ${res.map { it.toHexStrings()}}")
+            res.forEach { bytes ->
+                if (bytes.size < 5) return@forEach
+                // 设备具体数据由0x0011寄存器提供
+                updateDeviceType(bytes[0], bytes[4])
+                val type = when (bytes[4]) {
+                    DOCK_TYPE_KEY -> "钥匙底座"
+                    DOCK_TYPE_LOCK -> "锁具底座"
+                    DOCK_TYPE_ELEC_LOCK_BOARD -> "电磁锁控制板"
+                    DOCK_TYPE_PORTABLE -> "便携式底座"
+                    else -> "未知"
+                }
+                LogUtil.i("设备(${bytes[0].toInt()})类型:$type")
+            }
+            println("____________________________________")
+        }
+    }
 }

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

@@ -328,7 +328,7 @@ class ModBusManager(
      * 操作钥匙/便携式底座钥匙卡扣,一次只操作一个卡扣
      *
      * @param isOpen true:开操作 false:关操作
-     * @param index 0:左 1:右 便携式底座钥匙传1
+     * @param index 0:左 1:右 便携式底座钥匙传0
      */
     fun generateKeyBuckleCmd(isOpen: Boolean, index: Int): MBFrame {
         return MBFrame(

+ 1 - 1
app/src/main/java/com/grkj/iscs/presentation/PresentationActivity.kt

@@ -127,7 +127,7 @@ class PresentationActivity :
 
     override fun onDestroy() {
         super.onDestroy()
-        ModBusController.stop()
+        BusinessManager.disconnectDock()
     }
 
     data class LockerTogetherBean(var name: String? = null, var isChecked: Boolean = false)