diff --git a/config.json b/config.json index 853a458..dd0bc65 100644 --- a/config.json +++ b/config.json @@ -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 }}" ] } ] diff --git a/libs/keyfilter.js b/libs/keyfilter.js index 24c1f11..18b6056 100644 --- a/libs/keyfilter.js +++ b/libs/keyfilter.js @@ -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; \ No newline at end of file diff --git a/libs/logger.js b/libs/logger.js index f1980ad..d875147 100644 --- a/libs/logger.js +++ b/libs/logger.js @@ -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); } diff --git a/libs/util.js b/libs/util.js index 7489e2c..3977a06 100644 --- a/libs/util.js +++ b/libs/util.js @@ -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); }); }); } diff --git a/libs/watcher.js b/libs/watcher.js index a7608e0..62bba09 100644 --- a/libs/watcher.js +++ b/libs/watcher.js @@ -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(); }) diff --git a/libs/watchers.js b/libs/watchers.js index 4e250c1..227d539 100644 --- a/libs/watchers.js +++ b/libs/watchers.js @@ -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); }); }); } diff --git a/package.json b/package.json index b8bbbd9..3938fb8 100644 --- a/package.json +++ b/package.json @@ -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",