create LocalConfig DataStore

This commit is contained in:
andrekir 2022-06-11 18:36:57 -03:00
parent 42755e350e
commit 54f6112908
8 changed files with 198 additions and 31 deletions

View file

@ -18,6 +18,7 @@ import com.geeksville.mesh.android.hasBackgroundPermission
import com.geeksville.mesh.database.PacketRepository
import com.geeksville.mesh.database.entity.Packet
import com.geeksville.mesh.model.DeviceVersion
import com.geeksville.mesh.repository.datastore.LocalConfigRepository
import com.geeksville.mesh.repository.location.LocationRepository
import com.geeksville.mesh.repository.radio.BluetoothInterface
import com.geeksville.mesh.repository.radio.RadioInterfaceService
@ -57,6 +58,9 @@ class MeshService : Service(), Logging {
@Inject
lateinit var locationRepository: LocationRepository
@Inject
lateinit var localConfigRepository: LocalConfigRepository
companion object : Logging {
/// Intents broadcast by MeshService
@ -242,6 +246,11 @@ class MeshService : Service(), Logging {
serviceScope.handledLaunch {
radioInterfaceService.receivedData.collect(::onReceiveFromRadio)
}
serviceScope.handledLaunch {
localConfigRepository.localConfigFlow.collect { config ->
localConfig = config
}
}
// the rest of our init will happen once we are in radioConnection.onServiceConnected
}
@ -498,7 +507,7 @@ class MeshService : Service(), Logging {
newPrefs.regionValue = curRegionValue
newConfig.lora = newPrefs.build()
sendDeviceConfig(newConfig.build())
if (localConfig.lora != newConfig.lora) sendDeviceConfig(newConfig.build())
channels = fixupChannelList(asChannels)
}
@ -691,7 +700,7 @@ class MeshService : Service(), Logging {
if (u.time == 0 && packet.rxTime != 0)
u = u.toBuilder().setTime(packet.rxTime).build()
handleReceivedTelemetry(packet.from, u, dataPacket.time)
}
}
// Handle new style routing info
Portnums.PortNum.ROUTING_APP_VALUE -> {
@ -935,6 +944,12 @@ class MeshService : Service(), Logging {
}
}
private fun setLocalConfig (config: ConfigProtos.Config) {
serviceScope.handledLaunch {
localConfigRepository.setLocalConfig(config)
}
}
private fun currentSecond() = (System.currentTimeMillis() / 1000).toInt()
@ -1261,7 +1276,9 @@ class MeshService : Service(), Logging {
regenMyNodeInfo()
// We'll need to get a new set of channels and settings now
localConfig = LocalOnlyProtos.LocalConfig.getDefaultInstance()
serviceScope.handledLaunch {
localConfigRepository.clearLocalConfig()
}
// prefill the channel array with null channels
channels = fixupChannelList(listOf<ChannelProtos.Channel>())
@ -1476,19 +1493,6 @@ class MeshService : Service(), Logging {
setLocalConfig(c)
}
/** Set our localConfig
*/
private fun setLocalConfig(config: ConfigProtos.Config) {
val builder = localConfig.toBuilder()
if (config.hasDevice()) builder.device = config.device
if (config.hasPosition()) builder.position = config.position
if (config.hasPower()) builder.power = config.power
if (config.hasWifi()) builder.wifi = config.wifi
if (config.hasDisplay()) builder.display = config.display
if (config.hasLora()) builder.lora = config.lora
localConfig = builder.build()
}
/**
* Set our owner with either the new or old API
*/