-
Notifications
You must be signed in to change notification settings - Fork 64
/
server.js
1062 lines (906 loc) · 36.4 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ===============================================================
//
// "SPX Graphics Controller"
// (c) 2020- SPX Graphics (https://spx.graphics)
//
// An open source PROFESSIONAL LIVE GRAPHICS solution for
// PC, Mac, Linux or the cloud.
//
// Website .......... https://spx.graphics
// Join Discord ..... https://bit.ly/joinspx
// Knowledge Base ... https://spxgc.tawk.help
// Sourcecode ....... https://github.com/TuomoKu
//
// No front-end frameworks.
// Core functionality: NodeJS, Express, Handlebars -templating.
//
// Most serverside code is in /routes and /utils.
// Most clientside code is in /static/js/spx_gc.js
//
// Basic usage:
// node server.js [config.json]
//
// ===============================================================
const express = require('express')
const session = require('express-session')
const bodyParser = require('body-parser')
const cors = require('cors')
const exphbs = require('express-handlebars')
const app = express()
const ip = require('ip')
const fs = require('fs')
const morgan = require('morgan')
const path = require('path')
const rfs = require('rotating-file-stream')
//const open = require('open');
const open = import('open');
var macaddress = require('macaddress');
console.log('\n SPX Graphics Controller server is starting.\n Closing this window/process will stop the server.\n');
// EXIT HANDLER
process.on('exit', function(code) {
// usage: process.exit(1)
let codestr="Unspecified exit code";
switch (code) {
case 1:
codestr = "Error while reading config.json"
break;
case 2:
codestr = "Kill request received from SPX client. If pm2 used, the server will restart automatically."
break;
case 3:
codestr = "Port is already in use on this machine."
break;
default:
break;
}
return console.log(`\n\nSPX exit! (Errorcode ${code}: ${codestr})\n\n`);
});
// global.config
const cfg = require('./utils/spx_getconf.js');
cfg.readConfig()
if (!configfileref) {
process.exit(1)
}
const spx = require('./utils/spx_server_functions.js');
const logger = require('./utils/logger.js');
// STATICS
app.use(express.static(path.join(__dirname,('static')))) // the usual css/js stuff... embedded in pkg package
app.use(express.static(path.resolve(spx.getStartUpFolder(),'ASSETS'))) // http root in startup folder / ASSETS
const ipad = ip.address();
var pjson = require('./package.json');
var packageversion = pjson.version;
const vers = process.env.npm_package_version || packageversion || 'X.X.X';
// global.isDev = process.env.SPX_ROOT_FOLDER ? true : false; // value used internally for SPX & Template dev
global.vers = vers;
global.excel = {'readtime':0, 'filename':'', 'data':''}; // used as Excel cache
// Added in 1.1.1.
global.env = {'vendor':'', 'product':'', 'version':''};
let envIdfile = path.resolve(spx.getStartUpFolder(),'env.json') // fixed post 1.1.2
if (fs.existsSync(envIdfile)) {
var spxenv = require(envIdfile);
if (spxenv.vendor) {global.env.vendor = spxenv.vendor;}
if (spxenv.product) {global.env.product = spxenv.product;}
if (spxenv.version) {global.env.version = spxenv.version;}
}
// Added in 1.0.15. An memory persistent object to handle -------------
// rundown state to avoid unnecessary disk I/O and improve performance.
global.rundownData = {}
// --------------------------------------------------------------------
const port = config.general.port || 5656;
global.CCGSockets = [];
global.LastBrowsedTemplateFolder = '';
macaddress.one(function (err, mc) {
let macaddress = String(mc);
let pseudomac = macaddress.split(':').join('').substring(0,8);
global.hwid = config.general.hostname || pseudomac;
global.pmac = pseudomac; // an anonymous id
});
// Setup Morgan
let LOGFOLDER = config.general.logfolder || spx.getStartUpFolder() + '/LOG';
var logDirectory = path.normalize(LOGFOLDER);
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
var accessLogStream = rfs.createStream('access.log', {
interval: '1d', // rotate daily
path: logDirectory
})
app.use(morgan('combined', { stream: accessLogStream }))
// Handlebars templating, updated in 1.3.3
app.engine('handlebars', exphbs.engine({
extname: '.handlebars',
//defaultLayout: 'filelist',
helpers: {
// Convert JSON data and return as JSONstring.
// (used to store arrays to hidden form fields)
DataToJSONString: function (data) {
let s = encodeURI(JSON.stringify(data));
return s;
},
// conditional IF helper
ifValue: function(a, b, options) {
// console.log('a: ' + a + ', b: ' + b);
if(String(a) === String(b) ) {
return options.fn(this);
}
return options.inverse(this);
},
// conditional IF helper return value A if "value" == "compareTo", B if not
ifValueMatch: function(value, compareTo, A, B ) {
if(value == compareTo) {
return A;
} else {
return B;
}
},
// conditional IF helper
itemCount: function(arr) {
// console.log('a: ' + a + ', b: ' + b);
return arr.length;
},
// conditional IF helper
ifGreater: function(a, b, options) {
if(a > b) {
// console.log('TRUE. A (' + a + ') was bigger.');
return options.fn(this);
}
// console.log('FALSE. B (' + b + ') was bigger.');
return options.inverse(this);
},
// populate CasparCG options for show config templates
generateDataFormatOptions(currentDataformat='json') {
let html="";
let AvailableFormats=['json', 'xml'];
AvailableFormats.forEach((Option,i) => {
let curFormat = Option.toLowerCase();
let cfgFormat = currentDataformat.toLowerCase();
let sel = "";
if (curFormat==cfgFormat){ sel="selected" }
html += '<option value="' + curFormat + '" ' + sel + '>' + curFormat + '</option>';
});
return html
},
// populate CasparCG options for show config templates
generateCasparCgPlayoutOptions(currentServer) {
let html="";
let AvailableServers=['-'];
if (config.casparcg){
config.casparcg.servers.forEach((element,i) => {
AvailableServers.push(element.name);
});
}
AvailableServers.forEach((serverOption,i) => {
let curSrv = serverOption.toUpperCase();
let cfgSrv = currentServer.toUpperCase();
let sel = "";
if (curSrv==cfgSrv){ sel="selected" }
html += '<option value="' + curSrv + '" ' + sel + '>' + curSrv + '</option>';
});
return html
},
// generate Open datafolder -commands in the UI menu for different operating systems
GenerateOpenFolderCommands(){
let platform = process.platform;
let html="";
switch (platform) {
case 'win32':
html += '<!-- Below folder commands only available on Windows in version 1.0 -->\n';
html += '<br><A HREF="" onClick="spx_system(\'openDatafolder\');return false;"><i class="far fa-folder-open"></i> '+ spx.lang('button.exploredataroot') +'</A>\n';
html += '<br><A HREF="" onClick="spx_system(\'openTemplatefolder\');return false;"><i class="far fa-folder-open"></i> '+ spx.lang('button.exploretemplates') +'</A>\n';
break;
default:
html = "<!-- Sorry, only windows supported for Open Folder -commands in version 1.0 -->\n";
break;
}
return html;
},
// draw all custom controls in the argument array
GenerateCustomControls(arr){
if (!arr) {return}; // if no extras, do nuthin'
let html = "";
html += '<table style="width: 100%;" cellspacing="5" border="0">\n';
arr.forEach((control,index) => {
html += '<tr>'
html += '<td width="50%">'
html += '<span class="spxTableHead">' + control.description + '</span>'
html += '</td>'
html += '<td>'
switch (control.ftype){
case 'button':
html += '<button type="button" class="wide spxFlexItem btn ripple ' + control.bgclass + '" ';
html += 'data-spx-color="' + control.bgclass + '" ';
html += 'onClick="' + control.fcall + '" ';
html += '>' + control.text + '</button>\n';
break;
case 'togglebutton':
html += '<button type="button" class="wide spxFlexItem btn ripple ' + control.bgclass + '" ';
html += 'data-spx-color="' + control.bgclass + '" ';
html += 'data-spx-playtext="' + control.text0 + '" ';
html += 'data-spx-stoptext="' + control.text1 + '" ';
html += 'data-spx-status="false" ';
html += 'onClick="' + control.fcall + '" ';
html += '>' + control.text0 + '</button>\n';
break;
case 'selectbutton':
html += '<table class="wide" style="margin:0, padding:0; border:0px;">\n';
html += '<tr><td width="60%">\n';
html += '<select id="select' + index + '" class="wide btn ' + control.bgclass + '">\n';
if (control.items) {
control.items.forEach((option,nro) => {
html += '<option value="' + option.value + '">' + option.text + '</option>\n';
});
}
html += '</td><td>\n';
html += '<button type="button" class="wide spxFlexItem btn ripple ' + control.bgclass + '" ';
html += 'data-spx-color="' + control.bgclass + '" ';
html += 'onClick="' + control.fcall + '(\'select' + index + '\');" ';
html += '>' + control.text + '</button>\n';
html += '</td></tr></table>\n';
break;
default:
logger.warn('server.GenerateCustomControls error in item [' + index + '] unknown controltype [' + control.ftype + '].');
break;
}
html += '</td></tr>\n'
});
html += '</table><BR>\n'
return html;
},
// generate a pretty string for each template to display playout configs
GeneratePlayoutInfo(playserver='', playchannel='', playlayer='', webplayout='', out='' , itemid='') {
let html = '<div data-spx-name="playoutConfig">';
// html += '<input type="text" data-clipboard-action="copy" data-clipboard-target="#copy' + itemid + '">';
if (playserver !="-") {
html += "<span onmouseover=\"tip('" + spx.lang('hover.ccgserver') + "');\" data-name='srv'>" + playserver + "</span>-";
html += "<span onmouseover=\"tip('" + spx.lang('hover.ccgchannel') + "');\" data-name='cha'>" + playchannel + "</span>-";
html += "<span onmouseover=\"tip('" + spx.lang('hover.ccglayer') + "');\" data-name='lay'>" + playlayer + "</span>";
html += " ";
// html += " <span class='delim'></span> ";
};
if (webplayout !="-") {
html += "<span class=\"copyid\" onclick=\"revealItemLayer(this);\" id=\"layer" + itemid + "\" onmouseover=\"tip('" + spx.lang('hover.weblayer') + "');\" data-name='web'>" + webplayout + "</span>";
// html += " <span class='delim'></span> ";
};
if (out) {
html += "<span class=\"copyid\" onclick=\"revealItemTiming(this);\" id=\"out" + itemid + "\" onmouseover=\"tip('" + spx.lang('hover.outmode') + "');\" data-name='out'>" + out + "</span>";
};
html += "<span id=\"copy" + itemid + "\" class=\"copyid\" onclick=\"revealItemID(this);\" onmouseover=\"tip('Copy ID " + itemid + "');\" data-name='id'>ID</span>";
// help button
html += "<span class=\"helpid\" onclick=\"help('ITEM-DETAILS');\" onmouseover=\"tip('Help');\">?</span>";
html += "</div>"
return html
},
// populate filelist for the controller add CSV file view (for instance)
generateFileList(requestType) {
let folder = '' // folder to start to scan
let filter = '' // extensions to show ('csv' for example)
// requestType = search command, such as "CSV"
switch (requestType) {
case 'CSV':
folder = path.join(config.general.dataroot, currentShow, 'profile.json');
filter = 'csv';
break;
default:
logger.error('Unknown requestType ' + requestType + ', doing nothing');
return; // exit function
break;
}
let html="";
// get show profile
// iterate all templates
let profileFile = path.join(config.general.dataroot, currentShow, 'profile.json');
showProfileData = spx.GetJsonData(profileFile);
if(!showProfileData.templates) {
// if no templates assigned to this project
html += '<span style="display: inline-block; margin: 10px;padding: 0.6em 1.7em; background: #0000001c; color:#ffffff; font-size:0.7em; border-radius:2em; text-align:center;">' + spx.lang('warning.notemplates') + '</span>';
return html
}
showProfileData.templates.forEach((template,i) => {
html += '<div onClick="addSelectedTemplate(' + i + ');" class="addTemplateItem row_accent' + template.uicolor + '">\n';
html += '<button type="button" class="addbtn bg_blue ripple" onClick="addSelectedTemplate(' + i + ');" style="float:right;">+</button>\n';
html += '<span class="templateName">' + spx.prettifyName(template.relpath) + '</span><BR>\n';
html += '<span class="templateDesc">' + template.description + ' </span>\n';
html += '</div>\n';
});
return html
},
// populate templatelist for the controller add template view
generateTemplateList(currentShow) {
let html="";
// get show profile
// iterate all templates
let profileFile = path.join(config.general.dataroot, currentShow, 'profile.json');
showProfileData = spx.GetJsonData(profileFile);
if(!showProfileData.templates) {
// if no templates assigned to this project
html += '<span style="display: inline-block; width: 100%; margin: 10px;padding: 0.6em 1.7em; background: #0000001c; color:#ffffff; font-size:0.7em; border-radius:2em; text-align:center;">' + spx.lang('warning.notemplates') + '</span><br><br>';
return html
}
showProfileData.templates.forEach((template,i) => {
html += '<div onClick="addSelectedTemplate(' + i + ');" class="addTemplateItem row_accent' + template.uicolor + '">\n';
html += '<button type="button" class="addbtn bg_blue ripple" onClick="addSelectedTemplate(' + i + ');" style="float:right;">+</button>\n';
html += '<span class="templateName">' + spx.prettifyName(template.relpath) + '</span><BR>\n';
html += '<span class="templateDesc">' + template.description + ' </span>\n';
html += '</div>\n';
});
return html
},
// convert timestamp (epoch) to locale date string "31.12.2021 17:45"
generateTimestamp(epoch) {
let date = new Date(parseInt(epoch));
let fDate = date.toLocaleDateString();
let fTime = date.toLocaleTimeString();
return fDate + ' ' + fTime;
},
// populate WebPlayout options for show config templates
generateButtonColorClasses(currentValue)
{
let html="";
let AvailableOptions=[
{
text:spx.lang('color.red'),
value:'bg_red'
},
{
text:spx.lang('color.green'),
value:'bg_green'
},
{
text:spx.lang('color.grey'),
value:'bg_grey'
},
{
text:spx.lang('color.orange'),
value:'bg_orange'
},
{
text:spx.lang('color.blue'),
value:'bg_blue'
},
{
text:spx.lang('color.black'),
value:'bg_black'
},
{
text:spx.lang('color.light'),
value:'bg_grey15'
}
];
AvailableOptions.forEach((OptionItem,i) => {
let sel = "";
if (OptionItem.value==currentValue){ sel="selected" }
html += '<option value="' + OptionItem.value + '" ' + sel + '>' + OptionItem.text + '</option>';
});
return html
},
// return username
CurrentUserName()
{
return global.user;
},
// populate out options for show config templates
generateOutOptions(currentOut) {
logger.debug('Generating out options. This selection: ' + currentOut + '.');
let html="";
let AvailableOptions=[
{value:'manual', text:'Manually'},
{value:'none', text:'None (just play)'},
{value:'1000', text:'In 1 second'},
{value:'2000', text:'In 2 seconds'},
{value:'4000', text:'In 4 seconds'},
{value:'6000', text:'In 6 seconds'},
{value:'10000', text:'In 10 seconds'}
];
AvailableOptions.forEach((outOption,i) => {
let sel = "";
let CurVal = currentOut.toLowerCase();
let CurOpt = outOption.value.toLowerCase();
if (CurVal==CurOpt){ sel="selected" }
html += '<option value="' + CurOpt + '" ' + sel + '>' + outOption.text + '</option>';
});
return html
},
// populate WebPlayout options for show config templates
generateWebPlayoutOptions(currentLayer='-') {
logger.debug('Generating webplayout options. This selection: ' + currentLayer + '.');
let html="";
let AvailableLayers=['-',1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
AvailableLayers.forEach((layerOption,i) => {
let curSrv = layerOption || '-';
let cfgSrv = currentLayer || '-';
let sel = "";
if (curSrv==cfgSrv){ sel="selected" }
html += '<option value="' + curSrv + '" ' + sel + '>' + curSrv + '</option>';
});
return html
},
// preview span for collapsed view of each rundown item
generateCollapsedHeadline(DataFields) {
return spx.generateCollapsedHeadline(DataFields);
},
// generate radio buttons for show config templates
generateColorAccents(selectedIndex, templateIndex) {
let colors=['gra','red','ora','gre','blu','pin','vio','bla'];
let html ="";
let sel = "";
colors.forEach((color,loopindex) => {
if (selectedIndex==loopindex) { sel = "checked" } else {sel=""};
html += '<input type="radio" name="templateData[uicolor]" value="' + loopindex + '" id="' + color + '_' + templateIndex + '" ' + sel +'/><label for="'+ color + '_' + templateIndex +'"><span class="' + color + '"></span></label>';
});
return html
},
// Returns IP for socket server of this app's IP address
getServerAddress: function () {
var ip = require('ip');
var myIP = ip.address() + ':' + port;
return myIP;
},
// Returns resolution options for App Config > resolution
// Added in 1.3.0
generateResolutionOptions: function (resolutionOption) {
let html = '';
let options = [
{ 'value': 'HD', 'text': 'HD (1080p)' },
{ 'value': '4K', 'text': '4K (2160p)' },
{ 'value': 'AUTO', 'text': 'AUTO (responsive)' },
];
let selectedFound = false;
options.forEach((opt,i) => {
let sele = '';
if (opt.value.toUpperCase() == resolutionOption.toUpperCase()) {
selectedFound = true;
sele = 'selected'
};
html += '<option ' + sele + ' value=' + opt.value + '>' + opt.text + '</option>\n';
});
return html;
},
// See if cssSizeValue has 'vh' or 'vw' in it then return HD values
// Added in 1.3.0
localPreviewSize: function (cssSizeValue) {
if (cssSizeValue.includes('vw')) {
return '1920';
} else if (cssSizeValue.includes('vh')) {
return '1080';
} else {
return cssSizeValue;
}
},
// Returns unique ID for this host machine
getHostID: function () {
return global.pmac;
},
hookLoadControllerPlugins: function () {
// Loads init.js files from all sub folders of ASSETS/plugins/<plugName>/
let html = '';
try {
let BrowseFolder = path.join(spx.getStartUpFolder(), 'ASSETS', 'plugins');
const list = spx.GetFilesAndFolders(BrowseFolder);
list.foldArr.forEach(pluginName => {
if ( pluginName.charAt(0) == '_' || pluginName.charAt(0) == '.' || pluginName === 'lib' ) {
html += '<!-- Note: disabled plugin ' + pluginName + ' skipped. -->\n'
} else {
logger.verbose('Loading plugin ' + pluginName );
html += '<script type="module" src="/plugins/' + pluginName + '/init.js"></script>\n'
}
})
} catch (error) {
html = '<!-- hookLoadControllerPlugins error: ' + error + ' -->';
}
return html
},
// Convert JSON \\n to newline for textarea
injectNewlines(jsonString) {
let multiline = jsonString.replace('\\n', '\r\n')
return multiline;
},
// Language string helper
lang(str) {
return spx.lang(str);
},
// Return object as JSON string
myStringify(obj) {
return "// Stringified JSON\n\n" + JSON.stringify(obj, null, 4);
},
// Get template name from filepath
nameFromTemplatePath(filepath) {
return spx.prettifyName(filepath);
},
// Check config and return value
allowOpeningFolder() {
let disableButton = config.general.disableOpenFolderCommand || false;
// console.log('allowOpeningFolder: ' + disableButton);
if (disableButton==true || disableButton=="true") {
return 'disabled';
} else {
return '';
}
},
// Make long strings nicer to look at
shortifyUIstrings(str) {
return spx.shortifyName(str);
},
// generate checkbox to appconfig
OpenChromeCheck() {
let launch = config.general.launchBrowser || false;
if (launch){
return '<input type="checkbox" checked name="general[launchBrowser]">';
}
else {
return '<input type="checkbox" name="general[launchBrowser]">';
}
},
// Get a correct class for a play button
playButtonClass(onair) {
let value = onair || "false";
let ONAIR = value.toUpperCase();
if (ONAIR=="TRUE") {
return "bg_red";
}
else
{
return "bg_green";
}
},
// Get a correct class for a play button
playButtonText(onair) {
let value = onair || "false";
let ONAIR = value.toUpperCase();
if (ONAIR=="TRUE") {
return spx.lang('button.stop');
}
else
{
return spx.lang('button.play');
}
},
// Define a class for the playicon
playIconClass(onair) {
// console.log('Play icon [' + onair + ']');
let value = onair || "false";
let ONAIR = value.toUpperCase();
if (ONAIR=="TRUE") {
return "playTrue";
}
else
{
return "playFalse";
}
},
// Show username / password in the login view
ShowDemoLoginInfo() {
let showuserpass = config.general.showusercommapass || '';
if (showuserpass==""){
return "<!-- not present config.general.showusercommapass:user,pass -->";
}
let u = showuserpass.split(',')[0]
let p = showuserpass.split(',')[1]
let html="";
html += '<!-- config.general.showusercommapass -->';
html += '<tr>';
html += '<td colspan="2" class="center vmid" style="background-color: #1d1e252f; padding:0.8em; border-radius:0.8em; font-size:0.6em; line-height:1.5em;">';
html += '<span >';
html += 'Login to this instance with<BR>';
html += 'user <span style="color: rgb(252, 174, 30);"><B>' + u + '</B></span>, ';
html += 'pass <span style="color: rgb(252, 174, 30);"><B>' + p + '</B></span>';
html += '</span>';
html += '</td>';
html += '</tr>';
return html;
},
// Print out version number
ShowVersion() {
return vers;
},
// Return available lang files as options with selected one
DropdownOptionsLANG() {
let html = '';
let sel = '';
let localesFolder = path.join(spx.getStartUpFolder(), 'locales');
let fileListData = spx.getJSONFileList(localesFolder);
fileListData.files.forEach((element,i) => {
sel = '';
if (element.name == config.general.langfile)
{
sel = 'selected';
}
html += '<option value="' + element.name + '" ' + sel + '>' + element.name.replace('.json', '') + '</option>';
});
return html
},
// This is used to populate filelist dropdown control type in templates.
// Note: these files are returned as http-assets from SPX server
// Feature added in 1.0.3. and improved in 1.0.6, 1.0.9
// v1.0.15 adds relative assets (within template root folder).
PopulateFilelistOptions(assetfolder, extension, value, relpath='') {
let html = '';
let sel = '';
let fullFilePath = '';
let fullPath = '';
let SERVER_URL = 'http://' + ip.address() + ':' + port;
if ( assetfolder.startsWith('./') ) {
// relative path support added in 1.0.15
let templateFile = path.join(spx.getStartUpFolder(), 'ASSETS', 'templates', relpath);
let templateRootFolder = path.dirname(templateFile)
fullPath = path.join(templateRootFolder, assetfolder)
} else {
// absolute path in ASSETS
let assetPath = path.normalize(assetfolder || '');
fullPath = path.join(spx.getStartUpFolder(), 'ASSETS', assetPath); // fixed (again in 1.0.12)
}
let selectedValue = value;
let fileList = spx.getFileList(fullPath, extension);
// console.log('files', fileList);
if (fileList) {
fileList.forEach((fileRef,index) => {
if (assetfolder.substr(assetfolder.length - 1)!="/") { // fixed a bug in 1.1.1
assetfolder = assetfolder + "/";
}
fullFilePath = assetfolder + fileRef;
sel = "";
if (fullFilePath == selectedValue) {
sel = 'selected';
}
html += '<option value="' + assetfolder + fileRef + '" ' + sel + '>' + fileRef + '</option>';
});
}
return html
},
// Servers status check request
ServerStatus() {
let html = "";
if (config.casparcg) {
config.casparcg.servers.forEach((element,i) => {
let disabledClass = ''
if (element.disabled) {
disabledClass = 'disabledServer';
}
html += '<div class="dropdown serverbtn">';
html += ' <i id="indicator' + i + '" class="fas fa-check-circle" style="color: 00CC00;"></i> <span class="serverIndicator ' + disabledClass + '" id="server' + element.name + '">' + element.name + '</span>';
html += ' <div class="dropdown-content-right">';
html += ' <span style="display: block; opacity: 0.4; background-color: #000; width:100%;">' + element.host + ':' + element.port + '</span>';
html += ' <A href="#" OnClick="spx_system(\'CHECKCONNECTIONS\');return false;">Connection check</A><br>';
html += ' <A href="#" OnClick="clearUsedChannels(\'' + element.name + '\');">Clear all layers</A><br>';
html += ' <A href="#" OnClick="toggleActive(\'' + element.name + '\');">Toggle on/off</A><br>';
html += '</div>';
html += '</div>';
});
}
return html;
// end serverstatus
},
// See if there is custom content packages in the ASSETS folder
DetectCustomContentPackages() {
try {
const fileref = path.join(spx.getStartUpFolder(), 'ASSETS', 'spx-content-repo.txt');
if (fs.existsSync(fileref)) {
var contents = fs.readFileSync(fileref, 'utf8') || '';
if (contents == 'CONTENT_FULL') {
logger.debug('DetectCustomContentPackages: CONTENT_FULL');
return '';
} else {
logger.debug('DetectCustomContentPackages: returns ' + contents);
return '<div class="namedContentIndicator">' + contents + '</div>';
}
} else {
// No file found, return empty string
return '';
}
}
catch (error) {
logger.error('DetectCustomContentPackages Error: ' + error);
return "";
}
// end DetectCustomContentPackages
},
// What renderer options to generate into controller options.
// This same function can return <options> or a CSS classname.
// Added in 1.1.0 but not using it yet:
GenerateLocalRendererOptions(mode) {
return '<!-- Added in 1.1.0 but LocalRendererOptions are not in use yet -->\n';
/* =============== NOT IN USE YET ==================================
let activeRendererOption = config.general.localrenderer || 'program' ;
let html = '';
let sele = '';
switch (mode) {
// return list of renderer options
case 'options':
let options = [
{ 'value': 'program', 'text': 'PROGRAM' },
{ 'value': 'preview', 'text': 'PREVIEW SELECTED' }
];
options.forEach((opt,i) => {
sele = '';
if (opt.value == activeRendererOption) { sele = 'selected'};
html += '<option ' + sele + ' value=' + opt.value + '>' + opt.text + '</option>\n';
});
break;
case 'class':
// return just styling class for the collapsiple
html = 'edge_' + activeRendererOption; // add classes when needed
default:
break;
}
return html;
//============================ not in use YET =========== */
} // end LocalRendererOptions
} // end helpers
})); // end app.engine
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(session({
secret: 'salainenSalaisuus#007',
resave: false,
saveUninitialized: false
}));
global.user = "";
// ----> CasparCG connections are initialized in routes-caspar.js
// Router files
const ROUTEfiles = require('./routes/routes-api.js');
app.use('/api/', ROUTEfiles);
const ROUTEpubAPIv1 = require('./routes/routes-api-v1.js');
app.use('/api/v1/', ROUTEpubAPIv1);
let OSCport = null;
if (config.osc?.enable) {
const ROUTEosc = require('./routes/routes-osc.js');
OSCport = config.osc.port || 5666;
app.use('/osc/', ROUTEosc);
} else {
const ROUTEosc = require('./routes/routes-osc-disabled.js');
app.use('/osc/', ROUTEosc);
}
const ROUTEapp = require('./routes/routes-application.js');
app.use('/', ROUTEapp);
const ROUTEccg = require('./routes/routes-casparcg.js');
//const e = require('express')
app.use('/CCG', ROUTEccg);
process.on('uncaughtException', function(err) {
logger.error('Uncaught Exception: ' + err);
if(err.errno === 'EADDRINUSE') {
console.log('Process failed! Port is in use? [' + err + ']'); // most likely app running on this port already
setTimeout(function(){ process.exit(3); }, 2000);
} else if (err.errno === 'ENOENT') {
console.log('File not found error detected! [' + err + ']');
} else {
console.log('\n\nThis error in unhandled, consider SPX restart! ', err); // Added in 1.1.1
}
});
var server = app.listen(port, (err) => {
let splash = ' Copyright 2020- SPX Graphics\n\n' +
` SPX Server version ........ ${global.vers}\n` +
' License ................... See LICENSE.txt\n' +
` Config file ............... ${configfileref}\n` +
` Cfg / locale .............. ${config.general.langfile}\n` +
` Cfg / host-id (name) ...... ${global.pmac} (${config.general.hostname})\n` +
` Cfg / loglevel ............ ${config.general.loglevel} (options: error | warn | info | verbose | debug )\n` +
` Cfg / dataroot ............ ${path.resolve(config.general.dataroot)}\n` +
` Cfg / logfolder ........... ${logDirectory}\n`;
/* ` Cfg / lauchchrome ...... ${config.general.launchchrome}\n` */
if (config.general.apikey && config.general.apikey != '') {
splash += ` Cfg / apikey ........... Set in config.json\n`;
}
if (config.osc?.enable) {
splash += ` OSC enabled ............ port ${OSCport}\n`;
}
// Where are CasparCG templates loaded from (file:// or http://):
let TemplatesFromInfo;
let tmplSourceAddress = spx.getTemplateSourcePath();
if ( tmplSourceAddress == "") {
TemplatesFromInfo = "See caspar.config"
} else {
TemplatesFromInfo = tmplSourceAddress
}
splash +=
// ` Cfg / templatesource ... ${TemplatesFromInfo}\n\n` +
// ` See README.pdf and Knowledge Base for more info\n` +
// ` and please visit spx.graphics/store to support us.\n\n` +
`\n Explore SPX Graphics related resources:` +
`\n + Homepage ................ https://spx.graphics` +
`\n + Knowledge Base .......... https://spxgc.tawk.help` +
`\n + SPX in the Cloud ........ https://spxcloud.app` +
`\n + Creative Services ....... https://spx.graphics/contact` +
`\n + SPX Graphics for Zoom ... https://spxzoom.com` +
`\n + Buy and sell templates .. https://html.graphics/marketplace`;
console.log(splash);
let prompt = 'Open SPX in a browser:';
let spxUrl = `http://${ipad}:${port}`;
let urLeng = spxUrl.length;
let prLeng = prompt.length;
let maxWid = Math.max(urLeng, prLeng)
let minWid = Math.min(urLeng, prLeng)
let line1s = '╭' + "─".repeat(maxWid+4) + "╮"
let line2s = prompt + " ".repeat(maxWid-prLeng)
let line3s = '╰' + "─".repeat(maxWid+4) + "╯"
let spaCnt, spacer
if (urLeng > prLeng) {
spacer = ""
} else {
spacer = " ".repeat(maxWid-minWid)
}
console.log('\n ' + line1s);
console.log(' │ ' + line2s + ' │');
console.log(' │ ' + spxUrl + spacer + ' │');
console.log(' ' + line3s);
console.log('');
if ( config.general.launchBrowser || global.generatingDefaultConfig ) {
try {
(async () => {
await open(`http://${ipad}:${port}/`);
})();
} catch (error) {
console.warn('Could not open browser.')
}
}
});
// io must be declared as 'global', so routes can access it.
global.io = require('socket.io')(server);
var clients = {}
io.sockets.on('connection', function (socket) {
logger.verbose('*** Socket connection (' + socket.id + ") Connections: " + io.engine.clientsCount);
clients[socket.id] = socket;
notifyMultipleControllers(); // on Connection
socket.on('disconnect', async function () {
let SPXClientName = clients[socket.id].SPXClientName || '** no name **';
// console.log('[' + SPXClientName + '] disconnected (' + socket.id + "). Connections: " + io.engine.clientsCount);
logger.verbose('*** Socket disconnected (' + socket.id + ") Connections: " + io.engine.clientsCount);
delete clients[socket.id];
// Notify controller of other clients lost (such as renderers closed with X)
let data = {};
data.spxcmd = 'clientLostNotification'
data.source = 'windowClose'
data.clientName = SPXClientName
io.emit('SPXMessage2Client', data);