2021-04-06 16:41:49 +02:00
|
|
|
// requirements
|
|
|
|
const logger = require('./logger');
|
2021-04-07 12:04:06 +02:00
|
|
|
const config = require('../config.json');
|
2022-02-22 16:42:49 +01:00
|
|
|
const util = require('util');
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
// third party requirements
|
|
|
|
const blinkstick = require('blinkstick');
|
|
|
|
const hexcolor = require('hex-color-regex');
|
|
|
|
|
|
|
|
// constants
|
2022-02-22 16:42:49 +01:00
|
|
|
// const LEDS = [0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
const LEDS_ALL = 'index_all';
|
2022-02-21 11:56:49 +01:00
|
|
|
const RANDOM = 'random';
|
2021-04-06 16:41:49 +02:00
|
|
|
const ANIMATION_STATE_INPROGRESS = 1;
|
|
|
|
const ANIMATION_STATE_FINISH = 0;
|
|
|
|
|
2022-02-22 17:00:30 +01:00
|
|
|
const MODE_SET = 'set';
|
|
|
|
const MODE_MORPH = 'morph';
|
|
|
|
const MODE_PULSE = 'pulse';
|
|
|
|
const MODE_POWEROFF = 'poweroff';
|
|
|
|
|
2022-02-22 16:42:49 +01:00
|
|
|
// variables
|
2021-04-06 16:41:49 +02:00
|
|
|
let led;
|
2022-02-22 16:42:49 +01:00
|
|
|
const LEDS = new Map();
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
function findBlinkstick() {
|
2022-02-21 00:48:29 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-04-06 16:41:49 +02:00
|
|
|
led = blinkstick.findFirst();
|
|
|
|
if (led !== undefined && led !== null) {
|
|
|
|
return resolve(led);
|
|
|
|
}
|
2022-02-21 00:48:29 +01:00
|
|
|
return reject('could not find any blinkstick');
|
2021-04-06 16:41:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:42:49 +01:00
|
|
|
// light it up
|
|
|
|
async function illuminate(blinkstickConfig) {
|
|
|
|
led = await findBlinkstick();
|
|
|
|
let indices = getIndices(blinkstickConfig);
|
|
|
|
for (let index = 0; index < indices.length; index++) {
|
|
|
|
try {
|
|
|
|
await setLedState(indices[index], ANIMATION_STATE_INPROGRESS);
|
|
|
|
await singleAnimation(JSON.parse(JSON.stringify(blinkstickConfig)), indices[index]);
|
2022-02-22 17:00:30 +01:00
|
|
|
blinkstickConfig.options.pulse++;
|
|
|
|
if (maxPulsesReached(blinkstickConfig)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-22 16:42:49 +01:00
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 17:00:30 +01:00
|
|
|
if (!maxPulsesReached(blinkstickConfig)) {
|
|
|
|
return illuminate(blinkstickConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maxPulsesReached(blinkstickConfig) {
|
|
|
|
if (blinkstickConfig.mode !== MODE_PULSE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return (blinkstickConfig.options.pulse.max === 0 || blinkstickConfig.options.pulse.done > blinkstickConfig.options.pulse.max);
|
2022-02-22 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// turn the blinkstick or specified led off
|
|
|
|
async function powerOff(index) {
|
|
|
|
led = await findBlinkstick();
|
|
|
|
// check for NaN
|
|
|
|
if (index !== index) {
|
|
|
|
index = LEDS_ALL;
|
|
|
|
}
|
2022-02-22 17:00:30 +01:00
|
|
|
let config = {color: '#000000', mode: MODE_POWEROFF, options: {index: index}};
|
2022-02-22 16:42:49 +01:00
|
|
|
let indices = getIndices(config);
|
|
|
|
for (let index = 0; index < indices.length; index++) {
|
|
|
|
try {
|
|
|
|
await singleAnimation(JSON.parse(JSON.stringify(config)), indices[index]);
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index !== LEDS_ALL) {
|
|
|
|
logger.info('led \'' + index + '\' powered off');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
led.stop();
|
|
|
|
led.turnOff();
|
|
|
|
logger.info('blinkstick powered off');
|
|
|
|
}
|
|
|
|
|
|
|
|
// animations
|
|
|
|
function singleAnimation(config, index) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
config.options.index = index;
|
|
|
|
logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')...');
|
|
|
|
switch (config.mode) {
|
2022-02-22 17:00:30 +01:00
|
|
|
case MODE_MORPH:
|
2022-02-22 16:42:49 +01:00
|
|
|
led.morph(config.color, config.options, callback);
|
|
|
|
break;
|
2022-02-22 17:00:30 +01:00
|
|
|
case MODE_PULSE:
|
2022-02-22 16:42:49 +01:00
|
|
|
led.pulse(config.color, config.options, callback);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
led.setColor(config.color, config.options, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function callback(err) {
|
|
|
|
clearLedState(config.options.index);
|
|
|
|
if (err) {
|
|
|
|
reject('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' encountered an error > ' + err);
|
|
|
|
}
|
|
|
|
logger.debug('changed color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ')');
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-22 17:00:30 +01:00
|
|
|
// TODO: IMPLEMENT FUNCTION
|
|
|
|
function continousAnimation(config, index) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:42:49 +01:00
|
|
|
// start pulsing
|
|
|
|
function startPulsing(blinkstickConfig) {
|
2022-02-21 00:48:29 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-02-22 16:42:49 +01:00
|
|
|
let animation = getAnimation(blinkstickConfig);
|
|
|
|
if (animation?.state === ANIMATION_STATE_FINISH || (blinkstickConfig.options.pulse.max > 0 && animation.done === blinkstickConfig.options.pulse.max)) {
|
|
|
|
clearAnimationProperties(blinkstickConfig);
|
|
|
|
return resolve('finished pulsing \'' + blinkstickConfig.color + '\'');
|
|
|
|
}
|
|
|
|
let currentAnimation = getAnimationByIndex(blinkstickConfig?.options?.index);
|
|
|
|
if (currentAnimation !== undefined && currentAnimation.id !== blinkstickConfig.id) {
|
|
|
|
stopAnimation(currentAnimation)
|
|
|
|
.then(logger.info)
|
|
|
|
.then(() => {
|
|
|
|
startPulsing(blinkstickConfig, resolve, reject)
|
|
|
|
})
|
|
|
|
.catch(logger.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
animation = setAnimationProperties(blinkstickConfig);
|
|
|
|
led.pulse(blinkstickConfig.color, blinkstickConfig.options, (err) => {
|
|
|
|
if (err) {
|
|
|
|
clearAnimationProperties(blinkstickConfig);
|
|
|
|
return reject('error pulsing ' + msg + ' > ' + err);
|
|
|
|
}
|
|
|
|
animation.done++;
|
|
|
|
logger.debug('pulsed ' + msg + ' ' + animation.done + '/' + blinkstickConfig.options.pulse.max + ' times');
|
|
|
|
startPulsing(blinkstickConfig)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// led / index helper functions
|
|
|
|
function getIndices(blinkstickConfig) {
|
|
|
|
if (blinkstickConfig.options.index === LEDS_ALL) {
|
|
|
|
return [0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
}
|
|
|
|
return [blinkstickConfig.options.index];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setLedState(index, state) {
|
|
|
|
await stopLedAnimation(index);
|
|
|
|
LEDS.set(index, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearLedState(index) {
|
|
|
|
LEDS.delete(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isLedAnimated(index) {
|
|
|
|
return (LEDS.get(index) || LEDS.get(LEDS_ALL)) !== undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
function stopLedAnimation(index) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
waitForAnimationEnd(index, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForAnimationEnd(index, callback) {
|
|
|
|
if (!isLedAnimated(index)) {
|
|
|
|
return callback();
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
waitForAnimationEnd(index, callback);
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// color / parser functions
|
2021-04-07 12:04:06 +02:00
|
|
|
function parseColor(value) {
|
2022-02-21 12:20:37 +01:00
|
|
|
if (!value || value === RANDOM) {
|
2021-04-07 12:04:06 +02:00
|
|
|
return RANDOM;
|
|
|
|
}
|
|
|
|
let parsedColor = parseRGBColor(value);
|
|
|
|
if (!parsedColor) {
|
|
|
|
parsedColor = parseHexColor(value);
|
|
|
|
}
|
|
|
|
return parsedColor || function () {
|
2022-02-21 00:48:29 +01:00
|
|
|
logger.warn('could not parse color value \'' + value + '\', defaulting to \'' + config.api.post.color.default + '\'');
|
2021-04-07 12:04:06 +02:00
|
|
|
return config.api.post.color.default;
|
|
|
|
}();
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:04:06 +02:00
|
|
|
function parseRGBColor(value) {
|
2022-02-21 11:56:49 +01:00
|
|
|
if (value.indexOf(',') === -1 && isRGBValue(value)) {
|
2021-04-07 12:04:06 +02:00
|
|
|
return convertRGBToHex(parseInt(value) || 0, 0, 0);
|
2021-04-06 16:41:49 +02:00
|
|
|
} else {
|
2022-02-21 00:48:29 +01:00
|
|
|
const splittedValues = value.split(',');
|
2021-04-07 12:04:06 +02:00
|
|
|
let color = {};
|
2021-04-06 16:41:49 +02:00
|
|
|
for (let index = 0; index < splittedValues.length; index++) {
|
|
|
|
const value = splittedValues[index];
|
2022-02-21 11:56:49 +01:00
|
|
|
if (index === 0) {
|
2021-04-07 12:04:06 +02:00
|
|
|
color.red = parseInt(value) || 0;
|
2021-04-06 16:41:49 +02:00
|
|
|
} else if (index === 1) {
|
2021-04-07 12:04:06 +02:00
|
|
|
color.green = parseInt(value) || 0;
|
2021-04-06 16:41:49 +02:00
|
|
|
} else if (index === 2) {
|
2021-04-07 12:04:06 +02:00
|
|
|
color.blue = parseInt(value) || 0;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 12:04:06 +02:00
|
|
|
if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) {
|
|
|
|
return convertRGBToHex(color.red, color.green, color.blue);
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isRGBValue(value) {
|
2022-02-21 11:56:49 +01:00
|
|
|
return value !== undefined && !isNaN(value) && value >= 0 && value <= 255;
|
2021-04-07 12:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function convertRGBToHex(red, green, blue) {
|
2022-02-21 00:48:29 +01:00
|
|
|
return '#' + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1);
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 12:04:06 +02:00
|
|
|
function parseHexColor(value) {
|
2022-02-21 11:56:49 +01:00
|
|
|
if (value[0] !== '#') {
|
2021-04-07 12:04:06 +02:00
|
|
|
value = '#' + value;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
2021-04-07 12:04:06 +02:00
|
|
|
if (value.length === 4) {
|
|
|
|
value = (value[0] + value[1] + value[1] + value[2] + value[2] + value[3] + value[3]);
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
2022-02-21 12:20:37 +01:00
|
|
|
if (hexcolor({strict: true}).test(value)) {
|
2021-04-07 12:04:06 +02:00
|
|
|
return value;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
|
|
|
parseColor,
|
|
|
|
illuminate,
|
2022-02-22 16:42:49 +01:00
|
|
|
powerOff,
|
2022-02-22 17:00:30 +01:00
|
|
|
LEDS_ALL,
|
|
|
|
MODE_SET,
|
|
|
|
MODE_MORPH,
|
|
|
|
MODE_PULSE,
|
|
|
|
MODE_POWEROFF
|
2021-04-06 16:41:49 +02:00
|
|
|
};
|