-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
183 lines (155 loc) · 5.08 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const electron = require('electron');
const {app, BrowserWindow, ipcMain} = require('electron');
const log = require('electron-log');
const path = require('path');
const h = require('./modules/Helpers.js');
const LogHandler = require('./modules/LogHandler');
const Serialport2Keybind = require('./modules/Serialport2Keybind.js');
const {conf} = require('./config.js');
const KioskWindow = require('./modules/KioskWindow');
// const {download} = require('electron-dl');
// const fs = require('fs');
const confJson = require('electron-json-config');
const sendkeys = require('sendkeys');
const appData = {
locale : 'et',
idleTimeout : null,
s2Keybind : null,
logHandler : null,
canPressKey : true,
suspended : false,
currentStatus : '',
}
let windows = null;
conf.app.version = app.getVersion();
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true;
const defineWindows = () =>
{
return [
new KioskWindow({
id: 'main',
url: `file://${__dirname}/static/index.html`,
x: conf.display.primary.x, y: conf.display.primary.y,
w: conf.display.primary.w, h: conf.display.primary.h,
show: true,
nodeIntegration: false,
onbeforeunload: null,
}),
];
}
const createAllWindows = (w) => {
w.forEach(win => { createWindow(win);});
};
const createWindow = win => {
// special parent case for daemon window
let parent = null;
if (win.id == 'daemon') parent = h.getWin('main').instance;
// Create the browser window.
win.instance = new BrowserWindow({
name : win.id,
homeUrl : win.url,
title : conf.display.mainWinTitle,
backgroundColor: '#fff',
width: win.w,
height: win.h,
x: win.x,
y: win.y,
parent: parent,
show: win.show,
frame: false,
focusable : true,
resizable: true,
fullscreen: false,
kiosk: false,
// transparent: null,
webPreferences: {
nodeIntegration: win.nodeIntegration,
enableRemoteModule: true,
preload: path.resolve(__dirname, 'renderer.js'),
webSecurity: false
}
});
// load the url of the app.
win.instance.loadURL(win.url);
// hide menu
win.instance.setMenu(null);
// Open the DevTools.
const isOpenDevTools = win.openDevTools != null ? win.openDevTools : conf.display.openDevTools;
if(isOpenDevTools) win.instance.webContents.openDevTools();
// Emitted when the window is closed.
win.instance.on('closed', function () {
win.instance = null;
});
if(win.id === 'main')
{
// close app when main window is closed
win.instance.on('closed', () => {
app.quit();
});
// prevent html meta title override
win.instance.on('page-title-updated', function(e) {
e.preventDefault()
});
}
}
exports.exitApp = () =>
{
windows.forEach(element => {
if(element.instance) element.instance.close();
app.exit();
});
}
exports.handleKeypress = key => {
if(appData.canPressKey == false || appData.isSuspended == true) return;
appData.canPressKey = false;
const msg = {
action : 'keypress',
actionData : key,
timestamp : h.getTimestamp()
}
// send signal to renderer window
h.getWin('main').instance.webContents.send('keypress', msg);
// simulate keystroke
sendkeys(key).then(() =>
{
appData.logHandler.add({type : 'success', data : `Simulated key: ${key}`});
appData.canPressKey = true;
});
}
exports.toggleSuspend = () => {
appData.isSuspended = !appData.isSuspended;
return appData.isSuspended;
}
//////////////////////////////////////////////////////////
////////////////////// APP IS READY //////////////////////
//////////////////////////////////////////////////////////
app.on('ready', function(){
log.info('App starting...');
// check json config file, if no serial conf exists, set default
if(!confJson.has('serial') || confJson.get('serial') == '')
{
confJson.set('serial', conf.serial);
confJson.set('parsing', conf.parsing);
}
// browser window creation
if(conf.display.getScreenDimensions) h.getScreenDimensions(conf.display, electron.screen);
windows = defineWindows();
h.setWindows(windows);
createAllWindows(windows);
h.getWin('main').instance.webContents.once('did-finish-load', () => {
// pass helpers to loghandler so it can
// send ipc messages to renderer
appData.logHandler = new LogHandler(h, log);
appData.s2Keybind = new Serialport2Keybind(confJson, exports.handleKeypress, appData.logHandler);
})
// h.getWin('daemon').instance.hide();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
//#region ipc binds
ipcMain.on('online-status-changed', (event, status) => {
log.info('online-status-changed triggered', status);
});
//#endregion