From 7eb62ca37f3c3c2ba879419586774deaa63d286a Mon Sep 17 00:00:00 2001 From: Frost-ZX Date: Mon, 20 Jul 2026 22:03:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=A4=87=E6=B3=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/ipc-handlers.js | 8 +- app/src/main/mqtt-manager.js | 17 ++-- app/src/preload/index.js | 7 +- app/src/renderer/src/stores/mqtt.js | 18 +++-- app/src/renderer/src/views/Subscribe.vue | 98 ++++++++++++++++++++++-- app/types/jsdoc.d.ts | 4 + 6 files changed, 127 insertions(+), 25 deletions(-) diff --git a/app/src/main/ipc-handlers.js b/app/src/main/ipc-handlers.js index c3bb23e..8a06c3b 100644 --- a/app/src/main/ipc-handlers.js +++ b/app/src/main/ipc-handlers.js @@ -90,8 +90,8 @@ export function registerIpcHandlers() { // ==================== 主题管理 ==================== - ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos) => { - return mqttManager.addTopic(serverId, topic, qos); + ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos, comment) => { + return mqttManager.addTopic(serverId, topic, qos, comment); }); ipcMain.handle('mqtt:remove-topic', (_, serverId, topic) => { @@ -110,8 +110,8 @@ export function registerIpcHandlers() { return mqttManager.subscribeAll(serverId); }); - ipcMain.handle('mqtt:update-topic', (_, serverId, topicId, topic, qos) => { - return mqttManager.updateTopic(serverId, topicId, topic, qos); + ipcMain.handle('mqtt:update-topic', (_, serverId, topicId, topic, qos, comment) => { + return mqttManager.updateTopic(serverId, topicId, topic, qos, comment); }); // ==================== 消息发布 ==================== diff --git a/app/src/main/mqtt-manager.js b/app/src/main/mqtt-manager.js index 6bde1ed..3709ec0 100644 --- a/app/src/main/mqtt-manager.js +++ b/app/src/main/mqtt-manager.js @@ -82,6 +82,7 @@ class MqttManager { s.topics = (s.topics || []).map((t) => ({ ...t, id: t.id || 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6), + comment: t.comment ?? '', subscribed: false, })); this.servers.set(s.id, s); @@ -154,8 +155,8 @@ class MqttManager { reconnectInterval: 5000, status: 'disconnected', topics: [ - { id: 'topic_default_001', topic: 'sensor/+/temperature', qos: 1, subscribed: false }, - { id: 'topic_default_002', topic: 'device/status', qos: 2, subscribed: false }, + { id: 'topic_default_001', topic: 'sensor/+/temperature', qos: 1, comment: '', subscribed: false }, + { id: 'topic_default_002', topic: 'device/status', qos: 2, comment: '', subscribed: false }, ], }, ]; @@ -272,6 +273,7 @@ class MqttManager { id: t.id || 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6), topic: t.topic, qos: t.qos ?? 1, + comment: t.comment ?? '', subscribed: false, }; } @@ -361,6 +363,7 @@ class MqttManager { id: t.id, topic: t.topic, qos: t.qos, + comment: t.comment ?? '', }; } @@ -575,9 +578,10 @@ class MqttManager { * @param {string} serverId - 服务器 id * @param {string} topic - 主题名称 * @param {MqttQoS} qos - QoS 等级 + * @param {string} comment - 主题备注 * @returns {MqttTopic | null} 添加后的主题对象或 null */ - addTopic(serverId, topic, qos = 1) { + addTopic(serverId, topic, qos = 1, comment = '') { let server = this.servers.get(serverId); if (!server) { return null; @@ -588,7 +592,7 @@ class MqttManager { let topicId = 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6); - let newTopic = { id: topicId, topic, qos, subscribed: false }; + let newTopic = { id: topicId, topic, qos, comment, subscribed: false }; server.topics.push(newTopic); this.saveData(); @@ -686,9 +690,10 @@ class MqttManager { * @param {string} topicId - 主题 id * @param {string} newTopic - 新的主题名称 * @param {number} newQos - 新的 QoS 等级 + * @param {string} comment - 新的主题备注 * @returns {boolean} 是否更新成功 */ - updateTopic(serverId, topicId, newTopic, newQos) { + updateTopic(serverId, topicId, newTopic, newQos, comment = '') { let server = this.servers.get(serverId); @@ -721,6 +726,7 @@ class MqttManager { t.topic = newTopic; t.qos = newQos; + t.comment = comment; // 如果已连接且此前已订阅,重新订阅新主题 if (server.status === 'connected' && wasSubscribed) { @@ -733,6 +739,7 @@ class MqttManager { topic: newTopic, subscribed: t.subscribed, topicId, + comment: t.comment, }); return true; diff --git a/app/src/preload/index.js b/app/src/preload/index.js index 0523096..74bc7ac 100644 --- a/app/src/preload/index.js +++ b/app/src/preload/index.js @@ -20,13 +20,14 @@ const api = { disconnect: (id) => ipcRenderer.invoke('mqtt:disconnect', id), // 主题管理 - addTopic: (serverId, topic, qos) => ipcRenderer.invoke('mqtt:add-topic', serverId, topic, qos), + addTopic: (serverId, topic, qos, comment) => + ipcRenderer.invoke('mqtt:add-topic', serverId, topic, qos, comment), removeTopic: (serverId, topic) => ipcRenderer.invoke('mqtt:remove-topic', serverId, topic), subscribe: (serverId, topic) => ipcRenderer.invoke('mqtt:subscribe', serverId, topic), unsubscribe: (serverId, topic) => ipcRenderer.invoke('mqtt:unsubscribe', serverId, topic), subscribeAll: (serverId) => ipcRenderer.invoke('mqtt:subscribe-all', serverId), - updateTopic: (serverId, topicId, topic, qos) => - ipcRenderer.invoke('mqtt:update-topic', serverId, topicId, topic, qos), + updateTopic: (serverId, topicId, topic, qos, comment) => + ipcRenderer.invoke('mqtt:update-topic', serverId, topicId, topic, qos, comment), // 消息发布 publish: (serverId, topic, payload, opts) => diff --git a/app/src/renderer/src/stores/mqtt.js b/app/src/renderer/src/stores/mqtt.js index 1fe391c..c840e05 100644 --- a/app/src/renderer/src/stores/mqtt.js +++ b/app/src/renderer/src/stores/mqtt.js @@ -33,6 +33,7 @@ import { ref, computed } from 'vue'; * @property {string} serverId 所属服务器 id * @property {string} topic 主题名称 * @property {string} [topicId] 主题 id + * @property {string} [comment] 主题备注 * @property {boolean} subscribed 是否已订阅 */ @@ -57,12 +58,12 @@ import { ref, computed } from 'vue'; * @property {(serverList: ServerConfig[]) => Promise} importServers 导入服务器配置 * @property {(id: string) => Promise} connectServer 连接服务器 * @property {(id: string) => Promise} disconnectServer 断开服务器 - * @property {(serverId: string, topic: string, qos: MqttQoS) => Promise} addTopic 添加主题 + * @property {(serverId: string, topic: string, qos: MqttQoS, comment: string) => Promise} addTopic 添加主题 * @property {(serverId: string, topic: string) => Promise} removeTopic 移除主题 * @property {(serverId: string, topic: string) => Promise} subscribeTopic 订阅主题 * @property {(serverId: string, topic: string) => Promise} unsubscribeTopic 取消订阅主题 * @property {(serverId: string) => Promise} subscribeAllTopics 批量订阅所有主题 - * @property {(serverId: string, topicId: string, topic: string, qos: MqttQoS) => Promise} updateTopic 更新主题 + * @property {(serverId: string, topicId: string, topic: string, qos: MqttQoS, comment: string) => Promise} updateTopic 更新主题 * @property {(serverId: string, topic: string, payload: string, opts: PublishOptions) => Promise} publishMessage 发布消息 * @property {() => Promise} refreshMessages 刷新消息列表 * @property {() => Promise} refreshPublishHistory 刷新发布历史 @@ -181,6 +182,9 @@ function setupListeners() { if (t) { t.subscribed = data.subscribed; + if (typeof data.comment === 'string') { + t.comment = data.comment; + } if (data.topic && data.topic !== t.topic) { t.topic = data.topic; sortTopics(srv); @@ -400,11 +404,12 @@ export function useMqttStore() { * @param {string} serverId - 服务器 id * @param {string} topic - 主题名称 * @param {MqttQoS} qos - QoS 等级 + * @param {string} comment - 主题备注 * @returns {Promise} 添加后的主题对象或 null */ - async function addTopic(serverId, topic, qos) { + async function addTopic(serverId, topic, qos, comment) { - let result = await window.api.addTopic(serverId, topic, qos); + let result = await window.api.addTopic(serverId, topic, qos, comment); if (result) { let srv = servers.value.find((s) => s.id === serverId); @@ -472,10 +477,11 @@ export function useMqttStore() { * @param {string} topicId - 主题 id * @param {string} topic - 新的主题名称 * @param {MqttQoS} qos - 新的 QoS 等级 + * @param {string} comment - 新的主题备注 * @returns {Promise} 是否更新成功 */ - async function updateTopic(serverId, topicId, topic, qos) { - return await window.api.updateTopic(serverId, topicId, topic, qos); + async function updateTopic(serverId, topicId, topic, qos, comment) { + return await window.api.updateTopic(serverId, topicId, topic, qos, comment); } // ==================== 消息发布 ==================== diff --git a/app/src/renderer/src/views/Subscribe.vue b/app/src/renderer/src/views/Subscribe.vue index 0e66f1d..af74d18 100644 --- a/app/src/renderer/src/views/Subscribe.vue +++ b/app/src/renderer/src/views/Subscribe.vue @@ -1,7 +1,7 @@