style: 整理代码格式

This commit is contained in:
2026-07-19 17:15:28 +08:00
parent 6bd13851d9
commit 78797dee86
15 changed files with 1354 additions and 874 deletions
+62 -57
View File
@@ -1,18 +1,20 @@
import { ipcMain, BrowserWindow } from 'electron'
import mqttManager from './mqtt-manager.js'
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
const windows = BrowserWindow.getAllWindows();
return windows.length > 0 ? windows[0] : null;
}
// 向渲染进程发送事件
function sendToRenderer(channel, data) {
const win = getMainWindow()
const win = getMainWindow();
if (win && !win.isDestroyed()) {
win.webContents.send(channel, data)
win.webContents.send(channel, data);
}
}
@@ -20,100 +22,103 @@ export function registerIpcHandlers() {
mqttManager.addListener((event, data) => {
switch (event) {
case 'status-change':
sendToRenderer('mqtt:status-change', data)
break
sendToRenderer('mqtt:status-change', data);
break;
case 'message':
sendToRenderer('mqtt:message', data)
break
sendToRenderer('mqtt:message', data);
break;
case 'subscription-change':
sendToRenderer('mqtt:subscription-change', data)
break
sendToRenderer('mqtt:subscription-change', data);
break;
case 'subscription-error':
sendToRenderer('mqtt:subscription-error', data)
break
sendToRenderer('mqtt:subscription-error', data);
break;
case 'publish-error':
sendToRenderer('mqtt:publish-error', data)
break
sendToRenderer('mqtt:publish-error', data);
break;
}
})
});
// ==================== 服务器管理 ====================
ipcMain.handle('mqtt:get-servers', () => {
return mqttManager.getServers()
})
return mqttManager.getServers();
});
ipcMain.handle('mqtt:add-server', (_, config) => {
return mqttManager.addServer(config)
})
return mqttManager.addServer(config);
});
ipcMain.handle('mqtt:update-server', (_, id, config) => {
return mqttManager.updateServer(id, config)
})
return mqttManager.updateServer(id, config);
});
ipcMain.handle('mqtt:delete-server', (_, id) => {
mqttManager.deleteServer(id)
return true
})
mqttManager.deleteServer(id);
return true;
});
// ==================== 连接管理 ====================
ipcMain.handle('mqtt:connect', (_, id) => {
return mqttManager.connect(id)
})
return mqttManager.connect(id);
});
ipcMain.handle('mqtt:disconnect', (_, id) => {
mqttManager.disconnect(id)
return true
})
mqttManager.disconnect(id);
return true;
});
// ==================== 主题管理 ====================
ipcMain.handle('mqtt:add-topic', (_, serverId, topic, qos) => {
return mqttManager.addTopic(serverId, topic, qos)
})
return mqttManager.addTopic(serverId, topic, qos);
});
ipcMain.handle('mqtt:remove-topic', (_, serverId, topic) => {
return mqttManager.removeTopic(serverId, topic)
})
return mqttManager.removeTopic(serverId, topic);
});
ipcMain.handle('mqtt:subscribe', (_, serverId, topic) => {
return mqttManager.subscribe(serverId, topic)
})
return mqttManager.subscribe(serverId, topic);
});
ipcMain.handle('mqtt:unsubscribe', (_, serverId, topic) => {
return mqttManager.unsubscribe(serverId, topic)
})
return mqttManager.unsubscribe(serverId, topic);
});
ipcMain.handle('mqtt:subscribe-all', (_, serverId) => {
return mqttManager.subscribeAll(serverId)
})
return mqttManager.subscribeAll(serverId);
});
ipcMain.handle('mqtt:update-topic', (_, serverId, topicId, topic, qos) => {
return mqttManager.updateTopic(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)
})
return mqttManager.publish(serverId, topic, payload, opts);
});
// ==================== 消息管理 ====================
ipcMain.handle('mqtt:get-messages', (_, serverId) => {
return mqttManager.getMessages(serverId)
})
return mqttManager.getMessages(serverId);
});
ipcMain.handle('mqtt:get-publish-history', (_, serverId) => {
return mqttManager.getPublishHistory(serverId)
})
return mqttManager.getPublishHistory(serverId);
});
ipcMain.handle('mqtt:clear-messages', (_, serverId) => {
mqttManager.clearMessages(serverId)
return true
})
mqttManager.clearMessages(serverId);
return true;
});
ipcMain.handle('mqtt:clear-publish-history', (_, serverId) => {
mqttManager.clearPublishHistory(serverId)
return true
})
mqttManager.clearPublishHistory(serverId);
return true;
});
// 兼容旧的 ping 测试
ipcMain.on('ping', () => console.log('pong'))
}