From a598b50d4084b1c9e80b1f3deafff62e294aaab9 Mon Sep 17 00:00:00 2001 From: andrekir Date: Mon, 20 Jun 2022 22:46:45 -0300 Subject: [PATCH] handle updated wantConfig --- .../java/com/geeksville/mesh/model/UIState.kt | 11 ++-- .../geeksville/mesh/service/MeshService.kt | 59 ++++++++++--------- 2 files changed, 36 insertions(+), 34 deletions(-) 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 5c2677b58..e077d057d 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -67,15 +67,18 @@ class UIViewModel @Inject constructor( private val _allPacketState = MutableStateFlow>(emptyList()) val allPackets: StateFlow> = _allPacketState + private val _localConfig = MutableLiveData() + val localConfig: LiveData get() = _localConfig + init { viewModelScope.launch { packetRepository.getAllPackets().collect { packets -> _allPacketState.value = packets } } - viewModelScope.launch(Dispatchers.IO) { + viewModelScope.launch { localConfigRepository.localConfigFlow.collect { config -> - _localConfig.postValue(config) + _localConfig.value = config } } debug("ViewModel created") @@ -107,10 +110,6 @@ class UIViewModel @Inject constructor( _connectionState.value = connectionState } - /// various radio settings (including the channel) - private val _localConfig = MutableLiveData() - val localConfig: LiveData get() = _localConfig - private val _channels = MutableLiveData() val channels: LiveData get() = _channels 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 e5d135e5c..2ad378e87 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -303,7 +303,7 @@ class MeshService : Service(), Logging { val json = Json { isLenient = true } val asString = json.encodeToString(MeshServiceSettingsData.serializer(), settings) debug("Saving settings") - getPrefs().edit(commit = true) { + getPrefs().edit { // FIXME, not really ideal to store this bigish blob in preferences putString("json", asString) } @@ -319,10 +319,7 @@ class MeshService : Service(), Logging { nodeDBbyNodeNum.putAll(nodes.map { Pair(it.num, it) }) nodeDBbyID.putAll(nodes.mapNotNull { it.user?.let { user -> // ignore records that don't have a valid user - Pair( - user.id, - it - ) + Pair(user.id, it) } }) } @@ -359,7 +356,8 @@ class MeshService : Service(), Logging { var myNodeInfo: MyNodeInfo? = null - private var localConfig: LocalOnlyProtos.LocalConfig = LocalOnlyProtos.LocalConfig.getDefaultInstance() + private var localConfig: LocalOnlyProtos.LocalConfig = + LocalOnlyProtos.LocalConfig.newBuilder().build() private var channels = fixupChannelList(listOf()) @@ -749,7 +747,6 @@ class MeshService : Service(), Logging { val response = a.getConfigResponse debug("Admin: received config ${response.payloadVariantCase}") setLocalConfig(response) - if (response.hasLora()) requestChannel(0) // Now start reading channels } AdminProtos.AdminMessage.VariantCase.GET_CHANNEL_RESPONSE -> { @@ -844,7 +841,7 @@ class MeshService : Service(), Logging { processReceivedMeshPacket(packet) onNodeDBChanged() } else { - warn("Ignoring early received packet: $packet") + warn("Ignoring early received packet: ${packet.toOneLineString()}") //earlyReceivedPackets.add(packet) //logAssert(earlyReceivedPackets.size < 128) // The max should normally be about 32, but if the device is messed up it might try to send forever } @@ -944,12 +941,18 @@ class MeshService : Service(), Logging { } } - private fun setLocalConfig (config: ConfigProtos.Config) { + private fun setLocalConfig(config: ConfigProtos.Config) { serviceScope.handledLaunch { localConfigRepository.setLocalConfig(config) } } + private fun clearLocalConfig() { + serviceScope.handledLaunch { + localConfigRepository.clearLocalConfig() + } + } + private fun currentSecond() = (System.currentTimeMillis() / 1000).toInt() @@ -1116,24 +1119,14 @@ class MeshService : Service(), Logging { private fun onReceiveFromRadio(bytes: ByteArray) { try { - val proto = - MeshProtos.FromRadio.parseFrom(bytes) + val proto = MeshProtos.FromRadio.parseFrom(bytes) // info("Received from radio service: ${proto.toOneLineString()}") when (proto.payloadVariantCase.number) { - MeshProtos.FromRadio.PACKET_FIELD_NUMBER -> handleReceivedMeshPacket( - proto.packet - ) - - MeshProtos.FromRadio.CONFIG_COMPLETE_ID_FIELD_NUMBER -> handleConfigComplete( - proto.configCompleteId - ) - + MeshProtos.FromRadio.PACKET_FIELD_NUMBER -> handleReceivedMeshPacket(proto.packet) + MeshProtos.FromRadio.CONFIG_COMPLETE_ID_FIELD_NUMBER -> handleConfigComplete(proto.configCompleteId) MeshProtos.FromRadio.MY_INFO_FIELD_NUMBER -> handleMyInfo(proto.myInfo) - MeshProtos.FromRadio.NODE_INFO_FIELD_NUMBER -> handleNodeInfo(proto.nodeInfo) - - // MeshProtos.FromRadio.RADIO_FIELD_NUMBER -> handleDeviceConfig(proto.radio) - + MeshProtos.FromRadio.CONFIG_FIELD_NUMBER -> handleDeviceConfig(proto.config) else -> errormsg("Unexpected FromRadio variant") } } catch (ex: InvalidProtocolBufferException) { @@ -1150,6 +1143,18 @@ class MeshService : Service(), Logging { /// Used to make sure we never get foold by old BLE packets private var configNonce = 1 + private fun handleDeviceConfig(config: ConfigProtos.Config) { + debug("Received config ${config.toOneLineString()}") + val packetToSave = Packet( + UUID.randomUUID().toString(), + "Config ${config.payloadVariantCase}", + System.currentTimeMillis(), + config.toString() + ) + insertPacket(packetToSave) + setLocalConfig(config) + } + /** * Convert a protobuf NodeInfo into our model objects and update our node DB */ @@ -1276,9 +1281,7 @@ class MeshService : Service(), Logging { regenMyNodeInfo() // We'll need to get a new set of channels and settings now - serviceScope.handledLaunch { - localConfigRepository.clearLocalConfig() - } + clearLocalConfig() // prefill the channel array with null channels channels = fixupChannelList(listOf()) @@ -1384,9 +1387,9 @@ class MeshService : Service(), Logging { if (deviceVersion < minFirmwareVersion) { info("Device firmware is too old, faking config so firmware update can occur") + clearLocalConfig() onHasSettings() - } else - requestDeviceConfig() + } else requestChannel(0) // Now start reading channels } } else warn("Ignoring stale config complete")