|
|
@@ -1,148 +1,89 @@
|
|
|
package com.iscs.comm
|
|
|
|
|
|
import android.os.Bundle
|
|
|
-import android.util.Log
|
|
|
import androidx.activity.ComponentActivity
|
|
|
import androidx.activity.compose.setContent
|
|
|
import androidx.activity.enableEdgeToEdge
|
|
|
+import androidx.compose.foundation.layout.Arrangement
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
-import androidx.compose.foundation.layout.Row
|
|
|
-import androidx.compose.foundation.layout.Spacer
|
|
|
+import androidx.compose.foundation.layout.PaddingValues
|
|
|
+import androidx.compose.foundation.layout.aspectRatio
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
+import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
-import androidx.compose.foundation.layout.width
|
|
|
-import androidx.compose.material3.Button
|
|
|
+import androidx.compose.foundation.lazy.grid.GridCells
|
|
|
+import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
|
|
+import androidx.compose.foundation.lazy.grid.items
|
|
|
+import androidx.compose.material3.Card
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
import androidx.compose.material3.Text
|
|
|
+import androidx.compose.runtime.Composable
|
|
|
+import androidx.compose.runtime.DisposableEffect
|
|
|
+import androidx.compose.runtime.LaunchedEffect
|
|
|
+import androidx.compose.runtime.mutableStateListOf
|
|
|
import androidx.compose.ui.Modifier
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
-import androidx.lifecycle.lifecycleScope
|
|
|
+import androidx.compose.ui.unit.sp
|
|
|
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.intf.IDeviceListener
|
|
|
-import com.iscs.comm.ui.theme.CommunicationDemoTheme
|
|
|
-import kotlinx.coroutines.Dispatchers
|
|
|
-import kotlinx.coroutines.launch
|
|
|
+import com.iscs.comm.ui.theme.CommDemoTheme
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
|
|
|
|
- private val list = ArrayList<Device>()
|
|
|
-
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
super.onCreate(savedInstanceState)
|
|
|
enableEdgeToEdge()
|
|
|
setContent {
|
|
|
- CommunicationDemoTheme {
|
|
|
- Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
|
- Column(modifier = Modifier.padding(10.dp)) {
|
|
|
- Text("测试锁底座", modifier = Modifier.padding(innerPadding))
|
|
|
- Row {
|
|
|
- Button({ ctrlLockSlotLock(0, false) }) { Text("0-开") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(0, true) }) { Text("0-关") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(1, false) }) { Text("1-开") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(1, true) }) { Text("1-关") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(2, false) }) { Text("2-开") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(2, true) }) { Text("2-关") }
|
|
|
- }
|
|
|
- Row {
|
|
|
- Button({ ctrlLockSlotLock(3, false) }) { Text("3-开") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(3, true) }) { Text("3-关") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(4, false) }) { Text("4-开") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlLockSlotLock(4, true) }) { Text("4-关") }
|
|
|
- }
|
|
|
- Text("测试钥匙底座", modifier = Modifier.padding(innerPadding))
|
|
|
- Row {
|
|
|
- Button({ ctrlKeySlotLock(0, false) }) { Text("0-开锁") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotLock(0, true) }) { Text("0-关锁") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotCharge(0, true) }) { Text("0-开充电") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotCharge(0, false) }) { Text("0-关充电") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeyAllSlot(-1, false, false) }) { Text("全开关充电") }
|
|
|
- }
|
|
|
- Row {
|
|
|
- Button({ ctrlKeySlotLock(1, false) }) { Text("1-开锁") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotLock(1, true) }) { Text("1-关锁") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotCharge(1, true) }) { Text("1-开充电") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeySlotCharge(1, false) }) { Text("1-关充电") }
|
|
|
- Spacer(modifier = Modifier.width(10.dp))
|
|
|
- Button({ ctrlKeyAllSlot(-1, true, true) }) { Text("全关开充电") }
|
|
|
- }
|
|
|
- }
|
|
|
+ CommDemoTheme {
|
|
|
+ Scaffold(modifier = Modifier.fillMaxSize()) { pv ->
|
|
|
+ buildDeviceList(pv)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 做SDK初始化操作处理
|
|
|
- initISCSSDK()
|
|
|
}
|
|
|
|
|
|
- fun ctrlLockSlotLock(ch: Int, isLock: Boolean) {
|
|
|
- val device = list.find { it.frame.cmd == 0x0602 }
|
|
|
- if (device is DeviceLockSlot) {
|
|
|
- CommManager.write(device.ctrlSlotLock(ch, isLock))
|
|
|
- }
|
|
|
- }
|
|
|
+ @Composable
|
|
|
+ fun buildDeviceList(pv: PaddingValues) {
|
|
|
+ val list = mutableStateListOf<Device>()
|
|
|
+ val listener = object : IDeviceListener() {
|
|
|
|
|
|
- fun ctrlKeySlotLock(ch: Int, isLock: Boolean) {
|
|
|
- val device = list.find { it.frame.cmd == 0x0601 }
|
|
|
- if (device is DeviceKeySlot) {
|
|
|
- CommManager.write(device.ctrlSlotLock(ch, isLock))
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- fun ctrlKeySlotCharge(ch: Int, isCharge: Boolean) {
|
|
|
- val device = list.find { it.frame.cmd == 0x0601 }
|
|
|
- if (device is DeviceKeySlot) {
|
|
|
- CommManager.write(device.ctrlSlotCharge(ch, isCharge))
|
|
|
- }
|
|
|
- }
|
|
|
+ override fun onDeviceList(devices: List<Device>) {
|
|
|
+ list.clear()
|
|
|
+ list.addAll(devices.sortedBy { it.frame.cmd })
|
|
|
+ }
|
|
|
|
|
|
- fun ctrlKeyAllSlot(ch: Int, isLock: Boolean, isCharge: Boolean) {
|
|
|
- val device = list.find { it.frame.cmd == 0x0601 }
|
|
|
- if (device is DeviceKeySlot) {
|
|
|
- CommManager.write(device.ctrlSlotLockAndCharge(ch, isLock, isCharge))
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化ISCS SDK 操作
|
|
|
- */
|
|
|
- fun initISCSSDK() {
|
|
|
- lifecycleScope.launch(Dispatchers.IO) {
|
|
|
+ LaunchedEffect("") {
|
|
|
+ // 设置设备变化监听
|
|
|
+ CommManager.addOnDeviceListener(listener)
|
|
|
+ // SDK初始化
|
|
|
CommManager.init()
|
|
|
- CommManager.setOnDeviceListener(object : IDeviceListener {
|
|
|
-
|
|
|
- override fun onDeviceList(devices: List<Device>) {
|
|
|
- list.addAll(devices)
|
|
|
- }
|
|
|
-
|
|
|
- override fun onDeviceChanged(device: Device) {
|
|
|
- val msg = when (device.deviceStatus) {
|
|
|
- is DeviceStatusKeySlot -> (device.deviceStatus as DeviceStatusKeySlot).slotList.toString()
|
|
|
- is DeviceStatusLockSlot -> (device.deviceStatus as DeviceStatusLockSlot).slotList.toString()
|
|
|
- else -> "未知状态变化"
|
|
|
+ }
|
|
|
+ DisposableEffect("") {
|
|
|
+ // 移除页面监听
|
|
|
+ CommManager.removeOnDeviceListener(listener)
|
|
|
+ onDispose { }
|
|
|
+ }
|
|
|
+ LazyVerticalGrid(
|
|
|
+ columns = GridCells.Adaptive(minSize = 100.dp),
|
|
|
+ contentPadding = PaddingValues(10.dp, pv.calculateTopPadding() + 10.dp),
|
|
|
+ horizontalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
+ verticalArrangement = Arrangement.spacedBy(8.dp),
|
|
|
+ ) {
|
|
|
+ items(list) { device ->
|
|
|
+ Card(onClick = { openCtrlActivity(device.frame.cmd) }) {
|
|
|
+ Column(
|
|
|
+ modifier = Modifier
|
|
|
+ .padding(5.dp)
|
|
|
+ .aspectRatio(1.2f)
|
|
|
+ .fillMaxWidth()
|
|
|
+ ) {
|
|
|
+ Text(device.frame.cmd.toString(16))
|
|
|
+ Text("${device.deviceType}", fontSize = 12.sp)
|
|
|
}
|
|
|
- Log.d("xiaoming", "设备状态发生变化了 $msg")
|
|
|
}
|
|
|
-
|
|
|
- })
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|