-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
260 lines (221 loc) · 5.53 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
// Modules to control application life and create native browser window
const electron = require('electron')
const path = require('path')
const mqtt = require('mqtt')
const {app, BrowserWindow} = electron;
const express = require('express');
const proxy = require('express-http-proxy');
const exec = require('child_process').exec
const server = express();
let configID = 3;
let configs = [
{
num:0,
mode:"MACRO",
title:"MACRO PUPITRE GAUCHE",
ip:"192.168.1.50",
urls:[
"/#/macro/pupitre/pupitregauche",
],
},
{
num:1,
mode:"MACRO",
title:"MACRO PUPITRE DROIT",
ip:"192.168.1.51",
urls:[
"/#/macro/pupitre/pupitredroit",
],
},
{
num:2,
mode:"MICRO",
title:"CHAMBRE CLARA",
ip:"?",
urls:[
"/#/bedroom/chest",
"/#/bedroom/pc-interface",
],
},
{
num:3,
mode:"MICRO",
title:"SALON CLARA",
ip:"192.168.1.35",
urls:[
"/#/living-room/chest",
"/#/living-room/television",
],
},
{
num:4,
mode:"MICRO",
title:"MALETTE CHAMBRE",
ip:"192.168.1.16",
urls:[
"/#/headquarter/bedroom-bathroom/database",
"/#/headquarter/bedroom-bathroom/scan",
],
},
{
num:5,
mode:"MICRO",
title:"MALETTE SALON",
ip:"192.168.1.17",
urls:[
"/#/headquarter/kitchen-living-room/scan",
"/#/headquarter/kitchen-living-room/database",
],
},
{
num:6,
mode:"MICRO",
title:"BRIEFING-ROOM",
ip:"192.168.1.5",
urls:[
"/#/briefing/television",
],
},
{
num:7,
mode:"MICRO",
title:"VELLEDA",
ip:"192.168.1.12",
urls:[
"/#/corridor/velleda",
],
},
{
num:8,
mode:"MICRO",
title:"TESTS",
ip:"RTC",
urls:[
"/#/rtc/emiter/dev",
"/#/rtc/receiver/dev",
],
}
]
const environnement = configs[configID].mode;
let urls = configs[configID].urls;
let mqttBrokerUrl = 'mqtt://192.168.1.43:1883';
let baseUrl = "http://192.168.1.43:8080"
let serveUrl = "http://localhost:3000"
if(environnement == "MACRO"){
mqttBrokerUrl = 'mqtt://192.168.1.44:1883';
baseUrl = "http://192.168.1.44:8080"
// serveUrl = "file://public"
//server.use('/', express.static("public"));
server.use('/', proxy(baseUrl));
}
else{
server.use('/', proxy(baseUrl));
}
server.listen(3000, function () {})
let windows = [];
function createWindows () {
// Create the browser window.
const displays = electron.screen.getAllDisplays()
for(let i = 0; i<urls.length; i++) {
const url = urls[i];
const display = displays[Math.min(i,displays.length-1)];
const { x, y, width, height} = display.workArea;
let window = new BrowserWindow({
x, y, width, height, // set to display dimensions
fullscreen: true,
alwaysOnTop: true,
resizable: true,
movable: true,
frame: false,
show: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// Open the DevTools.
// window.webContents.openDevTools()
// and load the index.html of the app.
window.loadURL(serveUrl + url)
// remove security warning
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = 'true';
windows.push(window);
window.once('ready-to-show', () => {
window.show();
})
// Emitted when the window is closed.
window.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
let index = windows.indexOf(window);
if (index !== -1) windows.splice(index, 1);
})
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindows)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (window.length == 0) createWindows()
})
function reset() {
if (windows.length != 0) {
for (let index = 0; index < windows.length; index++) {
const atomicWindow = windows[index];
atomicWindow.webContents.reloadIgnoringCache()
atomicWindow.reload()
}
}
}
function shutdown() {
exec("shutdown /s")
}
function restart() {
exec("shutdown /r")
}
topicHandlers = [
{
topic:"/display/reset",
handler:(payload) => {
reset();
}
},
{
topic:"/computer/shutdown",
handler:(payload) => {
shutdown();
}
},
{
topic:"/computer/restart",
handler:(payload) => {
restart();
}
}
]
function handleMessage(topic, message) {
topicHandler = topicHandlers.find((t=>t.topic == topic))
if(topicHandler){
topicHandler.handler(message);
}
}
mqttClient = mqtt.connect(mqttBrokerUrl);
mqttClient.on("connect", function (status) {
console.log("MQTT connected with status: ", status)
})
mqttClient.on("message", function (topic, message) {
console.log("mqtt message", topic, message)
handleMessage(topic, message.toString())
})
for (const topicHandler of topicHandlers) {
mqttClient.subscribe(topicHandler.topic)
}