-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
409 lines (261 loc) · 9.91 KB
/
index.js
File metadata and controls
409 lines (261 loc) · 9.91 KB
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
const path = require('path');
global.BaseDir = __dirname;
// Set up paths and filenames, using defaults if these variables are not already defined
// This allows a different startup script to override these settings
global.ConfigDir = global.ConfigDir ?? __dirname;
global.ConfigFile = global.ConfigFile ?? path.join(__dirname, 'config.json');
global.ModuleDir = global.ModuleDir ?? path.join(__dirname, './modules');
global.AltModuleDir = global.AltModuleDir ?? path.join(__dirname, './modules_alt');
global.AuthDir = global.AuthDir ?? path.join(__dirname, './');
const fs = require('fs');
const fsPromises = fs.promises;
// Basic application configuration
global.WebserverPort = 8082;
const KeyboardUI = true; // This enables/disables the keyboard UI for the application
global.UseAlternatePort = false;
// debugging support
global.SoundOff = false; // This makes each module "Sound off" and identify itself on startup
global.ShowAllOSC = false;
///////////////////////////////////////
// Load application configuration
const DefaultConfig =
{
"WebserverPort":8086,
"OSCserverIP":"127.0.0.1",
"OSCserverPortIn":9090,
"OSCserverPortOut":1236,
"PassthroughMessages":false,
"PassthroughIP":"",
"PassthroughPort":1234,
};
let config = DefaultConfig
// We could fall back to the default config if the config file is not found
// but I prefer to ensure a config file is used
if (loadConfig() != true) {
console.log("Failed to load configuration");
process.exit(0);
}
///////////////////////////////////////
// I consider the webserver port as a critical setting, so if it is not found, exit
if (config.WebserverPort != undefined) {
global.WebserverPort = config.WebserverPort;
} else {
console.log("Failed to load configuration: WebserverPort not found");
process.exit(0);
}
// Command line overrides
const args = process.argv.slice(2); // Get command-line arguments
if (args.includes("-dbgosc")) {
global.ShowAllOSC = true;
}
if (args.includes("-altport")) {
global.UseAlternatePort = true;
}
if (args.includes("-altoscports")) {
global.UseAlternateOSCPorts = true;
}
if (global.UseAlternatePort == true) { global.WebserverPort = 8084; }
// These *must* be defined after the webServerPort is set
const {server,broadcastMessage, wsEmitter,registerRoute} = require('./server.js');
global.registerKeypress = function (...args) {
// Forward to the registerKeypress function in util_keys.js
// util_keys should be required here (once and cached)
if (!global._util_keys) {
global._util_keys = require('./util_keys.js');
}
return global._util_keys.registerKeypress(...args);
};
// Module handler
let loadedModules = {};
function registerModule(apiVersion, mod, shortname, title) {
if (apiVersion == "" || !mod || !shortname || !title) {
console.log("Failed to register module: "+shortname+" - Invalid module data");
return false; // Indicate failure
}
global.loadedModules = global.loadedModules || {};
if (global.loadedModules[shortname]) {
console.log("Failed to register module: "+shortname+" - Module with this name already exists");
return false; // Module with this name already exists
}
global.loadedModules[shortname] = {
apiVersion: apiVersion,
mod: mod,
title: title
};
if (global.SoundOff == true) {
console.log(`Registered: ${shortname} - ${title}`);
}
return true; // Indicate success
};
function includeIfExists(filePath) {
if (fs.existsSync(filePath)) {
return require(filePath);
} else {
console.log("Failed to find " + filePath);
}
return null;
}
function loadModule(modName) {
const modulePath = path.join(__dirname, modName);
return includeIfExists(modulePath)
}
// Load keyboard utilities
const { registerKeypress, showKeyboardHelp, handleKeypress } = require('./util_keys.js');
// Install keyboard interface
if (KeyboardUI == true) {
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
// Register a keypress to quit the application so that it appears in the key list
// but we will handle it outside of the keypress handler
registerKeypress('q', () => {
process.exit(0);
}, 'Quit', 'System');
process.stdin.on("keypress", (str, key) => {
if (key.name == 'q') process.exit(0);
handleKeypress(key);
});
}
registerKeypress('h', () => {
showKeyboardHelp();
}, 'Show keyboard help', 'System');
registerKeypress('f1', () => {
showKeyboardHelp();
}, 'Show keyboard help', 'System');
// Install OSC Listener
const OSCmanager = require('./OSCmanager');
let zOSCin = config.OSCserverPortIn;
let zOSCout = config.OSCserverPortOut;
if (global.UseAlternateOSCPorts == true) {
zOSCin = 9191;
zOSCout = 9190;
}
if (global.SoundOff == true) { console.log("OSC Params : "+config.OSCserverIP+" "+zOSCin+" "+zOSCout) }
// Create the default OSC manager instance to talk to ZoomOSC/ISO
let zosc = new OSCmanager(config.OSCserverIP,zOSCin,zOSCout);
if (config.PassthroughMessages == true) zosc.setPassthrough(config.PassthroughIP,config.PassthroughPort);
// install basic broadcast of ZoomOSC/ISO messages to the web UI
zosc.on("OSCmessage",(msg) => {
if (msg.length < 1) return;
if ( msg[0].endsWith('/user/videoOn')) {
}
if ( msg[0].endsWith('/user/videoOn')
|| msg[0].endsWith('/user/videoOff')
|| msg[0].endsWith('/user/mute')
|| msg[0].endsWith('/user/unMute')
|| msg[0].endsWith('/user/userNameChanged')
|| msg[0].endsWith('/user/online')
|| msg[0].endsWith('/user/offline')
|| msg[0].endsWith('/user/handRaised')
|| msg[0].endsWith('/user/handLowered')
){
if (msg.length>4) {
let notification = msg[0].substring(msg[0].indexOf('/user/') + '/user/'.length);
broadcastMessage(notification+":"+msg[4]+":"+msg[2]);
}
}
if (msg[0].endsWith('/user/list') && (msg.length>11)) {
const onlineState = msg[8];
const videoState = msg[9];
const audioState = msg[10];
const handState = msg[11];
broadcastMessage("list-"
+ onlineState
+ videoState
+ audioState
+ handState
+ ":"+msg[4]+":"+msg[2]);
}
});
function loadConfig(AltConfig="") {
const configPath = global.ConfigFile
try {
let newConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'));
config = newConfig;
if (AltConfig != "") {
// Load alternate config if specified
const altConfigKey = "AltConfig_" + AltConfig;
if (config[altConfigKey]) {
// Merge the alternate config settings into main config
Object.assign(config, config[altConfigKey]);
}
}
console.log('Configuration loaded successfully');
return true;
} catch(err) {
console.log('Failed to load configuration');
console.log(err);
return false;
}
}
wsEmitter.on("message",(data) => {
if (data == "Refresh") {
broadcastMessage('ClearLists')
zosc.sendZoomCommand("list");
}
});
// Load single script modules from the 'modules' directory and larger modules from any folder with a name starting with 'mod_'
async function loadModules(modulesDir="") {
if (modulesDir == "") {
return;
}
// For historical reasons we load module folders before modules that are single scripts
if (fs.existsSync(modulesDir)) {
try {
// Read all entries in the directory
const entries = await fsPromises.readdir(modulesDir, { withFileTypes: true });
// First load modules that are a folder with a name starting with 'mod_'
// Filter for directories that start with 'mod_'
const modDirs = entries.filter(entry =>
entry.isDirectory() && entry.name.startsWith('mod_')
);
// Process each module directory
for (const dir of modDirs) {
const moduleName = dir.name;
const moduleFile = path.join(modulesDir, dir.name, `${dir.name}.js`);
// Check if file exists and load it
if (fs.existsSync(moduleFile)) {
global[moduleName] = require(moduleFile);
} else {
console.log("Failed to find " + moduleFile);
}
}
// Now lod modules that are a single script
// Filter for files that start with 'mod_' and end with '_core.js'
const coreModules = entries.filter(entry =>
entry.isFile() &&
entry.name.startsWith('mod_') &&
entry.name.endsWith('_core.js')
);
// Process each core module file
for (const file of coreModules) {
// Extract module name by removing '_core.js' from the end
const moduleName = file.name.slice(0, -8); // Remove '_core.js' (8 chars)
const moduleFile = path.join(modulesDir, file.name);
// Load the module
try {
global[moduleName] = require(moduleFile);
} catch (err) {
console.log(`Failed to load ${moduleFile}: ${err}`);
}
}
} catch (error) {
console.error('Error loading modules', error);
}
}
}
// Execute the function
loadModules(global.ModuleDir);
if (global.AltModuleDir != "") {
loadModules(global.AltModuleDir);
}
// function registerModule(moduleName, module) {
// }
module.exports = {
zosc,
config,
KeyboardUI,
registerKeypress,
showKeyboardHelp,
registerModule
}