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": [
{
"device": "usb-Razer_Razer_Blade_Stealth-if01-event-kbd",
"device": "/dev/input/event10",
"keys": [
{
"key": "f9",
"key": "enter",
"type": "keydown",
"command": "notify-send",
"args": [
"F9 DOWN",
"$(date)"
"{{ key }}",
"{{ type }}"
]
},
{
"key": "f8",
"key": "esc",
"type": "keyup",
"delay": 2000,
"command": "notify-send",
"args": [
"F8 UP",
"$(date)"
"{{ key }}",
"{{ type }}"
]
},
{
"key": "f7",
"key": "space",
"type": "keyhold",
"delay": 1000,
"command": "notify-send",
"args": [
"F7 HOLD",
"$(date)"
"{{ key }}",
"{{ type }}"
]
}
]

View file

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

View file

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

View file

@ -4,7 +4,7 @@ const stat = require('fs/promises').stat;
function fileExists(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('error: no file given');
reject('can not check the existence of an undefined file');
}
resolvePath(file)
.then((path) => {
@ -20,12 +20,12 @@ function fileExists(file) {
function resolvePath(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
reject('error: no file given');
reject('can not resolve a path to an undefined file');
}
realpath(file)
.then(resolve)
.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) {
this[key] = config[key];
}
// if (!this.device.startsWith(inputDevices)) {
// this.device = inputDevices + this.device;
// }
this.keyfilter = new Keyfilter(config.keys);
this.callback = callback;
}
@ -99,17 +96,19 @@ class Watcher {
}
logger.debug('adding error listener to \'' + this.device + '\'...');
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() {
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) => {
if (result.path != this.device) {
logger.info('resolved watcher for device \'' + this.device + '\' to \'' + result.path + '\'')
}
logger.info('resolved ')
this.device = result.path;
resolve();
})

View file

@ -7,7 +7,7 @@ const watchers = [];
function initialize() {
return new Promise(function (resolve, reject) {
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 = []
for (var index = 0; index < configJSON.watchers.length; index++) {
@ -23,12 +23,12 @@ function check(watcher) {
}
watcher.check()
.then(() => {
logger.info('watcher \'' + watcher.device)
logger.info('watcher \'' + watcher.device + '\' initialized and capturing configured events...');
watcher.start();
watchers.push(watcher);
})
.catch((err) => {
logger.error(err);d
logger.error(err);
});
});
}

View file

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