cleaned code

This commit is contained in:
Daniel Sommer 2022-02-24 11:33:53 +01:00
parent 2a7386d262
commit 35b8c55966

View file

@ -1,6 +1,5 @@
const path = require('path'); const path = require('path');
const config = require('../config'); const config = require('../config');
const packageJSON = require('../package.json');
const logger = require('./logger'); const logger = require('./logger');
const blinkstick = require('./blinkstick'); const blinkstick = require('./blinkstick');
const parser = require('./parser.js'); const parser = require('./parser.js');
@ -45,72 +44,22 @@ function handleRequests() {
// POST '/set' // POST '/set'
app.post('/' + MODE_SET, (request, response) => { app.post('/' + MODE_SET, (request, response) => {
logger.http(request); logger.http(request);
blinkstick.simple(parser.parseRequest(request.body, MODE_SET)) handleSimpleAnimation(parser.parseRequest(request.body, MODE_SET), response);
.then((result) => {
response.end(JSON.stringify(result));
})
.catch((err) => {
response.status(500);
response.end({status: 'error', error: err});
});
}); });
// POST '/morph' // POST '/morph'
app.post('/' + MODE_MORPH, (request, response) => { app.post('/' + MODE_MORPH, (request, response) => {
logger.http(request); logger.http(request);
blinkstick.simple(parser.parseRequest(request.body, MODE_MORPH)) handleSimpleAnimation(parser.parseRequest(request.body, MODE_MORPH), response);
.then((result) => {
response.end(JSON.stringify(result));
})
.catch((err) => {
response.status(500);
response.end({status: 'error', error: err});
});
}); });
// POST '/blink' // POST '/blink'
app.post('/' + MODE_BLINK, (request, response) => { app.post('/' + MODE_BLINK, (request, response) => {
logger.http(request); logger.http(request);
const config = parser.parseRequest(request.body, MODE_BLINK); handleComplexAnimation(parser.parseRequest(request.body, MODE_BLINK), response);
if (blinkstick.isInfiniteAnimation(config)) {
response.end(JSON.stringify({status: 'ok'}));
response = undefined;
}
blinkstick.complex(config)
.then((result) => {
if (response === undefined) {
return;
}
response.end(JSON.stringify(result));
})
.catch((err) => {
if (response === undefined) {
return;
}
response.status(500);
response.end({status: 'error', error: err});
});
}); });
// POST '/pulse' // POST '/pulse'
app.post('/' + MODE_PULSE, (request, response) => { app.post('/' + MODE_PULSE, (request, response) => {
logger.http(request); logger.http(request);
const config = parser.parseRequest(request.body, MODE_PULSE); handleComplexAnimation(parser.parseRequest(request.body, MODE_PULSE), response);
if (blinkstick.isInfiniteAnimation(config)) {
response.end(JSON.stringify({status: 'ok'}));
response = undefined;
}
blinkstick.complex(config)
.then((result) => {
if (response === undefined) {
return;
}
response.end(JSON.stringify(result));
})
.catch((err) => {
if (response === undefined) {
return;
}
response.status(500);
response.end({status: 'error', error: err});
});
}); });
// POST '/poweroff' // POST '/poweroff'
app.post('/' + MODE_POWEROFF, (request, response) => { app.post('/' + MODE_POWEROFF, (request, response) => {
@ -126,8 +75,33 @@ function handleRequests() {
}); });
} }
function checkForInfiniteAnimation(response, config) { async function handleSimpleAnimation(config, response) {
try {
response.end(JSON.stringify(await blinkstick.simple(config)));
} catch (err) {
response.status(500);
response.end({status: 'error', error: err});
}
}
async function handleComplexAnimation(config, response) {
if (blinkstick.isInfiniteAnimation(config)) {
response.end(JSON.stringify({status: 'ok', time: 'infinite'}));
response = undefined;
}
try {
const result = await blinkstick.complex(config);
if (response === undefined) {
return;
}
response.end(JSON.stringify(result));
} catch (err) {
if (response === undefined) {
return;
}
response.status(500);
response.end({status: 'error', error: err});
}
} }
// exports // exports