From c3ab3c5ae90587063a7b3711803816280c9811a6 Mon Sep 17 00:00:00 2001 From: Andre K Date: Mon, 15 May 2023 17:49:13 -0300 Subject: [PATCH] feat: implement `PacketResponseState.Success` (#634) --- .../com/geeksville/mesh/IMeshService.aidl | 8 +-- .../java/com/geeksville/mesh/model/UIState.kt | 56 ++++++++----------- .../geeksville/mesh/service/MeshService.kt | 16 +++--- .../mesh/ui/DeviceSettingsFragment.kt | 25 ++++----- .../config/PacketResponseStateDialog.kt | 7 ++- 5 files changed, 53 insertions(+), 59 deletions(-) diff --git a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl index f1044070..cac319aa 100644 --- a/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl +++ b/app/src/main/aidl/com/geeksville/mesh/IMeshService.aidl @@ -127,16 +127,16 @@ interface IMeshService { void requestTraceroute(in int requestId, in int destNum); /// Send Shutdown admin packet to nodeNum - void requestShutdown(in int destNum); + void requestShutdown(in int requestId, in int destNum); /// Send Reboot admin packet to nodeNum - void requestReboot(in int destNum); + void requestReboot(in int requestId, in int destNum); /// Send FactoryReset admin packet to nodeNum - void requestFactoryReset(in int destNum); + void requestFactoryReset(in int requestId, in int destNum); /// Send NodedbReset admin packet to nodeNum - void requestNodedbReset(in int destNum); + void requestNodedbReset(in int requestId, in int destNum); /// Returns a ChannelSet protobuf byte []getChannelSet(); diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index e0848df1..67d56e0a 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -299,6 +299,30 @@ class UIViewModel @Inject constructor( "Request traceroute error" ) + fun requestShutdown(destNum: Int) = request( + destNum, + { service, packetId, dest -> service.requestShutdown(packetId, dest) }, + "Request shutdown error" + ) + + fun requestReboot(destNum: Int) = request( + destNum, + { service, packetId, dest -> service.requestReboot(packetId, dest) }, + "Request reboot error" + ) + + fun requestFactoryReset(destNum: Int) = request( + destNum, + { service, packetId, dest -> service.requestFactoryReset(packetId, dest) }, + "Request factory reset error" + ) + + fun requestNodedbReset(destNum: Int) = request( + destNum, + { service, packetId, dest -> service.requestNodedbReset(packetId, dest) }, + "Request NodeDB reset error" + ) + fun requestPosition(destNum: Int, position: Position = Position(0.0, 0.0, 0)) { try { meshService?.requestPosition(destNum, position) @@ -515,38 +539,6 @@ class UIViewModel @Inject constructor( val adminChannelIndex: Int get() = channelSet.settingsList.map { it.name.lowercase() }.indexOf("admin") - fun requestShutdown(idNum: Int) { - try { - meshService?.requestShutdown(idNum) - } catch (ex: RemoteException) { - errormsg("RemoteException: ${ex.message}") - } - } - - fun requestReboot(idNum: Int) { - try { - meshService?.requestReboot(idNum) - } catch (ex: RemoteException) { - errormsg("RemoteException: ${ex.message}") - } - } - - fun requestFactoryReset(idNum: Int) { - try { - meshService?.requestFactoryReset(idNum) - } catch (ex: RemoteException) { - errormsg("RemoteException: ${ex.message}") - } - } - - fun requestNodedbReset(idNum: Int) { - try { - meshService?.requestNodedbReset(idNum) - } catch (ex: RemoteException) { - errormsg("RemoteException: ${ex.message}") - } - } - /** * Write the persisted packet data out to a CSV file in the specified location. */ diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index 5adc6383..fbfbf723 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -1788,26 +1788,26 @@ class MeshService : Service(), Logging { }) } - override fun requestShutdown(idNum: Int) = toRemoteExceptions { - sendToRadio(newMeshPacketTo(idNum).buildAdminPacket { + override fun requestShutdown(requestId: Int, destNum: Int) = toRemoteExceptions { + sendToRadio(newMeshPacketTo(destNum).buildAdminPacket(id = requestId) { shutdownSeconds = 5 }) } - override fun requestReboot(idNum: Int) = toRemoteExceptions { - sendToRadio(newMeshPacketTo(idNum).buildAdminPacket { + override fun requestReboot(requestId: Int, destNum: Int) = toRemoteExceptions { + sendToRadio(newMeshPacketTo(destNum).buildAdminPacket(id = requestId) { rebootSeconds = 5 }) } - override fun requestFactoryReset(idNum: Int) = toRemoteExceptions { - sendToRadio(newMeshPacketTo(idNum).buildAdminPacket { + override fun requestFactoryReset(requestId: Int, destNum: Int) = toRemoteExceptions { + sendToRadio(newMeshPacketTo(destNum).buildAdminPacket(id = requestId) { factoryReset = 1 }) } - override fun requestNodedbReset(idNum: Int) = toRemoteExceptions { - sendToRadio(newMeshPacketTo(idNum).buildAdminPacket { + override fun requestNodedbReset(requestId: Int, destNum: Int) = toRemoteExceptions { + sendToRadio(newMeshPacketTo(destNum).buildAdminPacket(id = requestId) { nodedbReset = 1 }) } diff --git a/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsFragment.kt index 5c339e30..71f5f4bb 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/DeviceSettingsFragment.kt @@ -184,7 +184,7 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { val configResponse by viewModel.packetResponse.collectAsStateWithLifecycle() val deviceProfile by viewModel.deviceProfile.collectAsStateWithLifecycle() var packetResponseState by remember { mutableStateOf(PacketResponseState.Empty) } - val isWaiting = packetResponseState is PacketResponseState.Loading + val isWaiting = packetResponseState !is PacketResponseState.Empty var showEditDeviceProfileDialog by remember { mutableStateOf(false) } val importConfigLauncher = rememberLauncherForActivityResult( @@ -237,7 +237,7 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { } ) - if (isWaiting || packetResponseState is PacketResponseState.Error) PacketResponseStateDialog( + if (isWaiting) PacketResponseStateDialog( packetResponseState, onDismiss = { packetResponseState = PacketResponseState.Empty @@ -249,8 +249,11 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { val data = configResponse?.meshPacket?.decoded if (data?.portnumValue == Portnums.PortNum.ROUTING_APP_VALUE) { val parsed = MeshProtos.Routing.parseFrom(data.payload) - if (parsed.errorReason != MeshProtos.Routing.Error.NONE) - packetResponseState = PacketResponseState.Error(parsed.errorReason.toString()) + packetResponseState = if (parsed.errorReason == MeshProtos.Routing.Error.NONE) { + PacketResponseState.Success(emptyList()) + } else { + PacketResponseState.Error(parsed.errorReason.toString()) + } } if (data?.portnumValue == Portnums.PortNum.ADMIN_APP_VALUE) { viewModel.clearPacketResponse() @@ -267,24 +270,24 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { viewModel.getChannel(destNum, response.index + 1) } else { // Received the last channel, start channel editor - packetResponseState = PacketResponseState.Success(emptyList()) + packetResponseState = PacketResponseState.Empty navController.navigate("channels") } } else { // Received max channels, start channel editor - packetResponseState = PacketResponseState.Success(emptyList()) + packetResponseState = PacketResponseState.Empty navController.navigate("channels") } } AdminProtos.AdminMessage.PayloadVariantCase.GET_OWNER_RESPONSE -> { - packetResponseState = PacketResponseState.Success(emptyList()) + packetResponseState = PacketResponseState.Empty userConfig = parsed.getOwnerResponse navController.navigate("user") } AdminProtos.AdminMessage.PayloadVariantCase.GET_CONFIG_RESPONSE -> { - packetResponseState = PacketResponseState.Success(emptyList()) + packetResponseState = PacketResponseState.Empty val response = parsed.getConfigResponse radioConfig = response enumValues().find { it.name == "${response.payloadVariantCase}" } @@ -292,7 +295,7 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { } AdminProtos.AdminMessage.PayloadVariantCase.GET_MODULE_CONFIG_RESPONSE -> { - packetResponseState = PacketResponseState.Success(emptyList()) + packetResponseState = PacketResponseState.Empty val response = parsed.getModuleConfigResponse moduleConfig = response enumValues().find { it.name == "${response.payloadVariantCase}" } @@ -350,22 +353,18 @@ fun RadioConfigNavHost(node: NodeInfo, viewModel: UIViewModel = viewModel()) { } "REBOOT" -> { - packetResponseState = PacketResponseState.Empty viewModel.requestReboot(destNum) } "SHUTDOWN" -> { - packetResponseState = PacketResponseState.Empty viewModel.requestShutdown(destNum) } "FACTORY_RESET" -> { - packetResponseState = PacketResponseState.Empty viewModel.requestFactoryReset(destNum) } "NODEDB_RESET" -> { - packetResponseState = PacketResponseState.Empty viewModel.requestNodedbReset(destNum) } diff --git a/app/src/main/java/com/geeksville/mesh/ui/components/config/PacketResponseStateDialog.kt b/app/src/main/java/com/geeksville/mesh/ui/components/config/PacketResponseStateDialog.kt index 168d69a1..eb3a10b6 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/components/config/PacketResponseStateDialog.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/components/config/PacketResponseStateDialog.kt @@ -26,7 +26,7 @@ fun PacketResponseStateDialog( ) { AlertDialog( onDismissRequest = { }, - text = { + title = { Column( modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally @@ -42,6 +42,9 @@ fun PacketResponseStateDialog( color = MaterialTheme.colors.onSurface, ) } + if (state is PacketResponseState.Success) { + Text("Success!") + } if (state is PacketResponseState.Error) { Text("Error: ${state.error}") } @@ -54,7 +57,7 @@ fun PacketResponseStateDialog( ) { Button( onClick = onDismiss, - modifier = Modifier.padding(bottom = 16.dp) + modifier = Modifier.padding(top = 16.dp) ) { if (state is PacketResponseState.Loading) { Text(stringResource(R.string.cancel))