From 9ae6acba3686893f355fb41350c16a766a380bcf Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Feb 2022 14:53:17 +0100 Subject: [PATCH] fixed stopping of multiple active animations --- libs/blinkstick.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 6124c80..a4a5edb 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -12,7 +12,6 @@ const MODE_POWEROFF = 'poweroff'; const LEDS_ANIMATED = []; const LEDS_STOP = []; - // find connected blinkstick(s) async function findBlinkstick(index) { let blinksticks = blinkstick.findAll(); @@ -33,12 +32,9 @@ async function simple(config) { config.timestamp = new Date().getTime(); let indices = getIndices(config); for (let index = 0; index < indices.length; index++) { - try { - await setLedAnimated(indices[index]); - await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); - } finally { - await clearLedState(indices[index]); - } + await setLedAnimated(indices[index]); + await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); + await clearLedState(indices[index]); } return { status: 'ok', @@ -56,16 +52,13 @@ async function complex(config) { } let indices = getIndices(config); for (let index = 0; index < indices.length; index++) { - try { - if (shouldLedFinish(config)) { - return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'}; - } - await setLedAnimated(indices[index]); - await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); - config.repetitions.done++; - } finally { + if (shouldLedFinish(config)) { await clearLedState(indices[index]); + return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'}; } + await setLedAnimated(indices[index]); + await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); + config.repetitions.done++; } return complex(config); } @@ -74,7 +67,7 @@ async function complex(config) { async function powerOff(config) { config.timestamp = new Date().getTime(); let indices = getIndices(config); - await forceStop(indices); + await forceStop(); for (let index = 0; index < indices.length; index++) { await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]); logger.info('led \'' + indices[index] + '\' powered off'); @@ -121,7 +114,12 @@ async function singleAnimation(config, index) { async function stopLedsAccordingly(config) { if (config.options.index === LEDS_ALL) { - return await powerOff({id: Math.random(), mode: MODE_POWEROFF, color: '#000000', options: {index: LEDS_ALL}}); + return await powerOff({ + id: Math.random(), + mode: MODE_POWEROFF, + color: '#000000', + options: {index: LEDS_ALL} + }); } return stopLedAnimation(config.options.index); }