2026-07-19 17:15:28 +08:00
|
|
|
import { ipcMain, BrowserWindow } from 'electron';
|
|
|
|
|
|
|
|
|
|
import mqttManager from './mqtt-manager.js';
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
export function registerIpcHandlers() {
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
// 获取主窗口(用于推送事件)
|
|
|
|
|
function getMainWindow() {
|
2026-07-19 17:15:28 +08:00
|
|
|
const windows = BrowserWindow.getAllWindows();
|
|
|
|
|
return windows.length > 0 ? windows[0] : null;
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 向渲染进程发送事件
|
|
|
|
|
function sendToRenderer(channel, data) {
|
2026-07-19 17:15:28 +08:00
|
|
|
const win = getMainWindow();
|
2026-07-18 19:23:50 +08:00
|
|
|
if (win && !win.isDestroyed()) {
|
2026-07-19 17:15:28 +08:00
|
|
|
win.webContents.send(channel, data);
|
2026-07-18 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注册 MQTT 管理器的事件监听
|
|
|
|
|
mqttManager.addListener((event, data) => {
|
|
|
|
|
switch (event) {
|
|
|
|
|
case 'status-change':
|
2026-07-19 17:15:28 +08:00
|
|
|
sendToRenderer('mqtt:status-change', data);
|
|
|
|
|
break;
|
2026-07-18 19:23:50 +08:00
|
|
|
case 'message':
|
2026-07-19 17:15:28 +08:00
|
|
|
sendToRenderer('mqtt:message', data);
|
|
|
|
|
break;
|
2026-07-18 19:23:50 +08:00
|
|
|
case 'subscription-change':
|
2026-07-19 17:15:28 +08:00
|
|
|
sendToRenderer('mqtt:subscription-change', data);
|
|
|
|
|
break;
|
2026-07-18 19:23:50 +08:00
|
|
|
case 'subscription-error':
|
2026-07-19 17:15:28 +08:00
|
|
|
sendToRenderer('mqtt:subscription-error', data);
|
|
|
|
|
break;
|
2026-07-18 19:23:50 +08:00
|
|
|
case 'publish-error':
|
2026-07-19 17:15:28 +08:00
|
|
|
sendToRenderer('mqtt:publish-error', data);
|
|
|
|
|
break;
|
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
|
|
|
ipcMain.handle('mqtt:get-servers', () => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.getServers();
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:add-server', (_, config) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.addServer(config);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:update-server', (_, id, config) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.updateServer(id, config);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:delete-server', (_, id) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
mqttManager.deleteServer(id);
|
|
|
|
|
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
|
|
|
ipcMain.handle('mqtt:connect', (_, id) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.connect(id);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:disconnect', (_, id) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
mqttManager.disconnect(id);
|
|
|
|
|
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
|
|
|
ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.addTopic(serverId, topic, qos);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:remove-topic', (_, serverId, topic) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.removeTopic(serverId, topic);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:subscribe', (_, serverId, topic) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.subscribe(serverId, topic);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:unsubscribe', (_, serverId, topic) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.unsubscribe(serverId, topic);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:subscribe-all', (_, serverId) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.subscribeAll(serverId);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:update-topic', (_, serverId, topicId, topic, qos) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.updateTopic(serverId, topicId, topic, qos);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// ==================== 消息发布 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
ipcMain.handle('mqtt:publish', (_, serverId, topic, payload, opts) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.publish(serverId, topic, payload, opts);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
// ==================== 消息管理 ====================
|
2026-07-19 17:15:28 +08:00
|
|
|
|
2026-07-18 19:23:50 +08:00
|
|
|
ipcMain.handle('mqtt:get-messages', (_, serverId) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.getMessages(serverId);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:get-publish-history', (_, serverId) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
return mqttManager.getPublishHistory(serverId);
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:clear-messages', (_, serverId) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
mqttManager.clearMessages(serverId);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
ipcMain.handle('mqtt:clear-publish-history', (_, serverId) => {
|
2026-07-19 17:15:28 +08:00
|
|
|
mqttManager.clearPublishHistory(serverId);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2026-07-18 19:23:50 +08:00
|
|
|
|
|
|
|
|
}
|