-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
480 lines (440 loc) · 11.6 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
const {
app,
BrowserWindow,
desktopCapturer,
ipcMain,
} = require("electron/main");
const path = require("path");
const log = require("electron-log");
const fs = require("fs");
const oscServer = require("node-osc");
const { electron } = require("process");
const { shell } = require("electron");
//const exec = require("child_process").exec;
const util = require("util");
const exec = util.promisify(require("node:child_process").exec);
//macOS microphone permission
// const { systemPreferences } = require('electron')
// const microphone = systemPreferences.askForMediaAccess('microphone');
//#region log file
//Create log file of Pose landmarks
console.log = log.log;
log.transports.console.format = "[{h}:{i}:{s}.{ms}] {text}";
log.transports.console.level = "error";
log.transports.file.format = "[{h}:{i}:{s}.{ms}] {text}";
log.transports.file.maxSize = 5242880;
log.transports.file.archiveLog = (file) => {
file = file.toString();
const info = path.parse(file);
let currentDate = new Date().toJSON().slice(0, 19).replaceAll(":", "_");
try {
fs.renameSync(
file,
path.join(info.dir, info.name + currentDate + info.ext)
);
} catch (e) {
console.warn("Could not rotate log", e);
}
};
log.initialize();
log.info("Log from the main process");
//#endregion
//window variables
let mainWindow,
poseWindow,
audioWindow,
midiWindow,
gamepadWindow,
oscWindow,
sentimentWindow,
rtcWindow;
var windowSetup, source;
var webSocketDetails = {
websocketIP: "",
websocketPort: "",
websocketPassword: "",
};
//#region When app starts Create main window
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
//#endregion
//#region create main window
async function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1000,
height: 700,
x: 0,
y: 0,
title: __dirname,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
// and load the index.html of the app.
mainWindow.loadFile("index.html");
// Open the DevTools.
//mainWindow.webContents.openDevTools();
}
//#endregion
//#region Open-Pose-windows
ipcMain.on(
"open-pose-window",
(event, IP, Port, PW, projectorID, sourceName) => {
console.log("main received IPC");
poseWindow = new BrowserWindow({
width: 800,
height: 1000,
x: 100,
y: 100,
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "pose-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
windowID: projectorID,
sourceName: sourceName,
};
//console.log(windowSetup)
poseWindow.loadFile("pose.html");
}
);
//#endregion
//#region Open-audioinput-windows
ipcMain.on(
"open-audioinput-window",
(event, IP, Port, PW, inputID, sourceName) => {
console.log("main received IPC");
audioWindow = new BrowserWindow({
width: 400,
height: 400,
x: 200,
y: 100,
title: sourceName,
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "audioInput-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
inputID: inputID,
};
//console.log(windowSetup)
audioWindow.loadFile("audioInput.html");
//poseWindow.webContents.openDevTools()
}
);
//#endregion
//#region Open-gamepad-windows
ipcMain.on(
"open-gamepad-window",
(event, IP, Port, PW) => {
console.log("main received gamepad IPC");
gamepadWindow = new BrowserWindow({
width: 1000,
height: 600,
x: 200,
y: 100,
title: "GamePad Input",
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "gamepad-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
};
//console.log(windowSetup)
gamepadWindow.loadFile("gamepad.html");
//poseWindow.webContents.openDevTools()
}
);
//#endregion
//#region Open-midi-windows
ipcMain.on(
"open-midi-window",
(event, IP, Port, PW) => {
console.log("main received IPC");
midiWindow = new BrowserWindow({
width: 500,
height: 600,
x: 200,
y: 100,
title: "Midi Input",
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "midi-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
};
//console.log(windowSetup)
midiWindow.loadFile("midi.html");
//poseWindow.webContents.openDevTools()
}
);
//#endregion
//#region Open-osc-windows
ipcMain.on(
"open-osc-window",
(event, IP, Port, PW, oscIP, oscInPORT, oscOutPORT) => {
console.log("main received osc window IPC");
oscWindow = new BrowserWindow({
width: 400,
height: 400,
x: 200,
y: 100,
title: "osc window",
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
sandbox: false,
nodeIntegration: true,
backgroundThrottling: false,
preload: path.join(__dirname, "osc-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
oscInPORT: oscInPORT,
oscOutPORT: oscOutPORT,
oscIP: oscIP,
};
//console.log(windowSetup)
oscWindow.loadFile("osc.html");
//oscWindow.webContents.openDevTools()
}
);
//#endregion
//#region Open-sentiment-windows
ipcMain.on("open-sentiment-window", (event, IP, Port, PW) => {
console.log("main received sentiment IPC");
sentimentWindow = new BrowserWindow({
width: 800,
height: 1000,
x: 100,
y: 100,
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "sentiment-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
};
//console.log(windowSetup)
sentimentWindow.loadFile("sentiment.html");
//sentimentWindow.webContents.openDevTools()
});
//#endregion
//#region Open-webRTC-window
ipcMain.on("open-rtc-window",
(event, IP, Port, PW, rtcPort, rtcVideoId, rtcAudioId, rtcType) => {
console.log("main received rtc IPC");
rtcWindow = new BrowserWindow({
width: 400,
height: 200,
x: 100,
y: 100,
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "rtc-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
rtcClientID: rtcPort,
rtcVideoId:rtcVideoId,
rtcAudioId: rtcAudioId,
rtcType: rtcType
};
//console.log(rtcType);
rtcWindow.loadFile("rtc.html");
}
);
//#endregion
//#region Open-ptz-windows
ipcMain.on("open-ptz-window", (event, IP, Port, PW) => {
console.log("main received ptz IPC");
ptzWindow = new BrowserWindow({
width: 400,
height: 400,
x: 100,
y: 100,
frame: true,
resizable: true,
//roundedCorners: false,
movable: true,
titleBarOverlay: false,
transparent: false,
titleBarStyle: "default",
webPreferences: {
backgroundThrottling: false,
preload: path.join(__dirname, "ptz-preload.js"),
},
});
windowSetup = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
};
//console.log(windowSetup)
ptzWindow.loadFile("ptz.html");
//ptzWindow.webContents.openDevTools();
});
//#region IPC APIs
//Send websocket and projector window to renderer
ipcMain.handle("get-obsWSdetails", async () => {
console.log("sending window details to a new window");
console.log(windowSetup);
return windowSetup;
});
//#region IPC send desktop sources list to main window
ipcMain.handle("get-desktop-sources", async () => {
console.log("getting sources with handler");
var results = [];
results = await desktopCapturer
.getSources({ types: ["window", "screen"] })
.then(async (sources) => {
return sources.map((source) => {
source.name;
return source;
});
});
//console.log(results)
return results;
//map example
//console.log(results.map((result) => [result.name, result.id]));
});
//#endregion
//#region IPC set OBS connection
ipcMain.on("set-obs-connection", async (event, IP, Port, PW) => {
console.log("setting OBS Connection");
webSocketDetails = {
websocketIP: IP,
websocketPort: Port,
websocketPassword: PW,
};
console.log(
webSocketDetails.websocketIP,
webSocketDetails.websocketPort,
webSocketDetails.websocketPassword
);
//write to file
let sData = JSON.stringify(webSocketDetails);
const userpath = app.getPath("userData");
fs.writeFileSync(`${userpath}/webSocket_server_setting.txt`, sData);
});
ipcMain.handle("get-obs-connection", async (event) => {
console.log("set OBS connection defaults");
const userpath = app.getPath("userData");
console.log(userpath);
//console.log(fs.exists(`${userpath}/webSocket_server_setting.txt`))
if (fs.existsSync(`${userpath}/webSocket_server_setting.txt`)) {
let dt = fs.readFileSync(`${userpath}/webSocket_server_setting.txt`);
let data = JSON.parse(dt);
console.log(data);
return data;
}
});
ipcMain.on("wsConnect", (event) => {
console.log("sending websocket details to new window");
console.log(webSocketDetails);
event.returnValue = webSocketDetails;
});
//#endregion
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
//Commandline process function
//get the PTZ camera position and send the data to a renderer
ipcMain.handle("run-ptz-command-line", async (event, cmd) => {
var { stdout, stderr } = await exec(cmd);
if (stdout.includes("zoom")) {
stdout = `{${stdout}}`;
}
stdout = stdout.substring(0, stdout.indexOf("}") + 1);
stdout = stdout.replace("{", '{"').replace(",", ',"').replace(/(=)/gm, '":');
stdout = JSON.parse(stdout);
return JSON.stringify(stdout);
//console.error("stderr:", stderr);
});