-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
278 lines (251 loc) · 6.78 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const electron = require('electron')
const settings = require('electron-settings')
const mqtt = require('mqtt')
const {ipcMain} = require('electron')
const isDev = require('electron-is-dev')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
let mc = null // mqttClient
let mqttConnected = false
let settingsWin = null
let welcomeWin = null
let windows = [] // keeps references of all windows
//
// configuration defaults
//
let config = {
mqtt: {
enabled: true,
url: 'tcp://localhost',
ns: 'your/custom/namespace',
},
windows: [],
reloadTimeout: 5,
}
function initApp() {
// load persistant configuration
loadConfiguration()
// setup app menu
createMenu()
// start MQTT listener
if(config.mqtt.enabled) mqttConnect(config.mqtt)
// add monitor handler
initDisplayHandling()
// open windows
if(config.windows.length > 0) {
createWindows()
}else{
openWelcome()
}
}
function initDisplayHandling() {
electron.screen.on('display-added', (evt, disp) => {
createWindow(windows.length, disp)
})
}
function createMenu() {
const template = [{
label: app.getName(),
submenu: [
{role: 'about'},
{type: 'separator'},
{
label: 'Preferences',
click () { openSettings() }
},
{type: 'separator'},
{role: 'quit'}
]
}
]
if(isDev) {
template[0].submenu.push({type: 'separator'})
template[0].submenu.push({
label: 'Dev-Tools',
click () { require('electron-debug')({showDevTools: true}) }
})
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
function openSettings() {
if(settingsWin) return
settingsWin = new BrowserWindow({
frame: false,
height: 600,
resizable: false,
width: 500,
fullscreen: false,
})
settingsWin.loadURL('file://' + __dirname + '/app/settings.html')
settingsWin.on('closed', function () { settingsWin = null })
}
function openWelcome() {
if(welcomeWin) return
welcomeWin = new BrowserWindow({
frame: false,
height: 300,
resizable: false,
width: 600,
fullscreen: false,
})
welcomeWin.loadURL('file://' + __dirname + '/app/welcome.html')
welcomeWin.on('closed', function () { welcomeWin = null })
// welcomeWin.webContents.openDevTools()
}
function loadConfiguration() {
console.log('Current settings path: ', settings.file())
const c = settings.getAll()
if(Object.keys(c).length > 0) config = c
else settings.setAll(config) // use defaults
// add watch handler
settings.watch('reloadTimeout', (newValue, oldValue) => {
console.log('reloadTimeout changed from ' + oldValue + ' to ' + newValue)
config = settings.getAll()
})
}
function mqttConnect(cfg) {
mc = mqtt.connect(cfg.url, {rejectUnauthorized: false})
mc.on('connect', function () {
console.log('MQTT connected to to: ', cfg.url)
mc.subscribe(cfg.ns + '/#')
console.log('MQTT subscription: ', cfg.ns + '/#')
mc.publish(cfg.ns + '/status', 'connected')
mqttConnected = true
})
mc.on('message', function (topic, message) {
const action = topic.toString().split('/').pop()
const payload = message.toString()
const winNum = parseInt(payload)
// check action keyword
switch (action) {
case 'reload':
if(winNum >= 0) {
console.log('Reload browser window ' + winNum + '.')
if(windows[winNum]) windows[winNum].reload()
} else {
console.log('Reload all browser windows.')
for (var i = 0; i < windows.length; i++) {
if(windows[i]) windows[i].reload()
}
}
break
case 'open-dev-tools':
console.log('Open all developer tool windows.')
for (var i = 0; i < windows.length; i++) {
if(windows[i]) windows[i].webContents.openDevTools()
}
break
case 'toggle-dev-tools':
console.log('Open or close all developer tool windows.')
for (var i = 0; i < windows.length; i++) {
if(windows[i]) {
if(windows[i].webContents.isDevToolsOpened()) {
windows[i].webContents.closeDevTools()
}else{
windows[i].webContents.openDevTools()
}
}
}
break
}
})
}
function mqttReconnect() {
if(mc) {
mc.end(() => {
console.log('MQTT server connection closed, trying to reconnect.')
loadConfiguration()
mqttConnect(config.mqtt)
})
}
}
function createWindow(wndId, dsp) {
// Create the browser window for dsp
windows[wndId] = new BrowserWindow({
x: dsp.bounds.x,
y: dsp.bounds.y,
width: dsp.size.width,
height: dsp.size.height,
frame: false,
})
const wnd = windows[wndId]
wnd.loadURL(config.windows[wndId].url)
// add window events
wnd.on('closed', () => windows[wndId] = null)
wnd.webContents.on('did-fail-load', function(e) {
const timeout = (config.reloadTimeout) ? parseInt(config.reloadTimeout) * 1000 : false
console.log('Start reload in ' + timeout + ' milliseconds.')
if(windows[wndId] && timeout) {
setTimeout(() => {
console.log('Reload Window with ID: ' + wndId)
wnd.reload()
}, timeout)
}
})
// switch to fullscreen
// TODO add to monitor settings
wnd.setFullScreen(true)
}
function createWindows() {
let displays = electron.screen.getAllDisplays()
windows = []
for (var i = 0; i < displays.length; i++) {
const wndId = i
if(displays[wndId] && config.windows[wndId]) {
createWindow(wndId, displays[wndId])
}
}
}
app.on('ready', initApp)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (windows[0] === null) {
createWindow()
}
})
ipcMain.on('close-settings-window', function () {
if (settingsWin) {
settingsWin.close()
}
})
ipcMain.on('save-and-close-settings-window', function (evt, c) {
if (settingsWin) {
settings.setAll(c)
config = settings.getAll()
settingsWin.close()
}
})
ipcMain.on('open-settings-window', function () {
if(settingsWin) return
openSettings()
})
ipcMain.on('close-welcome-window', function () {
if (welcomeWin) {
welcomeWin.close()
}
})
ipcMain.on('get-mqtt-status', (event, arg) => {
event.sender.send('mqtt-status', mqttConnected)
})
ipcMain.on('mqtt-reconnect', () => {
console.log('mqtt-reconnect')
mqttReconnect()
})
ipcMain.on('reload-window-by-id', function (evt, id) {
const wndId = parseInt(id)
console.log('reload-window-by-id', wndId)
if (windows[wndId]) {
windows[wndId].loadURL(config.windows[wndId].url)
}else{
let displays = electron.screen.getAllDisplays()
if(displays[wndId] && config.windows[wndId]) {
createWindow(wndId, displays[wndId])
}
}
})