-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
109 lines (89 loc) · 2.37 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const { spawn } = require('child_process');
const findAvailablePort = require('./src/renderer/src/network/utils/findAvailablePort');
let window;
let pyprocess;
async function initializeApp() {
try {
const data_port = await findAvailablePort(8000, 8200);
const frames_port = await findAvailablePort(8201, 8400);
createMainWindow();
startPython(data_port, frames_port);
window.webContents.on('did-finish-load', () => {
window.webContents.send('set-port', { data_port, frames_port });
});
} catch (error) {
console.error('Erro ao encontrar uma porta disponível:', error);
app.quit();
}
}
function createMainWindow() {
window = new BrowserWindow({
title: 'LibrasController',
minWidth: 1280,
minHeight: 720,
backgroundColor: "#141414",
titleBarStyle: 'default',
autoHideMenuBar: true,
icon: path.join(__dirname, 'favicon.ico'),
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
devTools: true,
preload: path.join(__dirname, 'src/renderer/preload.js')
}
});
window.loadURL(`file://${path.join(__dirname, '/build/index.html')}`);
window.on('closed', () => {
window = null;
});
}
function startPython(port1, port2) {
pyprocess = spawn('python', [path.join(__dirname, 'src/main/main.py'), port1.toString(), port2.toString()]);
pyprocess.stdout.on('data', (data) => {
console.log("Iniciando Python");
console.log(`Output: ${data}`);
});
pyprocess.stderr.on('data', (data) => {
console.error(`Erro: ${data}`);
});
pyprocess.on('close', (code) => {
console.log(`Python fechou com o código: ${code}`);
app.quit();
});
}
let ports = {
data_port: 8000,
frames_port: 8201
};
ipcMain.handle('getDataPort', () => {
return ports.data_port;
});
ipcMain.handle('getFramesPort', () => {
return ports.frames_port;
});
ipcMain.handle('getPort', () => {
return ports;
});
app.whenReady().then(() => {
initializeApp();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
initializeApp();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
if (pyprocess) {
pyprocess.kill();
}
app.quit();
}
});
app.on('before-quit', () => {
if (pyprocess) {
pyprocess.kill();
}
});