added config parameter 'restart' and added functionality to restart watchers on close

This commit is contained in:
Daniel Sommer 2022-02-24 00:07:57 +01:00
parent 08067310cc
commit 246363b056
4 changed files with 44 additions and 21 deletions

View file

@ -6,6 +6,7 @@
"watchers": [
{
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
"restart": true,
"grep": "EV_KEY",
"keys": [
{

View file

@ -16,21 +16,23 @@ class Watcher {
this[key] = config[key];
}
this.keyfilter = new Keyfilter(config.keys);
this.restart = config.restart;
this.grep = config.grep;
this.callback = callback;
}
start() {
if (this.process != undefined) {
return;
}
logger.debug('starting watcher \'' + this.device + '\'...');
var args = [this.device];
if (this.grep) {
args.push('|', 'grep', this.grep);
}
this.process = spawn("evtest", args);
this.attachListeners();
logger.info('watcher \'' + this.device + '\' initialized and capturing configured events');
return new Promise((resolve, reject) => {
if (this.process != undefined) {
return;
}
logger.debug('starting watcher \'' + this.device + '\'...');
var args = [this.device];
if (this.grep) {
args.push('|', 'grep', this.grep);
}
this.process = spawn("evtest", args);
this.attachListeners(resolve, reject);
});
}
stop() {
if (this.process == undefined) {
@ -40,14 +42,15 @@ class Watcher {
this.process.kill();
logger.info('watcher \'' + this.device + '\' stopped');
}
attachListeners() {
attachListeners(resolve, reject) {
if (this.process == undefined) {
return;
}
this.addSpawnListener(resolve);
this.addErrorListener(reject);
this.addCloseListener();
this.addStdOutListener();
this.addStdErrListener();
this.addCloseListener();
this.addErrorListener();
}
addStdOutListener() {
if (this.process == undefined) {
@ -82,6 +85,13 @@ class Watcher {
logger.error(data);
});
}
addSpawnListener(resolve) {
logger.debug('adding spawn listener to watcher \'' + this.device + '\'...');
this.process.on('spawn', () => {
logger.info('watcher \'' + this.device + '\' initialized and capturing configured events');
resolve();
});
}
addCloseListener() {
if (this.process == undefined) {
return;
@ -91,20 +101,21 @@ class Watcher {
if (code == undefined) {
code = 0;
}
this.process = undefined;
this.code = code;
logger.info('watcher \'' + this.device + '\' finished with exit code ' + code);
if (this.callback != undefined) {
this.callback(this.device);
this.callback(this);
}
});
}
addErrorListener() {
addErrorListener(reject) {
if (this.process == undefined) {
return;
}
logger.debug('adding error listener to \'' + this.device + '\'...');
this.process.on('error', (err) => {
logger.error('watcher \'' + this.device + '\' encountered an error >>> ' + err);
reject(logger.error('watcher \'' + this.device + '\' encountered an error >>> ' + err));
});
}
check() {

View file

@ -14,7 +14,7 @@ async function initialize() {
var watcher = new Watcher(global.config.watchers[index], watcherCallback);
try {
await watcher.check();
watchers.push(watcher)
watchers.push(watcher);
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
} catch(err) {
logger.error(err);
@ -44,9 +44,18 @@ async function stop() {
}
}
function watcherCallback(watcher) {
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 + '\' from internal map');
if (watchers.length === 0) {
logger.info('no watchers are active any longer');
logger.info(global.appName + ' ' + global.appVersion + ' exiting with code \'0\'');
process.exit(0);
}
}
module.exports = {

View file

@ -5,6 +5,8 @@ const packageJSON = require('./package.json');
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
global.appName = packageJSON.name;
global.appVersion = packageJSON.version;
global.config = process.argv[2] || __dirname + '/config.json';
handleInterrupts();
@ -21,7 +23,7 @@ util.fileExists(config)
})
.then(logger.initialize)
.then(() => {
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
logger.info(appName + ' ' + appVersion + ' starting...');
})
.then(watchers.initialize)
.then(watchers.start)
@ -32,7 +34,7 @@ util.fileExists(config)
function exit(code) {
code = code || 0;
logger.info(packageJSON.name + ' ' + packageJSON.version + ' exiting with code \'' + code + '\'...');
logger.info(appName + ' ' + appVersion + ' exiting with code \'' + code + '\'...');
process.exit(code);
}