|
|
@@ -35,6 +35,7 @@ import androidx.compose.runtime.DisposableEffect
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
import androidx.compose.runtime.remember
|
|
|
+import androidx.compose.ui.Alignment
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.draw.clip
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
@@ -94,66 +95,74 @@ class CtrlActivity : ComponentActivity() {
|
|
|
@Composable
|
|
|
fun CtrlKeySlot(dev: DeviceKeySlot) {
|
|
|
val status = dev.deviceStatus as DeviceStatusKeySlot
|
|
|
- LazyVerticalGrid(
|
|
|
- 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 ->
|
|
|
- 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
|
|
|
+ Column {
|
|
|
+ Row(modifier = Modifier.padding(horizontal = 10.dp)) {
|
|
|
+ Button({ dev.ctrlSlotLockAndCharge(-1, false, false).writeByFrame() }) { Text("全开") }
|
|
|
+ Spacer(modifier = Modifier.width(10.dp))
|
|
|
+ Button({ dev.ctrlSlotLockAndCharge(-1, true, true).writeByFrame() }) { Text("全关") }
|
|
|
+ }
|
|
|
+ LazyVerticalGrid(
|
|
|
+ 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 ->
|
|
|
+ val rfid = remember { mutableStateOf(state.rfid) }
|
|
|
+ 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 && it.isChanged }?.let {
|
|
|
+ isLock.value = it.isSlotLock
|
|
|
+ isUsed.value = it.isUsed
|
|
|
+ rfid.value = it.rfid
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- LaunchedEffect("") {
|
|
|
- CommManager.addOnDeviceListener(listener)
|
|
|
- }
|
|
|
+ LaunchedEffect("") {
|
|
|
+ CommManager.addOnDeviceListener(listener)
|
|
|
+ }
|
|
|
|
|
|
- DisposableEffect("") {
|
|
|
- onDispose { CommManager.removeOnDeviceListener(listener) }
|
|
|
- }
|
|
|
- Card {
|
|
|
- Column(
|
|
|
- modifier = Modifier
|
|
|
- .padding(10.dp)
|
|
|
- .aspectRatio(1.3f)
|
|
|
- .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 {
|
|
|
- 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("开充电") }
|
|
|
+ DisposableEffect("") {
|
|
|
+ onDispose { CommManager.removeOnDeviceListener(listener) }
|
|
|
+ }
|
|
|
+ Card {
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(10.dp)
|
|
|
+ .aspectRatio(1.3f)
|
|
|
+ .fillMaxWidth(),
|
|
|
+ ) {
|
|
|
+ Row {
|
|
|
+ Text("${state.ch}路 ${rfid.value}")
|
|
|
+ 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))
|
|
|
- Button({ dev.ctrlSlotCharge(state.ch, false).writeByFrame() }) { Text("关充电") }
|
|
|
+ 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("关充电") }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -176,16 +185,26 @@ class CtrlActivity : ComponentActivity() {
|
|
|
verticalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
) {
|
|
|
items(status) { state ->
|
|
|
+ var lastRFID = state.rfid
|
|
|
+ val rfid = remember { mutableStateOf(state.rfid) }
|
|
|
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")
|
|
|
+ (device.deviceStatus as DeviceStatusLockSlot).slotList.find { it.ch == state.ch && it.isChanged }?.let {
|
|
|
isLock.value = it.isSlotLock
|
|
|
isUsed.value = it.isUsed
|
|
|
+ rfid.value = it.rfid
|
|
|
+ Log.d("xiaoming", "${state.ch} ${it.isChanged} ${rfid.value} ${lastRFID}")
|
|
|
+ // 这里可以做一些其他逻辑,比如如果当前有锁存在,但是没上锁,这里可执行上锁操作
|
|
|
+ if (it.isChanged && rfid.value.isNotEmpty() && rfid.value != lastRFID) {
|
|
|
+ lastRFID = rfid.value
|
|
|
+ dev.ctrlSlotLock(state.ch, true).writeByFrame()
|
|
|
+ } else if (rfid.value.isEmpty()) {
|
|
|
+ lastRFID = ""
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -198,34 +217,41 @@ class CtrlActivity : ComponentActivity() {
|
|
|
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 {
|
|
|
+ Box {
|
|
|
+ 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))
|
|
|
- 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)
|
|
|
- )
|
|
|
+ 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)
|
|
|
+ )
|
|
|
+ }
|
|
|
}
|
|
|
+ Text(
|
|
|
+ rfid.value, modifier = Modifier
|
|
|
+ .padding(start = 10.dp)
|
|
|
+ .align(Alignment.CenterStart)
|
|
|
+ )
|
|
|
}
|
|
|
}
|
|
|
}
|