2026-07-19 17:15:28 +08:00
|
|
|
import { app } from 'electron';
|
|
|
|
|
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
|
|
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
|
|
import mqtt from 'mqtt';
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
class MqttManager {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
constructor() {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.clients = new Map(); // serverId -> mqtt client
|
|
|
|
|
this.servers = new Map(); // serverId -> server config
|
|
|
|
|
this.messages = new Map(); // serverId -> message[]
|
|
|
|
|
this.publishHistory = new Map(); // serverId -> publishHistory[]
|
|
|
|
|
this.listeners = new Set(); // IPC callback functions
|
|
|
|
|
this.msgIdCounter = 0; // 用于生成唯一消息 id
|
|
|
|
|
this.dataPath = join(app.getPath('userData'), 'frost-mqtt-data.json');
|
|
|
|
|
this.loadData();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 21:57:56 +08:00
|
|
|
nextMsgId() {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.msgIdCounter = (this.msgIdCounter + 1) % Number.MAX_SAFE_INTEGER;
|
|
|
|
|
return 'msg_' + Date.now() + '_' + this.msgIdCounter;
|
2026-07-18 21:57:56 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
// ==================== 数据持久化 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
getDataDir() {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let dir = app.getPath('userData');
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (!existsSync(dir)) {
|
2026-07-19 17:15:28 +08:00
|
|
|
mkdirSync(dir, { recursive: true });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadData() {
|
|
|
|
|
try {
|
|
|
|
|
if (existsSync(this.dataPath)) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let raw = readFileSync(this.dataPath, 'utf-8');
|
|
|
|
|
let data = JSON.parse(raw);
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (data.servers) {
|
|
|
|
|
data.servers.forEach((s) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
s.status = 'disconnected';
|
2026-07-18 19:23:50 +08:00
|
|
|
s.topics = (s.topics || []).map((t) => ({
|
|
|
|
|
...t,
|
|
|
|
|
id: t.id || 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6),
|
2026-07-19 17:15:28 +08:00
|
|
|
subscribed: false,
|
|
|
|
|
}));
|
|
|
|
|
this.servers.set(s.id, s);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (data.messages) {
|
|
|
|
|
Object.entries(data.messages).forEach(([id, msgs]) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.messages.set(id, msgs);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (data.publishHistory) {
|
|
|
|
|
Object.entries(data.publishHistory).forEach(([id, history]) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.publishHistory.set(id, history);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
} else {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.initDefaultServers();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error('加载数据失败:', e.message);
|
|
|
|
|
this.initDefaultServers();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveData() {
|
|
|
|
|
try {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let data = {
|
2026-07-18 19:23:50 +08:00
|
|
|
servers: Array.from(this.servers.values()).map((s) => ({
|
|
|
|
|
...s,
|
|
|
|
|
status: 'disconnected',
|
2026-07-19 17:15:28 +08:00
|
|
|
topics: s.topics.map((t) => ({ ...t, subscribed: false })),
|
2026-07-18 19:23:50 +08:00
|
|
|
})),
|
|
|
|
|
messages: Object.fromEntries(this.messages),
|
2026-07-19 17:15:28 +08:00
|
|
|
publishHistory: Object.fromEntries(this.publishHistory),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
writeFileSync(this.dataPath, JSON.stringify(data, null, 2), 'utf-8');
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
} catch (e) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error('保存数据失败:', e.message);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initDefaultServers() {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let defaults = [
|
2026-07-18 19:23:50 +08:00
|
|
|
{
|
2026-07-18 22:02:53 +08:00
|
|
|
id: 'example_server',
|
2026-07-18 19:23:50 +08:00
|
|
|
name: 'EMQX 公共测试',
|
|
|
|
|
host: 'broker.emqx.io',
|
|
|
|
|
port: 1883,
|
|
|
|
|
protocol: 'mqtt',
|
|
|
|
|
clientId: 'frost_client_' + Math.random().toString(36).slice(2, 8),
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
keepAlive: 60,
|
|
|
|
|
cleanSession: true,
|
2026-07-19 18:36:03 +08:00
|
|
|
connectTimeout: 10000,
|
|
|
|
|
reconnect: true,
|
|
|
|
|
reconnectInterval: 5000,
|
2026-07-18 19:23:50 +08:00
|
|
|
status: 'disconnected',
|
|
|
|
|
topics: [
|
|
|
|
|
{ id: 'topic_default_001', topic: 'sensor/+/temperature', qos: 1, subscribed: false },
|
2026-07-19 17:15:28 +08:00
|
|
|
{ id: 'topic_default_002', topic: 'device/status', qos: 2, subscribed: false },
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
defaults.forEach((s) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.servers.set(s.id, s);
|
|
|
|
|
this.messages.set(s.id, []);
|
|
|
|
|
this.publishHistory.set(s.id, []);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.saveData();
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 监听器 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
addListener(callback) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.listeners.add(callback);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeListener(callback) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.listeners.delete(callback);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notify(event, data) {
|
|
|
|
|
this.listeners.forEach((cb) => {
|
|
|
|
|
try {
|
2026-07-19 17:15:28 +08:00
|
|
|
cb(event, data);
|
2026-07-18 19:23:50 +08:00
|
|
|
} catch (e) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error('通知失败:', e);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 服务器管理 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
getServers() {
|
|
|
|
|
return Array.from(this.servers.values()).map((s) => ({
|
|
|
|
|
...s,
|
2026-07-19 19:42:48 +08:00
|
|
|
password: s.password || '',
|
2026-07-19 17:15:28 +08:00
|
|
|
}));
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getServer(id) {
|
2026-07-19 17:15:28 +08:00
|
|
|
return this.servers.get(id) || null;
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 20:00:33 +08:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
addServer(config) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let id = 'srv_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6);
|
|
|
|
|
let server = {
|
2026-07-18 19:23:50 +08:00
|
|
|
id,
|
2026-07-19 20:00:33 +08:00
|
|
|
...this.buildServerFields(config),
|
2026-07-18 19:23:50 +08:00
|
|
|
status: 'disconnected',
|
2026-07-19 17:15:28 +08:00
|
|
|
topics: [],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.servers.set(id, server);
|
|
|
|
|
this.messages.set(id, []);
|
|
|
|
|
this.publishHistory.set(id, []);
|
|
|
|
|
this.saveData();
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateServer(id, config) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let server = this.servers.get(id);
|
|
|
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-19 20:00:33 +08:00
|
|
|
Object.assign(server, this.buildServerFields(config, server, { keepExistingPassword: true }));
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
this.saveData();
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteServer(id) {
|
|
|
|
|
// 先断开连接
|
2026-07-19 17:15:28 +08:00
|
|
|
this.disconnect(id);
|
|
|
|
|
this.servers.delete(id);
|
|
|
|
|
this.messages.delete(id);
|
|
|
|
|
this.publishHistory.delete(id);
|
|
|
|
|
this.saveData();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 20:00:33 +08:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
// ==================== 连接管理 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
connect(id) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let server = this.servers.get(id);
|
|
|
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 如果已连接,先断开
|
|
|
|
|
if (this.clients.has(id)) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.disconnect(id);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
server.status = 'connecting';
|
|
|
|
|
|
|
|
|
|
this.notify('status-change', { id, status: 'connecting' });
|
2026-07-18 19:23:50 +08:00
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let protocol = server.protocol || 'mqtt';
|
|
|
|
|
let url = `${protocol}://${server.host}:${server.port}`;
|
2026-07-18 19:23:50 +08:00
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let options = {
|
2026-07-18 19:23:50 +08:00
|
|
|
clientId: server.clientId,
|
|
|
|
|
username: server.username || undefined,
|
|
|
|
|
password: server.password || undefined,
|
|
|
|
|
keepalive: server.keepAlive,
|
|
|
|
|
clean: server.cleanSession,
|
2026-07-19 18:36:03 +08:00
|
|
|
reconnectPeriod: 0,
|
|
|
|
|
connectTimeout: server.connectTimeout,
|
2026-07-19 17:15:28 +08:00
|
|
|
};
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
try {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let client = mqtt.connect(url, options);
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.on('connect', () => {
|
2026-07-19 17:15:28 +08:00
|
|
|
server.status = 'connected';
|
|
|
|
|
this.notify('status-change', { id, status: 'connected' });
|
2026-07-19 18:36:03 +08:00
|
|
|
// 只在首次连接成功后,再根据配置启用自动重连
|
|
|
|
|
client.options.reconnectPeriod = server.reconnect ? server.reconnectInterval : 0;
|
2026-07-18 19:23:50 +08:00
|
|
|
// 自动订阅已有主题
|
|
|
|
|
server.topics.forEach((t) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
this._doSubscribe(id, t.topic, { qos: t.qos });
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.on('error', (err) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error(`[${server.name}] 连接错误:`, err.message);
|
|
|
|
|
server.status = 'error';
|
|
|
|
|
this.notify('status-change', { id, status: 'error', error: err.message });
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.on('close', () => {
|
|
|
|
|
if (server.status === 'connected') {
|
2026-07-19 17:15:28 +08:00
|
|
|
server.status = 'disconnected';
|
2026-07-18 19:23:50 +08:00
|
|
|
server.topics.forEach((t) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
t.subscribed = false;
|
|
|
|
|
});
|
|
|
|
|
this.notify('status-change', { id, status: 'disconnected' });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.on('message', (topic, payload, packet) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let msg = {
|
|
|
|
|
time: new Date().toLocaleTimeString('zh-CN', { hour12: false }) + '.' + String(Date.now() % 1000).padStart(3, '0'),
|
|
|
|
|
topic: topic,
|
2026-07-18 19:23:50 +08:00
|
|
|
payload: payload.toString(),
|
|
|
|
|
qos: packet.qos,
|
|
|
|
|
retain: packet.retain,
|
|
|
|
|
direction: 'sub',
|
2026-07-19 17:15:28 +08:00
|
|
|
id: this.nextMsgId(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let msgs = this.messages.get(id) || [];
|
|
|
|
|
|
|
|
|
|
msgs.unshift(msg);
|
|
|
|
|
|
|
|
|
|
if (msgs.length > 500) {
|
|
|
|
|
msgs.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.messages.set(id, msgs);
|
|
|
|
|
this.notify('message', { serverId: id, message: msg });
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.clients.set(id, client);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
} catch (err) {
|
2026-07-19 17:15:28 +08:00
|
|
|
server.status = 'error';
|
|
|
|
|
this.notify('status-change', { id, status: 'error', error: err.message });
|
|
|
|
|
return false;
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disconnect(id) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let client = this.clients.get(id);
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (client) {
|
2026-07-19 17:15:28 +08:00
|
|
|
client.end(true);
|
|
|
|
|
this.clients.delete(id);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let server = this.servers.get(id);
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (server) {
|
2026-07-19 17:15:28 +08:00
|
|
|
server.status = 'disconnected';
|
2026-07-18 19:23:50 +08:00
|
|
|
server.topics.forEach((t) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
t.subscribed = false;
|
|
|
|
|
});
|
|
|
|
|
this.notify('status-change', { id, status: 'disconnected' });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 订阅管理 ====================
|
|
|
|
|
_doSubscribe(serverId, topic, opts) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let client = this.clients.get(serverId);
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!client || !server || server.status !== 'connected') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.subscribe(topic, opts, (err) => {
|
|
|
|
|
if (err) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error(`订阅失败 [${topic}]:`, err.message);
|
|
|
|
|
this.notify('subscription-error', { serverId, topic, error: err.message });
|
2026-07-18 19:23:50 +08:00
|
|
|
} else {
|
2026-07-19 17:15:28 +08:00
|
|
|
let t = server.topics.find((t) => t.topic === topic);
|
|
|
|
|
if (t) {
|
|
|
|
|
t.subscribed = true;
|
|
|
|
|
}
|
|
|
|
|
this.notify('subscription-change', { serverId, topic, subscribed: true });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addTopic(serverId, topic, qos = 1) {
|
2026-07-19 17:15:28 +08:00
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
if (!server) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (server.topics.find((t) => t.topic === topic)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let topicId = 'topic_' + Date.now() + '_' + Math.random().toString(36).slice(2, 6);
|
|
|
|
|
|
|
|
|
|
server.topics.push({ id: topicId, topic, qos, subscribed: false });
|
|
|
|
|
|
|
|
|
|
this.saveData();
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 如果已连接,自动订阅
|
|
|
|
|
if (server.status === 'connected') {
|
2026-07-19 17:15:28 +08:00
|
|
|
this._doSubscribe(serverId, topic, { qos });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeTopic(serverId, topic) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 先取消订阅
|
2026-07-19 17:15:28 +08:00
|
|
|
this.unsubscribe(serverId, topic);
|
|
|
|
|
|
|
|
|
|
server.topics = server.topics.filter((t) => t.topic !== topic);
|
|
|
|
|
|
|
|
|
|
this.saveData();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscribe(serverId, topic) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let t = server.topics.find((t) => t.topic === topic);
|
|
|
|
|
|
|
|
|
|
if (!t) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._doSubscribe(serverId, topic, { qos: t.qos });
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsubscribe(serverId, topic) {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
let client = this.clients.get(serverId);
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!client || !server || server.status !== 'connected') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.unsubscribe(topic, {}, (err) => {
|
|
|
|
|
if (!err) {
|
2026-07-19 17:15:28 +08:00
|
|
|
let t = server.topics.find((t) => t.topic === topic);
|
|
|
|
|
if (t) {
|
|
|
|
|
t.subscribed = false;
|
|
|
|
|
}
|
|
|
|
|
this.notify('subscription-change', { serverId, topic, subscribed: false });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateTopic(serverId, topicId, newTopic, newQos) {
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let t = server.topics.find((item) => item.id === topicId);
|
|
|
|
|
|
|
|
|
|
if (!t) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let topicChanged = t.topic !== newTopic;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
if (
|
|
|
|
|
topicChanged &&
|
|
|
|
|
server.topics.find((item) => item.topic === newTopic && item.id !== topicId)
|
|
|
|
|
) {
|
2026-07-19 17:15:28 +08:00
|
|
|
return false;
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let oldTopic = t.topic;
|
|
|
|
|
let wasSubscribed = t.subscribed;
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 如果主题名变更且已订阅,先取消旧主题订阅
|
|
|
|
|
if (topicChanged && wasSubscribed) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.unsubscribe(serverId, oldTopic);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
t.topic = newTopic;
|
|
|
|
|
t.qos = newQos;
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 如果已连接且此前已订阅,重新订阅新主题
|
|
|
|
|
if (server.status === 'connected' && wasSubscribed) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this._doSubscribe(serverId, newTopic, { qos: newQos });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
this.saveData();
|
2026-07-18 19:23:50 +08:00
|
|
|
this.notify('subscription-change', {
|
|
|
|
|
serverId,
|
|
|
|
|
topic: newTopic,
|
|
|
|
|
subscribed: t.subscribed,
|
2026-07-19 17:15:28 +08:00
|
|
|
topicId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subscribeAll(serverId) {
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let client = this.clients.get(serverId);
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!client || !server || server.status !== 'connected') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let topicsToSubscribe = server.topics.filter((t) => !t.subscribed);
|
|
|
|
|
|
|
|
|
|
if (topicsToSubscribe.length === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2026-07-18 19:23:50 +08:00
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let subMap = Object.fromEntries(topicsToSubscribe.map((t) => [t.topic, { qos: t.qos }]));
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
client.subscribe(subMap, (err) => {
|
|
|
|
|
if (err) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error('批量订阅失败:', err.message);
|
2026-07-18 19:23:50 +08:00
|
|
|
topicsToSubscribe.forEach((t) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.notify('subscription-error', { serverId, topic: t.topic, error: err.message });
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
} else {
|
|
|
|
|
topicsToSubscribe.forEach((t) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
t.subscribed = true;
|
2026-07-18 19:23:50 +08:00
|
|
|
this.notify('subscription-change', {
|
|
|
|
|
serverId,
|
|
|
|
|
topic: t.topic,
|
|
|
|
|
subscribed: true,
|
2026-07-19 17:15:28 +08:00
|
|
|
topicId: t.id,
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 消息发布 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
publish(serverId, topic, payload, opts = {}) {
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let client = this.clients.get(serverId);
|
|
|
|
|
let server = this.servers.get(serverId);
|
|
|
|
|
|
|
|
|
|
if (!client || !server || server.status !== 'connected') {
|
|
|
|
|
return false;
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-19 17:15:28 +08:00
|
|
|
let pubOpts = {
|
|
|
|
|
qos: opts.qos ?? 1,
|
|
|
|
|
retain: opts.retain ?? false,
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
client.publish(topic, payload, pubOpts, (err) => {
|
|
|
|
|
if (err) {
|
2026-07-19 17:15:28 +08:00
|
|
|
console.error(`发布失败 [${topic}]:`, err.message);
|
|
|
|
|
this.notify('publish-error', { serverId, topic, error: err.message });
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 记录发布历史
|
2026-07-19 17:15:28 +08:00
|
|
|
let pubEntry = {
|
2026-07-18 19:23:50 +08:00
|
|
|
time:
|
|
|
|
|
new Date().toLocaleTimeString('zh-CN', { hour12: false }) +
|
|
|
|
|
'.' +
|
|
|
|
|
String(Date.now() % 1000).padStart(3, '0'),
|
|
|
|
|
topic,
|
|
|
|
|
payload,
|
|
|
|
|
qos: pubOpts.qos,
|
|
|
|
|
retain: pubOpts.retain,
|
2026-07-19 17:15:28 +08:00
|
|
|
direction: 'pub',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let history = this.publishHistory.get(serverId) || [];
|
|
|
|
|
|
|
|
|
|
history.unshift(pubEntry);
|
|
|
|
|
|
|
|
|
|
if (history.length > 50) {
|
|
|
|
|
history.pop();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
|
|
|
|
this.publishHistory.set(serverId, history);
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// 同时添加到消息日志
|
2026-07-19 17:15:28 +08:00
|
|
|
let pubMsgId = this.nextMsgId();
|
|
|
|
|
let msgs = this.messages.get(serverId) || [];
|
|
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
msgs.unshift({
|
|
|
|
|
...pubEntry,
|
2026-07-19 17:15:28 +08:00
|
|
|
id: pubMsgId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (msgs.length > 500) {
|
|
|
|
|
msgs.pop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.messages.set(serverId, msgs);
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
this.notify('message', {
|
|
|
|
|
serverId,
|
|
|
|
|
message: {
|
|
|
|
|
...pubEntry,
|
2026-07-19 17:15:28 +08:00
|
|
|
id: pubMsgId,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.saveData();
|
|
|
|
|
|
|
|
|
|
return true;
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 消息管理 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
getMessages(serverId) {
|
2026-07-19 17:15:28 +08:00
|
|
|
return this.messages.get(serverId) || [];
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getPublishHistory(serverId) {
|
2026-07-19 17:15:28 +08:00
|
|
|
return this.publishHistory.get(serverId) || [];
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearMessages(serverId) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.messages.set(serverId, []);
|
|
|
|
|
this.saveData();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearPublishHistory(serverId) {
|
2026-07-19 17:15:28 +08:00
|
|
|
this.publishHistory.set(serverId, []);
|
|
|
|
|
this.saveData();
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 单例
|
2026-07-19 17:15:28 +08:00
|
|
|
const mqttManager = new MqttManager();
|
|
|
|
|
export default mqttManager;
|