51 lines
No EOL
1.4 KiB
JavaScript
51 lines
No EOL
1.4 KiB
JavaScript
const logger = require('./logger.js');
|
|
const Watcher = require('./watcher.js');
|
|
|
|
const watchers = [];
|
|
|
|
async function initialize(config) {
|
|
if (config === undefined) {
|
|
throw new Error('could not initialize watchers, no config defined');
|
|
}
|
|
if (config.length === 0) {
|
|
throw new Error('no watchers defined');
|
|
}
|
|
for (var index = 0; index < config.length; index++) {
|
|
var watcher = new Watcher(config[index], watcherCallback);
|
|
await watcher.check();
|
|
watchers.push(watcher);
|
|
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
|
|
}
|
|
}
|
|
|
|
async function start() {
|
|
logger.info('starting ' + watchers.length + ' watcher(s)...');
|
|
for (var index = 0; index < watchers.length; index++) {
|
|
await watchers[index].start();
|
|
}
|
|
}
|
|
|
|
async function stop() {
|
|
logger.info('stopping ' + watchers.length + ' watcher(s)...');
|
|
for (var index = 0; index < watchers.length; index++) {
|
|
await watchers[index].stop();
|
|
}
|
|
}
|
|
|
|
async function watcherCallback(watcher) {
|
|
if (watcher.restart) {
|
|
await watcher.start();
|
|
return;
|
|
}
|
|
watchers.splice(watchers.findIndex((foundWatcher) => foundWatcher.device === watcher), 1);
|
|
logger.debug('removed watcher \'' + watcher.device + '\' from internal map');
|
|
if (watchers.length === 0) {
|
|
logger.warn('no watchers are active any longer');
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
initialize,
|
|
start,
|
|
stop
|
|
} |