Files
frost-mqtt-client/app/src/main/ipc-handlers.js
T

144 lines
3.9 KiB
JavaScript

import { ipcMain, BrowserWindow } from 'electron';
import mqttManager from './mqtt-manager.js';
/**
* 注册所有 IPC 处理器
* 将渲染进程的请求转发给 MQTT 管理器,并把 MQTT 事件推送给渲染进程
*/
export function registerIpcHandlers() {
/**
* 获取当前所有窗口中的第一个主窗口
* @returns {BrowserWindow|null} 主窗口实例或 null
*/
function getMainWindow() {
const windows = BrowserWindow.getAllWindows();
return windows.length > 0 ? windows[0] : null;
}
/**
* 向渲染进程发送事件
* @param {string} channel - IPC 通道名称
* @param {*} data - 要发送的数据
*/
function sendToRenderer(channel, data) {
const win = getMainWindow();
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data);
}
}
// 注册 MQTT 管理器的事件监听
mqttManager.addListener((event, data) => {
switch (event) {
case 'status-change':
sendToRenderer('mqtt:status-change', data);
break;
case 'message':
sendToRenderer('mqtt:message', data);
break;
case 'subscription-change':
sendToRenderer('mqtt:subscription-change', data);
break;
case 'subscription-error':
sendToRenderer('mqtt:subscription-error', data);
break;
case 'publish-error':
sendToRenderer('mqtt:publish-error', data);
break;
}
});
// ==================== 服务器管理 ====================
ipcMain.handle('mqtt:get-servers', () => {
return mqttManager.getServers();
});
ipcMain.handle('mqtt:add-server', (_, config) => {
return mqttManager.addServer(config);
});
ipcMain.handle('mqtt:update-server', (_, id, config) => {
return mqttManager.updateServer(id, config);
});
ipcMain.handle('mqtt:delete-server', (_, id) => {
mqttManager.deleteServer(id);
return true;
});
ipcMain.handle('mqtt:export-servers', () => {
return mqttManager.exportServers();
});
ipcMain.handle('mqtt:import-servers', (_, serverList) => {
return mqttManager.importServers(serverList);
});
// ==================== 连接管理 ====================
ipcMain.handle('mqtt:connect', (_, id) => {
return mqttManager.connect(id);
});
ipcMain.handle('mqtt:disconnect', (_, id) => {
mqttManager.disconnect(id);
return true;
});
// ==================== 主题管理 ====================
ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos, comment) => {
return mqttManager.addTopic(serverId, topic, qos, comment);
});
ipcMain.handle('mqtt:remove-topic', (_, serverId, topic) => {
return mqttManager.removeTopic(serverId, topic);
});
ipcMain.handle('mqtt:subscribe', (_, serverId, topic) => {
return mqttManager.subscribe(serverId, topic);
});
ipcMain.handle('mqtt:unsubscribe', (_, serverId, topic) => {
return mqttManager.unsubscribe(serverId, topic);
});
ipcMain.handle('mqtt:subscribe-all', (_, serverId) => {
return mqttManager.subscribeAll(serverId);
});
ipcMain.handle('mqtt:update-topic', (_, serverId, topicId, topic, qos, comment) => {
return mqttManager.updateTopic(serverId, topicId, topic, qos, comment);
});
// ==================== 消息发布 ====================
ipcMain.handle('mqtt:publish', (_, serverId, topic, payload, opts) => {
return mqttManager.publish(serverId, topic, payload, opts);
});
// ==================== 消息管理 ====================
ipcMain.handle('mqtt:get-messages', (_, serverId) => {
return mqttManager.getMessages(serverId);
});
ipcMain.handle('mqtt:get-publish-history', (_, serverId) => {
return mqttManager.getPublishHistory(serverId);
});
ipcMain.handle('mqtt:clear-messages', (_, serverId) => {
mqttManager.clearMessages(serverId);
return true;
});
ipcMain.handle('mqtt:clear-publish-history', (_, serverId) => {
mqttManager.clearPublishHistory(serverId);
return true;
});
}