-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·2026 lines (1882 loc) · 94.2 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
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
const app = require('express')();
const express = require('express');
const favicon = require('serve-favicon');
const compression = require('compression');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require('fs');
const moment = require('moment');
const fileUpload = require('express-fileupload');
const { spawn } = require('child_process');
const drivelist = require('drivelist');
const unzipper = require('unzipper');
require('sanic.js').changeMyWorld();
const cppaddon = require('./build/Release/cppaddon.node');
const QRCode = require('qrcode');
/*
Features:
- Get Fixtures - Done - Done UI
- Get Fixture Profiles - Done - Done UI
- Add Fixture - Done - Done UI
- Remove Fixture - Done - Done UI
- Get Fixture Settings - Done - Done UI
- Edit Fixture Settings - Done - Done UI
- Get Fixture Parameters - Done - Done UI
- Change Fixture Parameter Value - Done - Done UI
- Parameter Lock - Done - Done UI
- Reset Fixtures - Done - Done UI
- Get Cues - Done - Done UI
- Record Cue - Done - Done UI
- Get Cue Settings - Done - Done UI
- Update Cue - Done - Done UI
- Edit Cue Settings - Done - Done UI
- Clone Cue Last - Done - Done UI
- Clone Cue Next - Done - Done UI
- Move Cue Up - Done - Done UI
- Move Cue Down - Done - Done UI
- Remove Cue - Done - Done UI
- Go To Next Cue - Done - Done UI
- Go To Last Cue - Done - Done UI
- Go To Specific Cue - Done - Done UI
- Stop Running Cue - Done - Done UI
- Get Groups - Done - Done UI
- Add Group - Done - Done UI
- Get Group Parameters - Done - Done UI
- Change Group Parameter Value - Done - Done UI
- Get Group Settings - Done - Done UI
- Edit Group Settings - Done - Done UI
- Remove Group - Done - Done UI
- Reset Group - Done - Done UI
- Reset Groups - Done - Done UI
- Remove Group Fixture - Done - Done UI
- Save Show - Done - Done UI
- Open Show From File - Done - Done UI
- Save Show To File - Done - Done UI
- Save Show To USB - Done - Done UI
- Import Fixture Definition From File - Done - Done UI
- View Docs - Done - Done UI
- View Settings - Done - Done UI
- Save Settings - Done - Done UI
- Update Firmware - Done - Done UI
- View Presets - Done - Done UI
- Record Preset - Done - Done UI
- Edit Preset - Done - Done UI
- Activate/Deactivate Preset - Done UI
- Remove Preset - Done - Done UI
- Preset Kiosk Page - Done - Done UI
- Grandmaster - Done - Done UI
- Blackout - Done - Done UI
- Auto Mark - Done - Done UI
- Fine Control - Done - Done UI
- Reset Presets - Done - Done UI
*/
var SETTINGS = {
device: "linux", // linux, rpi, win, macos
url: "localhost", // http web UI location
port: 3000,
defaultUpTime: 3,
defaultDownTime: 3,
desktop: true, // desktop vs embeded
udmx: false,
automark: true,
artnetIP: null, // ArtNet output IP
artnetHost: '255.255.255.255', // Artnet network host
sacnIP: null // sACN output IP
}
var STARTED = false;
const VERSION = "2.0.0 Beta 5";
fs.exists(process.cwd() + '/settings.json', function (exists) {
if (exists == false) {
saveSettings();
}
openSettings();
});
var fixtures = [];
var cues = [];
var groups = [];
var presets = [];
var currentCue = "";
var lastCue = "";
var currentCueID = "";
var blackout = false;
var grandmaster = 100;
// Set up dmx variables for integrations used later on
var e131 = null;
var client = null;
var packet = null;
var slotsData = null;
var channels = null;
var artnet = null;
var cp = null;
require.extensions['.jlib'] = require.extensions['.json'];
// Load the Tonalite settings from file
function openSettings() {
fs.readFile(process.cwd() + '/settings.json', function (err, data) {
if (err) logError(err);
var settings = JSON.parse(data);
SETTINGS = settings;
if (STARTED == false) {
STARTED = true;
e131 = require('e131');
client = new e131.Client(SETTINGS.sacnIP);
packet = client.createPacket(512);
slotsData = packet.getSlotsData();
channels = slotsData;
cp = cp;
artnet = require('artnet')({ iface: SETTINGS.artnetIP, host: SETTINGS.artnetHost, sendAll: true });
fs.exists(process.cwd() + '/presets.json', function (exists) {
if (exists == true) {
openPresets();
}
});
http.listen(SETTINGS.port, SETTINGS.url, function () {
var msg = "Desktop";
if (SETTINGS.desktop === false)
msg = "Embeded";
console.log(`Tonalite ${msg} v${VERSION} - DMX Lighting Control System`);
console.log(`The web UI can be found at http://${SETTINGS.url}:${SETTINGS.port}`);
});
if (SETTINGS.udmx == true) {
if (SETTINGS.device === "linux") {
cp = spawn(process.cwd() + '/uDMXArtnet/uDMXArtnet_minimal_64');
} else if (SETTINGS.device === "rpi") {
cp = spawn(process.cwd() + '/uDMXArtnet/uDMXArtnet_PI_minimal_32', ['-i', '192.168.4.1']);
} else if (SETTINGS.device === "win") {
cp = spawn(process.cwd() + '/uDMXArtnet/uDMXArtnet_Minimal.exe');
} else {
console.log("Selected platform not supported by uDMX, falling back to ArtNet.");
}
}
// Output DMX frames 40 times a second
setInterval(dmxLoop, 25);
}
});
}
// Save the Tonalite settings to a file
function saveSettings() {
fs.writeFile(process.cwd() + "/settings.json", JSON.stringify(SETTINGS, null, 4), (err) => {
if (err) {
logError(err);
return false;
};
});
return true;
};
function updateFirmware(callback) {
var uploadComplete = false;
drivelist.list((error, drives) => {
if (error) {
logError(error);
}
drives.forEach((drive) => {
if (drive.enumerator == 'USBSTOR' || drive.isUSB === true) {
fs.exists(drive.mountpoints[0].path + "/tonalite.zip", function (exists) {
if (exists) {
fs.createReadStream(drive.mountpoints[0].path + "/tonalite.zip").pipe(unzipper.Extract({ path: process.cwd() }));
uploadComplete = true;
return callback(uploadComplete);
}
});
}
});
});
};
function importFixtures(callback) {
var importComplete = false;
drivelist.list((error, drives) => {
if (error) {
logError(error);
}
drives.forEach((drive) => {
if (drive.enumerator == 'USBSTOR' || drive.isUSB === true) {
fs.readdir(drive.mountpoints[0].path, (err, files) => {
files.forEach(file => {
fs.copyFile(drive.mountpoints[0].path + "/" + file, process.cwd() + "/fixtures/" + file, (err) => {
if (err) logError(err);
importComplete = true;
});
});
});
fs.exists(drive.mountpoints[0].path + "/fixtures.zip", function (exists) {
if (exists) {
fs.createReadStream(drive.mountpoints[0].path + "/fixtures.zip").pipe(unzipper.Extract({ path: process.cwd() }));
importComplete = true;
}
});
}
});
});
return callback(importComplete);
};
function logError(msg) {
var datetime = new Date();
fs.appendFile('error-' + datetime + '.txt', msg, (err) => {
if (err) logError(err);
});
};
function moveArrayItem(arr, old_index, new_index) {
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length;
while ((k--) + 1) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr;
};
function generateID() {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
};
function generateGroupParameters(newGroup) {
var parameterCats = [];
var parameters = [];
let i = 0; const iMax = newGroup.ids.length; for (; i < iMax; i++) {
var fixture = fixtures[fixtures.map(el => el.id).indexOf(newGroup.ids[i])];
let c = 0; const cMax = fixture.parameters.length; for (; c < cMax; c++) {
var newParameter = JSON.parse(JSON.stringify(fixture.parameters[c]));
if (!parameterCats.includes(newParameter.name + ":" + newParameter.type)) {
newParameter.value = newParameter.home;
parameters.push(newParameter);
parameterCats.push(newParameter.name + ":" + newParameter.type);
}
}
}
return parameters;
};
function cleanFixtures() {
var newFixtures = JSON.parse(JSON.stringify(fixtures));
let f = 0; const fMax = newFixtures.length; for (; f < fMax; f++) {
delete newFixtures.effects;
delete newFixtures.chips;
let p = 0; const pMax = newFixtures[f].parameters.length; for (; p < pMax; p++) {
delete newFixtures[f].parameters[p].max;
delete newFixtures[f].parameters[p].min;
delete newFixtures[f].parameters[p].home;
delete newFixtures[f].parameters[p].coarse;
newFixtures[f].parameters[p].displayValue = Math.round(newFixtures[f].parameters[p].displayValue);
}
}
return newFixtures;
};
function cleanFixtureForCue(fixture) {
var newFixture = JSON.parse(JSON.stringify(fixture));
delete newFixture.name;
delete newFixture.modelName;
delete newFixture.shortName;
delete newFixture.manufacturerName;
delete newFixture.hasLockedParameters;
delete newFixture.chips;
delete newFixture.dcid;
delete newFixture.colortable;
newFixture.effects = cleanEffectsForCue(newFixture.effects);
let p = 0; const pMax = newFixture.parameters.length; for (; p < pMax; p++) {
delete newFixture.parameters[p].name;
delete newFixture.parameters[p].displayValue;
delete newFixture.parameters[p].home;
delete newFixture.parameters[p].locked;
delete newFixture.parameters[p].ranges;
delete newFixture.parameters[p].highlight;
delete newFixture.parameters[p].snap;
delete newFixture.parameters[p].size;
}
return newFixture;
};
function cleanEffect(effect) {
var newEffect = JSON.parse(JSON.stringify(effect));
delete newEffect.steps;
delete newEffect.valueCount;
delete newEffect.absolute;
delete newEffect.resolution;
delete newEffect.parameterNames;
delete newEffect.step;
delete newEffect.speed;
delete newEffect.depth;
delete newEffect.chroma;
delete newEffect.fan;
delete newEffect.aspect;
delete newEffect.rotation;
return newEffect;
};
function cleanEffects(ineffects) {
var newEffects = JSON.parse(JSON.stringify(ineffects));
let e = 0; const eMax = newEffects.length; for (; e < eMax; e++) {
newEffects[e] = cleanEffect(newEffects[e]);
}
return newEffects;
};
function cleanEffectForCue(effect) {
var newEffect = JSON.parse(JSON.stringify(effect));
delete newEffect.steps;
delete newEffect.valueCount;
delete newEffect.absolute;
delete newEffect.resolution;
delete newEffect.parameterNames;
delete newEffect.step;
delete newEffect.type;
delete newEffect.name;
return newEffect;
};
function cleanEffectsForCue(ineffects) {
var newEffects = JSON.parse(JSON.stringify(ineffects));
let e = 0; const eMax = newEffects.length; for (; e < eMax; e++) {
newEffects[e] = cleanEffectForCue(newEffects[e]);
}
return newEffects;
};
function cleanFixturesForCue() {
var newFixtures = [];
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
newFixtures.push(cleanFixtureForCue(fixtures[f]));
}
return newFixtures;
};
function cleanGroups() {
var newGroups = JSON.parse(JSON.stringify(groups));
let g = 0; const gMax = newGroups.length; for (; g < gMax; g++) {
delete newGroups[g].ids;
let p = 0; const pMax = newGroups[g].parameters.length; for (; p < pMax; p++) {
delete newGroups[g].parameters[p].max;
delete newGroups[g].parameters[p].min;
delete newGroups[g].parameters[p].home;
delete newGroups[g].parameters[p].coarse;
}
}
return newGroups;
};
function cleanCues() {
var newCues = JSON.parse(JSON.stringify(cues));
let c = 0; const cMax = newCues.length; for (; c < cMax; c++) {
delete newCues[c].upTime;
delete newCues[c].downTime;
delete newCues[c].follow;
delete newCues[c].upStep;
delete newCues[c].downStep;
delete newCues[c].following;
delete newCues[c].fixtures;
}
return newCues;
};
function cleanPresets() {
var newPresets = JSON.parse(JSON.stringify(presets));
let p = 0; const pMax = newPresets.length; for (; p < pMax; p++) {
delete newPresets[p].parameters;
}
return newPresets;
};
function getGroupFixtures(groupID) {
var group = groups[groups.map(el => el.id).indexOf(groupID)];
var fixtureStarts = [];
let i = 0; const iMax = group.ids.length; for (; i < iMax; i++) {
var fixture = fixtures[fixtures.map(el => el.id).indexOf(group.ids[i])];
fixtureStarts.push([fixture.name, fixture.startDMXAddress, fixture.id]);
}
return fixtureStarts;
};
// Set the output channel values to those of the current fixture values
function calculateChannels() {
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
let p = 0; const pMax = fixtures[f].parameters.length; for (; p < pMax; p++) {
if (fixtures[f].parameters[p].fadeWithIntensity == true || fixtures[f].parameters[p].type == 1) {
if (blackout === false) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = ((fixtures[f].parameters[p].value >> 8) / 100.0) * grandmaster;
if (fixtures[f].parameters[p].fine != null) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].fine] = ((fixtures[f].parameters[p].value & 0xff) / 100.0) * grandmaster;
}
} else {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = (fixtures[f].parameters[p].min >> 8);
if (fixtures[f].parameters[p].fine != null) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].fine] = (fixtures[f].parameters[p].min & 0xff);
}
}
} else {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = (fixtures[f].parameters[p].value >> 8);
if (fixtures[f].parameters[p].fine != null) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].fine] = (fixtures[f].parameters[p].value & 0xff);
}
}
}
}
};
function calculateChannelsList() {
var chans = [];
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
let p = 0; const pMax = fixtures[f].parameters.length; for (; p < pMax; p++) {
chans[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = (fixtures[f].parameters[p].value >> 8);
if (fixtures[f].parameters[p].fine != null) {
chans[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].fine] = (fixtures[f].parameters[p].value & 0xff);
}
}
}
return chans;
};
// Set the cue's output channel values to the correct values from the fixtures. This is basically saving the cue.
function calculateCue(cue) {
var outputChannels = new Array(512).fill(0);
let f = 0; const fMax = cue.fixtures.length; for (; f < fMax; f++) {
var startFixture = fixtures[fixtures.map(el => el.id).indexOf(cue.fixtures[f].id)];
let e = 0; const eMax = cue.fixtures[f].effects.length; for (; e < eMax; e++) {
if (startFixture.effects[e].id == cue.fixtures[f].effects[e].id) {
startFixture.effects[e].fan = cue.fixtures[f].effects[e].fan;
if (cue.fixtures[f].effects[e].active != startFixture.effects[e].active) {
startFixture.effects[e].step = startFixture.effects[e].fan;
}
startFixture.effects[e].depth = cue.fixtures[f].effects[e].depth;
startFixture.effects[e].speed = cue.fixtures[f].effects[e].speed;
startFixture.effects[e].chroma = cue.fixtures[f].effects[e].chroma;
startFixture.effects[e].aspect = cue.fixtures[f].effects[e].aspect;
startFixture.effects[e].rotation = cue.fixtures[f].effects[e].rotation;
startFixture.effects[e].active = cue.fixtures[f].effects[e].active;
}
}
let c = 0; const cMax = cue.fixtures[f].parameters.length; for (; c < cMax; c++) {
if (startFixture.parameters[c].locked === false) {
var startParameter = startFixture.parameters[c].value;
var endParameter = cue.fixtures[f].parameters[c].value;
// If the end parameter is greater than the start parameter, the value is going in, out is going out if less
if (endParameter >= startParameter) {
// Make sure that the step does not dip below 0 (finished)
if (cue.upStep >= 0) {
if (cue.fixtures[f].parameters[c].fadeWithIntensity == true || cue.fixtures[f].parameters[c].type == 1) {
if (blackout === false) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = (((endParameter + (((startParameter - endParameter) / (cue.upTime * 40)) * cue.upStep)) >> 8) / 100.0) * grandmaster;
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = (((endParameter + (((startParameter - endParameter) / (cue.upTime * 40)) * cue.upStep)) & 0xff) / 100.0) * grandmaster;
}
} else {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = (startFixture.parameters[c].min >> 8);
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = (startFixture.parameters[c].min & 0xff);
}
}
} else {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = ((endParameter + (((startParameter - endParameter) / (cue.upTime * 40)) * cue.upStep)) >> 8);
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = ((endParameter + (((startParameter - endParameter) / (cue.upTime * 40)) * cue.upStep)) & 0xff);
}
}
fixtures[fixtures.map(el => el.id).indexOf(cue.fixtures[f].id)].parameters[c].displayValue = cppaddon.mapRange(cue.fixtures[f].parameters[c].value + (((startFixture.parameters[c].value - cue.fixtures[f].parameters[c].value) / (cue.upTime * 40)) * cue.upStep), cue.fixtures[f].parameters[c].min, cue.fixtures[f].parameters[c].max, 0, 100);
}
} else {
// Make sure that the step does not dip below 0 (finished)
if (cue.downStep >= 0) {
if (cue.fixtures[f].parameters[c].fadeWithIntensity == true || cue.fixtures[f].parameters[c].type == 1) {
if (blackout === false) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = (((endParameter + (((startParameter - endParameter) / (cue.downTime * 40)) * cue.downStep)) >> 8) / 100.0) * grandmaster;
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = (((endParameter + (((startParameter - endParameter) / (cue.downTime * 40)) * cue.downStep)) & 0xff) / 100.0) * grandmaster;
}
} else {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = (startFixture.parameters[c].min >> 8);
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = (startFixture.parameters[c].min & 0xff);
}
}
} else {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = ((endParameter + (((startParameter - endParameter) / (cue.downTime * 40)) * cue.downStep)) >> 8);
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = ((endParameter + (((startParameter - endParameter) / (cue.downTime * 40)) * cue.downStep)) & 0xff);
}
}
fixtures[fixtures.map(el => el.id).indexOf(cue.fixtures[f].id)].parameters[c].displayValue = cppaddon.mapRange(cue.fixtures[f].parameters[c].value + (((startFixture.parameters[c].value - cue.fixtures[f].parameters[c].value) / (cue.downTime * 40)) * cue.downStep), cue.fixtures[f].parameters[c].min, cue.fixtures[f].parameters[c].max, 0, 100);
}
}
} else {
var startParameter = startFixture.parameters[c].value;
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].coarse] = (startParameter >> 8);
if (cue.fixtures[f].parameters[c].fine != null) {
outputChannels[(cue.fixtures[f].startDMXAddress - 1) + cue.fixtures[f].parameters[c].fine] = (startParameter & 0xff);
}
}
}
}
return outputChannels;
};
function calculateStack() {
// If there is a running cue
if (currentCue != "") {
// Get the current cue
cue = cues[cues.map(el => el.id).indexOf(currentCue)];
channels = calculateCue(cue);
cue.upStep -= 1;
cue.downStep -= 1;
// Check if the cue needs to be followed by another cue
if (cue.upStep < 0 && cue.downStep < 0) {
if (cue.follow != -1) {
cue.active = false;
if (cue.following === false) {
cue.upStep = cue.follow * 40;
cue.downStep = cue.follow * 40;
cue.following = true;
} else {
cue.upStep = cue.upTime * 40;
cue.downStep = cue.downTime * 40;
cue.following = false;
if (cues.map(el => el.id).indexOf(currentCue) === cues.length - 1) {
currentCue = cues[0].id;
} else {
currentCue = cues[cues.map(el => el.id).indexOf(currentCue) + 1].id;
}
lastCue = currentCue;
cues[cues.map(el => el.id).indexOf(currentCue)].active = true;
currentCueID = currentCue;
}
} else {
currentCue = "";
cue.upStep = cue.upTime * 40;
cue.downStep = cue.downTime * 40;
cue.active = false;
io.emit('cueActionBtn', false);
}
// Set the fixture's display and real values to the correct values from the cue
let f = 0; const fMax = cue.fixtures.length; for (; f < fMax; f++) {
var startFixtureParameters = fixtures[fixtures.map(el => el.id).indexOf(cue.fixtures[f].id)].parameters;
let c = 0; const cMax = cue.fixtures[f].parameters.length; for (; c < cMax; c++) {
if (startFixtureParameters[c].locked === false) {
startFixtureParameters[c].value = cue.fixtures[f].parameters[c].value;
startFixtureParameters[c].displayValue = cppaddon.mapRange(cue.fixtures[f].parameters[c].value, cue.fixtures[f].parameters[c].min, cue.fixtures[f].parameters[c].max, 0, 100);
}
}
}
if (SETTINGS.automark === true) {
if (cues.map(el => el.id).indexOf(lastCue) + 1 === cues.length) {
var nextCue = cues[0];
} else {
var nextCue = cues[cues.map(el => el.id).indexOf(lastCue) + 1];
}
f = 0; const fMax1 = nextCue.fixtures.length; for (; f < fMax1; f++) {
startFixtureParameters = fixtures[fixtures.map(el => el.id).indexOf(nextCue.fixtures[f].id)].parameters;
nextCueFixtureParameters = nextCue.fixtures[f].parameters;
if (fixtures[fixtures.map(el => el.id).indexOf(nextCue.fixtures[f].id)].hasIntensity == true) {
if (startFixtureParameters[startFixtureParameters.map(el => el.type).indexOf(1)].value === 0 && nextCueFixtureParameters[nextCueFixtureParameters.map(el => el.type).indexOf(1)].value > 0) {
c = 0; const cMax1 = nextCueFixtureParameters.length; for (; c < cMax1; c++) {
if (startFixtureParameters[c].locked === false && startFixtureParameters[c].type != 1) {
startFixtureParameters[c].value = nextCueFixtureParameters[c].value;
startFixtureParameters[c].displayValue = cppaddon.mapRange(nextCueFixtureParameters[c].value, nextCueFixtureParameters[c].min, nextCueFixtureParameters[c].max, 0, 100);
}
}
}
}
}
}
io.sockets.emit('currentCue', currentCueID);
io.sockets.emit('cues', cleanCues());
}
io.sockets.emit('fixtures', cleanFixtures());
}
if (blackout === false) {
//var displayChanged = false;
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
let e = 0; const eMax = fixtures[f].effects.length; for (; e < eMax; e++) {
if (fixtures[f].effects[e].active == true) {
let p = 0; const pMax = fixtures[f].parameters.length; for (; p < pMax; p++) {
if (fixtures[f].parameters[p].locked === false) {
var effectChanIndex = fixtures[f].effects[e].parameterNames.findIndex(function (element) { return element == fixtures[f].parameters[p].name });
if (effectChanIndex > -1) {
var effectValue = fixtures[f].effects[e].steps[fixtures[f].effects[e].step][effectChanIndex];
if (fixtures[f].effects[e].resolution == 8) {
effectValue = cppaddon.mapRange(effectValue, 0, 255, fixtures[f].parameters[p].min, fixtures[f].parameters[p].max);
}
effectValue = (effectValue * fixtures[f].effects[e].depth) + ((fixtures[f].parameters[p].value >> 8) * (1 - fixtures[f].effects[e].depth));
/*if (fixtures[f].parameters[p].type == 1) {
fixtures[f].parameters[p].displayValue = cppaddon.mapRange(effectValue, fixtures[f].parameters[p].min, fixtures[f].parameters[p].max, 0, 100);
displayChanged = true;
}*/
if (fixtures[f].parameters[p].fadeWithIntensity == true || fixtures[f].parameters[p].type == 1) {
effectValue = (effectValue / 100.0) * grandmaster;
}
if (fixtures[f].effects[e].resolution == 16) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = (effectValue >> 8);
//channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = fixtures[f].effects[e].steps[fixtures[f].effects[e].step][effectChanIndex];
if (fixtures[f].parameters[p].fine != null) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].fine] = (effectValue & 0xff);
}
} else if (fixtures[f].effects[e].resolution == 8) {
channels[(fixtures[f].startDMXAddress - 1) + fixtures[f].parameters[p].coarse] = effectValue;
}
}
}
}
if (fixtures[f].effects[e].step + fixtures[f].effects[e].speed >= fixtures[f].effects[e].steps.length - 1) {
fixtures[f].effects[e].step = fixtures[f].effects[e].fan;
} else {
fixtures[f].effects[e].step = fixtures[f].effects[e].step + fixtures[f].effects[e].speed;
}
}
}
}
/*if (displayChanged === true) {
io.sockets.emit('fixtures', cleanFixtures());
}*/
}
// Allow presets to overide everything else for channels in which they have higher values
let p = 0; const pMax = presets.length; for (; p < pMax; p++) {
if (presets[p].active) {
let c = 0; const cMax = presets[p].parameters.length; for (; c < cMax; c++) {
if (presets[p].parameters[c] != null) {
var tempvalue = (presets[p].parameters[c] / 100.0) * presets[p].intensity;
if (tempvalue > channels[c]) {
channels[c] = tempvalue;
}
}
}
}
}
};
// Set the fixture values for each group equal to the group's parameter value
function setFixtureGroupValues(group, parameter) {
let i = 0; const iMax = group.ids.length; for (; i < iMax; i++) {
var fixture = fixtures[fixtures.map(el => el.id).indexOf(group.ids[i])];
let c = 0; const cMax = fixture.parameters.length; for (; c < cMax; c++) {
if (fixture.parameters[c].name === parameter.name && fixture.parameters[c].type === parameter.type) {
if (fixture.parameters[c].locked != true) {
fixture.parameters[c].value = parameter.value;
fixture.parameters[c].displayValue = cppaddon.mapRange(fixture.parameters[c].value, fixture.parameters[c].min, fixture.parameters[c].max, 0, 100);
}
}
}
}
};
// Reset the parameter values for each fixture
function resetFixtures() {
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
let c = 0; const cMax = fixtures[f].parameters.length; for (; c < cMax; c++) {
if (fixtures[f].parameters[c].locked != true) {
fixtures[f].parameters[c].value = fixtures[f].parameters[c].home;
fixtures[f].parameters[c].displayValue = cppaddon.mapRange(fixtures[f].parameters[c].value, fixtures[f].parameters[c].min, fixtures[f].parameters[c].max, 0, 100);
}
}
let e = 0; const eMax = fixtures[f].effects.length; for (; e < eMax; e++) {
fixtures[f].effects[e].active = false;
}
}
};
// Reset the parameter values for each group
function resetGroups() {
let g = 0; const gMax = groups.length; for (; g < gMax; g++) {
let c = 0; const cMax = groups[g].parameters.length; for (; c < cMax; c++) {
groups[g].parameters[c].value = groups[g].parameters[c].home;
groups[g].parameters[c].displayValue = cppaddon.mapRange(groups[g].parameters[c].value, groups[g].parameters[c].min, groups[g].parameters[c].max, 0, 100);
setFixtureGroupValues(groups[g], groups[g].parameters[c]);
}
}
};
// This is the output dmx loop. It gathers the parameter and calculates what the output values should be.
function dmxLoop() {
// Reset DMX values
let c = 0; const cMax = channels.length; for (; c < cMax; c++) {
channels[c] = 0;
}
calculateChannels();
calculateStack();
slotsData = channels;
client.send(packet);
artnet.set(channels);
};
// Load the fixtures, cues, and groups from file
function openShow(file = "show.json") {
fs.readFile(process.cwd() + '/' + file, (err, data) => {
if (err) logError(err);
let show = JSON.parse(data);
fixtures = show[0];
cues = show[1];
groups = show[2];
io.emit('fixtures', cleanFixtures());
io.emit('currentCue', currentCueID);
io.emit('cues', cleanCues());
io.emit('groups', cleanGroups());
});
};
// Save the fixtures, cues, and groups of the show to file
function saveShow() {
fs.writeFile(process.cwd() + "/show.json", JSON.stringify([fixtures, cues, groups]), (err) => {
if (err) {
logError(err);
return false;
};
});
return true;
};
// Load the presets from file
function openPresets() {
fs.readFile(process.cwd() + '/presets.json', (err, data) => {
if (err) logError(err);
presets = JSON.parse(data);
io.emit('presets', cleanPresets());
});
};
// Save the presets to file
function savePresets() {
fs.writeFile(process.cwd() + "/presets.json", JSON.stringify(presets), (err) => {
if (err) {
logError(err);
return false;
};
});
return true;
};
app.use('/static', express.static(__dirname + '/static'));
app.use('/docs', express.static(__dirname + '/docs/dist'));
app.use(fileUpload());
app.use(compression());
app.use(favicon(__dirname + '/static/img/favicon.ico'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.min.html');
});
app.get('/presets', function (req, res) {
res.sendFile(__dirname + '/presets.min.html');
});
app.get('/open-source-licenses', function (req, res) {
res.sendFile(__dirname + '/open-source-licenses.txt');
});
app.get('/showFile', function (req, res) {
res.download(process.cwd() + '/show.json', moment().format() + '.tonalite', { headers: { 'Content-Disposition': 'attachment', 'Content-Type': 'application/octet-stream' } });
});
// Upload Show File
app.post('/showFile', (req, res) => {
if (Object.keys(req.files).length == 0) {
return res.status(400).send('No files were uploaded.');
}
let showFile = req.files.showFile;
showFile.mv(process.cwd() + '/show.json', function (err) {
if (err)
return res.status(500).send(err);
openShow();
res.redirect('/');
});
});
app.post('/importFixtureDefinition', (req, res) => {
if (Object.keys(req.files).length == 0) {
return res.status(400).send('No files were uploaded.');
}
let fixtureDefinition = req.files.fixtureDefinition;
if (fixtureDefinition.mimetype == "application/json") {
fixtureDefinition.mv(process.cwd() + '/fixtures/' + req.files.fixtureDefinition.name, function (err) {
if (err)
return res.status(500).send(err);
io.emit('message', { type: "info", content: "The fixture profile has been imported!" });
});
} else {
io.emit('message', { type: "error", content: "The fixture profile was not a json file!" });
}
});
fs.exists(process.cwd() + '/show.json', function (exists) {
if (exists == false) {
saveShow();
}
openShow();
});
io.on('connection', function (socket) {
socket.emit('currentCue', currentCueID);
socket.emit('fixtures', cleanFixtures());
socket.emit('cues', cleanCues());
socket.emit('groups', cleanGroups());
socket.emit('presets', cleanPresets());
socket.emit('blackout', blackout);
socket.emit('grandmaster', grandmaster);
QRCode.toDataURL(`http://${SETTINGS.url}:${SETTINGS.port}`, function (err, url) {
socket.emit('meta', { desktop: SETTINGS.desktop, version: VERSION, qrcode: url, url: `http://${SETTINGS.url}:${SETTINGS.port}` });
});
if (currentCue === "") {
socket.emit('cueActionBtn', false);
} else {
socket.emit('cueActionBtn', true);
}
socket.on('openShowFromUSB', function (data) {
fs.copyFile(data[1] + '/' + data[0], process.cwd() + '/show.json', function (err) {
if (err) {
logError(err);
socket.emit('message', { type: "error", content: "The show could not be opened!" });
} else {
openShow();
io.emit('message', { type: "info", content: "The show has been opened!" });
}
});
});
socket.on('resetShow', function () {
fixtures = [];
cues = [];
groups = [];
currentCue = "";
lastCue = "";
io.emit('fixtures', cleanFixtures());
io.emit('currentCue', currentCueID);
io.emit('cues', cleanCues());
io.emit('groups', cleanGroups());
io.emit('cueActionBtn', false);
io.emit('message', { type: "info", content: "A new show has been created!" });
saveShow();
});
socket.on('resetPresets', function () {
presets = [];
socket.emit('presets', cleanPresets());
io.emit('presets', cleanPresets());
io.emit('message', { type: "info", content: "The presets have been cleared!" });
savePresets();
});
socket.on('getFixtureProfiles', function () {
var startDMXAddress = 1;
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
startDMXAddress += fixtures[f].maxOffset + 1;
}
fs.readdir(process.cwd() + "/fixtures", (err, files) => {
var fixturesList = [];
files.forEach(file => {
var fixture = require(process.cwd() + "/fixtures/" + file);
fixture.personalities.forEach(function (personality) {
fixturesList.push([personality.modelName, personality.modeName, personality.manufacturerName, file, personality.dcid]);
});
});
socket.emit('fixtureProfiles', [fixturesList, startDMXAddress]);
});
});
socket.on('getEffects', function (fixtureid) {
fs.readdir(process.cwd() + "/effects", (err, files) => {
var effectsList = [];
files.forEach(file => {
var effect = require(process.cwd() + "/effects/" + file).effectTable;
if (JSON.stringify(effect.parameterNames) == JSON.stringify(["Red", "Green", "Blue"])) {
effect.type = "Color";
} else if (JSON.stringify(effect.parameterNames) == JSON.stringify(["Intensity"])) {
effect.type = "Intensity";
} else if (JSON.stringify(effect.parameterNames) == JSON.stringify(["Pan", "Tilt"])) {
effect.type = "Shape";
} else if (JSON.stringify(effect.parameterNames) == JSON.stringify(["Parameter"])) {
effect.type = "Parameter";
}
effectsList.push([effect.name, effect.type, file]);
});
socket.emit('fixtureEffects', [fixtureid, effectsList]);
});
});
socket.on('getShowsFromUSB', function () {
if (SETTINGS.desktop === false) {
drivelist.list((error, drives) => {
var done = false;
if (error) {
logError(error);
}
drives.forEach((drive) => {
if (done == false) {
if (drive.enumerator == 'USBSTOR' || drive.isUSB === true) {
fs.readdir(drive.mountpoints[0].path, (err, files) => {
var showsList = [];
files.forEach(file => {
if (file.slice(-8) === "tonalite") {
showsList.push(file);
}
});
socket.emit('shows', [showsList, drive.mountpoints[0].path]);
});
done = true;
}
}
});
if (done == false) {
socket.emit('message', { type: "error", content: "Shows could not be read of of a USB drive. Is there one connected?" });
}
});
}
});
socket.on('addFixture', function (msg) {
var startDMXAddress = parseInt(msg.startDMXAddress);
let f = 0; const fMax = fixtures.length; for (; f < fMax; f++) {
if (fixtures[f].startDMXAddress == startDMXAddress) {
startDMXAddress = null;
}
if (startDMXAddress >= fixtures[f].startDMXAddress && startDMXAddress < parseInt(fixtures[f].startDMXAddress) + parseInt(fixtures[f].maxOffset + 1)) {
startDMXAddress = null;
}
}
if (startDMXAddress) {
let i = 0; const iMax = parseInt(msg.creationCount); for (; i < iMax; i++) {
// Add a fixture using the fixture spec file in the fixtures folder
var fixture = require(process.cwd() + "/fixtures/" + msg.fixtureName);
fixture = fixture.personalities[fixture.personalities.map(el => el.dcid).indexOf(msg.dcid)];
fixture.startDMXAddress = startDMXAddress;
fixture.dmxUniverse = 0;
fixture.hasLockedParameters = false;
fixture.name = fixture.modelName;
fixture.chips = [];
fixture.effects = [];
if (fixture.colortable == "3874B444-A11E-47D9-8295-04556EAEBEA7") {
// RGB
colortable = JSON.parse(JSON.stringify(require(process.cwd() + "/chips/rgb.json")));
let col = 0; const colMax = colortable.length; for (; col < colMax; col++) {
var color = { color: colortable[col].color, parameters: [] };
color.parameters.push({ name: "Red", value: colortable[col].parameters[0] });
color.parameters.push({ name: "Green", value: colortable[col].parameters[1] });
color.parameters.push({ name: "Blue", value: colortable[col].parameters[2] });
fixture.chips.push(color);
}
} else if (fixture.colortable == "77A82F8A-9B24-4C3F-98FC-B6A29FB1AAE6") {
// RGBW
colortable = JSON.parse(JSON.stringify(require(process.cwd() + "/chips/rgb.json")));
let col = 0; const colMax = colortable.length; for (; col < colMax; col++) {
var color = { color: colortable[col].color, parameters: [] };