forked from zaheerbasra/NodePlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
524 lines (474 loc) · 18.7 KB
/
server.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
'use strict';
/*
Debug setup
*/
const ALL_DEBUG = 1;
const BASIC_DEBUG = 2;
const DATA_DEBUG = 4; // Deeper data messaging
const FOCUS_DEBUG = 8; // Focus on something new
const ERROR_DEBUG = 16;
const WATCHER_DEBUG = 32; // Debug messages related to watching certain variable values, like counting up regions and loading them
const WEBSOCK_DEBUG = 64; // Triggers when messages are sent or received via websock
const ZIP_DEBUG = 128; // Messages related to unzipping files
const PRICE_DEBUG = 256; // Anything pricing related
const TIME_DEBUG = 512; // Related to adjusting timezones for modifying the date/time to match EST
const EXEC_DEBUG = 1024; // Debugs related to starting programs
const TRIGGER_DEBUG = 2048; // Debugs related to trigger contents
var debug = BASIC_DEBUG | TRIGGER_DEBUG | DATA_DEBUG;
function debugLog(flag, msg) {
if (debug & flag || flag & ERROR_DEBUG || debug & ALL_DEBUG) {
console.log(msg);
}
}
/*
Web server setup
*/
var express = require('express');
var app = express();
var fs = require('fs'), parseString = require('xml2js').parseString;
var ejs = require('ejs');
var http = require('http')
var server = require('http').createServer();
var unzip = require('decompress');
app.set('view engine', 'ejs');
var rootpath = (process.platform === 'win32' ? __dirname.replace(/\\/g, '/').replace(/^C:/i, '') : __dirname) + '/';
var triggerContentTimeOut = 50000;
var timezoneOffset
/*
Directory watching and remote restarting
*/
var watch = require('watch');
var WebSocket = require('ws');
var wss = new WebSocket.Server({server: server});
wss.broadcast = function broadcast(data) {
debugLog(WEBSOCK_DEBUG, 'Broadcast called');
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
debugLog(WEBSOCK_DEBUG, 'Sending ' + data + ' to client');
client.send(data);
}
});
};
wss.on('connection', function connection(ws) {
debugLog(BASIC_DEBUG, 'Client connected');
ws.on('message', function incoming(data) {
debugLog(WEBSOCK_DEBUG, 'Received message: ' + data);
});
ws.on('close', function () {debugLog(BASIC_DEBUG, 'Client disconnected');});
ws.send('connected');
});
watch.watchTree('/Urchannel/SignalFiles', function (f, curr, prev) {
debugLog(WEBSOCK_DEBUG, 'Found change in signal files, triggering reload');
wss.broadcast('reload');
});
if (fs.existsSync('/btv/incoming/PriceFiles/price.xml')) {
watch.watchTree('/btv/incoming/PriceFiles', function (f, curr, prev) {
debugLog(WEBSOCK_DEBUG, 'Found change in price file, triggering reload');
wss.broadcast('reload');
});
}
/*
Execution setup
Allows execution of outside programs. Specifically we want to run Chrome at the moment in kiosk mode, but it could also run other programs like our entire suite of upkeep programs
*/
var path = require("path");
var exec = require("child_process").exec;
var pathToChrome = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe';
var chromex86 = true;
if (!fs.existsSync(pathToChrome)) {
debugLog(EXEC_DEBUG, 'Unable to find ' + pathToChrome);
chromex86 = false;
}
/*
Watcher setup for fs.existsSync checking
*/
function timedFSCheck(callback) {
debugLog(FOCUS_DEBUG, 'Retrying render');
callback();
}
/*
Implementation
*/
// A test based callback trigger. When the specified variable meets the specified test, perform callback function
// watchVar is a variable reference. ChangeTest is a function receiving the watchVar. Callback is the function to run (without arguments).
function Watcher(watchVar, ChangeTest, Callback) {
this.watchVar = watchVar;
this.ChangeTest = ChangeTest;
this.Callback = Callback;
this.GetValue = function() {
return this.watchVar;
}
this.SetValue = function(val) {
this.watchVar = val;
debugLog('Set value: ' + this.watchVar);
if (this.ChangeTest(this.watchVar)) {
debugLog(WATCHER_DEBUG, 'Doing callback');
this.Callback();
}
}
}
// Get price files for use in playback.
function getPriceFile() {
// Hard coded location because this is where the price file is placed by other programs
var priceFile = null;
if (fs.existsSync('/btv/incoming/PriceFiles/price.xml')) {
priceFile = fs.readFileSync('/btv/incoming/PriceFiles/price.xml', 'utf-8', function (err) {
if (err) debugLog(ERROR_DEBUG, err);
});
} else {
debugLog(BASIC_DEBUG, 'No price file present, returning null')
}
if (priceFile !== null) {
parseString(priceFile, function(err, result) {
if (err) { debugLog(ERROR_DEBUG, err); }
debugLog(PRICE_DEBUG, result);
if (result.specials !== null) {
priceFile = result.specials;
}
});
debugLog(PRICE_DEBUG, 'Total of ' + Object.keys(priceFile).length + ' entries');
debugLog(PRICE_DEBUG, 'Found items:');
for (item in priceFile.keys) {
priceFile[item] = priceFile[item][0].trim();
debugLog(PRICE_DEBUG, '|-' + item + ':' + priceFile[item]);
}
}
return priceFile;
}
// Removes a directory, recursively. Used to clear old extracted zip files for HTML content.
function rmdirRecursively(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file, index){
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
rmdirRecursively(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
// Determines whether the first date is before or after the second, in terms of hh:mm:ss
// Returns -1 for before, 1 for after, or 0 for exactly the same time of day (even if not the same date)
// Modified to include an hour offset to correct to EST, because Aziz magic
function checkDate(check1, check2) {
if (check1.getHours() + getHourOffset() > check2.getHours() + getHourOffset()) { // If the hours are greater, you know it comes after
return 1;
} else if (check1.getHours() < check2.getHours()) { // Likewise if they're less, then you know it comes before
return -1;
} else {
if (check1.getMinutes() > check2.getMinutes()) { // Same with minutes
return 1;
} else if (check1.getMinutes() < check2.getMinutes()) {
return -1;
} else {
if (check1.getSeconds() > check2.getSeconds()) {
return 1;
} else if (check1.getSeconds() < check2.getSeconds()) {
return -1;
} else {
return 0; // At this stage there's functionally no difference in the time of day, barring milliseconds, and we don't track that precisely.
}
}
}
}
// Returns the hourly offset based on the timezone, the difference we need to add to make a time correct for our system
// Remember, getTimezoneOffset() returns minutes, not actual hours.
function getHourOffset() {
var today = new Date();
var tzOffset = today.getTimezoneOffset() / 60;
if (today.isDstObserved()) {
debugLog(TIME_DEBUG, "DST in effect. tzOffset + 1");
tzOffset += 1;
}
debugLog(TIME_DEBUG, "Timezone Offset: " + tzOffset);
if (tzOffset > 5) {
debugLog(TIME_DEBUG, "Returning " + (tzOffset - 5));
return (tzOffset - 5);
} else if (tzOffset < 5) {
debugLog(TIME_DEBUG, "Returning " + (tzOffset + 5));
return (tzOffset + 5);
} else {
return 0;
}
}
// Adjusts a date to be in line with EST
function adjustHours(inDate) {
var newDate = inDate;
debugLog(TIME_DEBUG, "Adjusting time by " + getHourOffset());
newDate.setTime(newDate.getTime() + (getHourOffset() * 60 * 60 * 1000));
return newDate;
}
// Returns the absolute seconds difference between two dates, ignoring days
function timeDiff(date1, date2) {
return Math.abs(((date1.getHours() * 60*60) + (date1.getMinutes() * 60) + date1.getSeconds()) - ((date2.getHours() * 60*60) + (date2.getMinutes() * 60) + date2.getSeconds()));
}
function getTriggerdContents() {
var playerId = process.env.hostname;
debugLog(TRIGGER_DEBUG, playerId);
var options = {
hostname: "lorcos.ur-channel.com",
port: 80,
path: "/TriggerApp/TriggeredContentService/" + playerId,
method: "GET"
};
var req = http.request(options, function(res) {
var responseBody = "";
debugLog(TRIGGER_DEBUG,`Server Status: ${res.statusCode}`);
//debugLog(TRIGGER_DEBUG,"Response Headers: %j", res.headers);
res.setEncoding("UTF-8");
res.on("data", function(chunk){
responseBody += chunk;
});
res.on("end", function(chunk){
if(responseBody.length < 1)
{
responseBody = `{
"MediaItems":[{
"AssetType":"Movie",
"Duration":30,
"Height":1080,
"Left":0,
"Path":"COBS_Bread_Cape_Baguette_Final_Feb15.mp4",
"Top":0,
"Width":1920,
"Zindex":0
}],
"SignalKey":"DEMOLEFTA0"
}`
}
if(responseBody.length > 1)
{
//debugLog(TRIGGER_DEBUG,responseBody);
var obj = JSON.parse(responseBody);
//debugLog(TRIGGER_DEBUG,obj.MediaItems);
if(obj.MediaItems.length > 0)
{
wss.broadcast('triggercontent:' + responseBody);
}
}
});
});
req.on("error", function(err){
console.log(`Problem with the request: ${err.message}`);
});
req.end();
}
// this will keep triggering the get contents every 10 second
setInterval(getTriggerdContents, triggerContentTimeOut);
// Internet copy pasta for determining DST
Date.prototype.stdTimezoneOffset = function () {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.isDstObserved = function () {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
// Content retrieval: https://127.0.0.1/incoming/Media/<file name> returns the requested file, be it HTML or other.
// TODO: Spits out an error if the file doesn't exist, should look into catching that at some point.
// Note: Zip file downloads are not supported, as we should never be sending zips to the browser. Just their contents.
app.all('/incoming/Media/:path*', function (req, res, next) {
switch (req.params[0].slice(req.params[0].lastIndexOf('.'))) {
case '.zip':
res.send('File request: /incoming/Media/' + req.params.path + req.params[0]);
break;
case '.html':
debugLog(DATA_DEBUG, "Got HTML request: " + rootpath + "../incoming/Media/" + req.params.path + req.params[0]);
debugLog(DATA_DEBUG, "Renaming index.html to ejs");
var htmlToEjs = req.params.path + req.params[0].replace(/\..{0,4}$/i, '.ejs');
fs.rename(rootpath + '../incoming/Media/' + req.params.path + req.params[0], rootpath + '../incoming/Media/'+htmlToEjs, function(err) {
if (err) {
if (err.code !== 'ENOENT') {
throw err;
}
}
});
if (fs.existsSync(rootpath + '../incoming/Media/' + htmlToEjs)) {
res.render(rootpath + '../incoming/Media/'+ htmlToEjs, getPriceFile());
} else {
debugLog(FOCUS_DEBUG, 'Unzip running slow, adding timer to retry render');
var contentTimer = setInterval(function () {
if (fs.existsSync(rootpath + '../incoming/Media/' + htmlToEjs)) {
res.render(rootpath + '../incoming/Media/'+ htmlToEjs, getPriceFile());
clearInterval(contentTimer);
}
}, 1000);
}
break;
default:
var options = {
root: rootpath + '../incoming/Media/',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.sendFile(req.params.path + req.params[0], options, function (err) {
if (err) {
if (err.code !== 'ECONNABORTED') {
next(err);
}
} else {
debugLog(DATA_DEBUG, 'Served file: ' + req.params.path + req.params[0]);
}
});
}
});
// Main block
// Returns a base HTML page using EJS to populate fields.
// Parses the appropriate show file for necessary region data and sets up specified regions as DIV tags.
app.get('/', function (req, res) {
var showFileName;
debugLog(BASIC_DEBUG, 'Looking in ' + rootpath + '../SignalFiles');
// Find all the signal files, the _show[r0c0] one will be the one we want to load
fs.readdir(rootpath + '../SignalFiles', function(err, files) {
debugLog(DATA_DEBUG, 'Files: ' + files);
for (var x = 0; x < files.length; x++ ) {
if (files[x].search(/_show\[r0c0\]/) >= 0) {
showFileName = files[x].replace("_show\[r0c0\].sgf", ".show");
debugLog(BASIC_DEBUG, "Found signal file: " + files[x] + " - Renaming to " + showFileName);
break;
}
}
// Load the specified show file, extracting the item info from the first (and hopefully only!) region that has a date range including today
fs.readFile(rootpath + '../incoming/ShowFiles/' + showFileName, 'utf-8', function (err, data) {
var showFile = JSON.parse(data);
var showStartDate = new Date(parseInt(showFile.StartTime.slice(6,-2)));
showStartDate = adjustHours(showStartDate);
var showEndDate = new Date(parseInt(showFile.EndTime.slice(6,-2)));
showEndDate = adjustHours(showEndDate);
debugLog(BASIC_DEBUG, 'Show times: ' + showStartDate.getTime() + ' - ' + showEndDate.getTime());
var regions = [];
var htmlOut = '';
//var completeRegions = new Watcher(0, function(val) {return val >= regions.length;}, function() {res.render('index', {htmlOut: htmlOut});});
var itemCount = 0;
var countdownTime = -1;
for (var region of showFile.Regions) {
debugLog(BASIC_DEBUG, 'Region: ' + region.Name);
// Iterate over the region entries looking for the current playlist
for (var playlist of region.Playlists) {
debugLog(DATA_DEBUG, playlist.Name);
debugLog(DATA_DEBUG, 'date string:'+playlist.StartTime.slice(6,-2));
var today = new Date();
//today = adjustHours(today);
var regionStartDate = new Date(parseInt(playlist.StartTime.slice(6,-2)));
regionStartDate = adjustHours(regionStartDate);
var regionEndDate = new Date(parseInt(playlist.EndTime.slice(6,-2)));
regionEndDate = adjustHours(regionEndDate);
if (regionEndDate.getSeconds() == 0) {
regionEndDate.setSeconds(59);
}
debugLog(BASIC_DEBUG, 'startDate [' + regionStartDate + ']\ntoday ['+today+']\nendDate ['+regionEndDate+']');
if ((playlist.RecurrenceRule == null && today > regionStartDate && today < regionEndDate) ||
(playlist.RecurrenceRule != null && today > showStartDate && today < showEndDate && checkDate(today, regionStartDate) >= 0 && checkDate(today, regionEndDate) <= 0)) {
/*debugLog('File name = ' + playlist.Items[0].Source);
debugLog('mediaName = ' + playlist.Items[0].Name.replace(/[\&\s]/g,'').replace(/\..{0,4}$/i, ''));*/
if (playlist.RecurrenceRule != null) {
if (countdownTime < 0) {
countdownTime = (timeDiff(today, regionEndDate) + 1) * 1000;
} else {
var newTime = (timeDiff(today, regionEndDate) + 1) * 1000;
countdownTime = (countdownTime > newTime ? newTime : countdownTime);
}
debugLog(BASIC_DEBUG, 'RecurrenceRule not null, setting countdownTime to ' + countdownTime);
}
if (region.Width == 1279)
region.Width = 1280;
regions.push({
"x": (region.CanvasLeft / 1280) * 100,
"y": (region.CanvasTop / 720) * 100,
"w": (region.Width / 1280) * 100,
"h": (region.Height /720) * 100,
"name": region.Name,
"mediaName": playlist.Name.replace(/[\&\s]/g,''),
"mediaInfo": []
});
// Push a bunch of items onto the stack for this region for future playback
for (var item of playlist.Items) {
regions[regions.length-1].mediaInfo.push({ "source" : item.Source, "duration": item.DurationSeconds, "type" : item.AssetType.toLowerCase()});
}
}
}
itemCount++;
}
debugLog(BASIC_DEBUG, 'Found ' + itemCount + ' items to play');
if (countdownTime > 0) {
debugLog(BASIC_DEBUG, "A region has dayparting, setting timer to refresh page for " + countdownTime + "ms from now.");
setTimeout(function () {
debugLog(WEBSOCK_DEBUG, 'Dayparting timer expired, sending reload...');
wss.broadcast('reload');
}, countdownTime);
}
var completeRegions = new Watcher(0, function(val) {return val >= itemCount;}, function() {res.render('index', {htmlOut: htmlOut});});
var renames = [];
// Start building up HTML tags for the region <div> tags and the playlist <li> tags.
// Also extract any zip files (clearing out old data) for future use
for (var x = 0; x < regions.length; x++) {
debugLog(BASIC_DEBUG, "region["+x+"]: " + regions[x].x + "," + regions[x].y + " - " + regions[x].w + "," + regions[x].h + " - name: " + regions[x].name);
var positioningStuff = 'position:absolute;top:' + regions[x].y + '%;left:' + regions[x].x+ '%;width:' + regions[x].w + '%;height:' + regions[x].h +'%;z-index:'+x+';';
/*var divStart*/ htmlOut = htmlOut + '<div id="'+regions[x].name+'" style="' +positioningStuff+'"></div>\n';
htmlOut = htmlOut + '<div id="'+regions[x].name+'-playlist" style="display:none;">\n'
for (var y = 0; y < regions[x].mediaInfo.length ; y++) {
var jsonData = {"source": "/incoming" + regions[x].mediaInfo[y].source, "type" : regions[x].mediaInfo[y].type, "Duration" : regions[x].mediaInfo[y].duration};
if (regions[x].mediaInfo[y].type === 'zip') {
debugLog(ZIP_DEBUG, 'Extracting zip contents');
jsonData.source = jsonData.source.replace(/\..{0,4}$/i,'/index.html');
jsonData.type = 'html';
if (fs.existsSync(rootpath + '../incoming' + regions[x].mediaInfo[y].source.replace(/\..{0,4}$/i,''))) {
debugLog(ZIP_DEBUG, 'Deleting current unzipped folder');
rmdirRecursively(rootpath + '../incoming' + regions[x].mediaInfo[y].source.replace(/\..{0,4}$/i,''));
debugLog(ZIP_DEBUG, 'Complete');
}
unzip(rootpath + '../incoming' + regions[x].mediaInfo[y].source, rootpath + '../incoming/Media/', {
map: file => {
file.path = file.path.replace(/\.html$/i,'.ejs');
return file;
}
}).then(res => {
var indexPath = '';
if (res.path) {
indexPath = res.path;
} else if (res[0]) {
indexPath = res[0].path;
}
}).catch(err => {
debugLog(ERROR_DEBUG, 'Error in unzip process: ' + err.stack);
});
}
debugLog(DATA_DEBUG, 'Configured new jsonData: ' + JSON.stringify(jsonData));
htmlOut = htmlOut + '<li class>' + JSON.stringify(jsonData) + '</li>\n';
}
htmlOut = htmlOut + '</div>\n';
htmlOut = htmlOut + '<script>var ' + regions[x].name + ' = new RegionPlayer("' + regions[x].name + '");</script>\n';
completeRegions.SetValue(completeRegions.GetValue()+1);
}
});
});
});
// Get this party started
server.on('request', app);
server.listen(3000, function() {
debugLog(BASIC_DEBUG, 'Starting http server: *:3000');
debugLog(BASIC_DEBUG, 'Attempting to launch Chrome');
if (chromex86) {
exec('"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --kiosk --app-auto-launched 127.0.0.1:3000', (err, stdout, stderr) => {
if (err) {
debugLog(ERROR_DEBUG, 'Error in exec(): ' + err);
return;
}
debugLog(EXEC_DEBUG, 'stdout: ' +stdout);
debugLog(EXEC_DEBUG, 'stderr: ' +stderr);
});
} else {
exec('"C:/Program Files/Google/Chrome/Application/chrome.exe" --kiosk --app-auto-launched 127.0.0.1:3000', (err, stdout, stderr) => {
if (err) {
debugLog(ERROR_DEBUG, 'Error in exec(): ' + err);
return;
}
debugLog(EXEC_DEBUG, 'stdout: ' +stdout);
debugLog(EXEC_DEBUG, 'stderr: ' +stderr);
});
}
});