mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: traceroute log (#1348)
This commit is contained in:
parent
a3b4b70db9
commit
a557bff3d7
6 changed files with 245 additions and 8 deletions
|
|
@ -3,8 +3,10 @@ package com.geeksville.mesh.model
|
|||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.geeksville.mesh.MeshProtos.MeshPacket
|
||||
import com.geeksville.mesh.Portnums.PortNum
|
||||
import com.geeksville.mesh.TelemetryProtos.Telemetry
|
||||
import com.geeksville.mesh.database.MeshLogRepository
|
||||
import com.geeksville.mesh.database.entity.MeshLog
|
||||
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
|
|
@ -16,9 +18,11 @@ import kotlinx.coroutines.flow.stateIn
|
|||
import javax.inject.Inject
|
||||
|
||||
data class MetricsState(
|
||||
val isManaged: Boolean = true,
|
||||
val deviceMetrics: List<Telemetry> = emptyList(),
|
||||
val environmentMetrics: List<Telemetry> = emptyList(),
|
||||
val signalMetrics: List<MeshPacket> = emptyList(),
|
||||
val hasTracerouteLogs: Boolean = false,
|
||||
val environmentDisplayFahrenheit: Boolean = false,
|
||||
) {
|
||||
fun hasDeviceMetrics() = deviceMetrics.isNotEmpty()
|
||||
|
|
@ -30,35 +34,72 @@ data class MetricsState(
|
|||
}
|
||||
}
|
||||
|
||||
data class TracerouteLogState(
|
||||
val requests: List<MeshLog> = emptyList(),
|
||||
val results: List<MeshPacket> = emptyList(),
|
||||
) {
|
||||
companion object {
|
||||
val Empty = TracerouteLogState()
|
||||
}
|
||||
}
|
||||
|
||||
@HiltViewModel
|
||||
class MetricsViewModel @Inject constructor(
|
||||
meshLogRepository: MeshLogRepository,
|
||||
radioConfigRepository: RadioConfigRepository,
|
||||
private val radioConfigRepository: RadioConfigRepository,
|
||||
) : ViewModel() {
|
||||
private val destNum = MutableStateFlow(0)
|
||||
|
||||
private fun MeshPacket.hasValidSignal(): Boolean =
|
||||
rxTime > 0 && (rxSnr != 0f && rxRssi != 0) && (hopStart > 0 && hopStart - hopLimit == 0)
|
||||
|
||||
private fun MeshLog.hasValidTraceroute(): Boolean = with(fromRadio.packet) {
|
||||
hasDecoded() && decoded.wantResponse && from == 0 && to == destNum.value
|
||||
}
|
||||
|
||||
fun getUser(nodeNum: Int) = radioConfigRepository.getUser(nodeNum)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val tracerouteState = destNum.flatMapLatest { destNum ->
|
||||
combine(
|
||||
meshLogRepository.getLogsFrom(nodeNum = 0, PortNum.TRACEROUTE_APP_VALUE),
|
||||
meshLogRepository.getMeshPacketsFrom(destNum),
|
||||
) { request, response ->
|
||||
val test = request.filter { it.hasValidTraceroute() }
|
||||
TracerouteLogState(
|
||||
requests = test,
|
||||
results = response,
|
||||
)
|
||||
}
|
||||
}.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = WhileSubscribed(stopTimeoutMillis = 5000L),
|
||||
initialValue = TracerouteLogState.Empty,
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
val state = destNum.flatMapLatest { destNum ->
|
||||
combine(
|
||||
meshLogRepository.getTelemetryFrom(destNum),
|
||||
meshLogRepository.getMeshPacketsFrom(destNum),
|
||||
radioConfigRepository.moduleConfigFlow,
|
||||
) { telemetry, meshPackets, config ->
|
||||
meshLogRepository.getLogsFrom(nodeNum = 0, PortNum.TRACEROUTE_APP_VALUE),
|
||||
radioConfigRepository.deviceProfileFlow,
|
||||
) { telemetry, meshPackets, traceroute, profile ->
|
||||
val moduleConfig = profile.moduleConfig
|
||||
MetricsState(
|
||||
isManaged = profile.config.security.isManaged,
|
||||
deviceMetrics = telemetry.filter { it.hasDeviceMetrics() },
|
||||
environmentMetrics = telemetry.filter {
|
||||
it.hasEnvironmentMetrics() && it.environmentMetrics.relativeHumidity >= 0f
|
||||
},
|
||||
signalMetrics = meshPackets.filter { it.hasValidSignal() },
|
||||
environmentDisplayFahrenheit = config.telemetry.environmentDisplayFahrenheit,
|
||||
hasTracerouteLogs = traceroute.any { it.hasValidTraceroute() },
|
||||
environmentDisplayFahrenheit = moduleConfig.telemetry.environmentDisplayFahrenheit,
|
||||
)
|
||||
}
|
||||
}.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = WhileSubscribed(),
|
||||
started = WhileSubscribed(stopTimeoutMillis = 5000L),
|
||||
initialValue = MetricsState.Empty,
|
||||
)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue