Files
cuframes-composer/include/cuframes_composer/health.h
T
gx 636b70b64c Phase 4: ZMQ control plane + MQTT health publisher
Phase 4a — control plane через ZMQ REP socket с JSON-командами.
Позволяет операторам менять text overlay'и runtime'ом без рестарта.
Live-validated: команды set_text label-parking → "⚠ ВНИМАНИЕ ⚠"
и set_text label-gate → "Машина у ворот" отрабатывают, текст
обновляется в потоке RTSP без перерывов.

Phase 4b — MQTT health publisher через libmosquitto. Каждые 10с в
composer/<instance>/health публикуется JSON {active,stale,dead,total,
uptime_s} с retain=true. Опционально публикуется HA MQTT discovery
config — четыре сенсора (active/stale/dead/total) появляются в HA
автоматически с expire_after=30s.

Содержимое:

- include/cuframes_composer/overlay.h — cfc_overlay_set_id/get_id/get_type
  для lookup'а через control plane.
- include/cuframes_composer/composer.h — cfc_composer_find_overlay(id).
- include/cuframes_composer/control.h — cfc_control_config_t (endpoint +
  composer + cuda_ctx для text rebuild в worker thread).
- src/control.c — ZMQ REP socket в фоновом потоке + zmq_poll с 200мс
  timeout'ом для проверки stop_flag. JSON-диспатчер для команд ping /
  health / set_text / set_visible / list_overlays. cuCtxSetCurrent
  на старте worker'а — без этого update_text валится на cuMemAlloc.
- include/cuframes_composer/health.h — cfc_health_config_t (host/port/
  user/pass + topic_prefix/instance + interval + publish_discovery).
- src/health.c — mosquitto_connect_async + loop_start + background
  thread с publish health каждые N секунд + один разовый publish_discovery
  для HA.
- examples/grid_record — флаги --control tcp://0.0.0.0:5599,
  --mqtt host[:port], --mqtt-instance NAME, --mqtt-user/--mqtt-pass.
  Для text overlay'ев — prefix "id=NAME:" в --text задаёт control-plane ID.

CMakeLists.txt — find_library(zmq), find_library(json-c),
find_library(mosquitto) + linkage всех трёх.

Production TODO: создать отдельного MQTT user'а для composer'а вместо
переиспользования frigate creds (используется только в smoke).
2026-06-03 06:20:38 +01:00

50 lines
2.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* cuframes-composer — MQTT health publisher для observability.
*
* Подключается к MQTT-брокеру, периодически публикует JSON со статистикой
* композитора (active/stale/dead источников, framerate, uptime).
*
* Топик: <prefix>/<instance>/health QoS 1, retain=true.
* Так же при старте публикует HA discovery config — для автоматического
* появления сенсора в Home Assistant. expire_after короткий (30 секунд) —
* если поток упал, HA подсветит сенсор «Unavailable» спустя 30с.
*
* Lifecycle:
* create: mosquitto_new + connect_async + начать background thread
* loop: каждые interval_sec секунд → mosquitto_publish + mosquitto_loop
* destroy: stop_flag + DISCONNECT + cleanup
*
* Лицензия: LGPL-2.1+
*/
#ifndef CUFRAMES_COMPOSER_HEALTH_H
#define CUFRAMES_COMPOSER_HEALTH_H
#include "composer.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct cfc_health_config {
const char *host; /* "cctv-mosquitto" / "192.168.88.23" */
int port; /* 1883 без TLS */
const char *username; /* nullable */
const char *password; /* nullable */
const char *topic_prefix; /* "composer" (топик: composer/<instance>/health) */
const char *instance; /* "cfc-grid" — уникальный ID composer'а */
int interval_sec; /* 10 = публикуем раз в 10 секунд */
cfc_composer_t *composer; /* читает get_health() */
int publish_discovery; /* 1 = опубликовать HA discovery config при старте */
} cfc_health_config_t;
typedef struct cfc_health cfc_health_t;
int cfc_health_create(const cfc_health_config_t *cfg, cfc_health_t **out);
int cfc_health_destroy(cfc_health_t *h);
#ifdef __cplusplus
}
#endif
#endif /* CUFRAMES_COMPOSER_HEALTH_H */