extended config and functionality to cache found blinksticks and filter them by serial(s)

This commit is contained in:
Daniel Sommer 2022-02-25 15:40:20 +01:00
parent 421c3be3dd
commit 4d2dcbf4f0
2 changed files with 45 additions and 16 deletions

View file

@ -3,6 +3,12 @@
"listen": "0.0.0.0",
"port": 3000
},
"blinkstick": {
"cache": true,
"serials": [
"BS006537-3.0"
]
},
"log": {
"level": "debug",
"timestamp": "DD.MM.YYYY HH:mm:ss:SS"

View file

@ -1,7 +1,7 @@
const logger = require('./logger');
const blinkstick = require('blinkstick');
const LEDS_ALL = 'all';
const ALL = 'all';
const MODE_SET = 'set';
const MODE_MORPH = 'morph';
@ -12,18 +12,40 @@ const MODE_POWEROFF = 'poweroff';
const LEDS_ANIMATED = [];
const LEDS_STOP = [];
let blinksticks;
// find connected blinkstick(s)
async function findBlinkstick(index) {
let blinksticks = blinkstick.findAll();
async function findBlinkstick(index, ignoreFilter) {
if (!global.config.blinkstick?.cache || blinksticks === undefined) {
blinksticks = blinkstick.findAll();
}
if (blinksticks.length === 0) {
throw new Error('could not find any blinkstick');
throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected');
}
if (index === undefined) {
index = 0;
} else if (index !== ALL) {
index = parseInt(index);
}
if (index <= blinksticks.length) {
return blinksticks[index];
if (isNaN(index)) {
index = 0;
}
if (global.config.blinkstick?.serials?.length > 0) {
blinksticks = blinksticks.filter((blinkstick) => {
return global.config.blinkstick.serials.includes(blinkstick.serial);
});
if (blinksticks.length === 0) {
throw new Error('could not find any blinkstick matching the defined serial(s)');
}
}
if (index > blinksticks.length - 1) {
throw new Error('there is no blinkstick for index \'' + index + '\'');
}
if (index === ALL) {
return blinksticks;
}
return blinksticks[index];
}
// simple animation (set the color / morph to color)
@ -58,7 +80,7 @@ async function complex(config) {
}
await setLedAnimated(indices[index]);
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
if (config.options.index === LEDS_ALL) {
if (config.options.index === ALL) {
await clearLedState(indices[index]);
}
config.repetitions.done++;
@ -75,7 +97,7 @@ async function powerOff(config) {
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
logger.info('led \'' + indices[index] + '\' powered off');
}
if (config.options.index === LEDS_ALL) {
if (config.options.index === ALL) {
const blinkstick = await findBlinkstick();
blinkstick.stop();
blinkstick.turnOff();
@ -116,20 +138,20 @@ async function singleAnimation(config, index) {
}
async function stopLedsAccordingly(config) {
if (config.options.index === LEDS_ALL) {
if (config.options.index === ALL) {
return await powerOff({
id: Math.random(),
mode: MODE_POWEROFF,
color: '#000000',
options: {index: LEDS_ALL}
options: {index: ALL}
});
}
return stopLedAnimation(config.options.index);
}
async function forceStop(index) {
if (index === LEDS_ALL) {
LEDS_STOP.push(LEDS_ALL);
if (index === ALL) {
LEDS_STOP.push(ALL);
return await new Promise((resolve, reject) => {
waitForAllAnimationsEnd(() => {
resolve();
@ -156,7 +178,7 @@ function shouldLedFinish(config) {
// led / index helper functions
function getIndices(blinkstickConfig) {
if (blinkstickConfig.options.index === LEDS_ALL) {
if (blinkstickConfig.options.index === ALL) {
return [0, 1, 2, 3, 4, 5, 6, 7];
}
return [blinkstickConfig.options.index];
@ -177,7 +199,7 @@ function setLedStopping(index) {
}
function clearLedState(index) {
if (index === LEDS_ALL) {
if (index === ALL) {
LEDS_ANIMATED.length = 0;
return;
}
@ -189,7 +211,7 @@ function isLedAnimated(index) {
}
function isLedStopping(index) {
if (LEDS_STOP.includes(LEDS_ALL)) {
if (LEDS_STOP.includes(ALL)) {
return true;
}
return LEDS_STOP.includes(index);
@ -233,11 +255,12 @@ function isInfiniteAnimation(config) {
// exports
module.exports = {
findBlinkstick,
simple,
complex,
powerOff,
isInfiniteAnimation,
LEDS_ALL,
ALL,
MODE_SET,
MODE_MORPH,
MODE_BLINK,