Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.time.Instant
*/
object DiagnosticsExporter {
/** The keys `RingBLEClient` stores in `raw_packets.decodedJSON` for each packet. */
private val TRANSPORT_KEYS = setOf("characteristic", "generation", "attempt")
private val TRANSPORT_KEYS = setOf("characteristic", "generation", "attempt", "ring")

/**
* The transport facts of one stored packet, or null if it has none. Older rows may hold decoder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,39 @@ class DiagnosticsSubscriber(
fun start(scope: CoroutineScope) {
if (job != null) return
job = scope.launch(start = CoroutineStart.UNDISPATCHED) {
PulseEventBus.events.collect { event ->
try {
record(event)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e("Diagnostics", "Could not persist diagnostic event", e)
}
// The second ring's bus too (docs/ring-comparison.md): without it an export showed
// the comparison ring's requests and none of its replies, so "it sent nothing" and
// "we dropped what it sent" looked the same. Only the packets and a few status lines
// are taken; its readings still go nowhere but `comparison_readings`.
launch(start = CoroutineStart.UNDISPATCHED) {
ComparisonBus.events.collect { event -> safely { recordComparison(event) } }
}
PulseEventBus.events.collect { event -> safely { record(event) } }
}
}

private suspend fun safely(block: suspend () -> Unit) {
try {
block()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e("Diagnostics", "Could not persist diagnostic event", e)
}
}

private suspend fun recordComparison(event: PulseEvent) {
when (event) {
// Stored like the main ring's packets: masked on export by their own family's header,
// and marked `"ring":"comparison"` in their transport facts by the client.
is PulseEvent.RawPacket -> record(event)
is PulseEvent.SyncProgress ->
log(WearableLogCategory.SYNC, WearableLogLevel.INFO, "[comparison ring] Sync: ${event.stage}", deviceType = null)
is PulseEvent.BatteryLevel ->
log(WearableLogCategory.BATTERY, WearableLogLevel.INFO, "[comparison ring] Battery ${event.percent}%")
is PulseEvent.DeviceStateChanged ->
log(WearableLogCategory.CONNECTION, WearableLogLevel.INFO, "[comparison ring] Connection state: ${event.state.name}")
else -> { /* readings stay on the comparison bus */ }
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/pulseloop/ring/RingBLEClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,8 @@ class RingBLEClient(
put("characteristic", uuid)
put("generation", forgetGeneration)
if (attempt > 0) put("attempt", attempt)
// The second ring's packets share the diagnostics table; this says whose they are.
if (role == RingRole.COMPARISON) put("ring", "comparison")
}.toString()

// MARK: Scan callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class DiagnosticsTransportMetadataTest {
assertEquals(expected, DiagnosticsExporter.transportMetadata(metadata))
}

@Test fun `the second ring's packets say so`() {
val metadata = """{"characteristic":"6e400003-b5a3-f393-e0a9-e50e24dcca9e","generation":1,"ring":"comparison"}"""
assertEquals(Json.parseToJsonElement(metadata), DiagnosticsExporter.transportMetadata(metadata))
}

@Test fun `older rows with only decoder values do not export transport metadata`() {
assertNull(DiagnosticsExporter.transportMetadata("""{"bpm":72}"""))
assertNull(DiagnosticsExporter.transportMetadata(null))
Expand Down
4 changes: 3 additions & 1 deletion app/src/test/java/com/pulseloop/mcp/McpServerSocketTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class McpServerSocketTest {
assertEquals(first, server.rebind(listOf(loopback, InetAddress.getByName("8.8.8.8")), 0))
// The address went away: nothing listens there any more.
assertTrue(server.rebind(emptyList(), 0).isEmpty())
assertFalse(runCatching { Socket().use { it.connect(first[0], 500) } }.isSuccess)
// Asked of the server, not of the port: the freed ephemeral port can be taken at once by
// another test's listener in the same run, so "nothing answers there" was a race.
assertFalse(server.isRunning())
// It came back: listening again, and answering.
val again = server.rebind(listOf(loopback), 0)
assertEquals(1, again.size)
Expand Down
Loading