implemented new endpoint 'color' to retrieve current color information

This commit is contained in:
Daniel Sommer 2022-03-22 01:05:52 +01:00
parent e2a2804419
commit e0d402b438
6 changed files with 73 additions and 10 deletions

View file

@ -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` `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 and reboot your system
## systemd ## systemd
**for security reasons it is highly recommended to not run blinky with root permissions!** **for security reasons it is highly recommended to not run blinky with root permissions!**

View file

@ -25,7 +25,10 @@
}, },
"api": { "api": {
"get": { "get": {
"description": "show this page" "description": "show this page",
"endpoints": [
"/color"
]
}, },
"post": { "post": {
"endpoints": [ "endpoints": [

View file

@ -196,8 +196,42 @@ function getIndices(blinkstickConfig) {
return [blinkstickConfig.options.index]; 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) { async function getColor(config) {
const index = 0; let index = 0;
if (!isNaN(config.options.index)) { if (!isNaN(config.options.index)) {
index = parseInt(config.options.index); index = parseInt(config.options.index);
} }
@ -339,5 +373,6 @@ module.exports = {
simple, simple,
complex, complex,
powerOff, powerOff,
isInfiniteAnimation isInfiniteAnimation,
getColors
} }

View file

@ -20,9 +20,15 @@ function get() {
'<div>' + '<div>' +
'<h2>get:</h2>' + '<h2>get:</h2>' +
'<p>' + config.api.get.description + '</p>' + '<p>' + config.api.get.description + '</p>' +
'</div>'; '<h3>endpoints: </>';
for (let index = 0; index < config.api.get.endpoints.length; index++) {
if (index > 0) {
html += ', ';
}
html += config.api.get.endpoints[index];
}
html += html +=
'</div>' +
'<div>' + '<div>' +
'<h2>post:</h2>' + '<h2>post:</h2>' +
'<h3>endpoints: </>'; '<h3>endpoints: </>';

View file

@ -164,5 +164,7 @@ function parseHexColor(value) {
// exports // exports
module.exports = { module.exports = {
parseRequest parseRequest,
parseBlinkstick,
parseIndex
}; };

View file

@ -30,6 +30,11 @@ async function start() {
} }
function handleRequests() { function handleRequests() {
// GET '/color'
app.get('/color', (request, response) => {
logger.http(request);
handleGETColor(request.body.blinkstick, request.body.index, response);
});
// GET html page // GET html page
app.get('*', (request, response) => { app.get('*', (request, response) => {
logger.http(request); 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) { 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')) { if (response === undefined) {
await controller.resetBlinksticks(); return;
} }
logger.error(err); logger.error(err);
response.status(500); response.status(500);