feat: 初始版本,支持配置和连接多个 MQTT 服务器,支持配置订阅的主题列表,支持消息历史记录

This commit is contained in:
2026-07-18 19:23:50 +08:00
parent d0bbd32d08
commit 32643c2518
17 changed files with 2931 additions and 34 deletions
+119
View File
@@ -0,0 +1,119 @@
import { ipcMain, BrowserWindow } from 'electron'
import mqttManager from './mqtt-manager.js'
export function registerIpcHandlers() {
// 获取主窗口(用于推送事件)
function getMainWindow() {
const windows = BrowserWindow.getAllWindows()
return windows.length > 0 ? windows[0] : null
}
// 向渲染进程发送事件
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:connect', (_, id) => {
return mqttManager.connect(id)
})
ipcMain.handle('mqtt:disconnect', (_, id) => {
mqttManager.disconnect(id)
return true
})
// ==================== 主题管理 ====================
ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos) => {
return mqttManager.addTopic(serverId, topic, qos)
})
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) => {
return mqttManager.updateTopic(serverId, topicId, topic, qos)
})
// ==================== 消息发布 ====================
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
})
// 兼容旧的 ping 测试
ipcMain.on('ping', () => console.log('pong'))
}