-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
340 lines (316 loc) · 15.5 KB
/
index.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
var fs = require("fs");
var express = require('express')();
var http = require('http').Server(express);
var io = require('socket.io')(http);
var colors = require('colors');
const {autoUpdater} = require("electron-updater");
const log = require('electron-log');
var guiManager = require(__dirname + '/guiManager');
colors.setTheme({
silly: 'rainbow',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
debug: 'blue',
error: 'red'
});
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
//Start of modules
var cardManager = require(__dirname + '/cardManager');
var databaseManager = require(__dirname + '/databaseManager.js');
express.set("view engine", "ejs");
const electron = require('electron'),
{ ipcMain } = require('electron'),
{ app, BrowserWindow, webContents } = electron;
//do NOT let this application be throttled, the server always needs
//to be ready to respond to a client, regardless if it is the foremost
//application or not. This may disable sleeping on the client's computer, however.
electron.powerSaveBlocker.start('prevent-app-suspension');
var stations,
cardFolderLocation = __dirname + "/cards";
rebuildCards = false,
rebuildThemes = false,
overidePort = false,
overrideingPortNumber = 3000,
httpAlreadyInit = false,
databaseListeners = [],
portNumberFromUserPrefs = overrideingPortNumber,
saveDatabase = false;
app.on('ready', function() {
//check for updates
//autoUpdater.checkForUpdatesAndNotify();
//start file management
for (var i = 0; i < process.argv.length; i++) {
//do we need to build cards?
if (process.argv[i].includes("--buildCards=")) {
rebuildCards = (process.argv[i].split("=")[1] == "true");
}
//shortcut for build cards command?
if (process.argv[i] == ("--buildCards")) {
rebuildCards = true;
}
//shortcut for build themes command?
if (process.argv[i] == ("--buildThemes")) {
rebuildThemes = true;
}
//saveDatabase?
if (process.argv[i].includes("--saveDatabase=")) {
saveDatabase = (process.argv[i].split("=")[1] == "true");
}
//shortcut for save database?
if (process.argv[i] == ("--saveDatabase")) {
saveDatabase = true;
}
//override port?
if (process.argv[i].includes("--portOverride=")) {
overidePort = true;
overrideingPortNumber = parseInt(process.argv[i].split("=")[1]);
console.log("port set to " + overrideingPortNumber.toString().bold);
}
}
if (saveDatabase == true) {
console.log("-[WARNING]-\nWARNING!!! THE SAVE DATABASE OPTION FOR IFDATABASE CURRENTLY DOESN'T WORK!\n(it won't crash anything, it just won't work)\n-[WARNING]-".error.bold);
}
if (overidePort == true) {
portNumberFromUserPrefs = overrideingPortNumber;
}
guiManager.init(ipcMain, app, databaseManager, cardManager.getCards(),cardManager.getThemes(),overidePort ? overrideingPortNumber : portNumberFromUserPrefs, function(portNumberFromUserPrefs, loadedStations) {
stations = loadedStations;
guiManager.onStationChange(function(newData) {
stations = newData;
console.log("stations set to " + stations);
});
cardManager.init(rebuildCards,rebuildThemes, function() {
if(httpAlreadyInit){
return;
}
httpAlreadyInit = true;
http.listen(overidePort ? overrideingPortNumber : portNumberFromUserPrefs, function(socket) {
console.log('=====[' + 'listening on port '.bold + overidePort ? overrideingPortNumber : portNumberFromUserPrefs.toString().bold + ']=====');
express.get('/', function(req, res) {
res.sendFile(__dirname + '/grabStations.html');
});
express.get('/databaseWindow', function(req, res) {
res.render('databaseView', {
'port': overidePort ? overrideingPortNumber : portNumberFromUserPrefs,
'cards': cardManager.getCards(),
'stations': stations
});
});
express.get('/cardWindow', function(req, res) {
res.render('cardView');
});
express.get('/stationServerSelect', function(req, res) {
res.sendFile(__dirname + '/stationServerSelect.html');
});
express.get("/jquery", function(req, res) {
console.log("Loading JQUERY For GUI...".grey);
res.sendFile(__dirname + "/public/jquery.js");
});
express.get('/public/:path', function(req, res) {
res.sendFile(__dirname + req.url);
});
express.get('/guiScript', function(req, res) {
console.log("Loading JavaScript For GUI...".grey);
res.sendFile(__dirname + "/guiScript.js");
});
express.get('/card/:cardName', function(req, res) {
//useless get request?
console.log(req.url);
});
express.get('/cardImage', function(req, res) {
console.log("LOADING CARD IMAGE " + req.query.card.toString());
res.sendFile(cardFolderLocation + "/" + req.query.card.toString() + "/preview.png");
});
express.get('/views/client', function(req, res) {
res.render('client.ejs', {
'port': overidePort ? overrideingPortNumber : portNumberFromUserPrefs,
'cards': cardManager.getCards(),
'stations': stations
});
});
express.get('/resource', function(req, res) {
console.log("LOADING RESOURCE " + __dirname + "/" + req.query.path.toString());
res.sendFile(__dirname + "/" + req.query.path);
});
io.on('connect', function(socket) {
let socketID = socket.id;
databaseManager.setClientCount(Object.keys(io.sockets.sockets));
console.log("[+] STATION CONNECTED (" + socketID + ", " + socket.handshake.address + ")");
console.log("sending " + stations + " for client");
socket.emit('stationsSent', stations);
socket.on('disconnect', function(disconnectedSocket) {
databaseManager.setClientCount(Object.keys(io.sockets.sockets));
console.log("[!] STATION DISCONNECTED");
var databaseListenerToRemove = -1;
for(var i = 0;i < databaseListeners.length;i++){
if(databaseListeners[i].socket.id == socket.id){
databaseListenerToRemove = i;
}
}
if(databaseListenerToRemove != -1){
databaseListeners.splice(databaseListenerToRemove,1);
}
socket = null;
});
socket.on('getCardFiles', function(data) {
cardManager.serveCard(data, function(bufferStream) {
if (bufferStream) {
socket.emit('recieveCardFiles', {
"cardName": data,
"bufferStream": bufferStream
});
}
});
});
socket.on('getThemeFiles', function(data) {
cardManager.getThemeFiles(data, function(bufferStream) {
if (bufferStream) {
socket.emit('recieveThemeFiles', {
"themeName": data,
"folderName" : cardManager.getThemeFolderName(),
"bufferStream": bufferStream
});
}
});
});
socket.on('getDatabaseValue', function(data) {
databaseManager.getDatabaseValue(data, function(databaseValueFromMongo) {
var databaseValue = {
"key": data,
"dataValue": databaseValueFromMongo
}
socket.emit("databaseValueForKey", databaseValue);
databaseValue = null;
});
});
socket.on('getDatabaseValueForServerFunction', function(data) {
databaseManager.getDatabaseValue(data, function(databaseValueFromMongo) {
var databaseValue = {
"key": data,
"dataValue": databaseValueFromMongo
}
socket.emit("databaseValueForKeyForServerFunction", databaseValue);
});
});
socket.on('setDatabaseListeners',function(data){
for(var i = 0;i < databaseListeners.length;i++){
if(databaseListeners[i].socket == socket){
databaseListeners[i].values = data;
return;
}
}
//if we get here, this socket has not been added to database listeners yet
databaseListeners.splice(databaseListeners.length,0,{"socket" : socket,"values" : data})
});
socket.on('setDatabaseValue', function(data) {
databaseManager.setDatabaseValue(data.key, data.dataValue);
var clientsUpdated = [];
for(var i = 0;i < databaseListeners.length;i++){
for(var j = 0;j < databaseListeners[i].values.length;j++){
if(databaseListeners[i].values[j] == data.key){
var detected = false;
for(var x = 0;x < clientsUpdated.length;x++){
if(clientsUpdated[x] == i){
detected = true;
}
}
if(!detected){
clientsUpdated.splice(clientsUpdated.length,0,i);
databaseListeners[i].socket.emit("databaseValueDidChange",data);
}
}
}
}
//io.emit("databaseValueDidChange", data);
//deallocate
data = null;
//socket.broadcast.emit("databaseValueDidChange",data);
//socket.emit("databaseValueDidChange",data);
});
socket.on('clearDatabase', function(data) {
databaseManager.clearDatabase();
io.emit("databaseValueDidChange", "all");
});
socket.on('getViewscreenCard', function(data) {
let connectedSocket = socket;
console.log("Sending viewscreen card...");
cardManager.serveCard("viewscreen", function(bufferStream) {
if (bufferStream) {
connectedSocket.emit('recieveViewscreenCard', {
"cardName": "viewscreen",
"bufferStream": bufferStream
});
}
});
});
socket.on('getDMXCard', function(data) {
console.log("Sending DMX card...");
cardManager.serveCard("dmx", function(bufferStream) {
if (bufferStream) {
socket.emit('recieveDMXCard', {
"cardName": "dmx",
"bufferStream": bufferStream
});
}
});
});
socket.on('getResourceFiles',function() {
cardManager.getResourceFiles(function(bufferStream) {
socket.emit('recieveResourceFiles', bufferStream);
});
});
socket.on('getMenu', function() {
cardManager.getMenu(function(bufferStream) {
if (bufferStream) {
socket.emit('recieveMenu', {
"cardName": "menu",
"bufferStream": bufferStream
});
}
});
});
socket.on('getCards', function(data) {
var stationNames = [];
for (var i = 0; i < stations.length; i++) {
stationNames.push(stations[i].stationInfo.name);
}
for (var i = 0; i < stationNames.length; i++) {
if (stationNames[i] == data) {
//station found
console.log('sending cards for station ' + data);
var cards = [];
for(var clientCards = 0;clientCards < stations[i].stationInfo.cards.length;clientCards++){
for(var possibleCards = 0;possibleCards < cardManager.cards.length;possibleCards++){
if(cardManager.cards[possibleCards].cardInfo.cardName == stations[i].stationInfo.cards[clientCards]){
cards.splice(cards.length,0,cardManager.cards[possibleCards]);
}
}
}
socket.emit('recieveCards', cards);
return;
}
}
var errorMessage = "Client tried to call cards for nonexsistant station ".bold + data;
console.log(errorMessage.error);
});
});
var mainWindow = new BrowserWindow({
width: 1100,
height: 650,
center : true,
minWidth: 1000,
minHeight: 550
});
mainWindow.loadURL('http://localhost:' + (overidePort ? overrideingPortNumber : portNumberFromUserPrefs) + '/views/client');
express.use(require("express-status-monitor")());
});
});
});
});