Redundant methods in RadioConfigRepository (#3198)

This commit is contained in:
Phil Oliver 2025-09-25 09:57:26 -04:00 committed by GitHub
parent 8be9c38ae6
commit 8317b704ea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 97 additions and 171 deletions

View file

@ -55,6 +55,7 @@ import com.geeksville.mesh.android.hasLocationPermission
import com.geeksville.mesh.concurrent.handledLaunch
import com.geeksville.mesh.copy
import com.geeksville.mesh.database.MeshLogRepository
import com.geeksville.mesh.database.NodeRepository
import com.geeksville.mesh.database.PacketRepository
import com.geeksville.mesh.fromRadio
import com.geeksville.mesh.model.NO_DEVICE_SELECTED
@ -80,11 +81,13 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.meshtastic.core.database.entity.MeshLog
import org.meshtastic.core.database.entity.MetadataEntity
import org.meshtastic.core.database.entity.MyNodeEntity
import org.meshtastic.core.database.entity.NodeEntity
import org.meshtastic.core.database.entity.Packet
@ -144,6 +147,10 @@ class MeshService :
@Inject lateinit var radioConfigRepository: RadioConfigRepository
@Inject lateinit var serviceRepository: ServiceRepository
@Inject lateinit var nodeRepository: NodeRepository
@Inject lateinit var mqttRepository: MQTTRepository
@Inject lateinit var serviceNotifications: MeshServiceNotifications
@ -215,7 +222,7 @@ class MeshService :
private val clientPackages = mutableMapOf<String, String>()
private val serviceBroadcasts =
MeshServiceBroadcasts(this, clientPackages) {
connectionState.also { radioConfigRepository.setConnectionState(it) }
connectionState.also { serviceRepository.setConnectionState(it) }
}
private val packetHandler: PacketHandler by lazy {
PacketHandler(
@ -338,8 +345,8 @@ class MeshService :
radioConfigRepository.localConfigFlow.onEach { localConfig = it }.launchIn(serviceScope)
radioConfigRepository.moduleConfigFlow.onEach { moduleConfig = it }.launchIn(serviceScope)
radioConfigRepository.channelSetFlow.onEach { channelSet = it }.launchIn(serviceScope)
radioConfigRepository.serviceAction.onEach(::onServiceAction).launchIn(serviceScope)
radioConfigRepository.myNodeInfo
serviceRepository.serviceAction.onEach(::onServiceAction).launchIn(serviceScope)
nodeRepository.myNodeInfo
.flatMapLatest { myNodeEntity ->
// When myNodeInfo changes, set up emissions for the "provide-location-nodeNum" pref.
if (myNodeEntity == null) {
@ -443,8 +450,8 @@ class MeshService :
private fun loadSettings() = serviceScope.handledLaunch {
discardNodeDB() // Get rid of any old state
myNodeInfo = radioConfigRepository.myNodeInfo.value
nodeDBbyNodeNum.putAll(radioConfigRepository.getNodeDBbyNum())
myNodeInfo = nodeRepository.myNodeInfo.value
nodeDBbyNodeNum.putAll(nodeRepository.getNodeDBbyNum().first())
// Note: we do not haveNodeDB = true because that means we've got a valid db from a real
// device (rather than
// this possibly stale hint)
@ -543,7 +550,7 @@ class MeshService :
}
}
private fun getUserName(num: Int): String = with(radioConfigRepository.getUser(num)) { "$longName ($shortName)" }
private fun getUserName(num: Int): String = with(nodeRepository.getUser(num)) { "$longName ($shortName)" }
private val numNodes
get() = nodeDBbyNodeNum.size
@ -569,7 +576,7 @@ class MeshService :
updateFn(info)
if (info.user.id.isNotEmpty() && haveNodeDB) {
serviceScope.handledLaunch { radioConfigRepository.upsert(info) }
serviceScope.handledLaunch { nodeRepository.upsert(info) }
}
if (withBroadcast) {
@ -834,7 +841,7 @@ class MeshService :
val u = MeshProtos.Routing.parseFrom(data.payload)
if (u.errorReason == MeshProtos.Routing.Error.DUTY_CYCLE_LIMIT) {
radioConfigRepository.setErrorMessage(getString(R.string.error_duty_cycle))
serviceRepository.setErrorMessage(getString(R.string.error_duty_cycle))
}
handleAckNak(data.requestId, fromId, u.errorReasonValue)
@ -884,7 +891,7 @@ class MeshService :
} else {
full
}
radioConfigRepository.setTracerouteResponse(response)
serviceRepository.setTracerouteResponse(response)
}
}
@ -934,7 +941,7 @@ class MeshService :
AdminProtos.AdminMessage.PayloadVariantCase.GET_DEVICE_METADATA_RESPONSE -> {
debug("Admin: received DeviceMetadata from $fromNodeNum")
serviceScope.handledLaunch {
radioConfigRepository.insertMetadata(fromNodeNum, a.getDeviceMetadataResponse)
nodeRepository.insertMetadata(MetadataEntity(fromNodeNum, a.getDeviceMetadataResponse))
}
}
@ -1188,7 +1195,7 @@ class MeshService :
)
insertMeshLog(packetToSave)
serviceScope.handledLaunch { radioConfigRepository.emitMeshPacket(packet) }
serviceScope.handledLaunch { serviceRepository.emitMeshPacket(packet) }
// Update last seen for the node that sent the packet, but also for _our node_ because
// anytime a packet
@ -1488,7 +1495,7 @@ class MeshService :
insertMeshLog(packetToSave)
setLocalConfig(config)
val configCount = localConfig.allFields.size
radioConfigRepository.setStatusMessage("Device config ($configCount / $configTotal)")
serviceRepository.setStatusMessage("Device config ($configCount / $configTotal)")
}
private fun handleModuleConfig(config: ModuleConfigProtos.ModuleConfig) {
@ -1504,7 +1511,7 @@ class MeshService :
insertMeshLog(packetToSave)
setLocalModuleConfig(config)
val moduleCount = moduleConfig.allFields.size
radioConfigRepository.setStatusMessage("Module config ($moduleCount / $moduleTotal)")
serviceRepository.setStatusMessage("Module config ($moduleCount / $moduleTotal)")
}
private fun handleChannel(ch: ChannelProtos.Channel) {
@ -1520,7 +1527,7 @@ class MeshService :
insertMeshLog(packetToSave)
if (ch.role != ChannelProtos.Channel.Role.DISABLED) updateChannelSettings(ch)
val maxChannels = myNodeInfo?.maxChannels ?: 8
radioConfigRepository.setStatusMessage("Channels (${ch.index + 1} / $maxChannels)")
serviceRepository.setStatusMessage("Channels (${ch.index + 1} / $maxChannels)")
}
/** Convert a protobuf NodeInfo into our model objects and update our node DB */
@ -1584,7 +1591,7 @@ class MeshService :
insertMeshLog(packetToSave)
newNodes.add(info)
radioConfigRepository.setStatusMessage("Nodes (${newNodes.size})")
serviceRepository.setStatusMessage("Nodes (${newNodes.size})")
}
private var rawMyNodeInfo: MeshProtos.MyNodeInfo? = null
@ -1619,7 +1626,7 @@ class MeshService :
deviceId = deviceId.toStringUtf8(),
)
}
serviceScope.handledLaunch { radioConfigRepository.insertMetadata(mi.myNodeNum, metadata) }
serviceScope.handledLaunch { nodeRepository.insertMetadata(MetadataEntity(mi.myNodeNum, metadata)) }
newMyNodeInfo = mi
}
}
@ -1693,7 +1700,7 @@ class MeshService :
private fun handleClientNotification(notification: MeshProtos.ClientNotification) {
debug("Received clientNotification ${notification.toOneLineString()}")
radioConfigRepository.setClientNotification(notification)
serviceRepository.setClientNotification(notification)
serviceNotifications.showClientNotification(notification)
// if the future for the originating request is still in the queue, complete as unsuccessful
// for now
@ -1774,7 +1781,7 @@ class MeshService :
.onEach { message ->
packetHandler.sendToRadio(ToRadio.newBuilder().apply { mqttClientProxyMessage = message })
}
.catch { throwable -> radioConfigRepository.setErrorMessage("MqttClientProxy failed: $throwable") }
.catch { throwable -> serviceRepository.setErrorMessage("MqttClientProxy failed: $throwable") }
.launchIn(serviceScope)
}
}
@ -1857,8 +1864,8 @@ class MeshService :
newNodes.clear() // Just to save RAM ;-)
serviceScope.handledLaunch {
radioConfigRepository.installMyNodeInfo(myNodeInfo!!)
radioConfigRepository.installNodeDb(nodeDBbyNodeNum.values.toList())
nodeRepository.installMyNodeInfo(myNodeInfo!!)
nodeRepository.installNodeDb(nodeDBbyNodeNum.values.toList())
}
haveNodeDB = true // we now have nodes from real hardware
@ -2058,7 +2065,7 @@ class MeshService :
fun clearDatabases() = serviceScope.handledLaunch {
debug("Clearing nodeDB")
radioConfigRepository.clearNodeDB()
nodeRepository.clearNodeDB()
}
private fun updateLastAddress(deviceAddr: String?) {