From 2a7386d2621efe5de956343d4f01faea8522bd45 Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Feb 2022 10:57:41 +0100 Subject: [PATCH] extended function to find connected blinkstick(s) --- libs/blinkstick.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libs/blinkstick.js b/libs/blinkstick.js index 441b866..98e53b5 100644 --- a/libs/blinkstick.js +++ b/libs/blinkstick.js @@ -15,15 +15,18 @@ const LEDS = new Map(); let stop = false; -// find a connected blinkstick -function findBlinkstick() { - return new Promise((resolve, reject) => { - let led = blinkstick.findFirst(); - if (led === undefined) { - reject('could not find any blinkstick'); - } - resolve(led); - }); +// find connected blinkstick(s) +async function findBlinkstick(index) { + let blinksticks = blinkstick.findAll(); + if (blinksticks.length === 0) { + throw new Error('could not find any blinkstick'); + } + if (index === undefined) { + index = 0; + } + if (index <= blinksticks.length) { + return blinksticks[index]; + } } // simple animation (set the color / morph to color) @@ -69,7 +72,6 @@ async function complex(config) { return complex(config); } catch (err) { logger.error(err); - return err; } } @@ -91,7 +93,8 @@ async function powerOff(config) { } return {status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms'}; } catch (err) { - return err; + logger.error(err); + throw err; } } @@ -126,7 +129,7 @@ async function singleAnimation(config, index) { } }); } catch (err) { - return err; + throw err; } }