Prechádzať zdrojové kódy

1. 去除控制还需要手动调用下发的问题,优化直接可控

bjb 3 mesiacov pred
rodič
commit
b6a14c0fc1

+ 10 - 10
app/src/main/java/com/iscs/comm/CtrlActivity.kt

@@ -96,9 +96,9 @@ class CtrlActivity : ComponentActivity() {
         val status = dev.deviceStatus as DeviceStatusKeySlot
         Column {
             Row(modifier = Modifier.padding(horizontal = 10.dp)) {
-                Button({ dev.ctrlSlotLockAndCharge(-1, false, false).writeByFrame() }) { Text("全开") }
+                Button({ dev.ctrlSlotLockAndCharge(-1, false, false) }) { Text("全开") }
                 Spacer(modifier = Modifier.width(10.dp))
-                Button({ dev.ctrlSlotLockAndCharge(-1, true, true).writeByFrame() }) { Text("全关") }
+                Button({ dev.ctrlSlotLockAndCharge(-1, true, true) }) { Text("全关") }
             }
             LazyVerticalGrid(
                 columns = GridCells.Adaptive(minSize = 180.dp),
@@ -122,7 +122,7 @@ class CtrlActivity : ComponentActivity() {
                                     if (it.isChanged && rfid.value.isNotEmpty() && rfid.value != lastRFID) {
                                         lastRFID = rfid.value
                                         // 硬件逻辑已经存在
-                                        dev.ctrlSlotLockAndCharge(state.ch, true, true).writeByFrame()
+                                        dev.ctrlSlotLockAndCharge(state.ch, true, true)
                                     } else if (rfid.value.isEmpty()) {
                                         lastRFID = ""
                                     }
@@ -157,23 +157,23 @@ class CtrlActivity : ComponentActivity() {
                             Spacer(modifier = Modifier.weight(1f))
                             Row {
                                 Button(
-                                    { dev.ctrlSlotLockAndCharge(state.ch, false, false).writeByFrame() },
+                                    { dev.ctrlSlotLockAndCharge(state.ch, false, false) },
                                     contentPadding = PaddingValues(horizontal = 6.dp, vertical = 0.dp)
                                 ) { Text("打开锁") }
                                 Spacer(modifier = Modifier.weight(1f))
                                 Button(
-                                    { dev.ctrlSlotLockAndCharge(state.ch, true, true).writeByFrame() },
+                                    { dev.ctrlSlotLockAndCharge(state.ch, true, true) },
                                     contentPadding = PaddingValues(horizontal = 6.dp, vertical = 0.dp)
                                 ) { Text("关闭锁") }
                             }
                             Row {
                                 Button(
-                                    { dev.ctrlSlotCharge(state.ch, true).writeByFrame() },
+                                    { dev.ctrlSlotCharge(state.ch, true) },
                                     contentPadding = PaddingValues(horizontal = 6.dp, vertical = 0.dp)
                                 ) { Text("开充电") }
                                 Spacer(modifier = Modifier.weight(1f))
                                 Button(
-                                    { dev.ctrlSlotCharge(state.ch, false).writeByFrame() },
+                                    { dev.ctrlSlotCharge(state.ch, false) },
                                     contentPadding = PaddingValues(horizontal = 6.dp, vertical = 0.dp)
                                 ) { Text("关充电") }
                             }
@@ -193,9 +193,9 @@ class CtrlActivity : ComponentActivity() {
         val status = ArrayList((dev.deviceStatus as DeviceStatusLockSlot).slotList)
         Column {
             Row(modifier = Modifier.padding(horizontal = 10.dp)) {
-                Button({ dev.ctrlSlotLock(-1, false).writeByFrame() }) { Text("全开") }
+                Button({ dev.ctrlSlotLock(-1, false) }) { Text("全开") }
                 Spacer(modifier = Modifier.width(10.dp))
-                Button({ dev.ctrlSlotLock(-1, true).writeByFrame() }) { Text("全关") }
+                Button({ dev.ctrlSlotLock(-1, true) }) { Text("全关") }
             }
             LazyVerticalGrid(
                 columns = GridCells.Adaptive(minSize = 100.dp),
@@ -260,7 +260,7 @@ class CtrlActivity : ComponentActivity() {
                                     Switch(
                                         isLock.value, { value ->
                                             isLock.value = value
-                                            dev.ctrlSlotLock(state.ch, value).writeByFrame()
+                                            dev.ctrlSlotLock(state.ch, value)
                                         }, modifier = Modifier
                                             .graphicsLayer(scaleX = 0.5f, 0.5f)
                                             .offset(30.dp, 30.dp)

+ 9 - 0
transport/src/main/java/com/iscs/comm/CommManager.kt

@@ -100,6 +100,15 @@ object CommManager {
         manager?.writeWithResponse(frame) ?: Frame()
     }
 
+    /**
+     * 发送数据帧,带响应
+     */
+    fun Frame.writeWithResponse(done: (rsp: Frame) -> Unit) {
+        manager?.scope?.launch {
+            done(manager?.writeWithResponse(this@writeWithResponse) ?: Frame())
+        }
+    }
+
     /**
      * 初始化设备列表
      */

+ 17 - 8
transport/src/main/java/com/iscs/comm/entity/device/DeviceKeySlot.kt

@@ -1,5 +1,6 @@
 package com.iscs.comm.entity.device
 
+import com.iscs.comm.CommManager.writeWithResponse
 import com.iscs.comm.entity.Frame
 import com.iscs.comm.entity.device.status.DeviceStatus
 import com.iscs.comm.entity.device.status.DeviceStatusKeySlot
@@ -88,11 +89,16 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param ch        控制槽位 默认-1控制所有
      * @param isLock    是否锁定
      */
-    fun ctrlSlotLock(ch: Int, isLock: Boolean): Frame {
-        if (ch !in 0..1) return Frame(-1, byteArrayOf())
+    fun ctrlSlotLock(ch: Int, isLock: Boolean, done: (rsp: Frame) -> Unit = {}) {
+        if (ch !in 0..1) {
+            done(Frame(-1, byteArrayOf()))
+            return
+        }
         // 不控制的一项从本地缓存状态中获取写入
         val status = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
-        return frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, isLock, status?.isCharging ?: false)
+        (frame.newFrame().apply { cmd = frame.cmd }
+            .buildCtrlKeySlotLockAndCharge(ch, isLock, status?.isCharging ?: false))
+            .writeWithResponse(done)
     }
 
     /**
@@ -101,11 +107,14 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param ch        控制槽位
      * @param isCharge  是否充电
      */
-    fun ctrlSlotCharge(ch: Int, isCharge: Boolean): Frame {
-        if (ch !in 0..1) return Frame(-1, byteArrayOf())
+    fun ctrlSlotCharge(ch: Int, isCharge: Boolean, done: (rsp: Frame) -> Unit = {}) {
+        if (ch !in 0..1) {
+            done(Frame(-1, byteArrayOf()))
+            return
+        }
         // 不控制的一项从本地缓存状态中获取写入
         val status = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
-        return frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, status?.isSlotLock ?: false, isCharge)
+        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, status?.isSlotLock ?: false, isCharge)).writeWithResponse(done)
     }
 
     /**
@@ -115,8 +124,8 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param isLock    是否锁住
      * @param isCharge  是否充电
      */
-    fun ctrlSlotLockAndCharge(ch: Int, isLock: Boolean, isCharge: Boolean): Frame {
-        return frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, isLock, isCharge)
+    fun ctrlSlotLockAndCharge(ch: Int, isLock: Boolean, isCharge: Boolean, done: (rsp: Frame) -> Unit = {}) {
+        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, isLock, isCharge)).writeWithResponse(done)
     }
 
 }

+ 3 - 3
transport/src/main/java/com/iscs/comm/entity/device/DeviceLockSlot.kt

@@ -1,6 +1,6 @@
 package com.iscs.comm.entity.device
 
-import android.util.Log
+import com.iscs.comm.CommManager.writeWithResponse
 import com.iscs.comm.entity.Frame
 import com.iscs.comm.entity.device.status.DeviceStatus
 import com.iscs.comm.entity.device.status.DeviceStatusLockSlot
@@ -79,8 +79,8 @@ class DeviceLockSlot(frame: Frame) : Device(frame) {
      * @param ch        控制哪个槽位 默认-1控制所有
      * @param isLock    是否锁定
      */
-    fun ctrlSlotLock(ch: Int = -1, isLock: Boolean): Frame {
-        return frame.newFrame().apply { cmd = frame.cmd }.buildCtrlSlotLock(ch, isLock)
+    fun ctrlSlotLock(ch: Int = -1, isLock: Boolean, done: (rsp: Frame) -> Unit = {}) {
+        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlSlotLock(ch, isLock)).writeWithResponse(done)
     }
 
 }