kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
refactor: move `tracerouteResponse` logic to service
rodzic
e887336da3
commit
cf239e3634
|
@ -19,7 +19,6 @@ import com.geeksville.mesh.ConfigProtos.Config
|
|||
import com.geeksville.mesh.database.MeshLogRepository
|
||||
import com.geeksville.mesh.database.QuickChatActionRepository
|
||||
import com.geeksville.mesh.database.entity.Packet
|
||||
import com.geeksville.mesh.database.entity.MeshLog
|
||||
import com.geeksville.mesh.database.entity.QuickChatAction
|
||||
import com.geeksville.mesh.LocalOnlyProtos.LocalConfig
|
||||
import com.geeksville.mesh.LocalOnlyProtos.LocalModuleConfig
|
||||
|
@ -40,7 +39,6 @@ import kotlinx.coroutines.flow.flatMapLatest
|
|||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.BufferedWriter
|
||||
|
@ -157,8 +155,6 @@ class UIViewModel @Inject constructor(
|
|||
val myNodeInfo: StateFlow<MyNodeInfo?> get() = nodeDB.myNodeInfo
|
||||
val ourNodeInfo: StateFlow<NodeInfo?> get() = nodeDB.ourNodeInfo
|
||||
|
||||
private val requestIds = MutableStateFlow<HashMap<Int, Boolean>>(hashMapOf())
|
||||
|
||||
private val _snackbarText = MutableLiveData<Any?>(null)
|
||||
val snackbarText: LiveData<Any?> get() = _snackbarText
|
||||
|
||||
|
@ -193,12 +189,6 @@ class UIViewModel @Inject constructor(
|
|||
_channels.value = channelSet
|
||||
}.launchIn(viewModelScope)
|
||||
|
||||
viewModelScope.launch {
|
||||
combine(meshLogRepository.getAllLogs(9), requestIds) { list, ids ->
|
||||
val unprocessed = ids.filterValues { !it }.keys.ifEmpty { return@combine emptyList() }
|
||||
list.filter { log -> log.meshPacket?.decoded?.requestId in unprocessed }
|
||||
}.collect { it.forEach(::processPacketResponse) }
|
||||
}
|
||||
debug("ViewModel created")
|
||||
}
|
||||
|
||||
|
@ -297,7 +287,6 @@ class UIViewModel @Inject constructor(
|
|||
try {
|
||||
val packetId = meshService?.packetId ?: return
|
||||
meshService?.requestTraceroute(packetId, destNum)
|
||||
requestIds.update { it.apply { put(packetId, false) } }
|
||||
} catch (ex: RemoteException) {
|
||||
errormsg("Request traceroute error: ${ex.message}")
|
||||
}
|
||||
|
@ -591,29 +580,11 @@ class UIViewModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private val _tracerouteResponse = MutableLiveData<String?>(null)
|
||||
val tracerouteResponse: LiveData<String?> get() = _tracerouteResponse
|
||||
val tracerouteResponse: LiveData<String?>
|
||||
get() = radioConfigRepository.tracerouteResponse.asLiveData()
|
||||
|
||||
fun clearTracerouteResponse() {
|
||||
_tracerouteResponse.value = null
|
||||
}
|
||||
|
||||
private fun processPacketResponse(log: MeshLog?) {
|
||||
val packet = log?.meshPacket ?: return
|
||||
val data = packet.decoded
|
||||
|
||||
if (data?.portnumValue == Portnums.PortNum.TRACEROUTE_APP_VALUE) {
|
||||
val parsed = MeshProtos.RouteDiscovery.parseFrom(data.payload)
|
||||
fun nodeName(num: Int) = nodeDB.nodesByNum[num]?.user?.longName
|
||||
?: app.getString(R.string.unknown_username)
|
||||
|
||||
_tracerouteResponse.value = buildString {
|
||||
append("${nodeName(packet.to)} --> ")
|
||||
parsed.routeList.forEach { num -> append("${nodeName(num)} --> ") }
|
||||
append(nodeName(packet.from))
|
||||
}
|
||||
requestIds.update { it.apply { put(data.requestId, true) } }
|
||||
}
|
||||
radioConfigRepository.clearTracerouteResponse()
|
||||
}
|
||||
|
||||
private val _currentTab = MutableLiveData(0)
|
||||
|
|
|
@ -28,7 +28,7 @@ import javax.inject.Inject
|
|||
*/
|
||||
class RadioConfigRepository @Inject constructor(
|
||||
private val serviceRepository: ServiceRepository,
|
||||
val nodeDB: NodeDB,
|
||||
private val nodeDB: NodeDB,
|
||||
private val channelSetRepository: ChannelSetRepository,
|
||||
private val localConfigRepository: LocalConfigRepository,
|
||||
private val moduleConfigRepository: ModuleConfigRepository,
|
||||
|
@ -39,6 +39,11 @@ class RadioConfigRepository @Inject constructor(
|
|||
val connectionState get() = serviceRepository.connectionState
|
||||
fun setConnectionState(state: ConnectionState) = serviceRepository.setConnectionState(state)
|
||||
|
||||
/**
|
||||
* Flow representing the unique userId of our node.
|
||||
*/
|
||||
val myId: StateFlow<String?> get() = nodeDB.myId
|
||||
|
||||
/**
|
||||
* Flow representing the [MyNodeInfo] database.
|
||||
*/
|
||||
|
@ -151,4 +156,14 @@ class RadioConfigRepository @Inject constructor(
|
|||
moduleConfig = localModuleConfig
|
||||
}
|
||||
}
|
||||
|
||||
val tracerouteResponse: StateFlow<String?> get() = serviceRepository.tracerouteResponse
|
||||
|
||||
fun setTracerouteResponse(value: String?) {
|
||||
serviceRepository.setTracerouteResponse(value)
|
||||
}
|
||||
|
||||
fun clearTracerouteResponse() {
|
||||
setTracerouteResponse(null)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ class MQTTRepository @Inject constructor(
|
|||
}
|
||||
|
||||
val proxyMessageFlow: Flow<MqttClientProxyMessage> = callbackFlow {
|
||||
val ownerId = radioConfigRepository.nodeDB.myId.value ?: generateClientId()
|
||||
val ownerId = radioConfigRepository.myId.value ?: generateClientId()
|
||||
val channelSet = radioConfigRepository.channelSetFlow.first()
|
||||
val mqttConfig = radioConfigRepository.moduleConfigFlow.first().mqtt
|
||||
|
||||
|
|
|
@ -420,6 +420,9 @@ class MeshService : Service(), Logging {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getLongName(num: Int) =
|
||||
nodeDBbyNodeNum[num]?.user?.longName ?: getString(R.string.unknown_username)
|
||||
|
||||
private val numNodes get() = nodeDBbyNodeNum.size
|
||||
|
||||
/**
|
||||
|
@ -700,6 +703,15 @@ class MeshService : Service(), Logging {
|
|||
updateMessageNotification(u)
|
||||
}
|
||||
|
||||
Portnums.PortNum.TRACEROUTE_APP_VALUE -> {
|
||||
val parsed = MeshProtos.RouteDiscovery.parseFrom(data.payload)
|
||||
radioConfigRepository.setTracerouteResponse(buildString {
|
||||
append("${getLongName(packet.to)} --> ")
|
||||
parsed.routeList.forEach { num -> append("${getLongName(num)} --> ") }
|
||||
append(getLongName(packet.from))
|
||||
})
|
||||
}
|
||||
|
||||
else -> debug("No custom processing needed for ${data.portnumValue}")
|
||||
}
|
||||
|
||||
|
|
|
@ -25,4 +25,15 @@ class ServiceRepository @Inject constructor() {
|
|||
fun setConnectionState(connectionState: MeshService.ConnectionState) {
|
||||
_connectionState.value = connectionState
|
||||
}
|
||||
|
||||
private val _tracerouteResponse = MutableStateFlow<String?>(null)
|
||||
val tracerouteResponse: StateFlow<String?> get() = _tracerouteResponse
|
||||
|
||||
fun setTracerouteResponse(value: String?) {
|
||||
_tracerouteResponse.value = value
|
||||
}
|
||||
|
||||
fun clearTracerouteResponse() {
|
||||
setTracerouteResponse(null)
|
||||
}
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue