optimized some stuff, sanitized config, set defaults, etc.

This commit is contained in:
Daniel Sommer 2022-02-16 02:08:21 +01:00
parent c6894eff79
commit e68313270f
7 changed files with 46 additions and 30 deletions

View file

@ -5,35 +5,34 @@
}, },
"watchers": [ "watchers": [
{ {
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd", "device": "/dev/input/event10",
"keys": [ "keys": [
{ {
"key": "f9", "key": "enter",
"type": "keydown", "type": "keydown",
"command": "notify-send", "command": "notify-send",
"args": [ "args": [
"F9 DOWN", "{{ key }}",
"$(date)" "{{ type }}"
] ]
}, },
{ {
"key": "f8", "key": "esc",
"type": "keyup", "type": "keyup",
"delay": 2000,
"command": "notify-send", "command": "notify-send",
"args": [ "args": [
"F8 UP", "{{ key }}",
"$(date)" "{{ type }}"
] ]
}, },
{ {
"key": "f7", "key": "space",
"type": "keyhold", "type": "keyhold",
"delay": 1000, "delay": 1000,
"command": "notify-send", "command": "notify-send",
"args": [ "args": [
"F7 HOLD", "{{ key }}",
"$(date)" "{{ type }}"
] ]
} }
] ]

View file

@ -1,5 +1,3 @@
const logger = require('./logger.js');
const FILTER_START = 'Testing ... (interrupt to exit)'; const FILTER_START = 'Testing ... (interrupt to exit)';
const KEY_PREFIX = 'KEY_'; const KEY_PREFIX = 'KEY_';
@ -79,6 +77,9 @@ class Keyfilter {
} }
return new Date().getTime() - action.executed < action.delay; return new Date().getTime() - action.executed < action.delay;
} }
isValid() {
return this.actions != undefined && this.actions.size > 0;
}
} }
module.exports = Keyfilter; module.exports = Keyfilter;

View file

@ -11,8 +11,11 @@ const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2; const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3; const LOGLEVEL_ERROR = 3;
// set loglevel on 'require' const loglevel = getLogLevel();
const loglevel = function () { const timestamp = getTimestamp();
// get the loglevel
function getLogLevel() {
switch (config.log.level) { switch (config.log.level) {
case LOG_PREFIX_DEBUG: case LOG_PREFIX_DEBUG:
case LOGLEVEL_DEBUG: case LOGLEVEL_DEBUG:
@ -29,7 +32,15 @@ const loglevel = function () {
default: default:
return LOGLEVEL_INFO; return LOGLEVEL_INFO;
} }
}(); }
// get the timestamp format
function getTimestamp() {
if (config != undefined && config.log != undefined && config.log.format != undefined) {
return config.log.timestamp;
}
return "DD.MM.YYYY HH:mm:ss:SS";
}
// prefix log with 'info' // prefix log with 'info'
function info(message) { function info(message) {
@ -60,6 +71,12 @@ function error(message) {
if (loglevel > LOGLEVEL_ERROR) { if (loglevel > LOGLEVEL_ERROR) {
return; return;
} }
if (message.errors != undefined) {
for (var index = 0; index < message.errors.length; index++) {
trace(message.errors[index], 'error');
}
return;
}
trace(message, 'error'); trace(message, 'error');
} }
@ -85,7 +102,7 @@ function trace(message, prefix) {
default: default:
print = console.log; print = console.log;
} }
message = moment().format(config.log.timestamp) + ' | ' + prefix + ' > ' + message; message = moment().format(timestamp) + ' | ' + prefix + ' > ' + message;
print(message); print(message);
} }

View file

@ -4,7 +4,7 @@ const stat = require('fs/promises').stat;
function fileExists(file) { function fileExists(file) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (file == undefined) { if (file == undefined) {
reject('error: no file given'); reject('can not check the existence of an undefined file');
} }
resolvePath(file) resolvePath(file)
.then((path) => { .then((path) => {
@ -20,12 +20,12 @@ function fileExists(file) {
function resolvePath(file) { function resolvePath(file) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (file == undefined) { if (file == undefined) {
reject('error: no file given'); reject('can not resolve a path to an undefined file');
} }
realpath(file) realpath(file)
.then(resolve) .then(resolve)
.catch((err) => { .catch((err) => {
reject('error: resolving path \'' + file + '\' encountered an error >>> ' + err); reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
}); });
}); });
} }

View file

@ -15,9 +15,6 @@ class Watcher {
for (var key in config) { for (var key in config) {
this[key] = config[key]; this[key] = config[key];
} }
// if (!this.device.startsWith(inputDevices)) {
// this.device = inputDevices + this.device;
// }
this.keyfilter = new Keyfilter(config.keys); this.keyfilter = new Keyfilter(config.keys);
this.callback = callback; this.callback = callback;
} }
@ -99,17 +96,19 @@ class Watcher {
} }
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('error: watcher \'' + this.device + '\' encountered an error >>> ' + err); logger.error('watcher \'' + this.device + '\' encountered an error >>> ' + err);
}); });
} }
check() { check() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Promise.any([inputDevices + this.device, inputDevicesById + this.device].map(util.fileExists)) if (!this.keyfilter.isValid()) {
reject('no key(s) defined for watcher \'' + this.device + '\'');
}
Promise.any([this.device, inputDevices + this.device, inputDevicesById + this.device].map(util.fileExists))
.then((result) => { .then((result) => {
if (result.path != this.device) { if (result.path != this.device) {
logger.info('resolved watcher for device \'' + this.device + '\' to \'' + result.path + '\'') logger.info('resolved watcher for device \'' + this.device + '\' to \'' + result.path + '\'')
} }
logger.info('resolved ')
this.device = result.path; this.device = result.path;
resolve(); resolve();
}) })

View file

@ -7,7 +7,7 @@ const watchers = [];
function initialize() { function initialize() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (configJSON == undefined || configJSON.watchers == undefined || configJSON.watchers.length == 0) { if (configJSON == undefined || configJSON.watchers == undefined || configJSON.watchers.length == 0) {
reject('error: no input devices defined'); reject('no watchers in \'config.json\' defined');
} }
var tmp = [] var tmp = []
for (var index = 0; index < configJSON.watchers.length; index++) { for (var index = 0; index < configJSON.watchers.length; index++) {
@ -23,12 +23,12 @@ function check(watcher) {
} }
watcher.check() watcher.check()
.then(() => { .then(() => {
logger.info('watcher \'' + watcher.device) logger.info('watcher \'' + watcher.device + '\' initialized and capturing configured events...');
watcher.start(); watcher.start();
watchers.push(watcher); watchers.push(watcher);
}) })
.catch((err) => { .catch((err) => {
logger.error(err);d logger.error(err);
}); });
}); });
} }

View file

@ -4,7 +4,7 @@
"description": "node input watcher", "description": "node input watcher",
"main": "ninwa.js", "main": "ninwa.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
"type": "git", "type": "git",