Skip to content

Commit

Permalink
🎉 project general implementation
Browse files Browse the repository at this point in the history
- Add first version of exemple server
- add server core
  • Loading branch information
Johnatan Duarte committed Feb 16, 2024
1 parent 5fcdb45 commit a8b3970
Show file tree
Hide file tree
Showing 17 changed files with 2,182 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/node_modules
out/
/dist/
package-lock.json
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# IntelbrasModoOnlineElectron
Exemplo de servidor utilizando Electron para uso do modo online dos dispositivos de controle de acesso da intelbras.
30 changes: 30 additions & 0 deletions forge.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
packagerConfig: {
asar: true,
},
rebuildConfig: {},
makers: [
{
name: '@electron-forge/maker-squirrel',
config: {},
},
{
name: '@electron-forge/maker-zip',
platforms: ['darwin'],
},
{
name: '@electron-forge/maker-deb',
config: {},
},
{
name: '@electron-forge/maker-rpm',
config: {},
},
],
plugins: [
{
name: '@electron-forge/plugin-auto-unpack-natives',
config: {},
},
],
};
Binary file added icone.ico
Binary file not shown.
134 changes: 134 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const path = require('path');
const { app, BrowserWindow, Menu, ipcMain } = require('electron');
const isDev = false;
const isMac = process.platform === 'darwin';
const ServerProvider = require('./renderer/js/serverOnline');

let mainWindow;

// Main Window
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1024,
height: 900,
resizable: true,
center: true,
enableRemoteModule: true,
nodeIntegrationInWorker: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
});
mainWindow.loadFile(path.join(__dirname, './renderer/index.html'));
}

// Criar Janela
app.whenReady().then(() => {
createMainWindow();

const mainMenu = Menu.buildFromTemplate(menu);
Menu.setApplicationMenu(mainMenu);

// Clean Memory
mainWindow.on('closed', () => (mainWindow = null));
});

// Menu template
const menu = [
...(isDev
? [
{
label: 'Developer',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{ type: 'separator' },
{ role: 'toggledevtools' },
],
},
]
: []),
];

// -------------------------------------------------------------------------------------------------

async function Startserver(options) {
serverProvider = new ServerProvider(3000);
serverProvider.addRoute('POST', options.path_server, (data, res) => {
mainWindow.webContents.send('server:update_body', data.route.path + '\r\n');

var data_list = data.body.toString().split("--myboundary\r\n");

// trata o body text/plain
if (data_list.length > 0) {
for (var i = 0; i < data_list.length; i++) {
if (data_list[i].includes("Content-Type")) {
var lines = data_list[i].split("\r\n");
var a_type = lines[0].split(": ")[1];
if (a_type === "image/jpeg") {
console.log('Image in event :)')
} else {
var info = lines.slice(3, -1).join("\r\n");

// converte o texto de evento para um objeto do tipo JSON tratando corretamente os caracteres \n
var futuro_json = info.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f")
.replace(/\\/g, '')
.replace(/[\u0000-\u0019]+/g, "");


// replace do --myboundary-- para não dar erro no JSON.parse
futuro_json = futuro_json.replace("--myboundary--", "");

evento_json = JSON.parse(futuro_json);

mainWindow.webContents.send('server:update_body', info.toString());
}
}
}
}

// resposta ao dispositivo
res.send({ "message": options.message, "code": "200", "auth": options.auth.toString() });
});

serverProvider.addRoute('GET', options.path_keepalive, (data, res) => {
res.send('OK');
mainWindow.webContents.send('server:update_body', options.path_keepalive + '\r\n');
});

serverProvider.startServer();
mainWindow.webContents.send('server:update_body', 'INICIADO SERVIDOR -> http://IP_COMPUTADOR:3000' + '\r\n');
mainWindow.webContents.send('server:update_body', 'Rota eventos: /' + options.path_server + '\r\n');
mainWindow.webContents.send('server:update_body', 'Rota keepalive /' + options.path_keepalive + '\r\n');
mainWindow.webContents.send('server:update_body', 'AGUARDANDO EVENTOS... \r\n');

// StopServer
ipcMain.on('server:stop', (e, options) => {
serverProvider.stopServer();
mainWindow.webContents.send('server:update_body', 'SERVIDOR PARADO!' + '\r\n');
});
}

// StartServer
ipcMain.on('server:start', (e, options) => {
Startserver(options);
});

// Close Window
app.on('window-all-closed', () => {
if (!isMac) app.quit();
});

// Open a window if none are open (macOS)
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createMainWindow();
});
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "online-mode-server",
"version": "1.0.0",
"description": "Exemplo de Servidor Modo Online",
"main": "main.js",
"scripts": {
"start": "electron-forge start",
"build": "electron-builder build"
},
"author": "Johnatan Duarte",
"license": "ISC",
"devDependencies": {
"@electron-forge/cli": "^6.4.2",
"@electron-forge/maker-deb": "^6.4.2",
"@electron-forge/maker-rpm": "^6.4.2",
"@electron-forge/maker-squirrel": "^6.4.2",
"@electron-forge/maker-zip": "^6.4.2",
"@electron-forge/plugin-auto-unpack-natives": "^6.4.2",
"autoprefixer": "^10.4.16",
"electron": "^26.2.0",
"electron-builder": "^24.6.4"
},
"dependencies": {
"aes-js": "^3.1.2",
"axios": "^1.6.2",
"body-parser": "^1.20.2",
"buffer": "^6.0.3",
"crypto": "^1.0.1",
"electron-updater": "^6.1.4",
"express": "^4.18.2",
"fs": "^0.0.1-security",
"toastify-js": "^1.12.0"
},
"repository": "https://github.com/johwconst/IntelbrasModoOnlineElectron",
"publish": {
"provider": "github",
"releaseType": "release"
},
"build": {
"appId": "com.IntelbrasModoOnlineElectron.electron.exchange.stream",
"productName": "Bio-T API Shield",
"win": {
"publisherName": "johwconst",
"verifyUpdateCodeSignature": false,
"requestedExecutionLevel": "requireAdministrator",
"icon": "icone.ico",
"publish": [
"github"
],
"target": [
"nsis"
]
}
}
}
19 changes: 19 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const os = require('os');
const path = require('path');
const { contextBridge, ipcRenderer } = require('electron');
window.path = require('path');

contextBridge.exposeInMainWorld('os', {
homedir: () => os.homedir(),
});

contextBridge.exposeInMainWorld('path', {
join: (...args) => path.join(...args),
extname: (...args) => path.extname(...args),
});

contextBridge.exposeInMainWorld('ipcRenderer', {
send: (channel, data) => ipcRenderer.send(channel, data),
on: (channel, func) =>
ipcRenderer.on(channel, (event, ...args) => func(...args)),
});
1 change: 1 addition & 0 deletions renderer/css/boxicons.min.css

Large diffs are not rendered by default.

Loading

0 comments on commit a8b3970

Please sign in to comment.