cleaned code
This commit is contained in:
parent
2a7386d262
commit
35b8c55966
1 changed files with 30 additions and 56 deletions
|
@ -1,6 +1,5 @@
|
|||
const path = require('path');
|
||||
const config = require('../config');
|
||||
const packageJSON = require('../package.json');
|
||||
const logger = require('./logger');
|
||||
const blinkstick = require('./blinkstick');
|
||||
const parser = require('./parser.js');
|
||||
|
@ -45,72 +44,22 @@ function handleRequests() {
|
|||
// POST '/set'
|
||||
app.post('/' + MODE_SET, (request, response) => {
|
||||
logger.http(request);
|
||||
blinkstick.simple(parser.parseRequest(request.body, MODE_SET))
|
||||
.then((result) => {
|
||||
response.end(JSON.stringify(result));
|
||||
})
|
||||
.catch((err) => {
|
||||
response.status(500);
|
||||
response.end({status: 'error', error: err});
|
||||
});
|
||||
handleSimpleAnimation(parser.parseRequest(request.body, MODE_SET), response);
|
||||
});
|
||||
// POST '/morph'
|
||||
app.post('/' + MODE_MORPH, (request, response) => {
|
||||
logger.http(request);
|
||||
blinkstick.simple(parser.parseRequest(request.body, MODE_MORPH))
|
||||
.then((result) => {
|
||||
response.end(JSON.stringify(result));
|
||||
})
|
||||
.catch((err) => {
|
||||
response.status(500);
|
||||
response.end({status: 'error', error: err});
|
||||
});
|
||||
handleSimpleAnimation(parser.parseRequest(request.body, MODE_MORPH), response);
|
||||
});
|
||||
// POST '/blink'
|
||||
app.post('/' + MODE_BLINK, (request, response) => {
|
||||
logger.http(request);
|
||||
const config = parser.parseRequest(request.body, MODE_BLINK);
|
||||
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});
|
||||
});
|
||||
handleComplexAnimation(parser.parseRequest(request.body, MODE_BLINK), response);
|
||||
});
|
||||
// POST '/pulse'
|
||||
app.post('/' + MODE_PULSE, (request, response) => {
|
||||
logger.http(request);
|
||||
const config = parser.parseRequest(request.body, MODE_PULSE);
|
||||
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});
|
||||
});
|
||||
handleComplexAnimation(parser.parseRequest(request.body, MODE_PULSE), response);
|
||||
});
|
||||
// POST '/poweroff'
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue