From e0d402b43864cc5559742ec6546783d079d6ea5b Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 22 Mar 2022 01:05:52 +0100 Subject: [PATCH] implemented new endpoint 'color' to retrieve current color information --- README.md | 2 -- config.json | 5 ++++- libs/controller.js | 39 +++++++++++++++++++++++++++++++++++++-- libs/index.js | 10 ++++++++-- libs/parser.js | 4 +++- libs/server.js | 23 +++++++++++++++++++++-- 6 files changed, 73 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1eb1adb..6951d36 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,6 @@ run: `echo "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"20a0\", ATTRS{idProduct}==\"41e5\", MODE=\"0666\"" | sudo tee /etc/udev/rules.d/85-blinkstick-hid.rules` and reboot your system - - ## systemd **for security reasons it is highly recommended to not run blinky with root permissions!** diff --git a/config.json b/config.json index f6e4b89..a626d87 100644 --- a/config.json +++ b/config.json @@ -25,7 +25,10 @@ }, "api": { "get": { - "description": "show this page" + "description": "show this page", + "endpoints": [ + "/color" + ] }, "post": { "endpoints": [ diff --git a/libs/controller.js b/libs/controller.js index c5db499..ec03cc7 100644 --- a/libs/controller.js +++ b/libs/controller.js @@ -196,8 +196,42 @@ function getIndices(blinkstickConfig) { return [blinkstickConfig.options.index]; } +async function getColors(blinkstick, index) { + let blinksticksToCheck = []; + if (blinkstick === undefined) { + blinksticksToCheck = Array.from(blinksticks.keys()); + } else { + blinksticksToCheck.push(blinkstick); + } + let indices = [0, 1, 2, 3, 4, 5, 6, 7]; + if (index !== undefined && index !== constants.AL && !isNaN(index)) { + index = [index]; + } + let results = []; + for (let blinkstickIndex = 0; blinkstickIndex < blinksticksToCheck.length; blinkstickIndex++) { + const tmpBlinkstick = blinksticksToCheck[blinkstickIndex]; + let result = { + blinkstick: tmpBlinkstick, + leds: [] + }; + for (let ledIndex = 0; ledIndex < indices.length; ledIndex++) { + result.leds.push({ + index: ledIndex, + color: await getColor({ + blinkstick: tmpBlinkstick, + options: { + index: ledIndex + } + }) + }); + } + results.push(result); + } + return results; +} + async function getColor(config) { - const index = 0; + let index = 0; if (!isNaN(config.options.index)) { index = parseInt(config.options.index); } @@ -339,5 +373,6 @@ module.exports = { simple, complex, powerOff, - isInfiniteAnimation + isInfiniteAnimation, + getColors } \ No newline at end of file diff --git a/libs/index.js b/libs/index.js index c75366a..0eaac07 100644 --- a/libs/index.js +++ b/libs/index.js @@ -20,9 +20,15 @@ function get() { '
' + '

get:

' + '

' + config.api.get.description + '

' + - '
'; - + '

endpoints: '; + for (let index = 0; index < config.api.get.endpoints.length; index++) { + if (index > 0) { + html += ', '; + } + html += config.api.get.endpoints[index]; + } html += + '' + '
' + '

post:

' + '

endpoints: '; diff --git a/libs/parser.js b/libs/parser.js index 615c9ff..7ca721f 100644 --- a/libs/parser.js +++ b/libs/parser.js @@ -164,5 +164,7 @@ function parseHexColor(value) { // exports module.exports = { - parseRequest + parseRequest, + parseBlinkstick, + parseIndex }; \ No newline at end of file diff --git a/libs/server.js b/libs/server.js index 5bde9b8..ad9a79d 100644 --- a/libs/server.js +++ b/libs/server.js @@ -30,6 +30,11 @@ async function start() { } function handleRequests() { + // GET '/color' + app.get('/color', (request, response) => { + logger.http(request); + handleGETColor(request.body.blinkstick, request.body.index, response); + }); // GET html page app.get('*', (request, response) => { logger.http(request); @@ -63,12 +68,26 @@ function handleRequests() { }); } +async function handleGETColor(blinkstick, index, response) { + try { + let result = await controller.getColors(blinkstick, index); + response.end(JSON.stringify(result)); + } catch (err) { + if (response === undefined) { + return; + } + logger.error(err); + response.status(500); + response.end(JSON.stringify({ status: 'error', error: err.message })); + } +} + async function handleSimpleAnimation(config, response) { try { response.end(JSON.stringify(await controller.simple(config))); } catch (err) { - if (err.message.includes('feature report')) { - await controller.resetBlinksticks(); + if (response === undefined) { + return; } logger.error(err); response.status(500);