From 73179f3040356775e3dadc7fd75bd6b9a4082c4e Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Mon, 6 Apr 2026 20:28:49 +0200 Subject: [PATCH] fix: add topic selection checkbox that auto-creates datapoints When a user checks a topic checkbox, a datapoint is automatically created and the broker subscribes to that topic for live updates. Co-Authored-By: Claude Opus 4.6 (1M context) --- display/frontend/src/pages/MqttTopics.tsx | 23 +++++++++++++++++-- .../middleware/src/api/routes/mqtt-topics.ts | 22 ++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/display/frontend/src/pages/MqttTopics.tsx b/display/frontend/src/pages/MqttTopics.tsx index 48f80a5..50f1e7d 100644 --- a/display/frontend/src/pages/MqttTopics.tsx +++ b/display/frontend/src/pages/MqttTopics.tsx @@ -8,6 +8,7 @@ interface Topic { topic: string; last_payload: string | null; json_path: string | null; + selected: number; ignored: number; discovered_at: string; } @@ -46,6 +47,15 @@ export default function MqttTopics() { } } + async function handleSelect(id: number, selected: boolean) { + try { + await api.put(`/api/mqtt/brokers/${brokerId}/topics/${id}`, { selected: selected ? 1 : 0 }); + load(); + } catch (e) { + setError((e as Error).message); + } + } + async function handleIgnore(id: number, ignored: number) { try { await api.put(`/api/mqtt/brokers/${brokerId}/topics/${id}`, { ignored: ignored ? 0 : 1 }); @@ -100,6 +110,7 @@ export default function MqttTopics() { + @@ -108,10 +119,18 @@ export default function MqttTopics() { {filtered.length === 0 && ( - + )} {filtered.map((t) => ( - + +
Aktiv Topic Letzter Wert JSON-Pfad
Keine Topics gefunden
Keine Topics gefunden
+ handleSelect(t.id, e.target.checked)} + className="w-4 h-4 accent-green-500" + /> + {t.topic} {t.last_payload ?? '-'} diff --git a/display/middleware/src/api/routes/mqtt-topics.ts b/display/middleware/src/api/routes/mqtt-topics.ts index a374e3f..f99a79b 100644 --- a/display/middleware/src/api/routes/mqtt-topics.ts +++ b/display/middleware/src/api/routes/mqtt-topics.ts @@ -5,6 +5,9 @@ import { startDiscovery, addManualTopic, } from '../../modules/mqtt/topic-discovery'; +import { createDatapointFromMqtt, getDatapointByTopicId } from '../../modules/datapoints/datapoint-manager'; +import { getConnection } from './../../modules/mqtt/broker-manager'; +import { getDb } from '../../db/index'; const router = Router({ mergeParams: true }); @@ -21,8 +24,23 @@ router.get('/', (req, res): void => { // PUT /api/mqtt/brokers/:brokerId/topics/:id router.put('/:id', (req, res): void => { try { - const topic = updateTopic(parseInt(req.params.id), req.body); - if (!topic) { res.status(404).json({ error: 'Topic not found' }); return; } + const topicId = parseInt(req.params.id); + const topicRow = getDb().prepare('SELECT * FROM mqtt_topics WHERE id = ?').get(topicId) as any; + if (!topicRow) { res.status(404).json({ error: 'Topic not found' }); return; } + + const topic = updateTopic(topicId, req.body); + + // If selecting a topic, auto-create a datapoint + if (req.body.selected === 1 || req.body.selected === true) { + const existing = getDatapointByTopicId(topicId); + if (!existing) { + createDatapointFromMqtt(topicRow.broker_id, topicId, topicRow.topic); + } + // Subscribe to the topic on the broker connection + const client = getConnection(topicRow.broker_id); + if (client?.connected) client.subscribe(topicRow.topic); + } + res.json(topic); } catch (err) { res.status(500).json({ error: (err as Error).message });