added new config option for prefiltering with 'grep' and started replacing promises with await/async
This commit is contained in:
parent
2b84ef5e97
commit
75c08d5b53
4 changed files with 63 additions and 52 deletions
|
@ -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",
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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');
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
reject('could not initialize watchers, no config defined');
|
throw new Error('could not initialize watchers, no config defined');
|
||||||
}
|
}
|
||||||
if (global.config.watchers == undefined || global.config.watchers.length == 0) {
|
if (global.config.watchers == undefined || global.config.watchers.length == 0) {
|
||||||
reject('no watchers in config \'' + global.config.path + '\' defined');
|
throw new Error('no watchers in config \'' + global.config.path + '\' defined');
|
||||||
}
|
}
|
||||||
var tmp = []
|
|
||||||
for (var index = 0; index < global.config.watchers.length; index++) {
|
for (var index = 0; index < global.config.watchers.length; index++) {
|
||||||
tmp.push(new Watcher(global.config.watchers[index], watcherCallback));
|
var watcher = new Watcher(global.config.watchers[index], watcherCallback);
|
||||||
}
|
try {
|
||||||
Promise.all(tmp.map(check))
|
await watcher.check();
|
||||||
.then(resolve)
|
|
||||||
.catch(reject);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
function check(watcher) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
if (watcher == undefined) {
|
|
||||||
reject();
|
|
||||||
}
|
|
||||||
watcher.check()
|
|
||||||
.then(() => {
|
|
||||||
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) {
|
||||||
.then(resolve)
|
logger.error(err);
|
||||||
.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)...');
|
||||||
var promises = [];
|
|
||||||
for (var index = 0; index < watchers.length; index++) {
|
for (var index = 0; index < watchers.length; index++) {
|
||||||
promises.push(watchers[index].start());
|
try {
|
||||||
|
await 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)...');
|
||||||
var promises = [];
|
|
||||||
for (var index = 0; index < watchers.length; index++) {
|
for (var index = 0; index < watchers.length; index++) {
|
||||||
promises.push(watchers[index].stop());
|
try {
|
||||||
|
await 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) {
|
||||||
|
|
Loading…
Reference in a new issue