Kaynağa Gözat

1. 优化控制命令支持返回结果

bjb 3 ay önce
ebeveyn
işleme
1108942e73

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

@@ -3,6 +3,7 @@ package com.iscs.comm
 import android.content.Context
 import android.content.Intent
 import android.os.Bundle
+import android.util.Log
 import androidx.activity.ComponentActivity
 import androidx.activity.compose.setContent
 import androidx.activity.enableEdgeToEdge
@@ -40,7 +41,6 @@ import androidx.compose.ui.draw.clip
 import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.graphics.graphicsLayer
 import androidx.compose.ui.unit.dp
-import com.iscs.comm.CommManager.writeByFrame
 import com.iscs.comm.entity.device.Device
 import com.iscs.comm.entity.device.DeviceKeySlot
 import com.iscs.comm.entity.device.DeviceLockSlot
@@ -96,9 +96,23 @@ class CtrlActivity : ComponentActivity() {
         val status = dev.deviceStatus as DeviceStatusKeySlot
         Column {
             Row(modifier = Modifier.padding(horizontal = 10.dp)) {
-                Button({ dev.ctrlSlotLockAndCharge(-1, false, false) }) { Text("全开") }
+                Button({
+                    status.slotList.forEach { slot ->
+                        dev.ctrlSlotLock(slot.ch, false) {
+                            Log.d("xiaoming", "${slot.ch} <- ${it}")
+                        }
+                    }
+                    // dev.ctrlSlotLockAndCharge(-1, false, false)
+                }) { Text("全开") }
                 Spacer(modifier = Modifier.width(10.dp))
-                Button({ dev.ctrlSlotLockAndCharge(-1, true, true) }) { Text("全关") }
+                Button({
+                    status.slotList.forEach { slot ->
+                        dev.ctrlSlotLock(slot.ch, true) {
+                            Log.d("xiaoming", "${slot.ch} <- ${it}")
+                        }
+                    }
+                    // dev.ctrlSlotLockAndCharge(-1, true, true)
+                }) { Text("全关") }
             }
             LazyVerticalGrid(
                 columns = GridCells.Adaptive(minSize = 180.dp),

+ 7 - 2
transport/src/main/java/com/iscs/comm/CommManager.kt

@@ -9,6 +9,7 @@ import com.iscs.comm.intf.AbsCommBase
 import com.iscs.comm.intf.IDeviceListener
 import com.iscs.comm.manager.CanManager
 import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.async
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.isActive
 import kotlinx.coroutines.launch
@@ -103,9 +104,13 @@ object CommManager {
     /**
      * 发送数据帧,带响应
      */
-    fun Frame.writeWithResponse(done: (rsp: Frame) -> Unit) {
+    fun Frame.writeWithResponse(done: (Frame) -> Unit) {
         manager?.scope?.launch {
-            done(manager?.writeWithResponse(this@writeWithResponse) ?: Frame())
+            val frame = manager?.writeWithResponse(this@writeWithResponse) ?: Frame()
+            async {
+                delay(800)
+                done(frame)
+            }
         }
     }
 

+ 6 - 0
transport/src/main/java/com/iscs/comm/entity/CtrlResponse.kt

@@ -0,0 +1,6 @@
+package com.iscs.comm.entity
+
+/**
+ * 控制操作响应,对外暴露
+ */
+data class CtrlResponse(val code: Int = 500, val msg: String = "option error")

+ 37 - 9
transport/src/main/java/com/iscs/comm/entity/device/DeviceKeySlot.kt

@@ -1,6 +1,7 @@
 package com.iscs.comm.entity.device
 
 import com.iscs.comm.CommManager.writeWithResponse
+import com.iscs.comm.entity.CtrlResponse
 import com.iscs.comm.entity.Frame
 import com.iscs.comm.entity.device.status.DeviceStatus
 import com.iscs.comm.entity.device.status.DeviceStatusKeySlot
@@ -89,16 +90,19 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param ch        控制槽位 默认-1控制所有
      * @param isLock    是否锁定
      */
-    fun ctrlSlotLock(ch: Int, isLock: Boolean, done: (rsp: Frame) -> Unit = {}) {
+    fun ctrlSlotLock(ch: Int, isLock: Boolean, done: (CtrlResponse) -> Unit = {}) {
         if (ch !in 0..1) {
-            done(Frame(-1, byteArrayOf()))
+            done(CtrlResponse())
             return
         }
         // 不控制的一项从本地缓存状态中获取写入
         val status = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
         (frame.newFrame().apply { cmd = frame.cmd }
-            .buildCtrlKeySlotLockAndCharge(ch, isLock, status?.isCharging ?: false))
-            .writeWithResponse(done)
+            .buildCtrlKeySlotLockAndCharge(ch, isLock, isLock))
+            .writeWithResponse {
+                val slot = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
+                done(CtrlResponse(code = if (slot?.isSlotLock == isLock) 200 else 500, msg = if (slot?.isSlotLock == isLock) "success" else "fail"))
+            }
     }
 
     /**
@@ -107,14 +111,23 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param ch        控制槽位
      * @param isCharge  是否充电
      */
-    fun ctrlSlotCharge(ch: Int, isCharge: Boolean, done: (rsp: Frame) -> Unit = {}) {
+    fun ctrlSlotCharge(ch: Int, isCharge: Boolean, done: (CtrlResponse) -> Unit = {}) {
         if (ch !in 0..1) {
-            done(Frame(-1, byteArrayOf()))
+            done(CtrlResponse())
             return
         }
         // 不控制的一项从本地缓存状态中获取写入
         val status = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
-        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, status?.isSlotLock ?: false, isCharge)).writeWithResponse(done)
+        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlKeySlotLockAndCharge(ch, status?.isSlotLock ?: false, isCharge))
+            .writeWithResponse {
+                val slot = (deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == ch }
+                done(
+                    CtrlResponse(
+                        code = if (slot?.isCharging == isCharge) 200 else 500,
+                        msg = if (slot?.isCharging == isCharge) "success" else "fail"
+                    )
+                )
+            }
     }
 
     /**
@@ -124,8 +137,23 @@ class DeviceKeySlot(frame: Frame) : Device(frame) {
      * @param isLock    是否锁住
      * @param 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)
+    fun ctrlSlotLockAndCharge(ch: Int, isLock: Boolean, isCharge: Boolean, done: (CtrlResponse) -> Unit = {}) {
+        (frame.newFrame().apply { cmd = frame.cmd }
+            .buildCtrlKeySlotLockAndCharge(ch, isLock, isCharge))
+            .writeWithResponse {
+                if (ch < 0) {
+                    val status = (deviceStatus as DeviceStatusKeySlot).slotList
+                    // 全部控制
+                    val isOk = status.filter { it.isSlotLock == isLock && it.isCharging == isCharge }.size == status.size
+                    done(CtrlResponse(code = if (isOk) 200 else 500, msg = if (isOk) "success" else "fail"))
+                } else {
+                    // 单个控制
+                    val isOk = (deviceStatus as DeviceStatusKeySlot).slotList.find {
+                        it.ch == ch && it.isCharging == isCharge && it.isSlotLock == isLock
+                    } != null
+                    done(CtrlResponse(code = if (isOk) 200 else 500, msg = if (isOk) "success" else "fail"))
+                }
+            }
     }
 
 }

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

@@ -1,6 +1,7 @@
 package com.iscs.comm.entity.device
 
 import com.iscs.comm.CommManager.writeWithResponse
+import com.iscs.comm.entity.CtrlResponse
 import com.iscs.comm.entity.Frame
 import com.iscs.comm.entity.device.status.DeviceStatus
 import com.iscs.comm.entity.device.status.DeviceStatusLockSlot
@@ -79,8 +80,19 @@ class DeviceLockSlot(frame: Frame) : Device(frame) {
      * @param ch        控制哪个槽位 默认-1控制所有
      * @param isLock    是否锁定
      */
-    fun ctrlSlotLock(ch: Int = -1, isLock: Boolean, done: (rsp: Frame) -> Unit = {}) {
-        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlSlotLock(ch, isLock)).writeWithResponse(done)
+    fun ctrlSlotLock(ch: Int = -1, isLock: Boolean, done: (CtrlResponse) -> Unit = {}) {
+        (frame.newFrame().apply { cmd = frame.cmd }.buildCtrlSlotLock(ch, isLock)).writeWithResponse {
+            if (ch < 0) {
+                val status = (deviceStatus as DeviceStatusLockSlot).slotList
+                // 全部控制
+                val isOk = status.filter { it.isSlotLock == isLock }.size == status.size
+                done(CtrlResponse(code = if (isOk) 200 else 500, msg = if (isOk) "success" else "fail"))
+            } else {
+                // 单个控制
+                val isOk = (deviceStatus as DeviceStatusLockSlot).slotList.find { it.ch == ch && it.isSlotLock == isLock } != null
+                done(CtrlResponse(code = if (isOk) 200 else 500, msg = if (isOk) "success" else "fail"))
+            }
+        }
     }
 
 }

+ 1 - 1
transport/src/main/java/com/iscs/comm/intf/AbsCommBase.kt

@@ -53,7 +53,7 @@ abstract class AbsCommBase {
      * @param frame     请求的数据帧
      * @param timeout   配置响应式超时
      */
-    abstract suspend fun writeWithResponse(frame: Frame, timeout: Long = 5_000): Frame
+    abstract suspend fun writeWithResponse(frame: Frame, timeout: Long = 50): Frame
 
     /**
      * 读数据帧