mirror of
https://github.com/jhuebert/MeshCore.git
synced 2026-12-03 10:54:13 +00:00
feat: remove MQTT observer offline last will
This commit is contained in:
1 parent
98553c5c13
commit
28f9d93efd
6 files changed
+15
-43
No files matched your search
+7
-8
@@ -33,7 +33,7 @@ observer **cannot** see is a frame that never parsed; see
|
||||
Each enabled broker gets three feeds on the MeshCore topic convention:
|
||||
|
||||
```text
|
||||
meshcore/{IATA}/{DEVICE_PUBLIC_KEY}/status # retained-last-will aware online/offline
|
||||
meshcore/{IATA}/{DEVICE_PUBLIC_KEY}/status # online status
|
||||
meshcore/{IATA}/{DEVICE_PUBLIC_KEY}/packets # parsed packets, rx and tx
|
||||
meshcore/{IATA}/{DEVICE_PUBLIC_KEY}/raw # the original radio frame
|
||||
```
|
||||
@@ -214,7 +214,7 @@ These are shared across brokers:
|
||||
set mqtt.rx on|off # master gate for receiving anything at all
|
||||
set mqtt.packets on|off # publish parsed packets
|
||||
set mqtt.raw on|off # publish the original radio frame
|
||||
set mqtt.status on|off # publish periodic status (+ arms the offline last will)
|
||||
set mqtt.status on|off # publish periodic status
|
||||
set mqtt.tx off|on|advert # this node's own transmissions (see below)
|
||||
set mqtt.interval 1-60 # status publish interval, in minutes (default 5)
|
||||
```
|
||||
@@ -318,9 +318,9 @@ Publish `status: "online"` (QoS 1, non-retained) on connect and every
|
||||
"radio":"869.525,62.5,8,5","client_version":"meshcore-jhuebert/<version>"}
|
||||
```
|
||||
|
||||
Each broker also registers a **retained** `status: "offline"` last will on the
|
||||
status topic (while `mqtt.status` is on), so a feed flips the node offline as
|
||||
soon as the broker notices an unexpected session drop — no polling needed.
|
||||
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.
|
||||
|
||||
### Packet
|
||||
|
||||
@@ -353,8 +353,7 @@ routes add a `path` array of lowercase hex hop tokens:
|
||||
- Status is QoS 1; a failed status publish is retried no faster than every 30
|
||||
seconds per slot.
|
||||
|
||||
The broker applies its own retention policy to anything except messages the
|
||||
observer marks retained (the offline will).
|
||||
The broker applies its own retention policy to published messages.
|
||||
|
||||
---
|
||||
|
||||
@@ -481,7 +480,7 @@ mqtt wifi:up time:ok slots:1/2 rx:4231 pub:4200 drop:31 q:0/8 heap:118032/98304
|
||||
| Auth rejected | audience vs username/password | Audience set when you meant password (JWT wins); or the audience/token is wrong |
|
||||
| No TLS, immediately after boot | `time:` | Clock not yet synced; secure brokers need valid time |
|
||||
| `drop:` climbing steadily | `q:` | Broker disconnected too long, or an overloaded node — check the broker |
|
||||
| Node vanished from the feed | `get mqttN.status` | Last-will `offline` is expected during an outage; it returns on reconnect |
|
||||
| Node disappeared from the feed | `get mqttN.status` | Status is online-only; expire stale status in the consumer |
|
||||
| Duplicate packets in the feed | — | Normal: the hook is pre-dedup; multi-repeat receptions are real observations |
|
||||
|
||||
---
|
||||
|
||||
@@ -123,8 +123,7 @@ bool sameBrokerConnection(const MQTTObserverBrokerConfig& a,
|
||||
} // namespace
|
||||
|
||||
MQTTObserver::BrokerRuntime::BrokerRuntime()
|
||||
: client(nullptr), server_uri{}, client_id{}, status_topic{}, offline_payload{},
|
||||
username{}, password{}, jwt_username{},
|
||||
: client(nullptr), server_uri{}, client_id{}, username{}, password{}, jwt_username{},
|
||||
jwt_token{}, jwt_expiry(0),
|
||||
next_connect_ms(0), attempt_started(0), connected_at(0), last_status_ms(0),
|
||||
status_retry_ms(0), backoff_index(0), status_retry_pending(false),
|
||||
@@ -524,17 +523,6 @@ bool MQTTObserver::configureClient(size_t slot, const MQTTObserverConfig& config
|
||||
runtime.client->setCredentials(runtime.username[0] ? runtime.username : nullptr,
|
||||
runtime.password[0] ? runtime.password : nullptr);
|
||||
}
|
||||
// A retained last will flips the node offline on the feed as soon as the
|
||||
// broker notices the session drop. The topic and body must outlive connect()
|
||||
// (the library keeps the pointers), so they live in BrokerRuntime.
|
||||
if (config.status_enabled &&
|
||||
MQTTObserverConfigCodec::buildTopic(config, broker, _device_id, "status",
|
||||
runtime.status_topic, sizeof(runtime.status_topic))) {
|
||||
size_t will_len = 0;
|
||||
if (buildStatusMessage(false, runtime.offline_payload, sizeof(runtime.offline_payload), will_len))
|
||||
runtime.client->setWill(runtime.status_topic, 1, true, runtime.offline_payload,
|
||||
static_cast<int>(will_len));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -764,7 +752,7 @@ void MQTTObserver::worker() {
|
||||
}
|
||||
}
|
||||
|
||||
bool MQTTObserver::buildStatusMessage(bool online, char* out, size_t capacity, size_t& written) {
|
||||
bool MQTTObserver::buildStatusMessage(char* out, size_t capacity, size_t& written) {
|
||||
char radio[64];
|
||||
char origin[sizeof(_origin)];
|
||||
float freq, bw;
|
||||
@@ -785,7 +773,7 @@ bool MQTTObserver::buildStatusMessage(bool online, char* out, size_t capacity, s
|
||||
sizeof(timestamp))) return false;
|
||||
return MQTTObserverFormat::buildStatus(origin, _device_id, "MeshCore Repeater",
|
||||
_firmware, radio, timestamp,
|
||||
out, capacity, written, online);
|
||||
out, capacity, written);
|
||||
}
|
||||
|
||||
bool MQTTObserver::publishStatus(size_t slot, const MQTTObserverConfig& config) {
|
||||
@@ -797,7 +785,7 @@ bool MQTTObserver::publishStatus(size_t slot, const MQTTObserverConfig& config)
|
||||
if (!MQTTObserverConfigCodec::buildTopic(config, broker, _device_id, "status",
|
||||
topic, sizeof(topic))) return false;
|
||||
size_t written = 0;
|
||||
if (!buildStatusMessage(true, _json, sizeof(_json), written)) return false;
|
||||
if (!buildStatusMessage(_json, sizeof(_json), written)) return false;
|
||||
return runtime.client->publish(topic, 1, false, _json, written, true) >= 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,8 +38,6 @@ class MQTTObserver {
|
||||
PsychicMqttClient* client;
|
||||
char server_uri[sizeof(MQTTObserverBrokerConfig::server) + 16];
|
||||
char client_id[2 * PUB_KEY_SIZE + 3];
|
||||
char status_topic[128];
|
||||
char offline_payload[512]; // retained last-will body (setWill keeps the pointer)
|
||||
char username[65];
|
||||
char password[65];
|
||||
char jwt_username[3 + 2 * PUB_KEY_SIZE + 1];
|
||||
@@ -108,7 +106,7 @@ class MQTTObserver {
|
||||
bool createJwt(size_t slot, const MQTTObserverBrokerConfig& config, uint32_t& expires_at);
|
||||
bool publishEvent(const Event& event, const MQTTObserverConfig& config);
|
||||
bool publishStatus(size_t slot, const MQTTObserverConfig& config);
|
||||
bool buildStatusMessage(bool online, char* out, size_t capacity, size_t& written);
|
||||
bool buildStatusMessage(char* out, size_t capacity, size_t& written);
|
||||
void disconnectBroker(size_t slot);
|
||||
void trackDisconnectedQueue(uint32_t now);
|
||||
void flushQueue();
|
||||
|
||||
@@ -201,11 +201,11 @@ 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, bool online) {
|
||||
char* out, size_t capacity, size_t& written) {
|
||||
if (!origin || !origin_id || !model || !firmware || !radio || !timestamp) return false;
|
||||
JsonWriter writer(out, capacity);
|
||||
writer.append("{");
|
||||
appendKey(writer, "status", online ? "online" : "offline", false);
|
||||
appendKey(writer, "status", "online", false);
|
||||
appendKey(writer, "timestamp", timestamp);
|
||||
appendKey(writer, "origin", origin);
|
||||
appendKey(writer, "origin_id", origin_id);
|
||||
|
||||
@@ -34,6 +34,6 @@ 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);
|
||||
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, bool online = true);
|
||||
char* out, size_t capacity, size_t& written);
|
||||
|
||||
} // namespace MQTTObserverFormat
|
||||
@@ -440,19 +440,6 @@ TEST(MQTTObserverFormat, RawStatusAndTruncationAreBounded) {
|
||||
EXPECT_EQ(short_json[0], 0);
|
||||
}
|
||||
|
||||
TEST(MQTTObserverFormat, OfflineStatusIsARetainedLastWillBody) {
|
||||
char json[512];
|
||||
size_t written = 0;
|
||||
ASSERT_TRUE(MQTTObserverFormat::buildStatus("node", "AABB", "MeshCore", "1.0", "radio",
|
||||
"timestamp", json, sizeof(json), written, false));
|
||||
std::string payload(json, written);
|
||||
EXPECT_NE(payload.find("\"status\":\"offline\""), std::string::npos);
|
||||
EXPECT_EQ(payload.find("\"status\":\"online\""), std::string::npos);
|
||||
// Same identity/version fields as the online status message.
|
||||
EXPECT_NE(payload.find("\"origin_id\":\"AABB\""), std::string::npos);
|
||||
EXPECT_NE(payload.find("\"client_version\":\"meshcore-jhuebert/1.0\""), std::string::npos);
|
||||
}
|
||||
|
||||
TEST(MQTTObserverFormat, ClientVersionIsSharedByStatusAndClaims) {
|
||||
char version[96];
|
||||
ASSERT_TRUE(MQTTObserverFormat::buildClientVersion("1.2.3", version, sizeof(version)));
|
||||
|
||||
Reference in new issue
Block a user