2021-04-06 16:41:49 +02:00
|
|
|
const logger = require('./logger');
|
|
|
|
const blinkstick = require('blinkstick');
|
|
|
|
|
2022-02-25 15:40:20 +01:00
|
|
|
const ALL = 'all';
|
2021-04-06 16:41:49 +02:00
|
|
|
|
2022-02-22 17:00:30 +01:00
|
|
|
const MODE_SET = 'set';
|
|
|
|
const MODE_MORPH = 'morph';
|
2022-02-23 17:08:41 +01:00
|
|
|
const MODE_BLINK = 'blink';
|
2022-02-22 17:00:30 +01:00
|
|
|
const MODE_PULSE = 'pulse';
|
|
|
|
const MODE_POWEROFF = 'poweroff';
|
|
|
|
|
2022-02-24 14:42:30 +01:00
|
|
|
const LEDS_ANIMATED = [];
|
|
|
|
const LEDS_STOP = [];
|
2021-04-06 16:41:49 +02:00
|
|
|
|
2022-02-25 15:40:20 +01:00
|
|
|
let blinksticks;
|
|
|
|
|
2022-02-24 10:57:41 +01:00
|
|
|
// find connected blinkstick(s)
|
2022-02-25 15:40:20 +01:00
|
|
|
async function findBlinkstick(index, ignoreFilter) {
|
|
|
|
if (!global.config.blinkstick?.cache || blinksticks === undefined) {
|
|
|
|
blinksticks = blinkstick.findAll();
|
2022-02-25 15:50:32 +01:00
|
|
|
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)');
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 15:40:20 +01:00
|
|
|
}
|
2022-02-24 10:57:41 +01:00
|
|
|
if (blinksticks.length === 0) {
|
2022-02-25 15:40:20 +01:00
|
|
|
throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected');
|
2022-02-24 10:57:41 +01:00
|
|
|
}
|
|
|
|
if (index === undefined) {
|
|
|
|
index = 0;
|
2022-02-25 15:40:20 +01:00
|
|
|
} else if (index !== ALL) {
|
|
|
|
index = parseInt(index);
|
|
|
|
}
|
|
|
|
if (isNaN(index)) {
|
|
|
|
index = 0;
|
|
|
|
}
|
|
|
|
if (index > blinksticks.length - 1) {
|
|
|
|
throw new Error('there is no blinkstick for index \'' + index + '\'');
|
2022-02-24 10:57:41 +01:00
|
|
|
}
|
2022-02-25 15:40:20 +01:00
|
|
|
if (index === ALL) {
|
|
|
|
return blinksticks;
|
2022-02-24 10:57:41 +01:00
|
|
|
}
|
2022-02-25 15:40:20 +01:00
|
|
|
return blinksticks[index];
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
// simple animation (set the color / morph to color)
|
|
|
|
async function simple(config) {
|
2022-02-24 14:42:30 +01:00
|
|
|
await stopLedsAccordingly(config);
|
2022-02-24 11:52:16 +01:00
|
|
|
config.timestamp = new Date().getTime();
|
|
|
|
let indices = getIndices(config);
|
|
|
|
for (let index = 0; index < indices.length; index++) {
|
2022-02-24 14:53:17 +01:00
|
|
|
await setLedAnimated(indices[index]);
|
|
|
|
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
|
|
|
await clearLedState(indices[index]);
|
2022-02-22 17:00:30 +01:00
|
|
|
}
|
2022-02-24 11:52:16 +01:00
|
|
|
return {
|
|
|
|
status: 'ok',
|
|
|
|
color: config.color,
|
|
|
|
indices: indices,
|
|
|
|
time: (new Date().getTime() - config.timestamp) + 'ms'
|
|
|
|
};
|
2022-02-22 17:00:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
// complex animation (pulse / blink)
|
|
|
|
async function complex(config) {
|
2022-02-24 11:52:16 +01:00
|
|
|
if (config.timestamp === undefined) {
|
2022-02-24 14:42:30 +01:00
|
|
|
await stopLedsAccordingly(config);
|
2022-02-24 11:52:16 +01:00
|
|
|
config.timestamp = new Date().getTime();
|
|
|
|
}
|
|
|
|
let indices = getIndices(config);
|
|
|
|
for (let index = 0; index < indices.length; index++) {
|
2022-02-24 14:53:17 +01:00
|
|
|
if (shouldLedFinish(config)) {
|
2022-02-24 11:52:16 +01:00
|
|
|
await clearLedState(indices[index]);
|
2022-02-24 14:53:17 +01:00
|
|
|
return {status: 'ok', time: (new Date().getTime() - config.timestamp) + 'ms'};
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-02-24 14:53:17 +01:00
|
|
|
await setLedAnimated(indices[index]);
|
|
|
|
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
2022-02-25 15:40:20 +01:00
|
|
|
if (config.options.index === ALL) {
|
2022-02-24 15:24:08 +01:00
|
|
|
await clearLedState(indices[index]);
|
|
|
|
}
|
2022-02-24 14:53:17 +01:00
|
|
|
config.repetitions.done++;
|
2022-02-22 17:00:30 +01:00
|
|
|
}
|
2022-02-24 11:52:16 +01:00
|
|
|
return complex(config);
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
// power the blinkstick (or just a specific led) off
|
|
|
|
async function powerOff(config) {
|
2022-02-24 11:52:16 +01:00
|
|
|
config.timestamp = new Date().getTime();
|
|
|
|
let indices = getIndices(config);
|
2022-02-24 15:24:08 +01:00
|
|
|
await forceStop(config.options.index);
|
2022-02-24 11:52:16 +01:00
|
|
|
for (let index = 0; index < indices.length; index++) {
|
|
|
|
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
|
|
|
logger.info('led \'' + indices[index] + '\' powered off');
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
2022-02-25 15:40:20 +01:00
|
|
|
if (config.options.index === ALL) {
|
2022-02-24 11:52:16 +01:00
|
|
|
const blinkstick = await findBlinkstick();
|
|
|
|
blinkstick.stop();
|
|
|
|
blinkstick.turnOff();
|
|
|
|
logger.info('blinkstick powered off');
|
|
|
|
}
|
|
|
|
return {status: 'ok', indices: indices, time: (new Date().getTime() - config.timestamp) + 'ms'};
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// animations
|
2022-02-23 17:08:41 +01:00
|
|
|
async function singleAnimation(config, index) {
|
2022-02-24 11:52:16 +01:00
|
|
|
config.options.index = index;
|
|
|
|
const blinkstick = await findBlinkstick();
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...');
|
|
|
|
switch (config.mode) {
|
|
|
|
case MODE_MORPH:
|
|
|
|
blinkstick.morph(config.color, config.options, callback);
|
|
|
|
break;
|
|
|
|
case MODE_BLINK:
|
|
|
|
blinkstick.blink(config.color, config.options, callback);
|
|
|
|
break;
|
|
|
|
case MODE_PULSE:
|
|
|
|
blinkstick.pulse(config.color, config.options, callback);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
blinkstick.setColor(config.color, config.options, callback);
|
|
|
|
break;
|
|
|
|
}
|
2022-02-22 17:00:30 +01:00
|
|
|
|
2022-02-24 11:52:16 +01:00
|
|
|
function callback(err) {
|
|
|
|
if (err) {
|
|
|
|
reject(new Error('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' encountered an error > ' + err));
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-02-24 11:52:16 +01:00
|
|
|
logger.debug('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2022-02-22 17:00:30 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 14:42:30 +01:00
|
|
|
async function stopLedsAccordingly(config) {
|
2022-02-25 15:40:20 +01:00
|
|
|
if (config.options.index === ALL) {
|
2022-02-24 14:53:17 +01:00
|
|
|
return await powerOff({
|
|
|
|
id: Math.random(),
|
|
|
|
mode: MODE_POWEROFF,
|
|
|
|
color: '#000000',
|
2022-02-25 15:40:20 +01:00
|
|
|
options: {index: ALL}
|
2022-02-24 14:53:17 +01:00
|
|
|
});
|
2022-02-24 14:42:30 +01:00
|
|
|
}
|
|
|
|
return stopLedAnimation(config.options.index);
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:24:08 +01:00
|
|
|
async function forceStop(index) {
|
2022-02-25 15:40:20 +01:00
|
|
|
if (index === ALL) {
|
|
|
|
LEDS_STOP.push(ALL);
|
2022-02-24 15:24:08 +01:00
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
waitForAllAnimationsEnd(() => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setLedStopping(index);
|
2022-02-24 14:42:30 +01:00
|
|
|
return await new Promise((resolve, reject) => {
|
2022-02-24 15:24:08 +01:00
|
|
|
waitForAnimationEnd(index, () => {
|
2022-02-23 17:08:41 +01:00
|
|
|
resolve();
|
2022-02-22 16:42:49 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
function shouldLedFinish(config) {
|
2022-02-24 14:42:30 +01:00
|
|
|
if (isLedStopping(config.options.index)) {
|
2022-02-23 17:08:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-24 14:42:30 +01:00
|
|
|
if (config.mode === MODE_BLINK || config.mode === MODE_PULSE) {
|
|
|
|
return config.repetitions.max !== 0 && config.repetitions.done >= config.repetitions.max;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:42:49 +01:00
|
|
|
// led / index helper functions
|
|
|
|
function getIndices(blinkstickConfig) {
|
2022-02-25 15:40:20 +01:00
|
|
|
if (blinkstickConfig.options.index === ALL) {
|
2022-02-22 16:42:49 +01:00
|
|
|
return [0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
}
|
|
|
|
return [blinkstickConfig.options.index];
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:42:30 +01:00
|
|
|
function setLedAnimated(index) {
|
|
|
|
if (isLedAnimated(index)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LEDS_ANIMATED.push(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setLedStopping(index) {
|
|
|
|
if (!isLedAnimated(index) || isLedStopping(index)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LEDS_STOP.push(index);
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearLedState(index) {
|
2022-02-25 15:40:20 +01:00
|
|
|
if (index === ALL) {
|
2022-02-24 14:42:30 +01:00
|
|
|
LEDS_ANIMATED.length = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LEDS_ANIMATED.splice(LEDS_ANIMATED.indexOf(index), 1);
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLedAnimated(index) {
|
2022-02-24 14:42:30 +01:00
|
|
|
return LEDS_ANIMATED.includes(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isLedStopping(index) {
|
2022-02-25 15:40:20 +01:00
|
|
|
if (LEDS_STOP.includes(ALL)) {
|
2022-02-24 14:42:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return LEDS_STOP.includes(index);
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function stopLedAnimation(index) {
|
2022-02-24 14:42:30 +01:00
|
|
|
setLedStopping(index);
|
2022-02-22 16:42:49 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
waitForAnimationEnd(index, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForAnimationEnd(index, callback) {
|
|
|
|
if (!isLedAnimated(index)) {
|
2022-02-24 14:42:30 +01:00
|
|
|
LEDS_STOP.splice(LEDS_STOP.indexOf(index), 1);
|
2022-02-22 16:42:49 +01:00
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
waitForAnimationEnd(index, callback);
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
function waitForAllAnimationsEnd(callback) {
|
2022-02-24 14:42:30 +01:00
|
|
|
if (LEDS_ANIMATED.length === 0) {
|
|
|
|
LEDS_STOP.length = 0;
|
2022-02-23 17:08:41 +01:00
|
|
|
return callback();
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
2022-02-23 17:08:41 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
waitForAllAnimationsEnd(callback);
|
|
|
|
}, 100);
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
function isInfiniteAnimation(config) {
|
|
|
|
if (config.mode !== MODE_BLINK && config.mode !== MODE_PULSE) {
|
|
|
|
return false;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
2022-02-24 11:52:16 +01:00
|
|
|
return config.repetitions.max === 0;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
2022-02-25 15:40:20 +01:00
|
|
|
findBlinkstick,
|
2022-02-23 17:08:41 +01:00
|
|
|
simple,
|
|
|
|
complex,
|
2022-02-22 16:42:49 +01:00
|
|
|
powerOff,
|
2022-02-23 17:08:41 +01:00
|
|
|
isInfiniteAnimation,
|
2022-02-25 15:40:20 +01:00
|
|
|
ALL,
|
2022-02-22 17:00:30 +01:00
|
|
|
MODE_SET,
|
|
|
|
MODE_MORPH,
|
2022-02-23 17:08:41 +01:00
|
|
|
MODE_BLINK,
|
2022-02-22 17:00:30 +01:00
|
|
|
MODE_PULSE,
|
|
|
|
MODE_POWEROFF
|
2021-04-06 16:41:49 +02:00
|
|
|
};
|