-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
303 lines (241 loc) · 8.44 KB
/
index.ts
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
import { ActingAsDevice, StageLinqOptions, Services, DeviceId } from '../types';
import { StateData, StateMap, BeatData, BeatInfo, FileTransfer, Broadcast } from '../services';
import { Source } from '../Sources'
import { sleep } from '../utils/sleep';
import { StageLinq } from '../StageLinq';
import { Logger } from '../LogEmitter';
import * as fs from 'fs';
import * as os from 'os';
import * as Path from 'path';
require('console-stamp')(console, {
format: ':date(HH:MM:ss) :label',
});
function progressBar(size: number, bytes: number, total: number): string {
const progress = Math.ceil((bytes / total) * 10)
let progressArrary = new Array<string>(size);
progressArrary.fill(' ');
if (progress) {
for (let i = 0; i < progress; i++) {
progressArrary[i] = '|'
}
}
return `[${progressArrary.join('')}]`
}
// async function getTrackInfo(sourceName: string, deviceId: DeviceId, trackName: string) {
// while (!StageLinq.sources.hasSourceAndDB(sourceName, deviceId)) {
// await sleep(1000);
// }
// try {
// const source = StageLinq.sources.getSource(sourceName, deviceId);
// const connection = source.getDatabase().connection;
// const result = await connection.getTrackInfo(trackName);
// connection.close();
// return result;
// } catch (e) {
// console.error(e);
// }
// }
async function downloadFile(sourceName: string, deviceId: DeviceId, path: string, dest?: string) {
while (!StageLinq.sources.hasSource(sourceName, deviceId)) {
await sleep(250)
}
try {
const source = StageLinq.sources.getSource(sourceName, deviceId);
const data = await StageLinq.sources.downloadFile(source, path);
if (dest && data) {
const filePath = `${dest}/${path.split('/').pop()}`
fs.writeFileSync(filePath, Buffer.from(data));
}
} catch (e) {
console.error(`Could not download ${path}`);
console.error(e)
}
}
async function main() {
console.log('Starting CLI');
const stageLinqOptions: StageLinqOptions = {
downloadDbSources: true,
actingAs: ActingAsDevice.StageLinqJS,
services: [
Services.StateMap,
Services.FileTransfer,
Services.BeatInfo,
Services.Broadcast,
],
}
StageLinq.options = stageLinqOptions;
StageLinq.logger.on('error', (...args: any) => {
console.error(...args);
});
StageLinq.logger.on('warn', (...args: any) => {
console.warn(...args);
args.push("\n");
});
StageLinq.logger.on('info', (...args: any) => {
console.info(...args);
args.push("\n");
});
StageLinq.logger.on('log', (...args: any) => {
console.log(...args);
args.push("\n");
});
// StageLinq.logger.on('debug', (...args: any) => {
// console.debug(...args);
// args.push("\n");
// });
//Note: Silly is very verbose!
// stageLinq.logger.on('silly', (...args: any) => {
// console.debug(...args);
// });
StageLinq.discovery.on('listening', () => {
console.log(`[DISCOVERY] Listening`)
});
StageLinq.discovery.on('announcing', (info) => {
console.log(`[DISCOVERY] Broadcasting Announce ${info.deviceId.string} Port ${info.port} ${info.source} ${info.software.name}:${info.software.version}`)
});
StageLinq.discovery.on('newDiscoveryDevice', (info) => {
console.log(`[DISCOVERY] New Device ${info.deviceId.string} ${info.source} ${info.software.name} ${info.software.version}`)
});
StageLinq.discovery.on('updatedDiscoveryDevice', (info) => {
console.log(`[DISCOVERY] Updated Device ${info.deviceId.string} Port:${info.port} ${info.source} ${info.software.name} ${info.software.version}`)
});
StageLinq.devices.on('newDevice', (device) => {
console.log(`[DEVICES] New Device ${device.deviceId.string}`)
});
StageLinq.devices.on('newService', (device, service) => {
console.log(`[DEVICES] New ${service.name} Service on ${device.deviceId.string} port ${service.serverInfo.port}`)
});
if (stageLinqOptions.services.includes(Services.Broadcast)) {
Broadcast.emitter.on('message', async (deviceId: DeviceId, name: string, value) => {
console.log(`[BROADCAST] ${deviceId.string} ${name}`, value);
const db = StageLinq.sources.getDBByUuid(value.databaseUuid);
if (db.length) {
const connection = db[0].connection();
const track = await connection.getTrackById(value.trackId);
connection.close();
console.log('[BROADCAST] Track Changed:', track);
}
})
}
if (stageLinqOptions.services.includes(Services.StateMap)) {
async function deckIsMaster(data: StateData) {
if (data.json.state) {
const deck = parseInt(data.name.substring(12, 13));
await sleep(250);
const track = StageLinq.status.getTrack(data.deviceId, deck);
console.log(`Now Playing: `, track);
if (stageLinqOptions.services.includes(Services.FileTransfer) && StageLinq.options.downloadDbSources) {
downloadFile(track.source.name, track.source.location, track.source.path, Path.resolve(os.tmpdir()));
}
}
}
StateMap.emitter.on('newDevice', async (service: StateMap) => {
console.log(`[STATEMAP] Subscribing to States on ${service.deviceId.string}`);
for (let i = 1; i <= service.device.deckCount(); i++) {
service.addListener(`/Engine/Deck${i}/DeckIsMaster`, deckIsMaster);
}
service.subscribe();
});
StateMap.emitter.on('stateMessage', async (data: StateData) => {
Logger.info(`[STATEMAP] ${data.deviceId.string} ${data.name} => ${JSON.stringify(data.json)}`);
});
}
if (stageLinqOptions.services.includes(Services.FileTransfer)) {
FileTransfer.emitter.on('fileTransferProgress', (source, file, txid, progress) => {
Logger.debug(`[FILETRANSFER] ${source.name} id:{${txid}} Reading ${file}: ${progressBar(10, progress.bytesDownloaded, progress.total)} (${Math.ceil(progress.percentComplete)}%)`);
});
FileTransfer.emitter.on('fileTransferComplete', (source, file, txid) => {
console.log(`[FILETRANSFER] Complete ${source.name} id:{${txid}} ${file}`);
});
StageLinq.sources.on('newSource', (source: Source) => {
console.log(`[SOURCES] Source Available: (${source.name})`);
});
StageLinq.sources.on('dbDownloaded', (source: Source) => {
console.log(`[SOURCES] Database Downloaded: (${source.name})`);
});
StageLinq.sources.on('sourceRemoved', (sourceName: string, deviceId: DeviceId) => {
console.log(`[SOURCES] Source Removed: ${sourceName} on ${deviceId.string}`);
});
}
if (stageLinqOptions.services.includes(Services.BeatInfo)) {
/**
* Resolution for triggering callback
* 0 = every message WARNING, it's a lot!
* 1 = every beat
* 4 = every 4 beats
* .25 = every 1/4 beat
*/
const beatOptions = {
everyNBeats: 1,
}
/**
* User callback function.
* Will be triggered everytime a player's beat counter crosses the resolution threshold
* @param {BeatData} bd
*/
function beatCallback(bd: BeatData,) {
let deckBeatString = ""
for (let i = 0; i < bd.deckCount; i++) {
deckBeatString += `Deck: ${i + 1} Beat: ${bd.deck[i].beat.toFixed(3)}/${bd.deck[i].totalBeats.toFixed(0)} `
}
console.log(`[BEATINFO] ${bd.deviceId.string} clock: ${bd.clock} ${deckBeatString}`);
}
//// callback is optional, BeatInfo messages can be consumed by:
// - user callback
// - event messages
// - reading the register
const beatMethod = {
useCallback: true,
useEvent: false,
useRegister: false,
};
BeatInfo.emitter.on('newDevice', async (beatInfo: BeatInfo) => {
console.log(`[BEATINFO] New Device ${beatInfo.deviceId.string}`)
if (beatMethod.useCallback) {
beatInfo.startBeatInfo(beatOptions, beatCallback);
}
if (beatMethod.useEvent) {
beatInfo.startBeatInfo(beatOptions);
BeatInfo.emitter.on('beatMessage', (bd) => {
if (bd) {
beatCallback(bd);
}
});
}
if (beatMethod.useRegister) {
beatInfo.startBeatInfo(beatOptions);
function beatFunc(beatInfo: BeatInfo) {
const beatData = beatInfo.getBeatData();
if (beatData) beatCallback(beatData);
}
setTimeout(beatFunc, 4000, beatInfo)
}
})
}
/////////////////////////////////////////////////////////////////////////
// CLI
let returnCode = 0;
try {
process.on('SIGINT', async function () {
console.info('... exiting');
try {
await StageLinq.disconnect();
} catch (err: any) {
const message = err.stack.toString();
console.error(message);
}
process.exit(returnCode);
});
await StageLinq.connect();
while (true) {
await sleep(250);
}
} catch (err: any) {
const message = err.stack.toString();
console.error(message);
returnCode = 1;
}
await StageLinq.disconnect();
process.exit(returnCode);
}
main();