From 6ede57f7ffff026d690525cb570a697838127645 Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 1 Mar 2022 22:14:36 +0100 Subject: [PATCH] fixed modes 'set' and 'morph' with random color for all indexes --- libs/blinkstick.js | 75 +++++++++++++++++++--------------------------- libs/cli.js | 13 ++++---- libs/constants.js | 16 ++++++++++ libs/logger.js | 6 +++- libs/parser.js | 46 +++++++++++----------------- libs/server.js | 35 ++++++++++------------ 6 files changed, 88 insertions(+), 103 deletions(-) create mode 100644 libs/constants.js diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 2a5211d..1ba6bda 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -1,17 +1,8 @@ const logger = require('./logger.js'); const util = require('./util.js'); +const constants = require('./constants.js'); const blinkstick = require('blinkstick'); -const RANDOM = require('./parser').COLOR_RANDOM; - -const ALL = 'all'; - -const MODE_SET = 'set'; -const MODE_MORPH = 'morph'; -const MODE_BLINK = 'blink'; -const MODE_PULSE = 'pulse'; -const MODE_POWEROFF = 'poweroff'; - const LEDAnimations = new Map(); let blinksticks; @@ -34,7 +25,7 @@ async function findBlinkstick(index, ignoreFilter) { } if (index === undefined) { index = 0; - } else if (index !== ALL) { + } else if (index !== constants.ALL) { index = parseInt(index); } if (isNaN(index)) { @@ -43,7 +34,7 @@ async function findBlinkstick(index, ignoreFilter) { if (index > blinksticks.length - 1) { throw new Error('there is no blinkstick for index \'' + index + '\''); } - if (index === ALL) { + if (index === constants.ALL) { return blinksticks; } return blinksticks[index]; @@ -58,7 +49,7 @@ async function simple(config) { const tmpConfig = JSON.parse(JSON.stringify(config)); await singleAnimation(tmpConfig, indices[index]); if (index === 0) { - config = setColorIfRandom(config); + await setColorIfRandom(config); } } return { @@ -83,9 +74,6 @@ async function complex(config) { } const tmpConfig = JSON.parse(JSON.stringify(config)); await singleAnimation(tmpConfig, indices[index]); - if (index === 0) { - config = await setColorIfRandom(config); - } config.repetitions.done++; } return await complex(config); @@ -95,15 +83,15 @@ async function complex(config) { async function powerOff(config) { config.timestamp = new Date().getTime(); let indices = getIndices(config); - if (config.options.index === ALL) { - LEDAnimations.set(ALL, { stop: new Date().getTime() }); + if (config.options.index === constants.ALL) { + LEDAnimations.set(constants.ALL, { stop: new Date().getTime() }); } for (let index = 0; index < indices.length; index++) { await stopLEDAnimation(indices[index]); await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); logger.info('led \'' + indices[index] + '\' powered off'); } - if (config.options.index === ALL) { + if (config.options.index === constants.ALL) { const blinkstick = await findBlinkstick(); blinkstick.turnOff(); LEDAnimations.clear(); @@ -121,13 +109,13 @@ async function singleAnimation(config, index) { setLEDAnimated(config.options.index); blinkstick.animationsEnabled = true; switch (config.mode) { - case MODE_MORPH: + case constants.MODE_MORPH: blinkstick.morph(config.color, config.options, callback); break; - case MODE_BLINK: + case constants.MODE_BLINK: blinkstick.blink(config.color, config.options, callback); break; - case MODE_PULSE: + case constants.MODE_PULSE: blinkstick.pulse(config.color, config.options, callback); break; default: @@ -136,7 +124,7 @@ async function singleAnimation(config, index) { } function callback(err) { - if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) { + if (config.mode !== constants.MODE_BLINK && config.mode !== constants.MODE_PULSE) { clearLedState(config.options.index); } if (err) { @@ -150,7 +138,7 @@ async function singleAnimation(config, index) { // led / index helper functions function getIndices(blinkstickConfig) { - if (blinkstickConfig.options.index === ALL) { + if (blinkstickConfig.options.index === constants.ALL) { return [0, 1, 2, 3, 4, 5, 6, 7]; } return [blinkstickConfig.options.index]; @@ -163,7 +151,10 @@ async function getColor(index) { logger.debug('getting color for led with index \'' + index + '\''); const blinkstick = await findBlinkstick(); return await new Promise((resolve, reject) => { - blinkstick.getColorString(index, (color) => { + blinkstick.getColorString(index, (err, color) => { + if (err) { + reject(err); + } logger.debug('led with index \'' + index + '\' is set to color \'' + color + '\''); resolve(color); }); @@ -171,34 +162,34 @@ async function getColor(index) { } async function setColorIfRandom(config) { - if (config.options.index === ALL && config.color === RANDOM) { - config.color = await getColor(0); + if (config.options.index !== constants.ALL || config.color !== constants.RANDOM) { + return; } - return config; + config.color = await getColor(0); } async function stopLEDsAccordingly(config) { if (LEDAnimations.size === 0) { return; } - if (config.options.index === ALL) { + if (config.options.index === constants.ALL) { logger.debug('stopping all leds...'); return await powerOff({ id: Math.random(), - mode: MODE_POWEROFF, + mode: constants.MODE_POWEROFF, color: '#000000', - options: { index: ALL } + options: { index: constants.ALL } }); } return stopLEDAnimation(config.options.index); } function shouldLEDFinish(config) { - if (LEDAnimations.has(ALL) || (isLEDAnimated(config.options.index) && isLEDStopping(config.options.index))) { + if (LEDAnimations.has(constants.ALL) || (isLEDAnimated(config.options.index) && isLEDStopping(config.options.index))) { logger.debug('led \'' + config.options.index + '\' is set to \'stop\' and should finish now'); return true; } - if (config.mode === MODE_BLINK || config.mode === MODE_PULSE) { + if (config.mode === constants.MODE_BLINK || config.mode === constants.MODE_PULSE) { return config.repetitions.max !== 0 && config.repetitions.done >= config.repetitions.max; } return false; @@ -223,7 +214,7 @@ function setLEDStopping(index) { } function clearLedState(index) { - if (index === ALL) { + if (index === constants.ALL) { LEDAnimations.clear(); logger.debug('cleared animation state of all leds'); return; @@ -237,7 +228,7 @@ function isLEDAnimated(index) { } function isLEDStopping(index) { - if (LEDAnimations.has(ALL) && LEDAnimations.get(ALL).stop !== undefined) { + if (LEDAnimations.has(constants.ALL) && LEDAnimations.get(constants.ALL).stop !== undefined) { return true; } if (LEDAnimations.has(index) && LEDAnimations.get(index).stop !== undefined) { @@ -247,7 +238,7 @@ function isLEDStopping(index) { } async function stopLEDAnimation(index) { - if (index === ALL) { + if (index === constants.ALL) { for (const [key, value] of LEDAnimations) { setLEDStopping(key); } @@ -282,7 +273,7 @@ async function waitForAllAnimationsEnd(callback, timestamp) { } function isInfiniteAnimation(config) { - if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) { + if (config.mode !== constants.MODE_BLINK && config.mode !== constants.MODE_PULSE) { return false; } return config.repetitions.max === 0; @@ -294,11 +285,5 @@ module.exports = { simple, complex, powerOff, - isInfiniteAnimation, - ALL, - MODE_SET, - MODE_MORPH, - MODE_BLINK, - MODE_PULSE, - MODE_POWEROFF -}; \ No newline at end of file + isInfiniteAnimation +} \ No newline at end of file diff --git a/libs/cli.js b/libs/cli.js index 244d1f1..c3fc389 100644 --- a/libs/cli.js +++ b/libs/cli.js @@ -1,17 +1,15 @@ const logger = require('./logger.js'); const blinkstick = require('./blinkstick.js'); - -const ARG_CONFIG = '--config'; -const ARG_GET_SERIALS = '--get-blinksticks'; +const constants = require('./constants.js'); async function handleArguments() { let handled = 0; for (let index = 0; index < process.argv.length; index++) { switch (process.argv[index]) { - case ARG_CONFIG: + case constants.ARG_CONFIG: continue; - case ARG_GET_SERIALS: - logger.info('blinksticks: ' + JSON.stringify(await blinkstick.findBlinkstick(blinkstick.ALL))); + case constants.ARG_GET_SERIALS: + logger.info('blinksticks: ' + JSON.stringify(await blinkstick.findBlinkstick(constants.ALL))); handled++; break; } @@ -20,6 +18,5 @@ async function handleArguments() { } module.exports = { - handleArguments, - ARG_CONFIG + handleArguments }; \ No newline at end of file diff --git a/libs/constants.js b/libs/constants.js new file mode 100644 index 0000000..ab8b18c --- /dev/null +++ b/libs/constants.js @@ -0,0 +1,16 @@ +module.exports = { + ALL: 'all', + RANDOM: 'random', + + MODE_SET: 'set', + MODE_MORPH: 'morph', + MODE_BLINK: 'blink', + MODE_PULSE: 'pulse', + MODE_POWEROFF: 'poweroff', + + ARG_CONFIG: '--config', + ARG_GET_SERIALS: '--get-blinksticks', + + DURATION_DEFAULT: require('../config.json').api.post.duration.default || 1000, + DELAY_DEFAULT: require('../config.json').api.post.delay.default || 500 +} \ No newline at end of file diff --git a/libs/logger.js b/libs/logger.js index e442783..39c5025 100644 --- a/libs/logger.js +++ b/libs/logger.js @@ -95,6 +95,10 @@ function error(message) { if (loglevel > LOGLEVEL_ERROR) { return; } + if (message.stack) { + trace(message.stack, 'error'); + return; + } if (message.errors !== undefined) { for (let index = 0; index < message.errors.length; index++) { trace(message.errors[index], 'error'); @@ -103,7 +107,7 @@ function error(message) { } if (message.message) { trace(message.message, 'error'); - return + return; } trace(message, 'error'); } diff --git a/libs/parser.js b/libs/parser.js index d750073..82f10f7 100644 --- a/libs/parser.js +++ b/libs/parser.js @@ -1,21 +1,10 @@ +const constants = require('./constants.js'); const hexcolor = require('hex-color-regex'); -const LEDS_ALL = require('./blinkstick.js').ALL; -const MODE_SET = require('./blinkstick.js').MODE_SET; -const MODE_MORPH = require('./blinkstick.js').MODE_MORPH; -const MODE_BLINK = require('./blinkstick.js').MODE_BLINK; -const MODE_PULSE = require('./blinkstick.js').MODE_PULSE; -const MODE_POWEROFF = require('./blinkstick.js').MODE_POWEROFF; - -const DURATION_DEFAULT = require('../config.json').api.post.duration.default || 1000; -const DELAY_DEFAULT = require('../config.json').api.post.delay.default || 500; - -const COLOR_RANDOM = 'random'; - // parse a http post and return an object with sane defaults function parseRequest(requestBody, mode) { if (mode === undefined) { - mode = MODE_SET; + mode = constants.MODE_SET; } let config = { 'id': Math.random(), @@ -24,26 +13,26 @@ function parseRequest(requestBody, mode) { 'index': parseIndex(requestBody.index), } }; - if (config.mode === MODE_POWEROFF) { + if (config.mode === constants.MODE_POWEROFF) { config.color = '#000000'; return config; } else { config.color = parseColor(requestBody.color); } switch (config.mode) { - case MODE_MORPH: + case constants.MODE_MORPH: config.options.duration = parseDuration(requestBody.duration, config.options.index); config.options.steps = parseSteps(requestBody.steps, config.options.duration); break; - case MODE_BLINK: + case constants.MODE_BLINK: config.options.repeats = 1; config.repetitions = parseRepetitions(requestBody.repeats, config.options.index); config.options.delay = parseDelay(requestBody.delay); break; - case MODE_PULSE: + case constants.MODE_PULSE: config.options.repeats = 1; config.repetitions = parseRepetitions(requestBody.repeats, config.options.index); - config.options.duration = parseDuration(requestBody.duration, config.options.index, MODE_PULSE); + config.options.duration = parseDuration(requestBody.duration, config.options.index, constants.MODE_PULSE); config.options.steps = parseSteps(requestBody.steps, config.options.duration); break; } @@ -53,11 +42,11 @@ function parseRequest(requestBody, mode) { // parse the index function parseIndex(index) { if (index === undefined) { - return LEDS_ALL; + return constants.ALL; } index = parseInt(index); if (index !== index) { - return LEDS_ALL; + return constants.ALL; } if (index < 0) { index = 0; @@ -70,9 +59,9 @@ function parseIndex(index) { // parse the duration function parseDuration(duration, index, mode) { if (duration === undefined) { - duration = DURATION_DEFAULT; + duration = constants.DURATION_DEFAULT; } - if (index === LEDS_ALL && mode !== MODE_PULSE) { + if (index === constants.ALL && mode !== constants.MODE_PULSE) { duration = duration / 8; } if (duration < 100) { @@ -84,7 +73,7 @@ function parseDuration(duration, index, mode) { // parse the steps function parseSteps(steps, duration) { if (duration === undefined) { - duration = DURATION_DEFAULT; + duration = constants.DURATION_DEFAULT; } if (steps === undefined || steps === 0) { steps = duration / 10; @@ -104,21 +93,21 @@ function parseRepetitions(repeats, index) { // parse the delay function parseDelay(delay) { if (delay === undefined) { - delay = DELAY_DEFAULT; + delay = constants.DELAY_DEFAULT; } return delay; } // color / parser functions function parseColor(value) { - if (value === undefined || value === COLOR_RANDOM) { - return COLOR_RANDOM; + if (value === undefined || value === constants.RANDOM) { + return constants.RANDOM; } let parsedColor = parseHexColor(value); if (parsedColor !== undefined) { return parsedColor; } - return parseRGBColor(value) || COLOR_RANDOM; + return parseRGBColor(value) || constants.RANDOM; } function parseRGBColor(value) { @@ -166,6 +155,5 @@ function parseHexColor(value) { // exports module.exports = { - parseRequest, - COLOR_RANDOM + parseRequest }; \ No newline at end of file diff --git a/libs/server.js b/libs/server.js index e0de483..40a8b1c 100644 --- a/libs/server.js +++ b/libs/server.js @@ -1,8 +1,9 @@ -const path = require('path'); -const config = require('../config'); -const logger = require('./logger'); -const blinkstick = require('./blinkstick'); +const config = require('../config.json'); +const logger = require('./logger.js'); +const blinkstick = require('./blinkstick.js'); const parser = require('./parser.js'); +const constants = require('./constants.js'); +const path = require('path'); const express = require('express'); const favicon = require('serve-favicon'); @@ -13,12 +14,6 @@ app.use(favicon(path.join(path.dirname(__dirname), 'public', 'favicon.ico'))); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({extended: true})); -const MODE_SET = require('./blinkstick.js').MODE_SET; -const MODE_MORPH = require('./blinkstick.js').MODE_MORPH; -const MODE_BLINK = require('./blinkstick.js').MODE_BLINK; -const MODE_PULSE = require('./blinkstick.js').MODE_PULSE; -const MODE_POWEROFF = require('./blinkstick.js').MODE_POWEROFF; - const html = require('./index.js').get(); // run the express http server and handle gets/posts @@ -42,29 +37,29 @@ function handleRequests() { response.end(); }); // POST '/set' - app.post('/' + MODE_SET, (request, response) => { + app.post('/' + constants.MODE_SET, (request, response) => { logger.http(request); - handleSimpleAnimation(parser.parseRequest(request.body, MODE_SET), response); + handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_SET), response); }); // POST '/morph' - app.post('/' + MODE_MORPH, (request, response) => { + app.post('/' + constants.MODE_MORPH, (request, response) => { logger.http(request); - handleSimpleAnimation(parser.parseRequest(request.body, MODE_MORPH), response); + handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_MORPH), response); }); // POST '/blink' - app.post('/' + MODE_BLINK, (request, response) => { + app.post('/' + constants.MODE_BLINK, (request, response) => { logger.http(request); - handleComplexAnimation(parser.parseRequest(request.body, MODE_BLINK), response); + handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_BLINK), response); }); // POST '/pulse' - app.post('/' + MODE_PULSE, (request, response) => { + app.post('/' + constants.MODE_PULSE, (request, response) => { logger.http(request); - handleComplexAnimation(parser.parseRequest(request.body, MODE_PULSE), response); + handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_PULSE), response); }); // POST '/poweroff' - app.post('/' + MODE_POWEROFF, (request, response) => { + app.post('/' + constants.MODE_POWEROFF, (request, response) => { logger.http(request); - handlePowerOff(parser.parseRequest(request.body, MODE_POWEROFF), response); + handlePowerOff(parser.parseRequest(request.body, constants.MODE_POWEROFF), response); }); }