ninwa/libs/watchers.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-02-15 04:33:19 +01:00
const logger = require('./logger.js');
const Watcher = require('./watcher.js');
const watchers = [];
async function initialize() {
if (global.config == undefined) {
throw new Error('could not initialize watchers, no config defined');
}
if (global.config.watchers == undefined || global.config.watchers.length == 0) {
throw new Error('no watchers in config \'' + global.config.path + '\' defined');
}
for (var index = 0; index < global.config.watchers.length; index++) {
var watcher = new Watcher(global.config.watchers[index], watcherCallback);
try {
await watcher.check();
watchers.push(watcher)
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
} catch(err) {
logger.error(err);
}
}
2022-02-15 04:33:19 +01:00
}
async function start() {
logger.info('starting ' + watchers.length + ' watcher(s)...');
for (var index = 0; index < watchers.length; index++) {
try {
await watchers[index].start();
} catch (err) {
logger.error(err);
}
}
2022-02-15 04:33:19 +01:00
}
async function stop() {
logger.info('stopping ' + watchers.length + ' watcher(s)...');
for (var index = 0; index < watchers.length; index++) {
try {
await watchers[index].stop();
} catch (err) {
logger.error(err);
}
}
2022-02-15 04:33:19 +01:00
}
function watcherCallback(watcher) {
watchers.splice(watchers.findIndex((foundWatcher) => foundWatcher.device == watcher), 1);
logger.debug('removed watcher \'' + watcher + '\' from internal map');
2022-02-15 04:33:19 +01:00
}
module.exports = {
initialize,
start,
stop
}