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": {
|
||||
"level": "info",
|
||||
"level": "debug",
|
||||
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"
|
||||
},
|
||||
"watchers": [
|
||||
{
|
||||
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
|
||||
"grep": "EV_KEY",
|
||||
"keys": [
|
||||
{
|
||||
"key": "key_enter",
|
||||
|
|
|
@ -14,6 +14,7 @@ class Keyfilter {
|
|||
}
|
||||
this.actions = new Map();
|
||||
for (var index = 0; index < config.length; index++) {
|
||||
var grep = config[index].grep;
|
||||
var type = ACTION_KEYDOWN;
|
||||
switch (config[index].type.toLowerCase()) {
|
||||
case ACTION_KEYUP.action:
|
||||
|
@ -26,7 +27,7 @@ class Keyfilter {
|
|||
var command = config[index].command;
|
||||
var args = config[index].args;
|
||||
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) {
|
||||
|
|
|
@ -16,6 +16,7 @@ class Watcher {
|
|||
this[key] = config[key];
|
||||
}
|
||||
this.keyfilter = new Keyfilter(config.keys);
|
||||
this.grep = config.grep;
|
||||
this.callback = callback;
|
||||
}
|
||||
start() {
|
||||
|
@ -23,7 +24,11 @@ class Watcher {
|
|||
return;
|
||||
}
|
||||
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();
|
||||
logger.info('watcher \'' + this.device + '\' initialized and capturing configured events');
|
||||
}
|
||||
|
|
102
libs/watchers.js
102
libs/watchers.js
|
@ -3,62 +3,66 @@ const Watcher = require('./watcher.js');
|
|||
|
||||
const watchers = [];
|
||||
|
||||
function initialize() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (global.config == undefined) {
|
||||
reject('could not initialize watchers, no config defined');
|
||||
async function initialize() {
|
||||
if (global.config == undefined) {
|
||||
throw new Error('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() {
|
||||
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());
|
||||
async function start() {
|
||||
logger.info('starting ' + watchers.length + ' watcher(s)...');
|
||||
for (var index = 0; index < watchers.length; index++) {
|
||||
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() {
|
||||
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());
|
||||
async function stop() {
|
||||
logger.info('stopping ' + watchers.length + ' watcher(s)...');
|
||||
for (var index = 0; index < watchers.length; index++) {
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue