|
|
@@ -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
|
|
|
@@ -11,27 +12,42 @@ import androidx.compose.foundation.layout.Arrangement
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
import androidx.compose.foundation.layout.PaddingValues
|
|
|
+import androidx.compose.foundation.layout.Row
|
|
|
import androidx.compose.foundation.layout.Spacer
|
|
|
import androidx.compose.foundation.layout.aspectRatio
|
|
|
-import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
+import androidx.compose.foundation.layout.offset
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
+import androidx.compose.foundation.layout.size
|
|
|
+import androidx.compose.foundation.layout.width
|
|
|
import androidx.compose.foundation.lazy.grid.GridCells
|
|
|
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
|
|
import androidx.compose.foundation.lazy.grid.items
|
|
|
+import androidx.compose.foundation.shape.RoundedCornerShape
|
|
|
+import androidx.compose.material3.Button
|
|
|
import androidx.compose.material3.Card
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
+import androidx.compose.material3.Switch
|
|
|
import androidx.compose.material3.Text
|
|
|
import androidx.compose.runtime.Composable
|
|
|
+import androidx.compose.runtime.DisposableEffect
|
|
|
+import androidx.compose.runtime.LaunchedEffect
|
|
|
+import androidx.compose.runtime.mutableStateOf
|
|
|
+import androidx.compose.runtime.remember
|
|
|
import androidx.compose.ui.Modifier
|
|
|
+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.write
|
|
|
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
|
|
|
+import com.iscs.comm.entity.device.status.DeviceStatusKeySlot
|
|
|
import com.iscs.comm.entity.device.status.DeviceStatusLockSlot
|
|
|
import com.iscs.comm.enums.DeviceType
|
|
|
+import com.iscs.comm.intf.IDeviceListener
|
|
|
import com.iscs.comm.ui.theme.CommDemoTheme
|
|
|
|
|
|
/**
|
|
|
@@ -64,9 +80,11 @@ class CtrlActivity : ComponentActivity() {
|
|
|
fun BuildCtrlChild(pv: PaddingValues) {
|
|
|
val cmd = intent.getIntExtra("cmd_id", -1)
|
|
|
val device = CommManager.deviceList.find { it.frame.cmd == cmd }
|
|
|
+ if (device == null) return
|
|
|
Box(modifier = Modifier.padding(10.dp, pv.calculateTopPadding())) {
|
|
|
- when (device?.deviceType ?: DeviceType.NONE) {
|
|
|
- DeviceType.SLOT_LOCK -> CtrlLock5(device!! as DeviceLockSlot)
|
|
|
+ when (device.deviceType) {
|
|
|
+ DeviceType.SLOT_KEY -> CtrlKeySlot(device as DeviceKeySlot)
|
|
|
+ DeviceType.SLOT_LOCK -> CtrlLock5Slot(device as DeviceLockSlot)
|
|
|
// 未知设备
|
|
|
else -> Text("未知设备,无法控制")
|
|
|
}
|
|
|
@@ -74,33 +92,145 @@ class CtrlActivity : ComponentActivity() {
|
|
|
}
|
|
|
|
|
|
@Composable
|
|
|
- fun CtrlLock5(device: DeviceLockSlot) {
|
|
|
- val status = device.deviceStatus as DeviceStatusLockSlot
|
|
|
+ fun CtrlKeySlot(dev: DeviceKeySlot) {
|
|
|
+ val status = dev.deviceStatus as DeviceStatusKeySlot
|
|
|
LazyVerticalGrid(
|
|
|
- columns = GridCells.Adaptive(minSize = 100.dp),
|
|
|
+ columns = GridCells.Adaptive(minSize = 180.dp),
|
|
|
contentPadding = PaddingValues(10.dp, 10.dp),
|
|
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
verticalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
) {
|
|
|
items(ArrayList(status.slotList.sortedBy { it.ch })) { state ->
|
|
|
- Card(onClick = {
|
|
|
- state.isSlotLock = !state.isSlotLock
|
|
|
- device.ctrlSlotLock(state.ch, state.isSlotLock).writeByFrame()
|
|
|
- }) {
|
|
|
+ val isLock = remember { mutableStateOf(state.isSlotLock) }
|
|
|
+ val isUsed = remember { mutableStateOf(state.isUsed) }
|
|
|
+ val listener = object : IDeviceListener() {
|
|
|
+ override fun onDeviceChanged(device: Device) {
|
|
|
+ super.onDeviceChanged(device)
|
|
|
+ if (dev.frame.cmd == device.frame.cmd) {
|
|
|
+ (device.deviceStatus as DeviceStatusKeySlot).slotList.find { it.ch == state.ch }?.let {
|
|
|
+ Log.d("xiaoming", "$it")
|
|
|
+ isLock.value = it.isSlotLock
|
|
|
+ isUsed.value = it.isUsed
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LaunchedEffect("") {
|
|
|
+ CommManager.addOnDeviceListener(listener)
|
|
|
+ }
|
|
|
+
|
|
|
+ DisposableEffect("") {
|
|
|
+ onDispose { CommManager.removeOnDeviceListener(listener) }
|
|
|
+ }
|
|
|
+ Card {
|
|
|
Column(
|
|
|
modifier = Modifier
|
|
|
.padding(10.dp)
|
|
|
- .aspectRatio(1.2f)
|
|
|
- .background(if (state.isUsed) Color.Blue else Color.Transparent)
|
|
|
+ .aspectRatio(1.3f)
|
|
|
.fillMaxWidth(),
|
|
|
) {
|
|
|
- Text("${state.ch}路")
|
|
|
- Spacer(modifier = Modifier.fillMaxHeight())
|
|
|
- Text("是否上锁:${state.isSlotLock}")
|
|
|
+ Row {
|
|
|
+ Text("${state.ch}路")
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Spacer(
|
|
|
+ modifier = Modifier
|
|
|
+ .size(12.dp)
|
|
|
+ .clip(RoundedCornerShape(12.dp))
|
|
|
+ .background(if (isUsed.value) (if (isLock.value) Color.Red else Color.Blue) else Color.Green)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Row {
|
|
|
+ Button({ dev.ctrlSlotLock(state.ch, false).writeByFrame() }) { Text("打开锁") }
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Button({ dev.ctrlSlotLock(state.ch, true).writeByFrame() }) { Text("关闭锁") }
|
|
|
+ }
|
|
|
+ Row {
|
|
|
+ Button({ dev.ctrlSlotCharge(state.ch, true).writeByFrame() }) { Text("开充电") }
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Button({ dev.ctrlSlotCharge(state.ch, false).writeByFrame() }) { Text("关充电") }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 锁底座控制
|
|
|
+ */
|
|
|
+ @Composable
|
|
|
+ fun CtrlLock5Slot(dev: DeviceLockSlot) {
|
|
|
+ val status = ArrayList((dev.deviceStatus as DeviceStatusLockSlot).slotList)
|
|
|
+ Column {
|
|
|
+ Row(modifier = Modifier.padding(horizontal = 10.dp)) {
|
|
|
+ Button({ dev.ctrlSlotLock(-1, false).writeByFrame() }) { Text("全开") }
|
|
|
+ Spacer(modifier = Modifier.width(10.dp))
|
|
|
+ Button({ dev.ctrlSlotLock(-1, true).writeByFrame() }) { Text("全关") }
|
|
|
+ }
|
|
|
+ LazyVerticalGrid(
|
|
|
+ columns = GridCells.Adaptive(minSize = 100.dp),
|
|
|
+ contentPadding = PaddingValues(10.dp, 10.dp),
|
|
|
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
+ verticalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
+ ) {
|
|
|
+ items(status) { state ->
|
|
|
+ val isLock = remember { mutableStateOf(state.isSlotLock) }
|
|
|
+ val isUsed = remember { mutableStateOf(state.isUsed) }
|
|
|
+ val listener = object : IDeviceListener() {
|
|
|
+ override fun onDeviceChanged(device: Device) {
|
|
|
+ super.onDeviceChanged(device)
|
|
|
+ if (dev.frame.cmd == device.frame.cmd) {
|
|
|
+ (device.deviceStatus as DeviceStatusLockSlot).slotList.find { it.ch == state.ch }?.let {
|
|
|
+ Log.d("xiaoming", "$it")
|
|
|
+ isLock.value = it.isSlotLock
|
|
|
+ isUsed.value = it.isUsed
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ LaunchedEffect("") {
|
|
|
+ CommManager.addOnDeviceListener(listener)
|
|
|
+ }
|
|
|
+
|
|
|
+ DisposableEffect("") {
|
|
|
+ onDispose { CommManager.removeOnDeviceListener(listener) }
|
|
|
+ }
|
|
|
+ Card {
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(10.dp)
|
|
|
+ .aspectRatio(1.2f)
|
|
|
+ .fillMaxWidth(),
|
|
|
+ ) {
|
|
|
+ Row {
|
|
|
+ Text("${state.ch}路")
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Spacer(
|
|
|
+ modifier = Modifier
|
|
|
+ .size(12.dp)
|
|
|
+ .clip(RoundedCornerShape(12.dp))
|
|
|
+ .background(if (isUsed.value) (if (isLock.value) Color.Red else Color.Blue) else Color.Green)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Row {
|
|
|
+ Spacer(modifier = Modifier.weight(1f))
|
|
|
+ Switch(
|
|
|
+ isLock.value, { value ->
|
|
|
+ isLock.value = value
|
|
|
+ dev.ctrlSlotLock(state.ch, value).writeByFrame()
|
|
|
+ }, modifier = Modifier
|
|
|
+ .graphicsLayer(scaleX = 0.5f, 0.5f)
|
|
|
+ .offset(30.dp, 30.dp)
|
|
|
+ )
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|