added config parameter 'restart' and added functionality to restart watchers on close
This commit is contained in:
parent
08067310cc
commit
246363b056
4 changed files with 44 additions and 21 deletions
|
@ -6,6 +6,7 @@
|
||||||
"watchers": [
|
"watchers": [
|
||||||
{
|
{
|
||||||
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
|
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
|
||||||
|
"restart": true,
|
||||||
"grep": "EV_KEY",
|
"grep": "EV_KEY",
|
||||||
"keys": [
|
"keys": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,21 +16,23 @@ class Watcher {
|
||||||
this[key] = config[key];
|
this[key] = config[key];
|
||||||
}
|
}
|
||||||
this.keyfilter = new Keyfilter(config.keys);
|
this.keyfilter = new Keyfilter(config.keys);
|
||||||
|
this.restart = config.restart;
|
||||||
this.grep = config.grep;
|
this.grep = config.grep;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
start() {
|
start() {
|
||||||
if (this.process != undefined) {
|
return new Promise((resolve, reject) => {
|
||||||
return;
|
if (this.process != undefined) {
|
||||||
}
|
return;
|
||||||
logger.debug('starting watcher \'' + this.device + '\'...');
|
}
|
||||||
var args = [this.device];
|
logger.debug('starting watcher \'' + this.device + '\'...');
|
||||||
if (this.grep) {
|
var args = [this.device];
|
||||||
args.push('|', 'grep', this.grep);
|
if (this.grep) {
|
||||||
}
|
args.push('|', 'grep', this.grep);
|
||||||
this.process = spawn("evtest", args);
|
}
|
||||||
this.attachListeners();
|
this.process = spawn("evtest", args);
|
||||||
logger.info('watcher \'' + this.device + '\' initialized and capturing configured events');
|
this.attachListeners(resolve, reject);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
stop() {
|
stop() {
|
||||||
if (this.process == undefined) {
|
if (this.process == undefined) {
|
||||||
|
@ -40,14 +42,15 @@ class Watcher {
|
||||||
this.process.kill();
|
this.process.kill();
|
||||||
logger.info('watcher \'' + this.device + '\' stopped');
|
logger.info('watcher \'' + this.device + '\' stopped');
|
||||||
}
|
}
|
||||||
attachListeners() {
|
attachListeners(resolve, reject) {
|
||||||
if (this.process == undefined) {
|
if (this.process == undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
this.addSpawnListener(resolve);
|
||||||
|
this.addErrorListener(reject);
|
||||||
|
this.addCloseListener();
|
||||||
this.addStdOutListener();
|
this.addStdOutListener();
|
||||||
this.addStdErrListener();
|
this.addStdErrListener();
|
||||||
this.addCloseListener();
|
|
||||||
this.addErrorListener();
|
|
||||||
}
|
}
|
||||||
addStdOutListener() {
|
addStdOutListener() {
|
||||||
if (this.process == undefined) {
|
if (this.process == undefined) {
|
||||||
|
@ -82,6 +85,13 @@ class Watcher {
|
||||||
logger.error(data);
|
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() {
|
addCloseListener() {
|
||||||
if (this.process == undefined) {
|
if (this.process == undefined) {
|
||||||
return;
|
return;
|
||||||
|
@ -91,20 +101,21 @@ class Watcher {
|
||||||
if (code == undefined) {
|
if (code == undefined) {
|
||||||
code = 0;
|
code = 0;
|
||||||
}
|
}
|
||||||
|
this.process = undefined;
|
||||||
this.code = code;
|
this.code = code;
|
||||||
logger.info('watcher \'' + this.device + '\' finished with exit code ' + code);
|
logger.info('watcher \'' + this.device + '\' finished with exit code ' + code);
|
||||||
if (this.callback != undefined) {
|
if (this.callback != undefined) {
|
||||||
this.callback(this.device);
|
this.callback(this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
addErrorListener() {
|
addErrorListener(reject) {
|
||||||
if (this.process == undefined) {
|
if (this.process == undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.debug('adding error listener to \'' + this.device + '\'...');
|
logger.debug('adding error listener to \'' + this.device + '\'...');
|
||||||
this.process.on('error', (err) => {
|
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() {
|
check() {
|
||||||
|
|
|
@ -14,7 +14,7 @@ async function initialize() {
|
||||||
var watcher = new Watcher(global.config.watchers[index], watcherCallback);
|
var watcher = new Watcher(global.config.watchers[index], watcherCallback);
|
||||||
try {
|
try {
|
||||||
await watcher.check();
|
await watcher.check();
|
||||||
watchers.push(watcher)
|
watchers.push(watcher);
|
||||||
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
|
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
logger.error(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);
|
watchers.splice(watchers.findIndex((foundWatcher) => foundWatcher.device == watcher), 1);
|
||||||
logger.debug('removed watcher \'' + watcher + '\' from internal map');
|
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 = {
|
module.exports = {
|
||||||
|
|
6
ninwa.js
6
ninwa.js
|
@ -5,6 +5,8 @@ const packageJSON = require('./package.json');
|
||||||
|
|
||||||
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
|
const INTERRUPTS = ['SIGINT', 'SIGTERM'];
|
||||||
|
|
||||||
|
global.appName = packageJSON.name;
|
||||||
|
global.appVersion = packageJSON.version;
|
||||||
global.config = process.argv[2] || __dirname + '/config.json';
|
global.config = process.argv[2] || __dirname + '/config.json';
|
||||||
|
|
||||||
handleInterrupts();
|
handleInterrupts();
|
||||||
|
@ -21,7 +23,7 @@ util.fileExists(config)
|
||||||
})
|
})
|
||||||
.then(logger.initialize)
|
.then(logger.initialize)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
logger.info(packageJSON.name + ' ' + packageJSON.version + ' starting...');
|
logger.info(appName + ' ' + appVersion + ' starting...');
|
||||||
})
|
})
|
||||||
.then(watchers.initialize)
|
.then(watchers.initialize)
|
||||||
.then(watchers.start)
|
.then(watchers.start)
|
||||||
|
@ -32,7 +34,7 @@ util.fileExists(config)
|
||||||
|
|
||||||
function exit(code) {
|
function exit(code) {
|
||||||
code = code || 0;
|
code = code || 0;
|
||||||
logger.info(packageJSON.name + ' ' + packageJSON.version + ' exiting with code \'' + code + '\'...');
|
logger.info(appName + ' ' + appVersion + ' exiting with code \'' + code + '\'...');
|
||||||
process.exit(code);
|
process.exit(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue