2022-03-01 22:14:36 +01:00
|
|
|
const constants = require('./constants.js');
|
2022-02-23 17:08:41 +01:00
|
|
|
const hexcolor = require('hex-color-regex');
|
|
|
|
|
|
|
|
// parse a http post and return an object with sane defaults
|
|
|
|
function parseRequest(requestBody, mode) {
|
|
|
|
if (mode === undefined) {
|
2022-03-01 22:14:36 +01:00
|
|
|
mode = constants.MODE_SET;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
let config = {
|
|
|
|
'id': Math.random(),
|
2022-03-04 00:02:07 +01:00
|
|
|
'blinkstick': parseBlinkstick(requestBody.blinkstick),
|
2022-02-23 17:08:41 +01:00
|
|
|
'mode': mode,
|
|
|
|
'options': {
|
|
|
|
'index': parseIndex(requestBody.index),
|
|
|
|
}
|
|
|
|
};
|
2022-03-01 22:14:36 +01:00
|
|
|
if (config.mode === constants.MODE_POWEROFF) {
|
2022-02-23 17:08:41 +01:00
|
|
|
config.color = '#000000';
|
|
|
|
return config;
|
|
|
|
} else {
|
|
|
|
config.color = parseColor(requestBody.color);
|
|
|
|
}
|
|
|
|
switch (config.mode) {
|
2022-03-01 22:14:36 +01:00
|
|
|
case constants.MODE_MORPH:
|
2022-02-23 17:08:41 +01:00
|
|
|
config.options.duration = parseDuration(requestBody.duration, config.options.index);
|
|
|
|
config.options.steps = parseSteps(requestBody.steps, config.options.duration);
|
|
|
|
break;
|
2022-03-01 22:14:36 +01:00
|
|
|
case constants.MODE_BLINK:
|
2022-02-24 14:42:30 +01:00
|
|
|
config.options.repeats = 1;
|
2022-02-23 17:08:41 +01:00
|
|
|
config.repetitions = parseRepetitions(requestBody.repeats, config.options.index);
|
|
|
|
config.options.delay = parseDelay(requestBody.delay);
|
|
|
|
break;
|
2022-03-01 22:14:36 +01:00
|
|
|
case constants.MODE_PULSE:
|
2022-02-24 14:42:30 +01:00
|
|
|
config.options.repeats = 1;
|
|
|
|
config.repetitions = parseRepetitions(requestBody.repeats, config.options.index);
|
2022-03-01 22:14:36 +01:00
|
|
|
config.options.duration = parseDuration(requestBody.duration, config.options.index, constants.MODE_PULSE);
|
2022-02-23 17:08:41 +01:00
|
|
|
config.options.steps = parseSteps(requestBody.steps, config.options.duration);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2022-03-04 00:02:07 +01:00
|
|
|
// parse the blinkstick
|
|
|
|
function parseBlinkstick(blinkstick) {
|
|
|
|
if (blinkstick === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return blinkstick;
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
// parse the index
|
|
|
|
function parseIndex(index) {
|
|
|
|
if (index === undefined) {
|
2022-03-01 22:14:36 +01:00
|
|
|
return constants.ALL;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
index = parseInt(index);
|
|
|
|
if (index !== index) {
|
2022-03-01 22:14:36 +01:00
|
|
|
return constants.ALL;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
if (index < 0) {
|
|
|
|
index = 0;
|
|
|
|
} else if (index > 7) {
|
|
|
|
index = 7;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the duration
|
|
|
|
function parseDuration(duration, index, mode) {
|
|
|
|
if (duration === undefined) {
|
2022-03-01 22:14:36 +01:00
|
|
|
duration = constants.DURATION_DEFAULT;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-03-01 22:14:36 +01:00
|
|
|
if (index === constants.ALL && mode !== constants.MODE_PULSE) {
|
2022-02-23 17:08:41 +01:00
|
|
|
duration = duration / 8;
|
|
|
|
}
|
|
|
|
if (duration < 100) {
|
|
|
|
duration = 100;
|
|
|
|
}
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the steps
|
|
|
|
function parseSteps(steps, duration) {
|
|
|
|
if (duration === undefined) {
|
2022-03-01 22:14:36 +01:00
|
|
|
duration = constants.DURATION_DEFAULT;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
if (steps === undefined || steps === 0) {
|
|
|
|
steps = duration / 10;
|
|
|
|
}
|
|
|
|
return steps;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the repetitions
|
|
|
|
function parseRepetitions(repeats, index) {
|
|
|
|
repeats = parseInt(repeats);
|
|
|
|
if (repeats !== repeats) {
|
|
|
|
repeats = 0;
|
|
|
|
}
|
|
|
|
return {done: 0, max: repeats};
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the delay
|
|
|
|
function parseDelay(delay) {
|
|
|
|
if (delay === undefined) {
|
2022-03-01 22:14:36 +01:00
|
|
|
delay = constants.DELAY_DEFAULT;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
return delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
// color / parser functions
|
|
|
|
function parseColor(value) {
|
2022-03-01 22:14:36 +01:00
|
|
|
if (value === undefined || value === constants.RANDOM) {
|
|
|
|
return constants.RANDOM;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-02-25 15:39:32 +01:00
|
|
|
let parsedColor = parseHexColor(value);
|
|
|
|
if (parsedColor !== undefined) {
|
|
|
|
return parsedColor;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-03-01 22:14:36 +01:00
|
|
|
return parseRGBColor(value) || constants.RANDOM;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseRGBColor(value) {
|
|
|
|
if (value.indexOf(',') === -1 && isRGBValue(value)) {
|
2022-02-25 15:39:32 +01:00
|
|
|
value = parseInt(value);
|
|
|
|
return convertRGBToHex(value, value, value);
|
|
|
|
}
|
|
|
|
const splittedValues = value.split(',');
|
|
|
|
let color = {};
|
|
|
|
for (let index = 0; index < splittedValues.length; index++) {
|
|
|
|
const value = splittedValues[index];
|
|
|
|
if (index === 0) {
|
|
|
|
color.red = parseInt(value) || 0;
|
|
|
|
} else if (index === 1) {
|
|
|
|
color.green = parseInt(value) || 0;
|
|
|
|
} else if (index === 2) {
|
|
|
|
color.blue = parseInt(value) || 0;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 15:39:32 +01:00
|
|
|
if (isRGBValue(color.red) && isRGBValue(color.green) && isRGBValue(color.blue)) {
|
|
|
|
return convertRGBToHex(color.red, color.green, color.blue);
|
|
|
|
}
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function isRGBValue(value) {
|
|
|
|
return value !== undefined && !isNaN(value) && value >= 0 && value <= 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertRGBToHex(red, green, blue) {
|
|
|
|
return '#' + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseHexColor(value) {
|
|
|
|
if (value[0] !== '#') {
|
|
|
|
value = '#' + value;
|
|
|
|
}
|
2022-02-25 15:39:32 +01:00
|
|
|
if (value.length !== 7) {
|
|
|
|
return;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-02-25 15:39:32 +01:00
|
|
|
if (!hexcolor({strict: true}).test(value)) {
|
|
|
|
return;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
2022-02-25 15:39:32 +01:00
|
|
|
return value;
|
2022-02-23 17:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
2022-03-22 01:05:52 +01:00
|
|
|
parseRequest,
|
|
|
|
parseBlinkstick,
|
|
|
|
parseIndex
|
2022-02-23 17:08:41 +01:00
|
|
|
};
|