Skip to content

Commit

Permalink
Add more methods for Electron
Browse files Browse the repository at this point in the history
  • Loading branch information
Chi-EEE committed Mar 15, 2024
1 parent 3946922 commit b1c9691
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
51 changes: 45 additions & 6 deletions app/admin_panel/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class WebSocketServer {
/** @type {WebSocket.Server | undefined} */
this._wss = undefined;

/** @type {number | undefined} */
this._port = undefined;

/** @type {Code} */
this._code = new Code();

Expand All @@ -131,6 +134,7 @@ class WebSocketServer {
*/
connect(port) {
this._wss = new WebSocket.Server({ port: port });
this._port = port;
}

/**
Expand All @@ -142,11 +146,15 @@ class WebSocketServer {
}
}

code() {
generateCode() {
this._code.generate();
return this._code.get();
}

getCode() {
return this._code.get();
}

/**
* This function only allows a single connection to the WebSocket server.
*/
Expand All @@ -156,7 +164,7 @@ class WebSocketServer {
// console.log(req.socket.remoteAddress)
const uuid = crypto.randomUUID();
this._raspberry_pi_clients.set(uuid, ws);

ws.send(JSON.stringify({ uuid: uuid }));

mainWindow.webContents.send('onConnection', JSON.stringify({ uuid: uuid }));
Expand All @@ -169,7 +177,7 @@ class WebSocketServer {
});

ws.on('message', async (message) => {
if (this._client === ws) {
if (uuid !== this._client.uuid) {
return;
}
mainWindow.webContents.send('onMessage', message.toString());
Expand All @@ -189,7 +197,7 @@ async function startWebSocketServer(_event, args) {
try {
websocket_server.connect(args.port);
websocket_server.createConnection();
return { success: true, code: websocket_server.code() };
return { success: true, code: websocket_server.generateCode() };
} catch (error) {
return { success: false, message: `Unable to start WebSocket Server, Error: ${error}` };
}
Expand All @@ -199,11 +207,34 @@ function closeWebSocketServer(_event, _args) {
websocket_server.close();
}

function getWebSocketServer(_event, _args) {
if (websocket_server._wss !== undefined) {
return { success: true, code: websocket_server.getCode(), port: websocket_server._port };
}
else {
return { success: false };
}
}

function onClose() {
closeWebSocketServer();
}

function connectToRaspberryPi(_event, args) {
function connectRaspberryPi(_event, args) {
const uuid = args.uuid;
if (websocket_server._raspberry_pi_clients.has(uuid)) {
websocket_server._client = { uuid: uuid, ws: websocket_server._raspberry_pi_clients.get(uuid) };
return { success: true, message: `Connected to Raspberry Pi with UUID: ${uuid}` };
} else {
return { success: false, message: `Raspberry Pi with UUID: ${uuid} not found` };
}
}

function disconnectRaspberryPi(_event, _args) {
websocket_server._client = undefined;
}

function connectRaspberryPi(_event, args) {
const uuid = args.uuid;
if (websocket_server._raspberry_pi_clients.has(uuid)) {
websocket_server._client = websocket_server._raspberry_pi_clients.get(uuid);
Expand All @@ -213,9 +244,17 @@ function connectToRaspberryPi(_event, args) {
}
}

function getRaspberryPi(_event, _args) {
return { uuid: websocket_server._client.uuid }
}

app.on('window-all-closed', onClose);
ipcMain.handle('getLocalIPList', getLocalIPList);

ipcMain.handle('startWebSocketServer', startWebSocketServer);
ipcMain.handle('closeWebSocketServer', closeWebSocketServer);
ipcMain.handle('connectToRaspberryPi', connectToRaspberryPi);
ipcMain.handle('getWebSocketServer', getWebSocketServer);

ipcMain.handle('connectRaspberryPi', connectRaspberryPi);
ipcMain.handle('disconnectRaspberryPi', disconnectRaspberryPi);
ipcMain.handle('getRaspberryPi', getRaspberryPi);
5 changes: 4 additions & 1 deletion app/admin_panel/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const WINDOW_API = {
onConnection: (/** @type {(string) => void} */ callback) => ipcRenderer.on('onConnection', (_event, /** @type {string} */ value) => callback(value)),
onDisconnection: (/** @type {(string) => void} */ callback) => ipcRenderer.on('onDisconnection', (_event, /** @type {string} */ value) => callback(value)),

connectToRaspberryPi: (/** @type {string} */ uuid) => ipcRenderer.invoke("connectToRaspberryPi", uuid),
connectRaspberryPi: (/** @type {string} */ uuid) => ipcRenderer.invoke("connectRaspberryPi", uuid),
disconnectRaspberryPi: () => ipcRenderer.invoke("disconnectRaspberryPi"),

getRaspberryPi: () => ipcRenderer.invoke("disconnectRaspberryPi"),
}

contextBridge.exposeInMainWorld("api", WINDOW_API)

0 comments on commit b1c9691

Please sign in to comment.