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

View file

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

View file

@ -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
}

View file

@ -20,9 +20,15 @@ function get() {
'<div>' +
'<h2>get:</h2>' +
'<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 +=
'</div>' +
'<div>' +
'<h2>post:</h2>' +
'<h3>endpoints: </>';

View file

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

View file

@ -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);