added new config option for prefiltering with 'grep' and started replacing promises with await/async

This commit is contained in:
Daniel Sommer 2022-02-23 23:33:40 +01:00
parent 2b84ef5e97
commit 75c08d5b53
4 changed files with 63 additions and 52 deletions

View file

@ -1,11 +1,12 @@
{ {
"log": { "log": {
"level": "info", "level": "debug",
"timestamp": "DD.MM.YYYY HH:mm:ss:SS" "timestamp": "DD.MM.YYYY HH:mm:ss:SS"
}, },
"watchers": [ "watchers": [
{ {
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd", "device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
"grep": "EV_KEY",
"keys": [ "keys": [
{ {
"key": "key_enter", "key": "key_enter",

View file

@ -14,6 +14,7 @@ class Keyfilter {
} }
this.actions = new Map(); this.actions = new Map();
for (var index = 0; index < config.length; index++) { for (var index = 0; index < config.length; index++) {
var grep = config[index].grep;
var type = ACTION_KEYDOWN; var type = ACTION_KEYDOWN;
switch (config[index].type.toLowerCase()) { switch (config[index].type.toLowerCase()) {
case ACTION_KEYUP.action: case ACTION_KEYUP.action:
@ -26,7 +27,7 @@ class Keyfilter {
var command = config[index].command; var command = config[index].command;
var args = config[index].args; var args = config[index].args;
var delay = config[index].delay; var delay = config[index].delay;
this.actions.set(config[index].key.toUpperCase(), { type, command, args, delay }); this.actions.set(config[index].key.toUpperCase(), { grep, type, command, args, delay });
} }
} }
filterActive(input) { filterActive(input) {

View file

@ -16,6 +16,7 @@ class Watcher {
this[key] = config[key]; this[key] = config[key];
} }
this.keyfilter = new Keyfilter(config.keys); this.keyfilter = new Keyfilter(config.keys);
this.grep = config.grep;
this.callback = callback; this.callback = callback;
} }
start() { start() {
@ -23,7 +24,11 @@ class Watcher {
return; return;
} }
logger.debug('starting watcher \'' + this.device + '\'...'); logger.debug('starting watcher \'' + this.device + '\'...');
this.process = spawn("evtest", [this.device, "|", "grep", "EV_KEY"]); var args = [this.device];
if (this.grep) {
args.push('|', 'grep', this.grep);
}
this.process = spawn("evtest", args);
this.attachListeners(); this.attachListeners();
logger.info('watcher \'' + this.device + '\' initialized and capturing configured events'); logger.info('watcher \'' + this.device + '\' initialized and capturing configured events');
} }

View file

@ -3,62 +3,66 @@ const Watcher = require('./watcher.js');
const watchers = []; const watchers = [];
function initialize() { async function initialize() {
return new Promise(function (resolve, reject) { if (global.config == undefined) {
if (global.config == undefined) { throw new Error('could not initialize watchers, no config defined');
reject('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);
} }
if (global.config.watchers == undefined || global.config.watchers.length == 0) { }
reject('no watchers in config \'' + global.config.path + '\' defined');
}
var tmp = []
for (var index = 0; index < global.config.watchers.length; index++) {
tmp.push(new Watcher(global.config.watchers[index], watcherCallback));
}
Promise.all(tmp.map(check))
.then(resolve)
.catch(reject);
});
}
function check(watcher) {
return new Promise((resolve, reject) => {
if (watcher == undefined) {
reject();
}
watcher.check()
.then(() => {
watchers.push(watcher)
logger.debug('added watcher \'' + watcher.device + '\' to internal map');
})
.then(resolve)
.catch(reject);
});
} }
function start() { async function start() {
return new Promise((resolve, reject) => { logger.info('starting ' + watchers.length + ' watcher(s)...');
logger.info('starting ' + watchers.length + ' watcher(s)...'); for (var index = 0; index < watchers.length; index++) {
var promises = []; try {
for (var index = 0; index < watchers.length; index++) { await watchers[index].start();
promises.push(watchers[index].start()); } catch (err) {
logger.error(err);
} }
Promise.all(promises) }
.then(resolve)
.catch(reject); // 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);
// });
} }
function stop() { async function stop() {
return new Promise((resolve, reject) => { logger.info('stopping ' + watchers.length + ' watcher(s)...');
logger.info('stopping ' + watchers.length + ' watcher(s)...'); for (var index = 0; index < watchers.length; index++) {
var promises = []; try {
for (var index = 0; index < watchers.length; index++) { await watchers[index].stop();
promises.push(watchers[index].stop()); } catch (err) {
logger.error(err);
} }
Promise.all(promises) }
.then(resolve)
.catch(reject); // return new Promise((resolve, reject) => {
}); // var promises = [];
// for (var index = 0; index < watchers.length; index++) {
// promises.push(watchers[index].stop());
// }
// Promise.all(promises)
// .then(resolve)
// .catch(reject);
// });
} }
function watcherCallback(watcher) { function watcherCallback(watcher) {