moved 'constants' to a global variable
This commit is contained in:
parent
9c51e9ac5c
commit
b731d5b88e
7 changed files with 37 additions and 45 deletions
|
@ -6,8 +6,6 @@ const stat = require('fs/promises').stat;
|
||||||
|
|
||||||
const Message = require('./Message.js');
|
const Message = require('./Message.js');
|
||||||
|
|
||||||
const { CLIENT_STATE_REGISTERED, CLIENT_STATE_READY, CLIENT_STATE_PLAYING, CLIENT_STATE_PAUSED, CLIENT_STATE_STOPPED, CLIENT_STATE_ERROR } = require('../libs/constants.js');
|
|
||||||
|
|
||||||
class AudioServer {
|
class AudioServer {
|
||||||
|
|
||||||
constructor(file) {
|
constructor(file) {
|
||||||
|
@ -78,7 +76,7 @@ class AudioServer {
|
||||||
}
|
}
|
||||||
client.audiosocket = socket;
|
client.audiosocket = socket;
|
||||||
this.clients.push(client);
|
this.clients.push(client);
|
||||||
this.#setClientState(client, CLIENT_STATE_REGISTERED);
|
this.#setClientState(client, constants.CLIENT_STATE_REGISTERED);
|
||||||
client.audiosocket.on('connect', () => {
|
client.audiosocket.on('connect', () => {
|
||||||
logger.debug(client.getTag() + ' opened audio socket');
|
logger.debug(client.getTag() + ' opened audio socket');
|
||||||
});
|
});
|
||||||
|
@ -143,24 +141,24 @@ class AudioServer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
switch (client.state) {
|
switch (client.state) {
|
||||||
case CLIENT_STATE_REGISTERED:
|
case constants.CLIENT_STATE_REGISTERED:
|
||||||
return this.#handleStateRegistered(client);
|
return this.#handleStateRegistered(client);
|
||||||
case CLIENT_STATE_READY:
|
case constants.CLIENT_STATE_READY:
|
||||||
return this.#handleStateReady(client);
|
return this.#handleStateReady(client);
|
||||||
case CLIENT_STATE_PLAYING:
|
case constants.CLIENT_STATE_PLAYING:
|
||||||
return this.#handleStatePlaying(client);
|
return this.#handleStatePlaying(client);
|
||||||
case CLIENT_STATE_PAUSED:
|
case constants.CLIENT_STATE_PAUSED:
|
||||||
return this.#handleStatePaused(client, data);
|
return this.#handleStatePaused(client, data);
|
||||||
case CLIENT_STATE_STOPPED:
|
case constants.CLIENT_STATE_STOPPED:
|
||||||
return this.#handleStateStopped(client, data);
|
return this.#handleStateStopped(client, data);
|
||||||
case CLIENT_STATE_ERROR:
|
case constants.CLIENT_STATE_ERROR:
|
||||||
return this.#handleStateError(client, data);
|
return this.#handleStateError(client, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async #handleStateRegistered(client) {
|
async #handleStateRegistered(client) {
|
||||||
logger.debug(client.getTag() + ' has registered...');
|
logger.debug(client.getTag() + ' has registered...');
|
||||||
if (!this.#allClientsInState(CLIENT_STATE_REGISTERED)) {
|
if (!this.#allClientsInState(constants.CLIENT_STATE_REGISTERED)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#transmitFile();
|
this.#transmitFile();
|
||||||
|
@ -168,7 +166,7 @@ class AudioServer {
|
||||||
|
|
||||||
async #handleStateReady(client) {
|
async #handleStateReady(client) {
|
||||||
logger.debug(client.getTag() + ' is ready for playback...');
|
logger.debug(client.getTag() + ' is ready for playback...');
|
||||||
if (!this.#allClientsInState(CLIENT_STATE_READY)) {
|
if (!this.#allClientsInState(constants.CLIENT_STATE_READY)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#startPlayback();
|
this.#startPlayback();
|
||||||
|
@ -213,27 +211,27 @@ class AudioServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
async #announceAudioServer() {
|
async #announceAudioServer() {
|
||||||
this.broadcasts[CLIENT_STATE_REGISTERED] = await new Message('audio:initialize', {
|
this.broadcasts[constants.CLIENT_STATE_REGISTERED] = await new Message('audio:initialize', {
|
||||||
port: this.server.address().port,
|
port: this.server.address().port,
|
||||||
size: this.buffer.size,
|
size: this.buffer.size,
|
||||||
threshold: this.buffer.threshold
|
threshold: this.buffer.threshold
|
||||||
}).broadcast(true);
|
}).broadcast(true);
|
||||||
logger.debug('sent broadcast for audio server to client(s) \'' + this.broadcasts[CLIENT_STATE_REGISTERED] + '\'...');
|
logger.debug('sent broadcast for audio server to client(s) \'' + this.broadcasts[constants.CLIENT_STATE_REGISTERED] + '\'...');
|
||||||
}
|
}
|
||||||
|
|
||||||
async #startPlayback() {
|
async #startPlayback() {
|
||||||
this.broadcasts[CLIENT_STATE_PLAYING] = await new Message('audio:play', { position: this.position }).broadcast();
|
this.broadcasts[constants.CLIENT_STATE_PLAYING] = await new Message('audio:play', { position: this.position }).broadcast();
|
||||||
logger.debug('sent broadcast to start playback to client(s) \'' + this.broadcasts[CLIENT_STATE_PLAYING] + '\'...');
|
logger.debug('sent broadcast to start playback to client(s) \'' + this.broadcasts[constants.CLIENT_STATE_PLAYING] + '\'...');
|
||||||
}
|
}
|
||||||
|
|
||||||
async #stopPlayback() {
|
async #stopPlayback() {
|
||||||
this.broadcasts[CLIENT_STATE_STOPPED] = await new Message('audio:stop').broadcast();
|
this.broadcasts[constants.CLIENT_STATE_STOPPED] = await new Message('audio:stop').broadcast();
|
||||||
logger.debug('sent broadcast to stop playback to client(s) \'' + this.broadcasts[CLIENT_STATE_STOPPED] + '\'...');
|
logger.debug('sent broadcast to stop playback to client(s) \'' + this.broadcasts[constants.CLIENT_STATE_STOPPED] + '\'...');
|
||||||
}
|
}
|
||||||
|
|
||||||
async #pausePlayback() {
|
async #pausePlayback() {
|
||||||
this.broadcasts[CLIENT_STATE_PAUSED] = await new Message('audio:pause').broadcast();
|
this.broadcasts[constants.CLIENT_STATE_PAUSED] = await new Message('audio:pause').broadcast();
|
||||||
logger.debug('sent broadcast to pause playback to client(s) \'' + this.broadcasts[CLIENT_STATE_PAUSED] + '\'...');
|
logger.debug('sent broadcast to pause playback to client(s) \'' + this.broadcasts[constants.CLIENT_STATE_PAUSED] + '\'...');
|
||||||
}
|
}
|
||||||
|
|
||||||
async #transmitFile() {
|
async #transmitFile() {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
const { EVENT_DELIMITER } = require('../libs/constants.js');
|
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
class EventParser extends EventEmitter {
|
class EventParser extends EventEmitter {
|
||||||
|
@ -13,7 +12,7 @@ class EventParser extends EventEmitter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.buffer += data;
|
this.buffer += data;
|
||||||
const indexOfEnd = this.buffer.indexOf(EVENT_DELIMITER);
|
const indexOfEnd = this.buffer.indexOf(constants.EVENT_DELIMITER);
|
||||||
if (indexOfEnd === -1) {
|
if (indexOfEnd === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
const { EVENT_DELIMITER } = require('../libs/constants.js');
|
|
||||||
|
|
||||||
class Message {
|
class Message {
|
||||||
|
|
||||||
constructor(id, data) {
|
constructor(id, data) {
|
||||||
|
@ -29,7 +27,7 @@ class Message {
|
||||||
const data = this.toString();
|
const data = this.toString();
|
||||||
logger.debug(client.getTag() + ' sending data: ' + data);
|
logger.debug(client.getTag() + ' sending data: ' + data);
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
client.socket.write(this.toString() + EVENT_DELIMITER, resolve);
|
client.socket.write(this.toString() + constants.EVENT_DELIMITER, resolve);
|
||||||
});
|
});
|
||||||
return client.id;
|
return client.id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
const metadata = require('../libs/metadata.js');
|
const metadata = require('../libs/metadata.js');
|
||||||
const ext = require('path').extname;
|
const ext = require('path').extname;
|
||||||
|
|
||||||
const { FS_EVENT_ADD: EVENT_ADD, FS_EVENT_UNLINK: EVENT_UNLINK, FS_EVENT_CHANGE: EVENT_CHANGE } = require('../libs/constants.js');
|
|
||||||
|
|
||||||
class Queue {
|
class Queue {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -17,14 +15,14 @@ class Queue {
|
||||||
}
|
}
|
||||||
const element = { file: file };
|
const element = { file: file };
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case EVENT_ADD:
|
case constants.FS_EVENT_ADD:
|
||||||
element.event = EVENT_ADD;
|
element.event = constants.FS_EVENT_ADD;
|
||||||
break;
|
break;
|
||||||
case EVENT_UNLINK:
|
case constants.FS_EVENT_UNLINK:
|
||||||
element.event = EVENT_UNLINK;
|
element.event = constants.FS_EVENT_UNLINK;
|
||||||
break;
|
break;
|
||||||
case EVENT_CHANGE:
|
case constants.FS_EVENT_CHANGE:
|
||||||
element.event = EVENT_CHANGE;
|
element.event = constants.FS_EVENT_CHANGE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
|
@ -57,13 +55,13 @@ class Queue {
|
||||||
const timestamp = new Date().getTime();
|
const timestamp = new Date().getTime();
|
||||||
logger.debug('handling event \'' + element.event + '\' for queued file \'' + element.file + '\'...');
|
logger.debug('handling event \'' + element.event + '\' for queued file \'' + element.file + '\'...');
|
||||||
switch (element.event) {
|
switch (element.event) {
|
||||||
case EVENT_ADD:
|
case constants.FS_EVENT_ADD:
|
||||||
await this.eventAdd(element.file);
|
await this.eventAdd(element.file);
|
||||||
break;
|
break;
|
||||||
case EVENT_UNLINK:
|
case constants.FS_EVENT_UNLINK:
|
||||||
await this.eventUnlink(element.file);
|
await this.eventUnlink(element.file);
|
||||||
break;
|
break;
|
||||||
case EVENT_CHANGE:
|
case constants.FS_EVENT_CHANGE:
|
||||||
await this.eventChange(element.file);
|
await this.eventChange(element.file);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const chokidar = require('chokidar');
|
const chokidar = require('chokidar');
|
||||||
|
|
||||||
const { FS_EVENT_ADD: EVENT_ADD, FS_EVENT_UNLINK: EVENT_UNLINK, FS_EVENT_CHANGE: EVENT_CHANGE } = require('../libs/constants.js');
|
|
||||||
|
|
||||||
class Watcher {
|
class Watcher {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -27,16 +25,16 @@ class Watcher {
|
||||||
if (watcher === undefined) {
|
if (watcher === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
watcher.on(EVENT_ADD, (file, stats) => {
|
watcher.on(constants.FS_EVENT_ADD, (file, stats) => {
|
||||||
queue.add(EVENT_ADD, file, stats);
|
queue.add(constants.FS_EVENT_ADD, file, stats);
|
||||||
});
|
});
|
||||||
watcher.on(EVENT_UNLINK, (file, stats) => {
|
watcher.on(constants.FS_EVENT_UNLINK, (file, stats) => {
|
||||||
queue.add(EVENT_UNLINK, file, stats);
|
queue.add(constants.FS_EVENT_UNLINK, file, stats);
|
||||||
});
|
});
|
||||||
watcher.on(EVENT_CHANGE, (file, stats) => {
|
watcher.on(constants.FS_EVENT_CHANGE, (file, stats) => {
|
||||||
queue.add(EVENT_CHANGE, file, stats);
|
queue.add(constants.FS_EVENT_CHANGE, file, stats);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Watcher, EVENT_ADD, EVENT_UNLINK;
|
module.exports = Watcher;
|
|
@ -22,7 +22,7 @@
|
||||||
"password": "kannon"
|
"password": "kannon"
|
||||||
},
|
},
|
||||||
"audio": {
|
"audio": {
|
||||||
"threshold": 10,
|
"threshold": 30,
|
||||||
"bufferlimit": 64
|
"bufferlimit": 64
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -24,6 +24,7 @@ async function main() {
|
||||||
exit('could not read config file at \'' + configPath + '\'');
|
exit('could not read config file at \'' + configPath + '\'');
|
||||||
}
|
}
|
||||||
handleExit();
|
handleExit();
|
||||||
|
global.constants = require('./libs/constants.js');
|
||||||
global.logger.info("launching " + packageJSON.name + " " + packageJSON.version + "...");
|
global.logger.info("launching " + packageJSON.name + " " + packageJSON.version + "...");
|
||||||
try {
|
try {
|
||||||
// connect to the database and create/alter tables
|
// connect to the database and create/alter tables
|
||||||
|
|
Loading…
Reference in a new issue