This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (120 loc) · 4.45 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
"use strict";
const FileSystem = require('fs');
const WebSocket = require('ws');
const Request = require('request-promise');
const CheerIO = require('cheerio');
const settings = require('./settings.json');
var widgets = {};
var obsSocket;
var obsConnected = false;
function initializeObsSocket() {
obsSocket = new WebSocket(settings.obsWsAddress);
obsSocket.on('open', () => {
obsConnected = true;
console.log('Connected to the OBS WebSocket!');
});
obsSocket.on('close', () => {
obsConnected = false;
console.log('Disconnected from the OBS WebSocket!');
setTimeout(initializeObsSocket, 1000);
});
obsSocket.on('error', (err) => {
console.log(err);
setTimeout(initializeObsSocket, 1000);
});
obsSocket.on('message', (data) => {
var parsedData = JSON.parse(data);
if(parsedData['update-type'] == 'SwitchScenes') {
var sceneName = parsedData['scene-name'];
console.log('Switched to scene ' + sceneName);
if(!qlcConnected) {
console.log('QLC is disconnected, ignoring...');
return;
}
var buttonId = settings.sceneMappings[sceneName];
// Stop running show
for(var sceneName in settings.sceneMappings) {
var currentButtonId = settings.sceneMappings[sceneName];
if(buttonId == currentButtonId) {
continue;
}
var currentButton = widgets[currentButtonId];
if(currentButton.active) {
// Flip
qlcSocket.send(currentButtonId + '|255');
qlcSocket.send(currentButtonId + '|0');
console.log(currentButton.text + ' was active... disabled.');
}
}
// Find new light show
if(buttonId != undefined) {
var button = widgets[buttonId];
if(button != undefined) {
console.log('Using light show ' + button.text);
if(!button.active) {
qlcSocket.send(buttonId + '|255');
qlcSocket.send(buttonId + '|0');
} else {
console.log('The show was already running!');
}
}
}
}
});
};
var qlcSocket;
var qlcConnected = false;
function initializeQlcSocket() {
qlcSocket = new WebSocket(settings.qlcWsAddress);
qlcSocket.on('open', () => {
// Update widget status...
widgets = {};
Request(settings.qlcWebAddress)
.then(html => {
var $ = CheerIO.load(html);
$('.vcbutton').each((index, entry) => {
var style = entry.attribs['style'];
var button = {
id: entry.attribs['id'],
text: $(entry).text(),
active: style.includes('border: 3px solid #00E600;'),
monitoring: style.includes('border: 3px solid #FFAA00;')
};
widgets[button.id] = button;
});
qlcConnected = true;
console.log('Connected to the QLC WebSocket!');
});
});
qlcSocket.on('close', () => {
if(!qlcConnected) {
return;
}
qlcConnected = false;
console.log('Disconnected from the QLC WebSocket!');
setTimeout(initializeQlcSocket, 1000);
});
qlcSocket.on('error', (err) => {
if(err.code != 'ECONNREFUSED') {
console.log(err);
}
setTimeout(initializeQlcSocket, 1000);
});
qlcSocket.on('message', (data) => {
var splitted = data.split('|');
if(splitted[1] == 'BUTTON') {
var id = splitted[0];
var button = widgets[id];
var value = splitted[2];
if(button != undefined) {
button.active = value == 255;
button.monitoring = value == 127;
//console.log('Updated button ' + button.text + ' status! Act: ' + button.active + ' Mon: ' + button.monitoring);
}
}
});
};
// Initialize
initializeObsSocket();
initializeQlcSocket();
console.log('Ready!');