Fix sleep status

This commit is contained in:
loucass003 committed 2026-10-26 23:08:01 +01:00
1 parent b13754b1e0
commit 45b8af5ff7
3 files changed
+28 -18

No files matched your search

@@ -23,8 +23,8 @@ class HIDSleepLegacyBehaviour : HIDReceiverBehaviour {
// Legacy packets 6/7 carry a 16-bit millisecond timeout: 0 = no hint, 65535 = never sleep.
fun applyLegacyTimeout(hidId: Int, timeoutMs: Int) = when (timeoutMs) {
0 -> Unit
65535 -> timers.cancelSleep(hidId)
else -> timers.scheduleSleep(hidId, timeoutMs.toLong())
65535 -> timers.clearSleepDeadline(hidId)
else -> timers.setSleepDeadline(hidId, timeoutMs.toLong())
}
receiver.packetEvents.on<HIDRotationButtonLegacy> { packet ->
@@ -8,30 +8,40 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import solarxr_protocol.datatypes.TrackerStatus
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
private val idleTimeout = 2.seconds
// How early before the announced sleep deadline the last packet may land and still count as sleep.
// Firmware refreshes the deadline about every 100 ms, the rest covers lost packets.
private val sleepDeadlineMargin = 3.seconds
/**
* Sleep + idle-watchdog tracking, shared by the legacy and v3 sleep behaviours. One instance per
* behaviour; jobs run on the receiver's context scope and are dropped by [stop] on disconnect.
*
* A tracker that goes quiet is SLEEPING only when its last packet came close to the sleep deadline
* it announced. Any other silence is TIMED_OUT.
*/
class HidSleepTimers(private val receiver: HIDReceiver) {
private val startedAt = timeSource.markNow()
private val sleepJobs = mutableMapOf<Int, Job>()
private val idleJobs = mutableMapOf<Int, Job>()
private val lastSeen = mutableMapOf<Int, Duration>()
private val sleepDeadlines = mutableMapOf<Int, Duration>()
fun cancelSleep(hidId: Int) {
sleepJobs.remove(hidId)?.cancel()
fun clearSleepDeadline(hidId: Int) {
sleepDeadlines.remove(hidId)
}
fun scheduleSleep(hidId: Int, delayMs: Long) {
cancelSleep(hidId)
sleepJobs[hidId] = receiver.context.scope.launch {
delay(delayMs)
receiver.getTracker(hidId)?.context?.dispatch(TrackerActions.SetStatus(TrackerStatus.SLEEPING))
}
fun setSleepDeadline(hidId: Int, delayMs: Long) {
sleepDeadlines[hidId] = startedAt.elapsedNow() + delayMs.milliseconds
}
private fun silenceStatus(hidId: Int): TrackerStatus {
val deadline = sleepDeadlines[hidId] ?: return TrackerStatus.TIMED_OUT
val seen = lastSeen[hidId] ?: return TrackerStatus.TIMED_OUT
return if (seen >= deadline - sleepDeadlineMargin) TrackerStatus.SLEEPING else TrackerStatus.TIMED_OUT
}
private fun armIdleTimeout(hidId: Int) {
@@ -43,22 +53,22 @@ class HidSleepTimers(private val receiver: HIDReceiver) {
delay(remaining)
remaining = (lastSeen[hidId] ?: Duration.ZERO) + idleTimeout - startedAt.elapsedNow()
}
receiver.getTracker(hidId)?.context?.dispatch(TrackerActions.SetStatus(TrackerStatus.SLEEPING))
receiver.getTracker(hidId)?.context?.dispatch(TrackerActions.SetStatus(silenceStatus(hidId)))
}
}
fun onPacket(hidId: Int) {
val tracker = receiver.getTracker(hidId) ?: return
if (tracker.context.state.value.status == TrackerStatus.SLEEPING) {
cancelSleep(hidId)
val status = tracker.context.state.value.status
if (status == TrackerStatus.SLEEPING || status == TrackerStatus.TIMED_OUT) {
clearSleepDeadline(hidId)
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
}
armIdleTimeout(hidId)
}
fun stop() {
for (job in sleepJobs.values + idleJobs.values) job.cancel()
sleepJobs.clear()
for (job in idleJobs.values) job.cancel()
idleJobs.clear()
}
}
@@ -20,9 +20,9 @@ class HIDSleepV3Behaviour : HIDReceiverBehaviour {
receiver.packetEvents.on<HIDTimeout> { packet ->
timers.onPacket(packet.hidId)
if (packet.secondsUntilTimeout == HID_TIME_UNKNOWN) {
timers.cancelSleep(packet.hidId)
timers.clearSleepDeadline(packet.hidId)
} else {
timers.scheduleSleep(packet.hidId, packet.secondsUntilTimeout * 1000)
timers.setSleepDeadline(packet.hidId, packet.secondsUntilTimeout * 1000)
}
}.launchIn(receiver.context.scope)