code optimization
This commit is contained in:
parent
adc9f59adc
commit
7866ddb5df
7 changed files with 54 additions and 31 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
.vscode/
|
.vscode/
|
||||||
|
.idea/
|
||||||
node_modules/
|
node_modules/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
yarn.lock
|
yarn.lock
|
||||||
|
|
26
README.md
26
README.md
|
@ -4,9 +4,31 @@ control your blinkstick via http requests
|
||||||
|
|
||||||
## requirements
|
## requirements
|
||||||
|
|
||||||
- nodejs 8.x
|
- nodejs
|
||||||
- yarn / npm
|
- 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
|
### scripts
|
||||||
|
|
||||||
|
|
|
@ -30,9 +30,9 @@ function main() {
|
||||||
|
|
||||||
// ... and it all comes crashing down
|
// ... and it all comes crashing down
|
||||||
function exit(err, code) {
|
function exit(err, code) {
|
||||||
if (code == undefined) {
|
if (code === undefined) {
|
||||||
code = 0;
|
code = 0;
|
||||||
if (err != undefined) {
|
if (err !== undefined) {
|
||||||
code = 1;
|
code = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ function exit(err, code) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleInterrupts() {
|
function handleInterrupts() {
|
||||||
for (var index = 0; index < INTERRUPTS.length; index++) {
|
for (let index = 0; index < INTERRUPTS.length; index++) {
|
||||||
process.once(INTERRUPTS[index], (code) => {
|
process.once(INTERRUPTS[index], (code) => {
|
||||||
exit(null, code);
|
exit(null, code);
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,11 +7,11 @@ const blinkstick = require('blinkstick');
|
||||||
const hexcolor = require('hex-color-regex');
|
const hexcolor = require('hex-color-regex');
|
||||||
|
|
||||||
// constants
|
// constants
|
||||||
const RANDOM = 'random'
|
const RANDOM = 'random';
|
||||||
const ANIMATION_STATE_INPROGRESS = 1;
|
const ANIMATION_STATE_INPROGRESS = 1;
|
||||||
const ANIMATION_STATE_FINISH = 0;
|
const ANIMATION_STATE_FINISH = 0;
|
||||||
|
|
||||||
// variables
|
// letiables
|
||||||
let led;
|
let led;
|
||||||
let animation = {};
|
let animation = {};
|
||||||
|
|
||||||
|
@ -56,14 +56,14 @@ function parseColor(value) {
|
||||||
|
|
||||||
// parse rgb color values
|
// parse rgb color values
|
||||||
function parseRGBColor(value) {
|
function parseRGBColor(value) {
|
||||||
if (value.indexOf(',') == -1 && isRGBValue(value)) {
|
if (value.indexOf(',') === -1 && isRGBValue(value)) {
|
||||||
return convertRGBToHex(parseInt(value) || 0, 0, 0);
|
return convertRGBToHex(parseInt(value) || 0, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
const splittedValues = value.split(',');
|
const splittedValues = value.split(',');
|
||||||
let color = {};
|
let color = {};
|
||||||
for (let index = 0; index < splittedValues.length; index++) {
|
for (let index = 0; index < splittedValues.length; index++) {
|
||||||
const value = splittedValues[index];
|
const value = splittedValues[index];
|
||||||
if (index == 0) {
|
if (index === 0) {
|
||||||
color.red = parseInt(value) || 0;
|
color.red = parseInt(value) || 0;
|
||||||
} else if (index === 1) {
|
} else if (index === 1) {
|
||||||
color.green = parseInt(value) || 0;
|
color.green = parseInt(value) || 0;
|
||||||
|
@ -79,7 +79,7 @@ function parseRGBColor(value) {
|
||||||
|
|
||||||
// check a single rgb value
|
// check a single rgb value
|
||||||
function isRGBValue(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
|
// convert rgb to hex color values
|
||||||
|
@ -89,7 +89,7 @@ function convertRGBToHex(red, green, blue) {
|
||||||
|
|
||||||
// parse hex color values
|
// parse hex color values
|
||||||
function parseHexColor(value) {
|
function parseHexColor(value) {
|
||||||
if (value[0] != '#') {
|
if (value[0] !== '#') {
|
||||||
value = '#' + value;
|
value = '#' + value;
|
||||||
}
|
}
|
||||||
if (value.length === 4) {
|
if (value.length === 4) {
|
||||||
|
@ -155,11 +155,11 @@ function morph(blinkstickConfig, resolve, reject) {
|
||||||
|
|
||||||
// start pulsing
|
// start pulsing
|
||||||
function startPulsing(blinkstickConfig, resolve, reject) {
|
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();
|
clearAnimationProperties();
|
||||||
return resolve('finished pulsing color \'' + blinkstickConfig.color + '\'');
|
return resolve('finished pulsing color \'' + blinkstickConfig.color + '\'');
|
||||||
}
|
}
|
||||||
if (animation.id && animation.id != blinkstickConfig.id) {
|
if (animation.id && animation.id !== blinkstickConfig.id) {
|
||||||
stopAnimation()
|
stopAnimation()
|
||||||
.then(logger.info)
|
.then(logger.info)
|
||||||
.then(function () {
|
.then(function () {
|
||||||
|
@ -182,7 +182,7 @@ function startPulsing(blinkstickConfig, resolve, reject) {
|
||||||
|
|
||||||
// set properties for the current animation
|
// set properties for the current animation
|
||||||
function setAnimationProperties(blinkstickConfig) {
|
function setAnimationProperties(blinkstickConfig) {
|
||||||
if (animation.id == blinkstickConfig.id) {
|
if (animation.id === blinkstickConfig.id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
led.animationsEnabled = true;
|
led.animationsEnabled = true;
|
||||||
|
@ -219,7 +219,7 @@ function waitForAnimation(timestamp, resolve, reject) {
|
||||||
|
|
||||||
// is currently an animation in progress
|
// is currently an animation in progress
|
||||||
function isAnimationInProgress() {
|
function isAnimationInProgress() {
|
||||||
return animation != undefined && animation.state != undefined;
|
return animation !== undefined && animation.state !== undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// exports
|
// exports
|
||||||
|
|
|
@ -10,12 +10,12 @@ const LOGLEVEL_INFO = 1;
|
||||||
const LOGLEVEL_WARNING = 2;
|
const LOGLEVEL_WARNING = 2;
|
||||||
const LOGLEVEL_ERROR = 3;
|
const LOGLEVEL_ERROR = 3;
|
||||||
|
|
||||||
var loglevel = getLogLevel();
|
let loglevel = getLogLevel();
|
||||||
var timestamp = getTimestamp();
|
let timestamp = getTimestamp();
|
||||||
|
|
||||||
function initialize() {
|
function initialize() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (global.config == undefined) {
|
if (global.config === undefined) {
|
||||||
reject('could not initialize logger, config is undefined');
|
reject('could not initialize logger, config is undefined');
|
||||||
}
|
}
|
||||||
loglevel = getLogLevel();
|
loglevel = getLogLevel();
|
||||||
|
@ -26,7 +26,7 @@ function initialize() {
|
||||||
|
|
||||||
// get the loglevel
|
// get the loglevel
|
||||||
function getLogLevel() {
|
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;
|
return LOGLEVEL_INFO;
|
||||||
}
|
}
|
||||||
switch (global.config.log.level) {
|
switch (global.config.log.level) {
|
||||||
|
@ -49,7 +49,7 @@ function getLogLevel() {
|
||||||
|
|
||||||
// get the timestamp format
|
// get the timestamp format
|
||||||
function getTimestamp() {
|
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 global.config.log.timestamp;
|
||||||
}
|
}
|
||||||
return "DD.MM.YYYY HH:mm:ss:SS";
|
return "DD.MM.YYYY HH:mm:ss:SS";
|
||||||
|
@ -57,7 +57,7 @@ function getTimestamp() {
|
||||||
|
|
||||||
// log a http request
|
// log a http request
|
||||||
function http(request) {
|
function http(request) {
|
||||||
if (request == undefined) {
|
if (request === undefined) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let message = '[' + request.method + '] url: \'' + request.url + '\'';
|
let message = '[' + request.method + '] url: \'' + request.url + '\'';
|
||||||
|
@ -98,8 +98,8 @@ function error(message) {
|
||||||
if (loglevel > LOGLEVEL_ERROR) {
|
if (loglevel > LOGLEVEL_ERROR) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (message.errors != undefined) {
|
if (message.errors !== undefined) {
|
||||||
for (var index = 0; index < message.errors.length; index++) {
|
for (let index = 0; index < message.errors.length; index++) {
|
||||||
trace(message.errors[index], 'error');
|
trace(message.errors[index], 'error');
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -141,4 +141,4 @@ module.exports = {
|
||||||
debug,
|
debug,
|
||||||
error,
|
error,
|
||||||
http
|
http
|
||||||
}
|
};
|
|
@ -6,7 +6,7 @@ const logger = require('./logger');
|
||||||
const blinkstick = require('./blinkstick');
|
const blinkstick = require('./blinkstick');
|
||||||
// third party requirements
|
// third party requirements
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const favicon = require('serve-favicon')
|
const favicon = require('serve-favicon');
|
||||||
const parser = require('body-parser');
|
const parser = require('body-parser');
|
||||||
|
|
||||||
// setup express, blinkstick and other stuff
|
// setup express, blinkstick and other stuff
|
||||||
|
@ -58,7 +58,7 @@ function getHTML() {
|
||||||
"</table>" +
|
"</table>" +
|
||||||
"</div>" +
|
"</div>" +
|
||||||
"</body>" +
|
"</body>" +
|
||||||
"</html>"
|
"</html>";
|
||||||
return welcomeMessage;
|
return welcomeMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ function parseRequest(data) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (data.index != undefined) {
|
if (data.index !== undefined) {
|
||||||
blinkstickConfig.options.index = parseInt(data.index);
|
blinkstickConfig.options.index = parseInt(data.index);
|
||||||
if (blinkstickConfig.options.index < 0) {
|
if (blinkstickConfig.options.index < 0) {
|
||||||
blinkstickConfig.options.index = 0;
|
blinkstickConfig.options.index = 0;
|
||||||
|
@ -123,7 +123,7 @@ function parseRequest(data) {
|
||||||
if (blinkstickConfig.options.duration < 100) {
|
if (blinkstickConfig.options.duration < 100) {
|
||||||
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;
|
blinkstickConfig.options.steps = blinkstickConfig.options.duration / 10;
|
||||||
}
|
}
|
||||||
return blinkstickConfig;
|
return blinkstickConfig;
|
||||||
|
|
|
@ -3,7 +3,7 @@ const stat = require('fs').stat;
|
||||||
|
|
||||||
function fileExists(file) {
|
function fileExists(file) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (file == undefined) {
|
if (file === undefined) {
|
||||||
reject('can not check the existence of an undefined file');
|
reject('can not check the existence of an undefined file');
|
||||||
}
|
}
|
||||||
resolvePath(file)
|
resolvePath(file)
|
||||||
|
@ -21,7 +21,7 @@ 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('can not resolve a path to an undefined file');
|
reject('can not resolve a path to an undefined file');
|
||||||
}
|
}
|
||||||
realpath(file, (err, resolvedPath) => {
|
realpath(file, (err, resolvedPath) => {
|
||||||
|
@ -36,4 +36,4 @@ function resolvePath(file) {
|
||||||
module.exports = {
|
module.exports = {
|
||||||
fileExists,
|
fileExists,
|
||||||
resolvePath
|
resolvePath
|
||||||
}
|
};
|
Loading…
Reference in a new issue