Skip to content

Commit

Permalink
feat(serial): send heartbeat to keep connection alive
Browse files Browse the repository at this point in the history
closes #456
  • Loading branch information
andrekir committed Dec 14, 2024
1 parent 22fafe8 commit 193ee72
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 193ee72

Please sign in to comment.