code optimization

This commit is contained in:
Daniel Sommer 2022-02-21 11:56:49 +01:00
parent adc9f59adc
commit 7866ddb5df
7 changed files with 54 additions and 31 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.vscode/
.idea/
node_modules/
package-lock.json
yarn.lock

View file

@ -4,9 +4,31 @@ control your blinkstick via http requests
## requirements
- nodejs 8.x
- nodejs
- yarn / npm
- node-gyp (probably; to build the node-hid module)
- [nvm](https://github.com/nvm-sh/nvm)
**the node-hid module is a bit picky about node.js's version - use node.js 8.x or 15.x**
## setup
- clone the project
`git clone https://git.velvettear.de/velvettear/blinky.git`
- enter the cloned directory
`cd blinky`
- install and switch to a supported node.js version (automatically done via .nvmrc file)
`nvm install`
- install the required modules
`npm install`
- switch back to your system's default node.js version
`nvm deactivate`
- run it
`nvm run 15 blinky.js`
### scripts

View file

@ -30,9 +30,9 @@ function main() {
// ... and it all comes crashing down
function exit(err, code) {
if (code == undefined) {
if (code === undefined) {
code = 0;
if (err != undefined) {
if (err !== undefined) {
code = 1;
}
}
@ -46,7 +46,7 @@ function exit(err, code) {
}
function handleInterrupts() {
for (var index = 0; index < INTERRUPTS.length; index++) {
for (let index = 0; index < INTERRUPTS.length; index++) {
process.once(INTERRUPTS[index], (code) => {
exit(null, code);
});

View file

@ -7,11 +7,11 @@ const blinkstick = require('blinkstick');
const hexcolor = require('hex-color-regex');
// constants
const RANDOM = 'random'
const RANDOM = 'random';
const ANIMATION_STATE_INPROGRESS = 1;
const ANIMATION_STATE_FINISH = 0;
// variables
// letiables
let led;
let animation = {};
@ -56,14 +56,14 @@ function parseColor(value) {
// parse rgb color values
function parseRGBColor(value) {
if (value.indexOf(',') == -1 && isRGBValue(value)) {
if (value.indexOf(',') === -1 && isRGBValue(value)) {
return convertRGBToHex(parseInt(value) || 0, 0, 0);
} else {
const splittedValues = value.split(',');
let color = {};
for (let index = 0; index < splittedValues.length; index++) {
const value = splittedValues[index];
if (index == 0) {
if (index === 0) {
color.red = parseInt(value) || 0;
} else if (index === 1) {
color.green = parseInt(value) || 0;
@ -79,7 +79,7 @@ function parseRGBColor(value) {
// check a single rgb value
function isRGBValue(value) {
return value != undefined && !isNaN(value) && value >= 0 && value <= 255;
return value !== undefined && !isNaN(value) && value >= 0 && value <= 255;
}
// convert rgb to hex color values
@ -89,7 +89,7 @@ function convertRGBToHex(red, green, blue) {
// parse hex color values
function parseHexColor(value) {
if (value[0] != '#') {
if (value[0] !== '#') {
value = '#' + value;
}
if (value.length === 4) {
@ -155,11 +155,11 @@ function morph(blinkstickConfig, resolve, reject) {
// start pulsing
function startPulsing(blinkstickConfig, resolve, reject) {
if (animation.state == ANIMATION_STATE_FINISH || (blinkstickConfig.options.pulse.max > 0 && (blinkstickConfig.options.pulse.done && blinkstickConfig.options.pulse.done == blinkstickConfig.options.pulse.max))) {
if (animation.state === ANIMATION_STATE_FINISH || (blinkstickConfig.options.pulse.max > 0 && (blinkstickConfig.options.pulse.done && blinkstickConfig.options.pulse.done === blinkstickConfig.options.pulse.max))) {
clearAnimationProperties();
return resolve('finished pulsing color \'' + blinkstickConfig.color + '\'');
}
if (animation.id && animation.id != blinkstickConfig.id) {
if (animation.id && animation.id !== blinkstickConfig.id) {
stopAnimation()
.then(logger.info)
.then(function () {
@ -182,7 +182,7 @@ function startPulsing(blinkstickConfig, resolve, reject) {
// set properties for the current animation
function setAnimationProperties(blinkstickConfig) {
if (animation.id == blinkstickConfig.id) {
if (animation.id === blinkstickConfig.id) {
return;
}
led.animationsEnabled = true;
@ -219,7 +219,7 @@ function waitForAnimation(timestamp, resolve, reject) {
// is currently an animation in progress
function isAnimationInProgress() {
return animation != undefined && animation.state != undefined;
return animation !== undefined && animation.state !== undefined;
}
// exports

View file

@ -10,12 +10,12 @@ const LOGLEVEL_INFO = 1;
const LOGLEVEL_WARNING = 2;
const LOGLEVEL_ERROR = 3;
var loglevel = getLogLevel();
var timestamp = getTimestamp();
let loglevel = getLogLevel();
let timestamp = getTimestamp();
function initialize() {
return new Promise((resolve, reject) => {
if (global.config == undefined) {
if (global.config === undefined) {
reject('could not initialize logger, config is undefined');
}
loglevel = getLogLevel();
@ -26,7 +26,7 @@ function initialize() {
// get the loglevel
function getLogLevel() {
if (global.config == undefined || global.config.log == undefined || global.config.log.level == undefined) {
if (global.config === undefined || global.config.log === undefined || global.config.log.level === undefined) {
return LOGLEVEL_INFO;
}
switch (global.config.log.level) {
@ -49,7 +49,7 @@ function getLogLevel() {
// get the timestamp format
function getTimestamp() {
if (global.config != undefined && global.config.log != undefined && global.config.log.format != undefined) {
if (global.config !== undefined && global.config.log !== undefined && global.config.log.format !== undefined) {
return global.config.log.timestamp;
}
return "DD.MM.YYYY HH:mm:ss:SS";
@ -57,7 +57,7 @@ function getTimestamp() {
// log a http request
function http(request) {
if (request == undefined) {
if (request === undefined) {
return;
}
let message = '[' + request.method + '] url: \'' + request.url + '\'';
@ -98,8 +98,8 @@ function error(message) {
if (loglevel > LOGLEVEL_ERROR) {
return;
}
if (message.errors != undefined) {
for (var index = 0; index < message.errors.length; index++) {
if (message.errors !== undefined) {
for (let index = 0; index < message.errors.length; index++) {
trace(message.errors[index], 'error');
}
return;
@ -141,4 +141,4 @@ module.exports = {
debug,
error,
http
}
};

View file

@ -6,7 +6,7 @@ const logger = require('./logger');
const blinkstick = require('./blinkstick');
// third party requirements
const express = require('express');
const favicon = require('serve-favicon')
const favicon = require('serve-favicon');
const parser = require('body-parser');
// setup express, blinkstick and other stuff
@ -58,7 +58,7 @@ function getHTML() {
"</table>" +
"</div>" +
"</body>" +
"</html>"
"</html>";
return welcomeMessage;
}
@ -112,7 +112,7 @@ function parseRequest(data) {
}
}
};
if (data.index != undefined) {
if (data.index !== undefined) {
blinkstickConfig.options.index = parseInt(data.index);
if (blinkstickConfig.options.index < 0) {
blinkstickConfig.options.index = 0;
@ -123,7 +123,7 @@ function parseRequest(data) {
if (blinkstickConfig.options.duration < 100) {
blinkstickConfig.options.duration = 100;
}
if (blinkstickConfig.options.steps == undefined || blinkstickConfig.options.steps == 0) {
if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) {
blinkstickConfig.options.steps = blinkstickConfig.options.duration / 10;
}
return blinkstickConfig;

View file

@ -3,7 +3,7 @@ const stat = require('fs').stat;
function fileExists(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
if (file === undefined) {
reject('can not check the existence of an undefined file');
}
resolvePath(file)
@ -21,7 +21,7 @@ function fileExists(file) {
function resolvePath(file) {
return new Promise((resolve, reject) => {
if (file == undefined) {
if (file === undefined) {
reject('can not resolve a path to an undefined file');
}
realpath(file, (err, resolvedPath) => {
@ -36,4 +36,4 @@ function resolvePath(file) {
module.exports = {
fileExists,
resolvePath
}
};