feat(mqtt): add ts and duration_ms to talker start/stop events

Talker MQTT payloads now carry a Unix-epoch-ms `ts` field on both start
and stop. Stop additionally carries `duration_ms` whenever a matching
start was observed by the same MqttPublisher instance and the resulting
duration is non-negative. Both fields are additive — older consumers
that only read `callsign` continue to work unchanged.

Implementation
- MqttPublisher tracks per-(peer_id, tg) start_ms in a small std::map.
  Local talkers use peer_id="" as the map key. The entry is consumed on
  the matching stop.
- On stop, duration_ms is computed as `now - start_ms`. The field is
  omitted when:
  * no matching start was recorded (e.g. reflector restarted mid-talker)
  * the resulting duration is negative (backwards NTP correction during
    the transmission)
  Both edge cases are documented in docs/MQTT.md.
- Applies symmetrically across the four publish paths:
  onTalkerStart / onTalkerStop / onPeerTalkerStart / onPeerTalkerStop.

Tests
- test_trunk.py::test_18_mqtt_talker_event extended:
  * asserts `ts` is present on start, parses as int, lies within a
    +/- 60s window of the test's local clock.
  * asserts `ts` on stop is >= start ts.
  * asserts `duration_ms` on stop is present, >= the held-talker time
    (~500 ms), and equals stop_ts - start_ts.
- Edge-case behaviour (no-start, backwards clock jump) is guarded in
  code and documented; reaching it from the integration suite would
  require restarting the reflector mid-talker or injecting a clock
  step, so it is not asserted from outside.

Docs
- docs/MQTT.md: payload examples updated for both flat and
  peer-namespaced talker topics; new paragraph spelling out the two
  conditions under which duration_ms is omitted.
- docs/MESSAGING_IDEAS.md: Telegram-bot example extended to also
  handle /stop with a `duration_ms >= 1000` filter (kerchunk
  suppression) and a short note on how `ts`/`duration_ms` map to
  end-to-end pipeline latency and session length.
This commit is contained in:
Audric committed 2026-05-07 17:02:39 +02:00
1 parent 52b5e4f3cf
commit ccf34adbe6
5 files changed
+134 -9

No files matched your search

+47 -4
View File
@@ -1,5 +1,6 @@
#include "MqttPublisher.h"
#include <chrono>
#include <iostream>
#include <sstream>
#include <mosquitto.h>
@@ -9,6 +10,14 @@
using namespace std;
int64_t MqttPublisher::nowMs(void)
{
using namespace std::chrono;
return duration_cast<milliseconds>(
system_clock::now().time_since_epoch()).count();
}
static void on_connect_cb(struct mosquitto*, void*, int rc)
{
if (rc == 0)
@@ -239,9 +248,13 @@ void MqttPublisher::publish(const std::string& topic_suffix,
void MqttPublisher::onTalkerStart(uint32_t tg, const std::string& callsign,
bool is_trunk)
{
const int64_t ts = nowMs();
m_talker_start_ms[{string(), tg}] = ts;
Json::Value payload;
payload["callsign"] = callsign;
payload["source"] = is_trunk ? "trunk" : "local";
payload["source"] = is_trunk ? "trunk" : "local";
payload["ts"] = static_cast<Json::Int64>(ts);
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "talker/" + to_string(tg) + "/start";
@@ -252,9 +265,23 @@ void MqttPublisher::onTalkerStart(uint32_t tg, const std::string& callsign,
void MqttPublisher::onTalkerStop(uint32_t tg, const std::string& callsign,
bool is_trunk)
{
const int64_t ts = nowMs();
Json::Value payload;
payload["callsign"] = callsign;
payload["source"] = is_trunk ? "trunk" : "local";
payload["source"] = is_trunk ? "trunk" : "local";
payload["ts"] = static_cast<Json::Int64>(ts);
auto it = m_talker_start_ms.find({string(), tg});
if (it != m_talker_start_ms.end())
{
const int64_t dur = ts - it->second;
// Omit duration_ms on a backwards clock jump (NTP correction mid-talker).
// The map entry is still consumed — there is no recovery from a
// jumped clock at the next stop event.
if (dur >= 0) payload["duration_ms"] = static_cast<Json::Int64>(dur);
m_talker_start_ms.erase(it);
}
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "talker/" + to_string(tg) + "/stop";
@@ -272,9 +299,13 @@ void MqttPublisher::onPeerTalkerStart(const std::string& peer_id,
" (tg=", tg, " callsign='", callsign, "')");
return;
}
const int64_t ts = nowMs();
m_talker_start_ms[{peer_id, tg}] = ts;
Json::Value payload;
payload["callsign"] = callsign;
payload["tg"] = static_cast<Json::UInt>(tg);
payload["tg"] = static_cast<Json::UInt>(tg);
payload["ts"] = static_cast<Json::Int64>(ts);
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "peer/" + peer_id + "/talker/" + to_string(tg) + "/start";
@@ -292,9 +323,21 @@ void MqttPublisher::onPeerTalkerStop(const std::string& peer_id,
" (tg=", tg, " callsign='", callsign, "')");
return;
}
const int64_t ts = nowMs();
Json::Value payload;
payload["callsign"] = callsign;
payload["tg"] = static_cast<Json::UInt>(tg);
payload["tg"] = static_cast<Json::UInt>(tg);
payload["ts"] = static_cast<Json::Int64>(ts);
auto it = m_talker_start_ms.find({peer_id, tg});
if (it != m_talker_start_ms.end())
{
const int64_t dur = ts - it->second;
// Omit duration_ms on a backwards clock jump (NTP correction mid-talker).
if (dur >= 0) payload["duration_ms"] = static_cast<Json::Int64>(dur);
m_talker_start_ms.erase(it);
}
Json::StreamWriterBuilder wb;
wb["indentation"] = "";
string topic = "peer/" + peer_id + "/talker/" + to_string(tg) + "/stop";