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", "listen": "0.0.0.0",
"port": 3000 "port": 3000
}, },
"blinkstick": {
"cache": true,
"serials": [
"BS006537-3.0"
]
},
"log": { "log": {
"level": "debug", "level": "debug",
"timestamp": "DD.MM.YYYY HH:mm:ss:SS" "timestamp": "DD.MM.YYYY HH:mm:ss:SS"

View file

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