2022-02-15 04:33:19 +01:00
|
|
|
const logger = require('./logger.js');
|
|
|
|
const Watcher = require('./watcher.js');
|
|
|
|
|
|
|
|
const watchers = [];
|
|
|
|
|
2022-02-18 01:33:57 +01:00
|
|
|
function initialize(config) {
|
2022-02-15 04:33:19 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
2022-02-18 01:33:57 +01:00
|
|
|
if (config == undefined || config.path == undefined) {
|
|
|
|
reject('no config defined');
|
|
|
|
}
|
|
|
|
config = require(config.path);
|
|
|
|
if (config.watchers == undefined || config.watchers.length == 0) {
|
|
|
|
reject('no watchers in config defined');
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
var tmp = []
|
2022-02-18 01:33:57 +01:00
|
|
|
for (var index = 0; index < config.watchers.length; index++) {
|
|
|
|
tmp.push(new Watcher(config.watchers[index], watcherCallback));
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-02-16 03:28:17 +01:00
|
|
|
Promise.all(tmp.map(check))
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2022-02-15 04:33:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function check(watcher) {
|
2022-02-16 03:28:17 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-02-15 04:33:19 +01:00
|
|
|
if (watcher == undefined) {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
watcher.check()
|
|
|
|
.then(() => {
|
2022-02-16 03:28:17 +01:00
|
|
|
watchers.push(watcher)
|
|
|
|
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
|
2022-02-15 04:33:19 +01:00
|
|
|
})
|
2022-02-16 03:28:17 +01:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
2022-02-15 04:33:19 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function start() {
|
2022-02-16 03:28:17 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
logger.info('starting ' + watchers.length + ' watcher(s)...');
|
|
|
|
var promises = [];
|
|
|
|
for (var index = 0; index < watchers.length; index++) {
|
|
|
|
promises.push(watchers[index].start());
|
|
|
|
}
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function stop() {
|
2022-02-16 03:28:17 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
logger.info('stopping ' + watchers.length + ' watcher(s)...');
|
|
|
|
var promises = [];
|
|
|
|
for (var index = 0; index < watchers.length; index++) {
|
|
|
|
promises.push(watchers[index].stop());
|
|
|
|
}
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 03:28:17 +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
|
|
|
|
}
|