From 980d6e55a1667bf2c814f5852fc4feed2f1fca58 Mon Sep 17 00:00:00 2001 From: Jason Huebert Date: Sat, 17 Oct 2026 16:36:58 -0500 Subject: [PATCH] fix: report model, relay flag and device stats in observer status The status message omitted fields other MeshCore observers publish, so analyzer UIs showed the model as "MeshCore Repeater" and left Relay, Uptime and Noise Floor blank. Report the board name as `model`, this node's relay state as the top-level `repeat` flag, and battery/uptime/noise floor under `stats`, sourced live from the repeater through a registered callback. --- MQTT_OBSERVER.md | 10 +++++++-- examples/mqtt_observer/MQTTObserver.cpp | 19 +++++++++++++---- examples/mqtt_observer/MQTTObserver.h | 12 ++++++++++- examples/mqtt_observer/MQTTObserverFormat.cpp | 21 +++++++++++++++++-- examples/mqtt_observer/MQTTObserverFormat.h | 13 +++++++++++- examples/simple_repeater/MyMesh.cpp | 15 +++++++++++-- examples/simple_repeater/MyMesh.h | 2 ++ .../test_mqtt_observer/test_mqtt_observer.cpp | 18 +++++++++++++++- 8 files changed, 97 insertions(+), 13 deletions(-) diff --git a/MQTT_OBSERVER.md b/MQTT_OBSERVER.md index 6d9e3d27..76896313 100644 --- a/MQTT_OBSERVER.md +++ b/MQTT_OBSERVER.md @@ -314,10 +314,16 @@ Publish `status: "online"` (QoS 1, non-retained) on connect and every ```json {"status":"online","timestamp":"2026-10-16T20:05:00.123456+00:00","origin":"Roof Repeater", - "origin_id":"","model":"MeshCore Repeater","firmware_version":"...", - "radio":"869.525,62.5,8,5","client_version":"meshcore-jhuebert/"} + "origin_id":"","model":"Station G2","firmware_version":"...", + "radio":"869.525,62.5,8,5","client_version":"meshcore-jhuebert/","repeat":true, + "stats":{"battery_mv":4050,"uptime_secs":86400,"noise_floor":-110}} ``` +`model` is the board name (`getManufacturerName()`, e.g. `Station G2`) and `repeat` is +this node's relay state, so analyzers that key off `repeat` show the node as a +repeater. `stats` carries the live device readings: `battery_mv` (0 when the board +reports no battery), `uptime_secs`, and `noise_floor` (dBm). + Status messages report online on connect and periodically. The observer does not publish an offline message when a broker connection drops; consumers should expire stale status based on their own timeout or broker presence information. diff --git a/examples/mqtt_observer/MQTTObserver.cpp b/examples/mqtt_observer/MQTTObserver.cpp index 468cc463..3ca4a05f 100644 --- a/examples/mqtt_observer/MQTTObserver.cpp +++ b/examples/mqtt_observer/MQTTObserver.cpp @@ -102,7 +102,8 @@ MQTTObserver::BrokerRuntime::BrokerRuntime() state(STATE_OFF) {} MQTTObserver::MQTTObserver() - : _fs(nullptr), _identity(nullptr), _origin_source(nullptr), _origin{}, _firmware{}, + : _fs(nullptr), _identity(nullptr), _origin_source(nullptr), _origin{}, _model{}, + _firmware{}, _status_stats_fn(nullptr), _status_stats_ctx(nullptr), _radio_freq(0), _radio_bw(0), _radio_sf(0), _radio_cr(0), _config{}, _runtime_config{}, _work_config{}, _io_config{}, _save_record{}, _load_record{}, _config_mux(portMUX_INITIALIZER_UNLOCKED), _queue_mux(portMUX_INITIALIZER_UNLOCKED), @@ -117,7 +118,8 @@ MQTTObserver::MQTTObserver() } void MQTTObserver::begin(FILESYSTEM* fs, mesh::LocalIdentity* identity, const char* origin, - const char* firmware, float freq, float bw, uint8_t sf, uint8_t cr) { + const char* model, const char* firmware, float freq, float bw, + uint8_t sf, uint8_t cr) { _fs = fs; _identity = identity; _origin_source = origin; @@ -135,6 +137,8 @@ void MQTTObserver::begin(FILESYSTEM* fs, mesh::LocalIdentity* identity, const ch _radio_cr = cr; strncpy(_origin, origin ? origin : "", sizeof(_origin) - 1); _origin[sizeof(_origin) - 1] = 0; + strncpy(_model, model && *model ? model : "unknown", sizeof(_model) - 1); + _model[sizeof(_model) - 1] = 0; strncpy(_firmware, firmware && *firmware ? firmware : "unknown", sizeof(_firmware) - 1); _firmware[sizeof(_firmware) - 1] = 0; mesh::Utils::toHex(_device_id, identity->pub_key, PUB_KEY_SIZE); @@ -159,6 +163,11 @@ void MQTTObserver::begin(FILESYSTEM* fs, mesh::LocalIdentity* identity, const ch _state.store(STATE_ERROR, std::memory_order_relaxed); } +void MQTTObserver::setStatusStatsFn(StatusStatsFn fn, void* context) { + _status_stats_fn = fn; + _status_stats_ctx = context; +} + void MQTTObserver::taskEntry(void* context) { static_cast(context)->worker(); vTaskDelete(nullptr); @@ -743,8 +752,10 @@ bool MQTTObserver::buildStatusMessage(char* out, size_t capacity, size_t& writte char timestamp[40]; if (!MQTTObserverFormat::formatTimestamp(now.tv_sec, now.tv_usec, timestamp, sizeof(timestamp))) return false; - return MQTTObserverFormat::buildStatus(origin, _device_id, "MeshCore Repeater", - _firmware, radio, timestamp, + MQTTObserverFormat::StatusStats stats{}; + if (_status_stats_fn) _status_stats_fn(_status_stats_ctx, stats); + return MQTTObserverFormat::buildStatus(origin, _device_id, _model, + _firmware, radio, timestamp, stats, out, capacity, written); } diff --git a/examples/mqtt_observer/MQTTObserver.h b/examples/mqtt_observer/MQTTObserver.h index 9d1e500e..d0fc0534 100644 --- a/examples/mqtt_observer/MQTTObserver.h +++ b/examples/mqtt_observer/MQTTObserver.h @@ -12,6 +12,7 @@ #include "../simple_repeater/PersistUtil.h" #include "MQTTObserverConfig.h" +#include "MQTTObserverFormat.h" #include "MQTTObserverPolicy.h" #include "MQTTObserverRecord.h" @@ -63,11 +64,19 @@ class MQTTObserver { static const uint32_t kMinimumValidTime = 1735689600UL; static const uint32_t kJwtLifetimeSeconds = 86400; + // Supplies live device stats for the periodic status message; the repeater + // registers this so the observer reports battery/uptime/noise floor and the + // relay flag like other MeshCore observers. + using StatusStatsFn = void (*)(void* context, MQTTObserverFormat::StatusStats& stats); + FILESYSTEM* _fs; mesh::LocalIdentity* _identity; const char* _origin_source; char _origin[33]; + char _model[33]; char _firmware[64]; + StatusStatsFn _status_stats_fn; + void* _status_stats_ctx; float _radio_freq; float _radio_bw; uint8_t _radio_sf; @@ -129,7 +138,8 @@ public: MQTTObserver(); void begin(FILESYSTEM* fs, mesh::LocalIdentity* identity, const char* origin, - const char* firmware, float freq, float bw, uint8_t sf, uint8_t cr); + const char* model, const char* firmware, float freq, float bw, uint8_t sf, uint8_t cr); + void setStatusStatsFn(StatusStatsFn fn, void* context); void loop(); void setRadio(float freq, float bw, uint8_t sf, uint8_t cr); bool needsAwake() const; diff --git a/examples/mqtt_observer/MQTTObserverFormat.cpp b/examples/mqtt_observer/MQTTObserverFormat.cpp index da6a25cf..8eab8a25 100644 --- a/examples/mqtt_observer/MQTTObserverFormat.cpp +++ b/examples/mqtt_observer/MQTTObserverFormat.cpp @@ -83,6 +83,14 @@ void appendKey(JsonWriter& writer, const char* key, const char* value, bool comm writer.string(value); } +// Emits "key":raw where raw is a pre-rendered literal (a number or true/false). +void appendRaw(JsonWriter& writer, const char* key, const char* raw, bool comma = true) { + if (comma) writer.append(","); + writer.string(key); + writer.append(":"); + writer.append(raw); +} + } // namespace bool appendEscaped(const char* text, char* out, size_t capacity, size_t& length) { @@ -220,7 +228,7 @@ bool buildRaw(const char* origin, const char* origin_id, const char* timestamp, bool buildStatus(const char* origin, const char* origin_id, const char* model, const char* firmware, const char* radio, const char* timestamp, - char* out, size_t capacity, size_t& written) { + const StatusStats& stats, char* out, size_t capacity, size_t& written) { if (!origin || !origin_id || !model || !firmware || !radio || !timestamp) return false; JsonWriter writer(out, capacity); writer.append("{"); @@ -234,7 +242,16 @@ bool buildStatus(const char* origin, const char* origin_id, const char* model, char client_version[96]; if (!buildClientVersion(firmware, client_version, sizeof(client_version))) return false; appendKey(writer, "client_version", client_version); - writer.append("}"); + appendRaw(writer, "repeat", stats.can_relay ? "true" : "false"); + char number[16]; + writer.append(",\"stats\":{"); + snprintf(number, sizeof(number), "%u", static_cast(stats.battery_mv)); + appendRaw(writer, "battery_mv", number, false); + snprintf(number, sizeof(number), "%lu", static_cast(stats.uptime_secs)); + appendRaw(writer, "uptime_secs", number); + snprintf(number, sizeof(number), "%d", static_cast(stats.noise_floor)); + appendRaw(writer, "noise_floor", number); + writer.append("}}"); return writer.finish(written); } diff --git a/examples/mqtt_observer/MQTTObserverFormat.h b/examples/mqtt_observer/MQTTObserverFormat.h index 696b2470..3cde4cdb 100644 --- a/examples/mqtt_observer/MQTTObserverFormat.h +++ b/examples/mqtt_observer/MQTTObserverFormat.h @@ -38,8 +38,19 @@ bool appendEscaped(const char* text, char* out, size_t capacity, size_t& length) bool buildPacket(const PacketView& packet, char* out, size_t capacity, size_t& written); bool buildRaw(const char* origin, const char* origin_id, const char* timestamp, const uint8_t* bytes, size_t len, char* out, size_t capacity, size_t& written); + +// Live device stats for the periodic status message. Reported under "stats" +// (battery/uptime/noise floor) plus the top-level "repeat" relay flag, matching +// the schema other MeshCore observers publish. +struct StatusStats { + uint16_t battery_mv; + uint32_t uptime_secs; + int16_t noise_floor; + bool can_relay; +}; + bool buildStatus(const char* origin, const char* origin_id, const char* model, const char* firmware, const char* radio, const char* timestamp, - char* out, size_t capacity, size_t& written); + const StatusStats& stats, char* out, size_t capacity, size_t& written); } // namespace MQTTObserverFormat diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 57cdbb76..ef7c2a24 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1064,14 +1064,25 @@ static void batteryAlerts(BatteryGate& gate, FleetManager& fleet) { } } +#ifdef WITH_MQTT_OBSERVER +void MyMesh::mqttStatusStats(void* context, MQTTObserverFormat::StatusStats& stats) { + MyMesh* self = static_cast(context); + stats.battery_mv = board.getBattMilliVolts(); + stats.uptime_secs = static_cast(self->uptime_millis / 1000); + stats.noise_floor = static_cast(self->_radio->getNoiseFloor()); + stats.can_relay = !self->_prefs.disable_fwd; +} +#endif + void MyMesh::begin(FILESYSTEM *fs) { mesh::Mesh::begin(); _fs = fs; // load persisted prefs _cli.loadPrefs(_fs); #ifdef WITH_MQTT_OBSERVER - mqttObserver.begin(_fs, &self_id, _prefs.node_name, FIRMWARE_VERSION, - _prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + mqttObserver.begin(_fs, &self_id, _prefs.node_name, board.getManufacturerName(), + FIRMWARE_VERSION, _prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); + mqttObserver.setStatusStatsFn(mqttStatusStats, this); #endif acl.load(_fs, self_id); filter.begin(fs); diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 35d4d78a..775bff99 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -109,6 +109,8 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { MeshClockSync meshClock; #ifdef WITH_MQTT_OBSERVER MQTTObserver mqttObserver; + // supplies the observer's status-message stats from live node state + static void mqttStatusStats(void* context, MQTTObserverFormat::StatusStats& stats); #endif uint32_t pending_discover_tag; unsigned long pending_discover_until; diff --git a/test/test_mqtt_observer/test_mqtt_observer.cpp b/test/test_mqtt_observer/test_mqtt_observer.cpp index ade744f0..214059b7 100644 --- a/test/test_mqtt_observer/test_mqtt_observer.cpp +++ b/test/test_mqtt_observer/test_mqtt_observer.cpp @@ -433,10 +433,12 @@ TEST(MQTTObserverFormat, RawStatusAndTruncationAreBounded) { EXPECT_EQ(std::string(json, written), "{\"origin\":\"node\",\"origin_id\":\"AABB\"," "\"timestamp\":\"timestamp\",\"type\":\"RAW\",\"data\":\"0012AB\"}"); + MQTTObserverFormat::StatusStats stats{3900, 1234, -110, true}; ASSERT_TRUE(MQTTObserverFormat::buildStatus("node", "AABB", "MeshCore", "1.0", "radio", - "timestamp", json, sizeof(json), written)); + "timestamp", stats, json, sizeof(json), written)); EXPECT_NE(std::string(json, written).find("\"status\":\"online\""), std::string::npos); EXPECT_NE(std::string(json, written).find("\"origin_id\":\"AABB\""), std::string::npos); + EXPECT_NE(std::string(json, written).find("\"model\":\"MeshCore\""), std::string::npos); EXPECT_NE(std::string(json, written).find("\"client_version\":\"meshcore-jhuebert/1.0\""), std::string::npos); char short_json[8]; EXPECT_FALSE(MQTTObserverFormat::buildRaw("node", "AABB", "timestamp", raw, sizeof(raw), @@ -444,6 +446,20 @@ TEST(MQTTObserverFormat, RawStatusAndTruncationAreBounded) { EXPECT_EQ(short_json[0], 0); } +TEST(MQTTObserverFormat, StatusReportsRelayAndDeviceStats) { + char json[512]; + size_t written = 0; + MQTTObserverFormat::StatusStats stats{0, 0, -95, false}; + ASSERT_TRUE(MQTTObserverFormat::buildStatus("node", "AABB", "Station G2", "1.2", + "910.525,62.5,7,5", "timestamp", stats, + json, sizeof(json), written)); + std::string payload(json, written); + EXPECT_NE(payload.find("\"model\":\"Station G2\""), std::string::npos); + EXPECT_NE(payload.find("\"repeat\":false"), std::string::npos); + EXPECT_NE(payload.find("\"stats\":{\"battery_mv\":0,\"uptime_secs\":0,\"noise_floor\":-95}"), + std::string::npos); +} + TEST(MQTTObserverFormat, ClientVersionIsSharedByStatusAndClaims) { char version[96]; ASSERT_TRUE(MQTTObserverFormat::buildClientVersion("1.2.3", version, sizeof(version)));