feat: Improve connection state broadcast and timing (#4498)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2026-02-07 12:52:35 -06:00 committed by GitHub
parent 4303bfaac4
commit fad26f1273
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 127 additions and 10 deletions

View file

@ -20,6 +20,7 @@ const val PREFIX = "com.geeksville.mesh"
const val ACTION_NODE_CHANGE = "$PREFIX.NODE_CHANGE"
const val ACTION_MESH_CONNECTED = "$PREFIX.MESH_CONNECTED"
const val ACTION_CONNECTION_CHANGED = "$PREFIX.CONNECTION_CHANGED"
const val ACTION_MESSAGE_STATUS = "$PREFIX.MESSAGE_STATUS"
fun actionReceived(portNum: String) = "$PREFIX.RECEIVED.$portNum"

View file

@ -85,6 +85,9 @@ constructor(
} else {
myNodeInfo = newMyNodeInfo
Logger.i { "myNodeInfo committed successfully" }
connectionStateHolder.setState(ConnectionState.Connected)
serviceBroadcasts.broadcastConnection()
connectionManager.onRadioConfigLoaded()
}
scope.handledLaunch {
@ -120,9 +123,7 @@ constructor(
}
nodeManager.isNodeDbReady.value = true
nodeManager.allowNodeDbWrites.value = true
connectionStateHolder.setState(ConnectionState.Connected)
serviceBroadcasts.broadcastConnection()
connectionManager.onHasSettings()
connectionManager.onNodeDbReady()
}
}

View file

@ -199,9 +199,17 @@ constructor(
packetHandler.sendToRadio(ToRadio(want_config_id = NODE_INFO_NONCE))
}
fun onHasSettings() {
fun onRadioConfigLoaded() {
commandSender.processQueuedPackets()
val myNodeNum = nodeManager.myNodeNum ?: 0
// Set time
commandSender.sendAdmin(myNodeNum) {
AdminMessage(set_time_only = (System.currentTimeMillis() / MILLISECONDS_IN_SECOND).toInt())
}
}
fun onNodeDbReady() {
// Start MQTT if enabled
scope.handledLaunch {
val moduleConfig = radioConfigRepository.moduleConfigFlow.first()
@ -219,14 +227,10 @@ constructor(
scope.handledLaunch {
val moduleConfig = radioConfigRepository.moduleConfigFlow.first()
moduleConfig.store_forward?.let {
historyManager.requestHistoryReplay("onHasSettings", myNodeNum, it, "Unknown")
historyManager.requestHistoryReplay("onNodeDbReady", myNodeNum, it, "Unknown")
}
}
// Set time
commandSender.sendAdmin(myNodeNum) {
AdminMessage(set_time_only = (System.currentTimeMillis() / MILLISECONDS_IN_SECOND).toInt())
}
updateStatusNotification()
}

View file

@ -26,6 +26,7 @@ import org.meshtastic.core.model.MessageStatus
import org.meshtastic.core.model.NodeInfo
import org.meshtastic.core.model.util.toPIIString
import org.meshtastic.core.service.ServiceRepository
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
@ -75,9 +76,22 @@ constructor(
/** Broadcast our current connection status */
fun broadcastConnection() {
val connectionState = connectionStateHolder.connectionState.value
val intent = Intent(ACTION_MESH_CONNECTED).putExtra(EXTRA_CONNECTED, connectionState.toString())
// ATAK expects a String: "CONNECTED" or "DISCONNECTED"
// It uses equalsIgnoreCase, but we'll use uppercase to be specific.
val stateStr = connectionState.toString().uppercase(Locale.ROOT)
val intent = Intent(ACTION_MESH_CONNECTED).apply { putExtra(EXTRA_CONNECTED, stateStr) }
serviceRepository.setConnectionState(connectionState)
explicitBroadcast(intent)
// Restore legacy action for other consumers (e.g. mesh_service_example)
val legacyIntent =
Intent(ACTION_CONNECTION_CHANGED).apply {
putExtra(EXTRA_CONNECTED, stateStr)
// Legacy boolean extra often expected by older implementations
putExtra("connected", connectionState == org.meshtastic.core.service.ConnectionState.Connected)
}
explicitBroadcast(legacyIntent)
}
/**