-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
190 lines (156 loc) · 4.96 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
const sfCommandId = 'cjonk6xpp0000z010q1ijb59b';
const fs = require('fs');
const WebSocket = require('ws');
const fetch = require('node-fetch');
function debounce(func, wait, immediate) {
var timeout = void 0;
return function doDebounce() {
var context = this;
var args = arguments;
var later = function later() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}
function viennaConnection({ packageId }) {
var ws,
handlers = {},
matrixInfo = {},
matrixSize = { w: 0, h: 0 };
init();
//testSFConnection().catch(handleError);
async function testSFConnection() {
var data = {};
for (var i = 0; i < 10000; i++) {
data[i] = 'hejsa med digsa';
}
await sendToSoundFlow({ data });
}
function init() {
handlers.updateCellNames = updateCellNames;
handlers.resizeMatrix = resizeMatrix;
handlers.selectCellXY = selectCellXY;
console.log('Package Id: ' + packageId);
console.log('Listening for Vienna Instruments Pro instances...');
connect();
}
function connect() {
ws = new WebSocket('ws://127.0.0.1:8080/');
ws.on('open', onInit);
ws.on('message', function(message) {
//console.log('received: %s', message);
receiveMessage(message);
});
ws.on('error', function(err) {
if ((err + '').indexOf('ECONNREFUSED') < 0) {
//Only show error if it wasn't a simple ECONNREFUSED
console.log('Der skete en fejl i websocket: ' + err, err);
}
setTimeout(() => retry(), 1000);
return true;
});
}
function retry() {
connect();
}
function onInit() {
console.log('Opened... Calling init');
ws.send('INIT');
}
function handleError(err) {
console.log('ERROR: ' + err, err);
}
function receiveMessage(data) {
var s = data;
var i = s.indexOf(':::');
if (i >= 0) s = s.substring(i + 3);
i = s.indexOf('::');
if (i >= 0) s = s.substring(i + 2);
try {
for (let line of s.split('\n')) {
if (!line.trim()) continue;
var [msg, argsF] = line.split('(');
var args = JSON.parse('[' + argsF.slice(0, -2) + ']');
var handler = handlers[msg];
if (handler) {
//console.log(msg, args);
handler(args);
} else {
//console.log('Unsupported handler for msg: ' + msg);
}
}
} catch (e) {
console.log('ERROR: ' + e);
}
}
function resizeMatrix([w, h]) {
matrixSize = { w, h };
updateSoundFlow();
}
function updateCellNames([texts, changedRecords]) {
//console.log('New cell names: ', { texts, changedRecords });
for (let record of changedRecords) {
const [x, y, textIndex, flags] = record;
const isBlank = flags === 8;
const cellText = texts[textIndex];
matrixInfo[x + ',' + y] = cellText || '';
}
updateSoundFlow();
}
function selectCellXY([x, y]) {
reportSelectAsync(x, y).catch(handleError);
}
async function reportSelectAsync(x, y) {
await sendToSoundFlow({
selectedCell: { x, y },
});
}
const updateSoundFlow = debounce(
() => updateSoundFlowAsync().catch(handleError),
50,
);
function getMatrixRepresentation() {
let newMatrixInfo = {};
let { w, h } = matrixSize;
for (let x = 0; x < w; x++)
for (let y = 0; y < h; y++) {
let key = x + ',' + y;
newMatrixInfo[key] = matrixInfo[key] || '';
}
return newMatrixInfo;
}
async function updateSoundFlowAsync() {
await sendToSoundFlow({
buttonNames: getMatrixRepresentation(),
matrixSize: matrixSize,
});
}
async function sendToSoundFlow(data) {
console.log(data);
const fetchRes = await fetch(
'http://localhost:1780/command/' + packageId + ':' + sfCommandId,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
},
);
const response = await fetchRes.json();
console.log('Response from SF: ', response);
}
}
(function() {
var packageId = fs.readFileSync(__dirname + '/packageId.txt', 'utf8').toString().trim();
viennaConnection({ packageId });
})();