From 193ee727b5909308d99ed7475da4a0d77dc1757e Mon Sep 17 00:00:00 2001 From: andrekir Date: Sat, 14 Dec 2024 05:59:41 -0300 Subject: [PATCH] feat(serial): send heartbeat to keep connection alive closes #456 --- .../repository/radio/RadioInterfaceService.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt index 2b629cd69..b6963a233 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/radio/RadioInterfaceService.kt @@ -23,6 +23,7 @@ import androidx.core.content.edit import androidx.lifecycle.Lifecycle import androidx.lifecycle.coroutineScope import com.geeksville.mesh.CoroutineDispatchers +import com.geeksville.mesh.MeshProtos import com.geeksville.mesh.android.BinaryLogFile import com.geeksville.mesh.android.BuildUtils import com.geeksville.mesh.android.GeeksvilleApplication @@ -110,8 +111,20 @@ class RadioInterfaceService @Inject constructor( }.launchIn(processLifecycle.coroutineScope) } - companion object : Logging { + companion object { const val DEVADDR_KEY = "devAddr2" // the new name for devaddr + private const val HEARTBEAT_INTERVAL_MILLIS = 5 * 60 * 1000L + } + + private var lastHeartbeatMillis = 0L + private fun keepAlive(now: Long) { + if (now - lastHeartbeatMillis > HEARTBEAT_INTERVAL_MILLIS) { + info("Sending ToRadio heartbeat") + val heartbeat = MeshProtos.ToRadio.newBuilder() + .setHeartbeat(MeshProtos.Heartbeat.getDefaultInstance()).build() + handleSendToRadio(heartbeat.toByteArray()) + lastHeartbeatMillis = now + } } /** @@ -184,6 +197,10 @@ class RadioInterfaceService @Inject constructor( receivedPacketsLog.flush() } + if (radioIf is SerialInterface) { + keepAlive(System.currentTimeMillis()) + } + // ignoreException { debug("FromRadio: ${MeshProtos.FromRadio.parseFrom(p)}") } processLifecycle.coroutineScope.launch(dispatchers.io) {