Retain transcript messages and add MQTT publish diagnostics
Some checks failed
Build and Push EVS Bridge Image / docker (push) Has been cancelled

This commit is contained in:
Kai
2026-02-13 18:23:36 +01:00
parent 9153e06ce5
commit e4170d9f42
6 changed files with 28 additions and 5 deletions

View File

@@ -35,6 +35,8 @@ MQTT_TRANSCRIPT_TOPIC_TEMPLATE = os.getenv(
MQTT_STT_ERROR_TOPIC_TEMPLATE = os.getenv(
"MQTT_STT_ERROR_TOPIC_TEMPLATE", f"{MQTT_BASE_TOPIC}" + "/{device_id}/stt_error"
)
STT_TRANSCRIPT_RETAIN = getenv_bool("STT_TRANSCRIPT_RETAIN", True)
STT_ERROR_RETAIN = getenv_bool("STT_ERROR_RETAIN", False)
STT_MODEL = os.getenv("STT_MODEL", "small")
STT_DEVICE = os.getenv("STT_DEVICE", "cpu")
@@ -66,11 +68,22 @@ class WorkerState:
state = WorkerState()
def publish_json(topic: str, payload: dict) -> None:
def publish_json(topic: str, payload: dict, retain: bool = False) -> None:
if not state.client:
return
try:
state.client.publish(topic, json.dumps(payload), qos=0, retain=False)
body = json.dumps(payload)
info = state.client.publish(topic, body, qos=0, retain=retain)
rc = getattr(info, "rc", "n/a")
mid = getattr(info, "mid", "n/a")
log.info(
"mqtt publish: topic=%s retain=%s rc=%s mid=%s bytes=%s",
topic,
retain,
rc,
mid,
len(body),
)
except Exception:
log.exception("mqtt publish failed: topic=%s", topic)
@@ -93,7 +106,7 @@ def transcribe_wav(device_id: str, wav_path: str) -> None:
"wav_path": wav_path,
"error": "wav_not_found",
}
publish_json(topic_for_error(device_id), payload)
publish_json(topic_for_error(device_id), payload, retain=STT_ERROR_RETAIN)
log.warning("wav not found: device=%s path=%s", device_id, wav_path)
return
@@ -122,7 +135,7 @@ def transcribe_wav(device_id: str, wav_path: str) -> None:
"language_probability": getattr(info, "language_probability", 0.0),
"model": STT_MODEL,
}
publish_json(topic_for_transcript(device_id), payload)
publish_json(topic_for_transcript(device_id), payload, retain=STT_TRANSCRIPT_RETAIN)
log.info("transcript: device=%s chars=%s wav=%s", device_id, len(text), wav_path)
except Exception as exc:
payload = {
@@ -132,7 +145,7 @@ def transcribe_wav(device_id: str, wav_path: str) -> None:
"wav_path": wav_path,
"error": str(exc),
}
publish_json(topic_for_error(device_id), payload)
publish_json(topic_for_error(device_id), payload, retain=STT_ERROR_RETAIN)
log.exception("transcription failed: device=%s wav=%s", device_id, wav_path)