feat(mqtt): enhance periodic neighbors publishing with refresh logic
Updated the MQTT neighbors functionality to include a two-stage refresh process for periodic publishing. The first stage performs a 60-second zero-hop neighbor refresh, followed by querying the refreshed table for scopes before publishing. This change improves the accuracy of neighbor data and ensures timely updates in the network. Documentation has been updated to reflect these changes.
This commit is contained in:
1 parent
144e8f2399
commit
df13d181b2
4 files changed
+35
-4
No files matched your search
@@ -511,6 +511,8 @@ Minimal raw packet data for map integration.
|
||||
### Neighbors Topic: `meshcore/{IATA}/{DEVICE_PUBLIC_KEY}/neighbors`
|
||||
Cached zero-hop repeater neighbors with SNR, last-heard age, and flood-allowed scopes (retained, QoS 1). Published on `discover.scopes` or periodically when `mqtt.neighbors` is enabled (PSRAM observer builds only).
|
||||
|
||||
Periodic publishing first runs a 60-second zero-hop neighbor refresh equivalent to `discover.neighbors`, then queries the refreshed table for scopes and publishes when the scope-query phase completes. Manual `discover.scopes` remains a one-shot query of the current cache.
|
||||
|
||||
**Note**: `{DEVICE_PUBLIC_KEY}` is the device's public key in hexadecimal format (64 characters).
|
||||
|
||||
## JSON Message Formats
|
||||
|
||||
@@ -1127,6 +1127,8 @@ Requires WiFi connected and the MQTT bridge running.
|
||||
|
||||
**Note:** Requires PSRAM. Publishes to `meshcore/{IATA}/{device}/neighbors` (retained, QoS 1). Use `discover.scopes` for a one-shot publish.
|
||||
|
||||
Automatic publishes run in two stages: first the repeater performs the same 60-second zero-hop refresh as `discover.neighbors`, then it queries the refreshed neighbors for scopes and publishes after those queries complete.
|
||||
|
||||
---
|
||||
|
||||
#### View or change the speed of the bridge (RS-232 only)
|
||||
|
||||
@@ -1035,6 +1035,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
neighbor_discover_count = 0;
|
||||
neighbor_discover_active = false;
|
||||
neighbor_table_refresh_active = false;
|
||||
neighbor_discover_until = 0;
|
||||
next_neighbors_publish = 0;
|
||||
self_scopes_buf[0] = 0;
|
||||
@@ -1530,16 +1531,41 @@ void MyMesh::loop() {
|
||||
#endif
|
||||
|
||||
#if defined(WITH_MQTT_NEIGHBORS)
|
||||
bool periodic_neighbors_enabled = _cli.getObserverPrefs()->mqtt_neighbors_enabled;
|
||||
if (neighbor_discover_active) {
|
||||
loopNeighborDiscover();
|
||||
} else if (_cli.getObserverPrefs()->mqtt_neighbors_enabled && bridge && bridge->isRunning()) {
|
||||
if (next_neighbors_publish == 0 ||
|
||||
(next_neighbors_publish != 0 && millisHasNowPassed(next_neighbors_publish))) {
|
||||
} else if (neighbor_table_refresh_active) {
|
||||
if (!periodic_neighbors_enabled) {
|
||||
// Turning periodic publishing off cancels a refresh that has not yet
|
||||
// reached the scope-query/publish phase. Re-enabling starts fresh.
|
||||
neighbor_table_refresh_active = false;
|
||||
pending_discover_tag = 0;
|
||||
next_neighbors_publish = 0;
|
||||
} else if (pending_discover_tag == 0 || millisHasNowPassed(pending_discover_until)) {
|
||||
// The zero-hop discover.neighbors response window is complete. Freeze the
|
||||
// refreshed table into the scope-query overlay, then publish when those
|
||||
// per-neighbor requests finish.
|
||||
pending_discover_tag = 0;
|
||||
neighbor_table_refresh_active = false;
|
||||
char tmp_reply[80];
|
||||
if (startNeighborDiscover(tmp_reply)) {
|
||||
MESH_DEBUG_PRINTLN("%s", tmp_reply);
|
||||
MESH_DEBUG_PRINTLN("MQTT periodic %s", tmp_reply);
|
||||
} else {
|
||||
// Avoid retrying a failed refresh on every loop iteration.
|
||||
next_neighbors_publish = futureMillis(_cli.getObserverPrefs()->mqtt_neighbors_interval);
|
||||
MESH_DEBUG_PRINTLN("MQTT periodic neighbor scope discovery failed: %s", tmp_reply);
|
||||
}
|
||||
}
|
||||
} else if (periodic_neighbors_enabled && bridge && bridge->isRunning()) {
|
||||
if (next_neighbors_publish == 0 ||
|
||||
(next_neighbors_publish != 0 && millisHasNowPassed(next_neighbors_publish))) {
|
||||
// Refresh the zero-hop neighbor cache first (the same operation as the
|
||||
// discover.neighbors CLI command). Scope queries begin after its 60-second
|
||||
// collection window instead of publishing a stale cached table.
|
||||
sendNodeDiscoverReq();
|
||||
neighbor_table_refresh_active = true;
|
||||
MESH_DEBUG_PRINTLN("MQTT periodic neighbor table refresh started");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -152,6 +152,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
NeighborDiscoverEntry neighbor_discover[MAX_NEIGHBOURS];
|
||||
uint8_t neighbor_discover_count;
|
||||
bool neighbor_discover_active;
|
||||
bool neighbor_table_refresh_active;
|
||||
unsigned long neighbor_discover_until;
|
||||
unsigned long next_neighbors_publish;
|
||||
char self_scopes_buf[96];
|
||||
|
||||
Reference in new issue
Block a user