feat: 增加订阅主题备注功能

This commit is contained in:
2026-07-20 22:03:03 +08:00
parent cd2f926ba7
commit 7eb62ca37f
6 changed files with 127 additions and 25 deletions
+12 -5
View File
@@ -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;