diff --git a/MQTT_IMPLEMENTATION.md b/MQTT_IMPLEMENTATION.md index dcc175029..188fc4f4c 100644 --- a/MQTT_IMPLEMENTATION.md +++ b/MQTT_IMPLEMENTATION.md @@ -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 diff --git a/docs/cli_commands.md b/docs/cli_commands.md index c740ce1ff..d8618e67c 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -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) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 1ca5adba7..c60108712 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -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 diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index a2cfc1622..bb2c66981 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -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];