feat: 增加服务器信息导入、导出功能

This commit is contained in:
2026-07-19 20:00:33 +08:00
parent 2682c0a1d6
commit 21683cb790
5 changed files with 182 additions and 29 deletions
+94 -29
View File
@@ -164,23 +164,47 @@ class MqttManager {
return this.servers.get(id) || null;
}
getServerConfigSchema() {
return {
name: (c, e) => c.name ?? e?.name,
host: (c, e) => c.host ?? e?.host,
port: (c, e) => c.port || e?.port || 1883,
protocol: (c, e) => c.protocol || e?.protocol || 'mqtt',
clientId: (c, e) => c.clientId || e?.clientId || 'frost_' + Math.random().toString(36).slice(2, 10),
username: (c, e) => c.username ?? e?.username ?? '',
password: (c, e, opts) => c.password ?? (opts.keepExistingPassword ? e?.password : '') ?? '',
keepAlive: (c, e) => c.keepAlive || e?.keepAlive || 60,
cleanSession: (c) => c.cleanSession !== false,
connectTimeout: (c, e) => c.connectTimeout ?? e?.connectTimeout ?? 10000,
reconnect: (c) => c.reconnect !== false,
reconnectInterval: (c, e) => c.reconnectInterval ?? e?.reconnectInterval ?? 5000,
};
}
buildServerFields(config, existing = null, options = {}) {
let schema = this.getServerConfigSchema();
let fields = {};
for (let key of Object.keys(schema)) {
fields[key] = schema[key](config, existing, options);
}
return fields;
}
buildTopic(t) {
return {
id: t.id || 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6),
topic: t.topic,
qos: t.qos ?? 1,
subscribed: false,
};
}
addServer(config) {
let id = 'srv_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6);
let server = {
id,
name: config.name,
host: config.host,
port: config.port || 1883,
protocol: config.protocol || 'mqtt',
clientId: config.clientId || 'frost_' + Math.random().toString(36).slice(2, 10),
username: config.username || '',
password: config.password || '',
keepAlive: config.keepAlive || 60,
cleanSession: config.cleanSession !== false,
connectTimeout: config.connectTimeout ?? 10000,
reconnect: config.reconnect !== false,
reconnectInterval: config.reconnectInterval ?? 5000,
...this.buildServerFields(config),
status: 'disconnected',
topics: [],
};
@@ -202,23 +226,7 @@ class MqttManager {
return null;
}
Object.assign(server, {
name: config.name,
host: config.host,
port: config.port || 1883,
protocol: config.protocol || 'mqtt',
clientId: config.clientId || server.clientId,
username: config.username || '',
keepAlive: config.keepAlive || 60,
cleanSession: config.cleanSession !== false,
connectTimeout: config.connectTimeout ?? server.connectTimeout ?? 10000,
reconnect: config.reconnect !== false,
reconnectInterval: config.reconnectInterval ?? server.reconnectInterval ?? 5000,
});
if (config.password) {
server.password = config.password;
}
Object.assign(server, this.buildServerFields(config, server, { keepExistingPassword: true }));
this.saveData();
@@ -235,6 +243,63 @@ class MqttManager {
this.saveData();
}
exportServers() {
let schemaKeys = Object.keys(this.getServerConfigSchema());
return Array.from(this.servers.values()).map((s) => {
let exported = {};
for (let key of schemaKeys) {
exported[key] = s[key];
}
exported.topics = (s.topics || []).map((t) => this.buildTopicExport(t));
return exported;
});
}
buildTopicExport(t) {
return {
id: t.id,
topic: t.topic,
qos: t.qos,
};
}
importServers(serverList) {
if (!Array.isArray(serverList)) {
throw new Error('导入格式错误:必须是服务器配置数组');
}
// 断开所有现有连接
this.clients.forEach((client, id) => {
this.disconnect(id);
});
this.clients.clear();
this.servers.clear();
this.messages.clear();
this.publishHistory.clear();
serverList.forEach((config) => {
if (!config.name || !config.host) {
return;
}
let id = 'srv_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6);
let server = {
id,
...this.buildServerFields(config),
status: 'disconnected',
topics: (config.topics || []).map((t) => this.buildTopic(t)),
};
this.servers.set(id, server);
this.messages.set(id, []);
this.publishHistory.set(id, []);
});
this.saveData();
return this.getServers();
}
// ==================== 连接管理 ====================
connect(id) {