mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-21 09:31:20 -08:00
Better telemetry readings
This commit is contained in:
1 parent
0b235e6669
commit
768424d49c
15 files changed
+512
-280
No files matched your search
@@ -30,15 +30,10 @@ data class DeviceState(
|
||||
val status: TrackerStatus,
|
||||
val origin: DeviceOrigin,
|
||||
val driverName: String? = null,
|
||||
val packetsReceived: Long,
|
||||
val packetsLost: Long,
|
||||
val packetLossRecent: Float? = null,
|
||||
val samples: List<DevicePacketSample> = emptyList(),
|
||||
)
|
||||
|
||||
sealed interface DeviceActions {
|
||||
data class Update(val transform: DeviceState.() -> DeviceState) : DeviceActions
|
||||
data class PacketStats(val packetsReceived: Long, val packetsLost: Long) : DeviceActions
|
||||
}
|
||||
|
||||
typealias DeviceContext = Context<DeviceState, DeviceActions>
|
||||
@@ -48,7 +43,17 @@ class Device(
|
||||
val context: DeviceContext,
|
||||
val appContext: AppContextProvider,
|
||||
) {
|
||||
fun getStatsForWindow(windowMs: Long, now: Long = System.currentTimeMillis()): WindowedDeviceStats = computeWindowedStats(context.state.value.samples, windowMs, now)
|
||||
val packetHistory = DevicePacketHistory()
|
||||
|
||||
fun recordPacketStats(received: Int, lost: Int, at: Long = System.currentTimeMillis()) {
|
||||
packetHistory.record(DevicePacketSample(time = at, received = received, lost = lost))
|
||||
}
|
||||
|
||||
fun recordRssi(rssi: Int, at: Long = System.currentTimeMillis()) {
|
||||
packetHistory.recordRssi(DeviceRssiSample(time = at, rssi = rssi))
|
||||
}
|
||||
|
||||
fun getStatsForWindow(windowMs: Long, now: Long = System.currentTimeMillis()): WindowedDeviceStats = packetHistory.statsForWindow(windowMs, now)
|
||||
|
||||
fun startObserving() = context.observeAll(this)
|
||||
|
||||
@@ -84,9 +89,6 @@ class Device(
|
||||
boardType = BoardType.UNKNOWN,
|
||||
firmwareVersion = null,
|
||||
firmwareDate = null,
|
||||
packetsReceived = 0L,
|
||||
packetsLost = 0L,
|
||||
packetLossRecent = null,
|
||||
)
|
||||
|
||||
val context = Context.create(
|
||||
@@ -94,7 +96,6 @@ class Device(
|
||||
scope = scope,
|
||||
reducer = ::reduce,
|
||||
debugMiddleware = LoggingMiddleware(
|
||||
block = setOf(DeviceActions.PacketStats::class),
|
||||
diffStyle = DiffStyle.MULTILINE,
|
||||
),
|
||||
name = "Device[$address]",
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
package dev.slimevr.device
|
||||
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private const val MAX_SAMPLE_RETENTION_MS = 12_000L
|
||||
private const val MAX_SAMPLES = 4096
|
||||
|
||||
private const val MIN_PACKETS_FOR_LOSS = 20
|
||||
|
||||
private const val MAX_LOSS_LOOKBACK_MS = 5_000L
|
||||
|
||||
/** Fewer dropped packets than this is jitter, not an outage anyone can see in the pose */
|
||||
private const val MIN_PACKETS_FOR_GAP = 3
|
||||
|
||||
/**
|
||||
* Share of a sample that has to go missing before it reads as an outage.
|
||||
*
|
||||
* Sources deliver very different batches per sample: a datagram carries one packet, a dongle status
|
||||
* report carries a second's worth. A flat count would mark a run on the first and ordinary radio
|
||||
* jitter on the second. As a share, a run of three lost datagrams is most of its sample and still
|
||||
* counts, while a handful lost out of two hundred does not.
|
||||
*/
|
||||
private const val MIN_SHARE_FOR_GAP = 0.25f
|
||||
|
||||
data class DevicePacketSample(
|
||||
val time: Long,
|
||||
val received: Int,
|
||||
val lost: Int,
|
||||
)
|
||||
|
||||
data class DeviceRssiSample(
|
||||
val time: Long,
|
||||
val rssi: Int,
|
||||
)
|
||||
|
||||
data class WindowedDeviceStats(
|
||||
val rssiAvg: Int?,
|
||||
val rssiMin: Int?,
|
||||
val rssiMax: Int?,
|
||||
val packetLoss: Float?,
|
||||
val packetsReceived: Int?,
|
||||
val packetsLost: Int?,
|
||||
)
|
||||
|
||||
/**
|
||||
* Rolling packet telemetry for one device.
|
||||
*
|
||||
* This stays outside the device state, because it is too much data and changes to handle.
|
||||
* Since nothing really need to watch for all these changes it is easier to have it outside the device.
|
||||
*/
|
||||
class DevicePacketHistory {
|
||||
// Both series share one lock. Taking them separately would only add an ordering to get wrong
|
||||
private val lock = Any()
|
||||
|
||||
private val samples = ArrayDeque<DevicePacketSample>()
|
||||
|
||||
private val rssiSamples = ArrayDeque<DeviceRssiSample>()
|
||||
|
||||
private val mutableLatest = MutableSharedFlow<DevicePacketSample>(
|
||||
replay = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST,
|
||||
)
|
||||
|
||||
/** One emission per packet, for consumers measuring the gap between two of them */
|
||||
val latest: SharedFlow<DevicePacketSample> = mutableLatest
|
||||
|
||||
fun record(sample: DevicePacketSample) {
|
||||
synchronized(lock) {
|
||||
samples.addLast(sample)
|
||||
// Age bounds the window, the count bounds a device reporting faster than expected
|
||||
val cutoff = sample.time - MAX_SAMPLE_RETENTION_MS
|
||||
while (samples.size > 1 && (samples.first().time < cutoff || samples.size > MAX_SAMPLES)) {
|
||||
samples.removeFirst()
|
||||
}
|
||||
}
|
||||
mutableLatest.tryEmit(sample)
|
||||
}
|
||||
|
||||
fun recordRssi(sample: DeviceRssiSample) {
|
||||
synchronized(lock) {
|
||||
if (rssiSamples.lastOrNull()?.rssi == sample.rssi) return
|
||||
rssiSamples.addLast(sample)
|
||||
val cutoff = sample.time - MAX_SAMPLE_RETENTION_MS
|
||||
while (rssiSamples.size > 1 && (rssiSamples.first().time < cutoff || rssiSamples.size > MAX_SAMPLES)) {
|
||||
rssiSamples.removeFirst()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun statsForWindow(windowMs: Long, now: Long): WindowedDeviceStats = synchronized(lock) {
|
||||
computeWindowedStats(samples, rssiSamples, windowMs, now)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sums the packet increments falling in [windowMs] and reduces the signal strength readings over
|
||||
* the same span to an average and a range.
|
||||
*/
|
||||
fun computeWindowedStats(
|
||||
packetSamples: List<DevicePacketSample>,
|
||||
rssiSamples: List<DeviceRssiSample>,
|
||||
windowMs: Long,
|
||||
now: Long = System.currentTimeMillis(),
|
||||
): WindowedDeviceStats {
|
||||
val cutoff = now - windowMs
|
||||
val lookbackCutoff = now - maxOf(windowMs, MAX_LOSS_LOOKBACK_MS)
|
||||
var received = 0L
|
||||
var lost = 0L
|
||||
|
||||
for (index in packetSamples.indices.reversed()) {
|
||||
val sample = packetSamples[index]
|
||||
if (sample.time < lookbackCutoff) break
|
||||
if (sample.time < cutoff && received + lost >= MIN_PACKETS_FOR_LOSS) break
|
||||
received += sample.received
|
||||
lost += sample.lost
|
||||
}
|
||||
|
||||
val total = received + lost
|
||||
val packetLoss = if (total >= MIN_PACKETS_FOR_LOSS) lost.toFloat() / total.toFloat() else null
|
||||
|
||||
var rssiMin = Int.MAX_VALUE
|
||||
var rssiMax = Int.MIN_VALUE
|
||||
var weightedSum = 0.0
|
||||
var weightTotal = 0L
|
||||
val latestRssi = rssiSamples.lastOrNull()?.rssi
|
||||
|
||||
var nextTime = now
|
||||
for (index in rssiSamples.indices.reversed()) {
|
||||
val sample = rssiSamples[index]
|
||||
val to = minOf(nextTime, now)
|
||||
nextTime = sample.time
|
||||
|
||||
if (to <= cutoff) break
|
||||
|
||||
val from = maxOf(sample.time, cutoff)
|
||||
if (to <= from) continue
|
||||
|
||||
if (sample.rssi < rssiMin) rssiMin = sample.rssi
|
||||
if (sample.rssi > rssiMax) rssiMax = sample.rssi
|
||||
weightedSum += sample.rssi.toDouble() * (to - from)
|
||||
weightTotal += to - from
|
||||
}
|
||||
|
||||
val measured = weightTotal > 0
|
||||
return WindowedDeviceStats(
|
||||
rssiAvg = if (measured) (weightedSum / weightTotal).roundToInt() else latestRssi,
|
||||
rssiMin = if (measured) rssiMin else latestRssi,
|
||||
rssiMax = if (measured) rssiMax else latestRssi,
|
||||
packetLoss = packetLoss,
|
||||
packetsReceived = if (packetLoss != null) received.toInt() else null,
|
||||
packetsLost = if (packetLoss != null) lost.toInt() else null,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a sample lost enough, and a large enough share of itself, to read as an outage rather
|
||||
* than as the ordinary jitter of a radio link.
|
||||
*/
|
||||
fun isOutage(sample: DevicePacketSample): Boolean {
|
||||
if (sample.lost < MIN_PACKETS_FOR_GAP) return false
|
||||
val observed = sample.received + sample.lost
|
||||
return observed > 0 && sample.lost.toFloat() / observed.toFloat() >= MIN_SHARE_FOR_GAP
|
||||
}
|
||||
@@ -1,34 +1,5 @@
|
||||
package dev.slimevr.device
|
||||
|
||||
fun reduce(state: DeviceState, action: DeviceActions): DeviceState {
|
||||
var nextState = when (action) {
|
||||
is DeviceActions.Update -> action.transform(state)
|
||||
|
||||
is DeviceActions.PacketStats -> state.copy(
|
||||
packetsReceived = action.packetsReceived,
|
||||
packetsLost = action.packetsLost,
|
||||
)
|
||||
}
|
||||
|
||||
if (nextState.signalStrength == null && nextState.packetsReceived == 0L && nextState.packetsLost == 0L) {
|
||||
return nextState
|
||||
}
|
||||
|
||||
val prevSample = nextState.samples.lastOrNull()
|
||||
if (prevSample == null ||
|
||||
prevSample.rssi != nextState.signalStrength ||
|
||||
prevSample.packetsReceived != nextState.packetsReceived ||
|
||||
prevSample.packetsLost != nextState.packetsLost
|
||||
) {
|
||||
val now = System.currentTimeMillis()
|
||||
val sample = DevicePacketSample(
|
||||
time = now,
|
||||
rssi = nextState.signalStrength,
|
||||
packetsReceived = nextState.packetsReceived,
|
||||
packetsLost = nextState.packetsLost,
|
||||
)
|
||||
nextState = nextState.copy(samples = trimSamples(nextState.samples + sample, now))
|
||||
}
|
||||
|
||||
return nextState
|
||||
fun reduce(state: DeviceState, action: DeviceActions): DeviceState = when (action) {
|
||||
is DeviceActions.Update -> action.transform(state)
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package dev.slimevr.device
|
||||
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
data class DevicePacketSample(
|
||||
val time: Long,
|
||||
val rssi: Int?,
|
||||
val packetsReceived: Long?,
|
||||
val packetsLost: Long?,
|
||||
)
|
||||
|
||||
data class WindowedDeviceStats(
|
||||
val rssiAvg: Int?,
|
||||
val rssiMin: Int?,
|
||||
val rssiMax: Int?,
|
||||
val packetLoss: Float?,
|
||||
)
|
||||
|
||||
private const val MAX_SAMPLE_RETENTION_MS = 5_000L
|
||||
private const val MIN_SAMPLES_FOR_LOSS = 20
|
||||
|
||||
fun trimSamples(samples: List<DevicePacketSample>, now: Long = System.currentTimeMillis()): List<DevicePacketSample> {
|
||||
val cutoff = now - MAX_SAMPLE_RETENTION_MS
|
||||
val idx = samples.indexOfFirst { it.time >= cutoff }
|
||||
return if (idx <= 0) samples else samples.subList(idx, samples.size)
|
||||
}
|
||||
|
||||
fun computeWindowedStats(
|
||||
samples: List<DevicePacketSample>,
|
||||
windowMs: Long = 1000L,
|
||||
now: Long = System.currentTimeMillis(),
|
||||
): WindowedDeviceStats {
|
||||
if (samples.isEmpty()) {
|
||||
return WindowedDeviceStats(null, null, null, null)
|
||||
}
|
||||
|
||||
val cutoff = now - windowMs
|
||||
var windowSamples = samples.filter { it.time >= cutoff }
|
||||
|
||||
if (windowSamples.isEmpty()) {
|
||||
windowSamples = listOf(samples.last())
|
||||
}
|
||||
|
||||
val rssiValues = windowSamples.mapNotNull { it.rssi }
|
||||
val rssiAvg: Int?
|
||||
val rssiMin: Int?
|
||||
val rssiMax: Int?
|
||||
|
||||
if (rssiValues.isNotEmpty()) {
|
||||
rssiAvg = rssiValues.average().roundToInt()
|
||||
rssiMin = rssiValues.minOrNull()
|
||||
rssiMax = rssiValues.maxOrNull()
|
||||
} else {
|
||||
val latestRssi = samples.lastOrNull { it.rssi != null }?.rssi
|
||||
rssiAvg = latestRssi
|
||||
rssiMin = latestRssi
|
||||
rssiMax = latestRssi
|
||||
}
|
||||
|
||||
var lossSamples = samples.filter { it.packetsReceived != null && it.packetsLost != null }
|
||||
val recentLossSamples = lossSamples.filter { it.time >= cutoff }
|
||||
if (recentLossSamples.size >= MIN_SAMPLES_FOR_LOSS) {
|
||||
lossSamples = recentLossSamples
|
||||
} else if (lossSamples.size > MIN_SAMPLES_FOR_LOSS) {
|
||||
lossSamples = lossSamples.takeLast(MIN_SAMPLES_FOR_LOSS)
|
||||
}
|
||||
|
||||
val packetLoss: Float? = if (lossSamples.size >= 2) {
|
||||
val first = lossSamples.first()
|
||||
val last = lossSamples.last()
|
||||
val dRec = last.packetsReceived!! - first.packetsReceived!!
|
||||
val dLost = last.packetsLost!! - first.packetsLost!!
|
||||
val total = dRec + dLost
|
||||
if (total > 0) {
|
||||
(dLost.toFloat() / total.toFloat()).coerceIn(0f, 1f)
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
return WindowedDeviceStats(
|
||||
rssiAvg = rssiAvg,
|
||||
rssiMin = rssiMin,
|
||||
rssiMax = rssiMax,
|
||||
packetLoss = packetLoss,
|
||||
)
|
||||
}
|
||||
@@ -88,6 +88,7 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
)
|
||||
},
|
||||
)
|
||||
device.recordRssi(packet.rssi)
|
||||
|
||||
val tracker = receiver.getTracker(packet.hidId)
|
||||
?: receiver.appContext.server.context.state.value.trackers.values
|
||||
@@ -141,23 +142,25 @@ class HIDRotationBehaviour : HIDReceiverBehaviour {
|
||||
class HIDBatteryBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.on<HIDRotationBattery> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@on
|
||||
device.context.dispatch(
|
||||
DeviceActions.Update {
|
||||
copy(batteryLevel = packet.batteryLevel, batteryVoltage = packet.batteryVoltage, signalStrength = packet.rssi)
|
||||
},
|
||||
)
|
||||
device.recordRssi(packet.rssi)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.packetEvents.on<HIDRotationButton> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(signalStrength = packet.rssi) },
|
||||
)
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@on
|
||||
device.context.dispatch(DeviceActions.Update { copy(signalStrength = packet.rssi) })
|
||||
device.recordRssi(packet.rssi)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.packetEvents.on<HIDData> { packet ->
|
||||
receiver.getDevice(packet.hidId)?.context?.dispatch(
|
||||
DeviceActions.Update { copy(signalStrength = packet.rssi) },
|
||||
)
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@on
|
||||
device.context.dispatch(DeviceActions.Update { copy(signalStrength = packet.rssi) })
|
||||
device.recordRssi(packet.rssi)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.packetEvents.on<HIDRuntime> { packet ->
|
||||
@@ -246,25 +249,13 @@ class HIDStatusBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.on<HIDStatus> { packet ->
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@on
|
||||
val packetsReceived = packet.packetsReceived.toLong()
|
||||
val packetsLost = packet.packetsLost.toLong()
|
||||
|
||||
// Dispatched together so DeviceTelemetryBehaviour sees one atomic
|
||||
// state change per packet, not two separate samples microseconds apart.
|
||||
if (receiver.getTracker(packet.hidId) != null) {
|
||||
device.context.dispatch(
|
||||
DeviceActions.Update {
|
||||
copy(
|
||||
status = packet.status,
|
||||
signalStrength = packet.rssi,
|
||||
packetsReceived = packetsReceived,
|
||||
packetsLost = packetsLost,
|
||||
)
|
||||
},
|
||||
DeviceActions.Update { copy(status = packet.status, signalStrength = packet.rssi) },
|
||||
)
|
||||
} else {
|
||||
device.context.dispatch(DeviceActions.PacketStats(packetsReceived, packetsLost))
|
||||
}
|
||||
device.recordRssi(packet.rssi)
|
||||
device.recordPacketStats(received = packet.packetsReceived, lost = packet.packetsLost)
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
@@ -85,14 +85,15 @@ private fun createTracker(device: DeviceState, tracker: TrackerState, trackerMas
|
||||
origin = tracker.origin,
|
||||
)
|
||||
|
||||
private const val DEVICE_STATS_WINDOW_MS = 5_000L
|
||||
|
||||
private fun createDevice(
|
||||
device: Device,
|
||||
trackers: List<TrackerState>,
|
||||
datafeedConfig: DataFeedConfig,
|
||||
windowMs: Long,
|
||||
): DeviceData {
|
||||
val deviceState = device.context.state.value
|
||||
val stats = device.getStatsForWindow(windowMs)
|
||||
val stats = device.getStatsForWindow(DEVICE_STATS_WINDOW_MS)
|
||||
val trackerMask = datafeedConfig.dataMask?.trackerData
|
||||
|
||||
return DeviceData(
|
||||
@@ -105,8 +106,8 @@ private fun createDevice(
|
||||
rssi = stats.rssiAvg?.toShort(),
|
||||
rssiMin = stats.rssiMin?.toShort(),
|
||||
rssiMax = stats.rssiMax?.toShort(),
|
||||
packetsReceived = deviceState.packetsReceived.toInt(),
|
||||
packetsLost = deviceState.packetsLost.toInt(),
|
||||
packetsReceived = stats.packetsReceived,
|
||||
packetsLost = stats.packetsLost,
|
||||
packetLoss = stats.packetLoss,
|
||||
// TODO missing fields
|
||||
),
|
||||
@@ -167,9 +168,8 @@ fun createDatafeedFrame(
|
||||
): DataFeedMessageHeader {
|
||||
val serverState = server.context.state.value
|
||||
val trackers = serverState.trackers.values.map { it.context.state.value }
|
||||
val windowMs = datafeedConfig.minimumTimeSinceLast.toLong().coerceAtLeast(50L)
|
||||
val devices = if (datafeedConfig.dataMask?.deviceData != null) {
|
||||
serverState.devices.values.map { device -> createDevice(device, trackers, datafeedConfig, windowMs) }
|
||||
serverState.devices.values.map { device -> createDevice(device, trackers, datafeedConfig) }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
@@ -3,15 +3,12 @@ package dev.slimevr.solarxr.rpc
|
||||
import dev.slimevr.VRServer
|
||||
import dev.slimevr.device.Device
|
||||
import dev.slimevr.device.DevicePacketSample
|
||||
import dev.slimevr.device.computeWindowedStats
|
||||
import dev.slimevr.device.isOutage
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.solarxr.SolarXRBridgeBehaviour
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.filterNotNull
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
@@ -21,25 +18,15 @@ import solarxr_protocol.rpc.TelemetryGapEvent
|
||||
import solarxr_protocol.rpc.TelemetryGapResponse
|
||||
import solarxr_protocol.rpc.TelemetrySample
|
||||
import solarxr_protocol.rpc.TelemetryUpdateResponse
|
||||
import kotlin.math.max
|
||||
|
||||
private fun Device.isDeviceOk(server: VRServer): Boolean {
|
||||
val deviceState = context.state.value
|
||||
private fun isDeviceOk(device: Device, server: VRServer): Boolean {
|
||||
val deviceState = device.context.state.value
|
||||
if (deviceState.status == TrackerStatus.OK) return true
|
||||
val deviceTrackers = server.context.state.value.trackers.values
|
||||
.filter { it.context.state.value.deviceId == deviceState.id }
|
||||
return deviceTrackers.any { it.context.state.value.status == TrackerStatus.OK }
|
||||
}
|
||||
|
||||
private fun Device.getMeasuredTps(server: VRServer): Double {
|
||||
val deviceState = context.state.value
|
||||
val deviceTrackers = server.context.state.value.trackers.values
|
||||
.filter { it.context.state.value.deviceId == deviceState.id }
|
||||
return deviceTrackers
|
||||
.mapNotNull { it.context.state.value.tps.toDouble().takeIf { t -> t > 0.0 } }
|
||||
.maxOrNull() ?: 100.0
|
||||
}
|
||||
|
||||
class TelemetryBehaviour(private val server: VRServer) : SolarXRBridgeBehaviour {
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
var activeJobs: List<Job> = emptyList()
|
||||
@@ -62,24 +49,20 @@ class TelemetryBehaviour(private val server: VRServer) : SolarXRBridgeBehaviour
|
||||
|
||||
for (id in deviceIds) {
|
||||
val device = server.getDevice(id.toInt()) ?: continue
|
||||
val isOk = device.isDeviceOk(server)
|
||||
val isOk = isDeviceOk(device, server)
|
||||
|
||||
val deviceSamples = device.context.state.value.samples
|
||||
val stats = computeWindowedStats(deviceSamples, windowMs = 1000L, now = now)
|
||||
val lastSample = deviceSamples.lastOrNull()
|
||||
|
||||
val isTotalLoss = !isOk || (stats.packetLoss != null && stats.packetLoss >= 1.0f)
|
||||
val stats = device.getStatsForWindow(windowMs = 1000L, now = now)
|
||||
|
||||
samples.add(
|
||||
TelemetrySample(
|
||||
deviceId = id,
|
||||
time = now.toULong(),
|
||||
rssi = if (isTotalLoss) null else stats.rssiAvg?.toShort(),
|
||||
rssiMin = if (isTotalLoss) null else stats.rssiMin?.toShort(),
|
||||
rssiMax = if (isTotalLoss) null else stats.rssiMax?.toShort(),
|
||||
packetLossPct = if (!isOk) null else stats.packetLoss,
|
||||
packetsLost = lastSample?.packetsLost?.toInt(),
|
||||
packetsReceived = lastSample?.packetsReceived?.toInt(),
|
||||
rssi = if (isOk) stats.rssiAvg?.toShort() else null,
|
||||
rssiMin = if (isOk) stats.rssiMin?.toShort() else null,
|
||||
rssiMax = if (isOk) stats.rssiMax?.toShort() else null,
|
||||
packetLossPct = if (isOk) stats.packetLoss else null,
|
||||
packetsLost = if (isOk) stats.packetsLost else null,
|
||||
packetsReceived = if (isOk) stats.packetsReceived else null,
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -98,37 +81,28 @@ class TelemetryBehaviour(private val server: VRServer) : SolarXRBridgeBehaviour
|
||||
jobs.add(
|
||||
receiver.context.scope.launch {
|
||||
var prevSample: DevicePacketSample? = null
|
||||
device.context.state
|
||||
.map { it.samples.lastOrNull() }
|
||||
.distinctUntilChanged()
|
||||
.filterNotNull()
|
||||
device.packetHistory.latest
|
||||
.collect { cur ->
|
||||
val p = prevSample
|
||||
if (p != null && device.isDeviceOk(server)) {
|
||||
val durationMs = (cur.time - p.time).coerceAtLeast(0L)
|
||||
val hasPacketLoss = cur.packetsLost != null && p.packetsLost != null && cur.packetsLost > p.packetsLost
|
||||
|
||||
val deviceTps = device.getMeasuredTps(server)
|
||||
val expectedIntervalMs = 1000.0 / deviceTps.coerceIn(5.0, 200.0)
|
||||
val thresholdMs = max(3.0 * expectedIntervalMs, 30.0)
|
||||
|
||||
if (hasPacketLoss && durationMs.toDouble() >= thresholdMs) {
|
||||
val lostDiff = (cur.packetsLost - p.packetsLost).toUInt()
|
||||
receiver.sendRpc(
|
||||
TelemetryGapResponse(
|
||||
events = listOf(
|
||||
TelemetryGapEvent(
|
||||
deviceId = id,
|
||||
time = cur.time.toULong(),
|
||||
durationMs = durationMs.toUInt(),
|
||||
packetsLost = lostDiff,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
prevSample = cur
|
||||
if (p == null || !isDeviceOk(device, server)) return@collect
|
||||
|
||||
if (!isOutage(cur)) return@collect
|
||||
|
||||
val durationMs = (cur.time - p.time).coerceAtLeast(0L)
|
||||
|
||||
receiver.sendRpc(
|
||||
TelemetryGapResponse(
|
||||
events = listOf(
|
||||
TelemetryGapEvent(
|
||||
deviceId = id,
|
||||
time = cur.time.toULong(),
|
||||
durationMs = durationMs.toUInt(),
|
||||
packetsLost = cur.lost.toUInt(),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
@@ -45,33 +45,33 @@ class PacketBehaviour : UDPConnectionBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Packets missing between [last] and [num]. A number at or below the mark arrived, so it is not
|
||||
* loss, whether it is a duplicate or one that overtook its neighbours.
|
||||
*/
|
||||
internal fun packetsLostBetween(last: Long?, num: Long): Int = when {
|
||||
last == null || num <= last -> 0
|
||||
else -> (num - last - 1).coerceAtMost(Int.MAX_VALUE.toLong()).toInt()
|
||||
}
|
||||
|
||||
class PacketLossBehaviour : UDPConnectionBehaviour {
|
||||
override fun observe(receiver: UDPConnection) {
|
||||
var totalPacketsReceived = 0L
|
||||
var acceptedPackets = 0L
|
||||
var lastPacketCounterReset = System.currentTimeMillis()
|
||||
var lastPacketNumber = 0L
|
||||
var lastPacketNumber: Long? = null
|
||||
|
||||
// A tracker restarts its counter when it reconnects
|
||||
receiver.context.state
|
||||
.distinctUntilChangedBy { it.lastHandshake }
|
||||
.onEach { lastPacketNumber = null }
|
||||
.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.packetEvents.on<PacketEvent<UDPPacket>> { packet ->
|
||||
val num = packet.packetNumber ?: return@on
|
||||
val now = System.currentTimeMillis()
|
||||
val last = lastPacketNumber
|
||||
val lost = packetsLostBetween(last, num)
|
||||
// Highest number seen wins, so a late arrival cannot drag the mark backwards
|
||||
lastPacketNumber = if (last == null) num else maxOf(last, num)
|
||||
|
||||
if (now - lastPacketCounterReset >= 10_000L) {
|
||||
totalPacketsReceived = 0L
|
||||
acceptedPackets = 0L
|
||||
lastPacketCounterReset = now
|
||||
}
|
||||
|
||||
totalPacketsReceived++
|
||||
val accepted = num == 0L || num > lastPacketNumber
|
||||
if (accepted) {
|
||||
lastPacketNumber = num
|
||||
acceptedPackets++
|
||||
}
|
||||
|
||||
receiver.getDevice()?.context?.dispatch(
|
||||
DeviceActions.PacketStats(packetsReceived = totalPacketsReceived, packetsLost = totalPacketsReceived - acceptedPackets),
|
||||
)
|
||||
receiver.getDevice()?.recordPacketStats(received = 1, lost = lost, at = System.currentTimeMillis())
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
@@ -273,6 +273,7 @@ class DeviceStatsBehaviour : UDPConnectionBehaviour {
|
||||
receiver.packetEvents.onPacket<SignalStrength> { event ->
|
||||
val device = receiver.getDevice() ?: return@onPacket
|
||||
device.context.dispatch(DeviceActions.Update { copy(signalStrength = event.data.signal) })
|
||||
device.recordRssi(event.data.signal)
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,12 @@ fun reduce(state: UDPConnectionState, action: UDPConnectionActions): UDPConnecti
|
||||
|
||||
is UDPConnectionActions.StartPing -> state.copy(lastPing = state.lastPing.copy(startTime = action.startTime, id = action.pingId))
|
||||
|
||||
is UDPConnectionActions.Handshake -> state.copy(didHandshake = true, lastHandshake = System.currentTimeMillis(), deviceId = action.deviceId)
|
||||
is UDPConnectionActions.Handshake -> state.copy(
|
||||
didHandshake = true,
|
||||
lastHandshake = System.currentTimeMillis(),
|
||||
deviceId = action.deviceId,
|
||||
lastPacketNum = 0,
|
||||
)
|
||||
|
||||
is UDPConnectionActions.AssignTracker -> state.copy(trackerIds = state.trackerIds + action.trackerId)
|
||||
|
||||
|
||||
Reference in new issue
Block a user