added support for multiple blinksticks

This commit is contained in:
Daniel Sommer 2022-03-04 00:02:07 +01:00
parent 20efca9234
commit fe545fc39d
6 changed files with 105 additions and 37 deletions

View file

@ -26,7 +26,7 @@ async function main() {
if (await cli.handleArguments()) { if (await cli.handleArguments()) {
util.exit(); util.exit();
} }
await controller.findBlinkstick(); await controller.mapBlinkSticks();
logger.info(await server.start()); logger.info(await server.start());
server.handleRequests(); server.handleRequests();
} }

View file

@ -5,6 +5,16 @@
}, },
"blinkstick": { "blinkstick": {
"cache": true, "cache": true,
"map": [
{
"id": "square",
"serial": "BS006537-3.0"
},
{
"id": "strip",
"serial": "BS042165-3.0"
}
],
"serials": [ "serials": [
"BS006537-3.0" "BS006537-3.0"
] ]

View file

@ -11,7 +11,7 @@ async function handleArguments() {
continue; continue;
case constants.ARG_LIST: case constants.ARG_LIST:
case constants.ARG_LIST_SHORT: case constants.ARG_LIST_SHORT:
logger.info('blinksticks: ' + JSON.stringify(await controller.findBlinkstick(constants.ALL, true))); logger.info('blinksticks: ' + JSON.stringify(await controller.findBlinkstick('strip11', true)));
handled++; handled++;
break; break;
} }

View file

@ -5,36 +5,81 @@ const blinkstick = require('blinkstick');
const LEDAnimations = new Map(); const LEDAnimations = new Map();
let blinksticks; let blinksticks = new Map();
// find connected blinkstick(s) // get connected blinkstick by id (or return the first one found)
async function findBlinkstick(index, ignoreFilter) { async function getBlinkstick(id, filter) {
if (!global.config.blinkstick?.cache || blinksticks === undefined) { if (!global.config.blinkstick?.cache || blinksticks.size === 0) {
blinksticks = blinkstick.findAll(); mapBlinkSticks(filter);
if (!ignoreFilter && 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 (id === undefined) {
return blinksticks.values().next().value;
}
if (id === constants.ALL) {
return Array.from(blinksticks.values());
}
if (!blinksticks.has(id)) {
throw new Error('could not find any blinkstick matching the given id \'' + id + '\'');
}
return blinksticks.get(id);
}
// map found blinksticks
function mapBlinkSticks(filter) {
const foundBlinksticks = blinkstick.findAll();
if (filter === undefined) {
filter = global.config?.blinkstick?.map?.length > 0;
}
blinksticks = new Map();
filter = filter && global.config.blinkstick?.map?.length > 0;
for (let blinkstickIndex = 0; blinkstickIndex < foundBlinksticks.length; blinkstickIndex++) {
const serial = foundBlinksticks[blinkstickIndex].serial;
if (!filter) {
blinksticks.set(serial, foundBlinksticks[blinkstickIndex]);
continue;
}
for (filterIndex = 0; filterIndex < global.config.blinkstick.map.length; filterIndex++) {
let tmp = global.config.blinkstick.map[filterIndex];
if (tmp.serial !== serial) {
continue;
}
blinksticks.set(tmp.id || serial, foundBlinksticks[blinkstickIndex]);
break;
} }
} }
if (blinksticks.length === 0) { if (blinksticks.length === 0) {
if (filter) {
throw new Error('could not find any blinkstick matching the given serial(s)');
} else {
throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected'); throw new Error('could not find any blinkstick, make sure at least one blinkstick is connected');
} }
if (index === undefined) {
index = 0;
} else if (index !== constants.ALL) {
index = parseInt(index) || 0;
} }
if (index > blinksticks.length - 1) {
throw new Error('there is no blinkstick for index \'' + index + '\'');
} }
if (index === constants.ALL) {
return blinksticks; // reset a blinkstick
async function resetBlinkstick(id) {
if (blinksticks === undefined || blinksticks.length === 0) {
return;
} }
return blinksticks[index]; let tmp;
if (id === constants.ALL) {
tmp = await getBlinkstick(id);
for (let index = 0; index < tmp.length; index++) {
if (tmp[index] === undefined) {
continue;
}
tmp[index].close();
}
blinksticks.clear();
} else {
tmp = await getBlinkstick(id);
if (tmp === undefined) {
return;
}
tmp.close();
blinksticks.delete(id);
}
mapBlinkSticks();
} }
// simple animation (set the color / morph to color) // simple animation (set the color / morph to color)
@ -89,7 +134,7 @@ async function powerOff(config) {
logger.info('led \'' + indexes[index] + '\' powered off'); logger.info('led \'' + indexes[index] + '\' powered off');
} }
if (config.options.index === constants.ALL) { if (config.options.index === constants.ALL) {
const blinkstick = await findBlinkstick(); const blinkstick = await getBlinkstick();
blinkstick.turnOff(); blinkstick.turnOff();
LEDAnimations.clear(); LEDAnimations.clear();
logger.info('blinkstick powered off'); logger.info('blinkstick powered off');
@ -100,7 +145,7 @@ async function powerOff(config) {
// animations // animations
async function singleAnimation(config, index) { async function singleAnimation(config, index) {
config.options.index = index; config.options.index = index;
const blinkstick = await findBlinkstick(); const blinkstick = await getBlinkstick(config.blinkstick);
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ' | options: ' + JSON.stringify(config.options) + ')...'); logger.debug('changing color of led \'' + config.options.index + '\' to \'' + config.color + '\' (mode: ' + config.mode + ' | options: ' + JSON.stringify(config.options) + ')...');
setLEDAnimated(config.options.index); setLEDAnimated(config.options.index);
@ -141,12 +186,13 @@ function getIndices(blinkstickConfig) {
return [blinkstickConfig.options.index]; return [blinkstickConfig.options.index];
} }
async function getColor(index) { async function getColor(config) {
if (index === undefined) { const index = 0;
index = 0; if (!isNaN(config.options.index)) {
index = parseInt(config.options.index);
} }
logger.debug('getting color for led with index \'' + index + '\''); logger.debug('getting color for led with index \'' + index + '\'');
const blinkstick = await findBlinkstick(); const blinkstick = await getBlinkstick(config.blinkstick);
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
blinkstick.getColorString(index, (err, color) => { blinkstick.getColorString(index, (err, color) => {
if (err) { if (err) {
@ -162,7 +208,7 @@ async function setColorIfRandom(config) {
if (config.options.index !== constants.ALL || config.color !== constants.RANDOM) { if (config.options.index !== constants.ALL || config.color !== constants.RANDOM) {
return; return;
} }
config.color = await getColor(0); config.color = await getColor(config);
} }
async function stopLEDsAccordingly(config) { async function stopLEDsAccordingly(config) {
@ -278,7 +324,7 @@ function isInfiniteAnimation(config) {
// exports // exports
module.exports = { module.exports = {
findBlinkstick, mapBlinkSticks,
simple, simple,
complex, complex,
powerOff, powerOff,

View file

@ -8,6 +8,7 @@ function parseRequest(requestBody, mode) {
} }
let config = { let config = {
'id': Math.random(), 'id': Math.random(),
'blinkstick': parseBlinkstick(requestBody.blinkstick),
'mode': mode, 'mode': mode,
'options': { 'options': {
'index': parseIndex(requestBody.index), 'index': parseIndex(requestBody.index),
@ -39,6 +40,14 @@ function parseRequest(requestBody, mode) {
return config; return config;
} }
// parse the blinkstick
function parseBlinkstick(blinkstick) {
if (blinkstick === undefined) {
return undefined;
}
return blinkstick;
}
// parse the index // parse the index
function parseIndex(index) { function parseIndex(index) {
if (index === undefined) { if (index === undefined) {

View file

@ -67,6 +67,9 @@ async function handleSimpleAnimation(config, response) {
try { try {
response.end(JSON.stringify(await controller.simple(config))); response.end(JSON.stringify(await controller.simple(config)));
} catch (err) { } catch (err) {
if (err.message.includes('feature report')) {
await controller.resetBlinksticks();
}
logger.error(err); logger.error(err);
response.status(500); response.status(500);
response.end(JSON.stringify({ status: 'error', error: err.message })); response.end(JSON.stringify({ status: 'error', error: err.message }));