-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
187 lines (157 loc) · 5.56 KB
/
index.ts
File metadata and controls
187 lines (157 loc) · 5.56 KB
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
import { ActingAsDevice, PlayerStatus } from '../types';
import { DbConnection } from "../Databases";
import { sleep } from '../utils/sleep';
import { StageLinqInstance } from '../StageLinq';
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',
});
/**
* Get track information for latest playing song.
*
* @param stageLinq Instance of StageLinq.
* @param status Player to get track info from.
* @returns Track info
*/
function getTrackInfo(stageLinq: StageLinqInstance, status: PlayerStatus) {
try {
const dbPath = stageLinq.databases.getDbPath(status.dbSourceName)
const connection = new DbConnection(dbPath);
const result = connection.getTrackInfo(status.trackPath);
connection.close();
console.log('Database entry:', result);
return result;
} catch(e) {
console.error(e);
}
}
/**
* Download the currently playing song from the media.
*
* @param stageLinq Instance of StageLinq.
* @param status Player to download the current song from.
* @param dest Path to save file to.
*/
async function downloadFile(stageLinq: StageLinqInstance, status: PlayerStatus, dest: string) {
try {
const data = await stageLinq.devices.downloadFile(status.deviceId, status.trackPathAbsolute);
if (data) {
fs.writeFileSync(dest, Buffer.from(data));
console.log(`Downloaded ${status.trackPathAbsolute} to ${dest}`);
}
} catch(e) {
console.error(`Could not download ${status.trackPathAbsolute}`);
}
}
async function main() {
console.log('Starting CLI');
const stageLinqOptions = {
// If set to true, download the source DBs in a temporary location.
// (default: true)
downloadDbSources: false,
// Max number of attempts to connect to a StageLinq device.
// (default: 3)
maxRetries: 3,
// What device to emulate on the network.
// (default: Now Playing)
actingAs: ActingAsDevice.NowPlaying
}
const stageLinq = new StageLinqInstance(stageLinqOptions);
// Setup how you want to handle logs coming from StageLinq
stageLinq.logger.on('error', (...args: any) => {
console.error(...args);
});
stageLinq.logger.on('warn', (...args: any) => {
console.warn(...args);
});
stageLinq.logger.on('info', (...args: any) => {
console.info(...args);
});
stageLinq.logger.on('log', (...args: any) => {
console.log(...args);
});
stageLinq.logger.on('debug', (...args: any) => {
console.debug(...args);
});
// Note: Silly is very verbose!
// stageLinq.logger.on('silly', (...args: any) => {
// console.debug(...args);
// });
// Fires when we connect to any device
stageLinq.devices.on('connected', async (connectionInfo) => {
console.log(`Successfully connected to ${connectionInfo.software.name}`);
if (stageLinq.options.downloadDbSources) {
// Fires when the database source starts downloading.
stageLinq.databases.on('dbDownloading', (sourceName, dbPath) => {
console.log(`Downloading ${sourceName} to ${dbPath}`);
});
// Fires while the database source is being read
stageLinq.databases.on('dbProgress', (sourceName, total, bytes, percent) => {
console.debug(`Reading ${sourceName}: ${bytes}/${total} (${Math.ceil(percent)}%)`);
});
// Fires when the database source has been read and saved to a temporary path.
stageLinq.databases.on('dbDownloaded', (sourceName, dbPath) => {
console.log(`Database (${sourceName}) has been downloaded to ${dbPath}`);
});
}
});
// Fires when StageLinq and all devices are ready to use.
stageLinq.devices.on('ready', () => {
console.log(`StageLinq is ready!`);
});
// Fires when a new track is loaded on to a player.
stageLinq.devices.on('trackLoaded', async (status) => {
// Example of how to connect to the database using this library's
// implementation of BetterSqlite3 to get additional information.
if (stageLinq.options.downloadDbSources) {
getTrackInfo(stageLinq, status);
}
// Example of how to download the actual track from the media.
await downloadFile(stageLinq, status, path.resolve(os.tmpdir(), 'media'));
});
// Fires when a track has started playing.
stageLinq.devices.on('nowPlaying', (status) => {
console.log(`Now Playing on [${status.deck}]: ${status.title} - ${status.artist}`)
});
// Fires when StageLinq receives messages from a device.
stageLinq.devices.on('message', (connectionInfo, data) => {
const msg = data.message.json
? JSON.stringify(data.message.json)
: data.message.interval;
console.debug(`${connectionInfo.address}:${connectionInfo.port} ` +
`${data.message.name} => ${msg}`);
});
// Fires when the state of a device has changed.
stageLinq.devices.on('stateChanged', (status) => {
console.log(`Updating state [${status.deck}]`, status)
});
/////////////////////////////////////////////////////////////////////////
// CLI
let returnCode = 0;
try {
process.on('SIGINT', async function () {
console.info('... exiting');
// Ensure SIGINT won't be impeded by some error
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();