2021-04-06 16:41:49 +02:00
|
|
|
const path = require('path');
|
|
|
|
const config = require('../config');
|
|
|
|
const packageJSON = require('../package.json');
|
|
|
|
const logger = require('./logger');
|
|
|
|
const blinkstick = require('./blinkstick');
|
2022-02-23 17:08:41 +01:00
|
|
|
const parser = require('./parser.js');
|
|
|
|
|
2021-04-06 16:41:49 +02:00
|
|
|
const express = require('express');
|
2022-02-21 11:56:49 +01:00
|
|
|
const favicon = require('serve-favicon');
|
2022-02-23 17:08:41 +01:00
|
|
|
const bodyparser = require('body-parser');
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
const app = express();
|
2022-02-23 17:08:41 +01:00
|
|
|
app.use(favicon(path.join(path.dirname(__dirname), 'public', 'favicon.ico')));
|
|
|
|
app.use(bodyparser.json());
|
|
|
|
app.use(bodyparser.urlencoded({extended: true}));
|
2021-04-06 16:41:49 +02:00
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
const MODE_SET = require('./blinkstick.js').MODE_SET;
|
|
|
|
const MODE_MORPH = require('./blinkstick.js').MODE_MORPH;
|
|
|
|
const MODE_BLINK = require('./blinkstick.js').MODE_BLINK;
|
|
|
|
const MODE_PULSE = require('./blinkstick.js').MODE_PULSE;
|
|
|
|
const MODE_POWEROFF = require('./blinkstick.js').MODE_POWEROFF;
|
|
|
|
|
|
|
|
const html = require('./index.js').get();
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
// run the express http server and handle gets/posts
|
|
|
|
function start() {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
app.listen(config.server.port, config.server.listen)
|
2022-02-23 17:08:41 +01:00
|
|
|
.on('listening', function () {
|
|
|
|
return resolve('server listening on ' + config.server.listen + ':' + config.server.port + '...')
|
2021-04-06 16:41:49 +02:00
|
|
|
})
|
2022-02-23 17:08:41 +01:00
|
|
|
.on('error', function (err) {
|
|
|
|
return reject('error starting server (' + err + ')');
|
2021-04-06 16:41:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
async function handleRequests() {
|
|
|
|
// GET html page
|
2022-02-21 00:48:29 +01:00
|
|
|
app.get('*', (request, response) => {
|
|
|
|
logger.http(request);
|
2022-02-23 17:08:41 +01:00
|
|
|
response.send(html);
|
2021-04-06 16:41:49 +02:00
|
|
|
response.end();
|
|
|
|
});
|
2022-02-23 17:08:41 +01:00
|
|
|
// POST '/set'
|
|
|
|
app.post('/' + MODE_SET, (request, response) => {
|
2022-02-21 00:48:29 +01:00
|
|
|
logger.http(request);
|
2022-02-23 17:08:41 +01:00
|
|
|
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});
|
|
|
|
});
|
2022-02-21 00:48:29 +01:00
|
|
|
});
|
2022-02-23 17:08:41 +01:00
|
|
|
// POST '/morph'
|
|
|
|
app.post('/' + MODE_MORPH, (request, response) => {
|
2022-02-21 00:48:29 +01:00
|
|
|
logger.http(request);
|
2022-02-23 17:08:41 +01:00
|
|
|
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});
|
|
|
|
});
|
2021-04-06 16:41:49 +02:00
|
|
|
});
|
2022-02-23 17:08:41 +01:00
|
|
|
// 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;
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
2022-02-23 17:08:41 +01:00
|
|
|
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'
|
|
|
|
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;
|
2022-02-21 01:14:42 +01:00
|
|
|
}
|
2022-02-23 17:08:41 +01:00
|
|
|
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'
|
|
|
|
app.post('/' + MODE_POWEROFF, (request, response) => {
|
|
|
|
logger.http(request);
|
|
|
|
blinkstick.powerOff(parser.parseRequest(request.body, MODE_POWEROFF))
|
|
|
|
.then((result) => {
|
|
|
|
response.end(JSON.stringify(result));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
response.status(500);
|
|
|
|
response.end({status: 'error', error: err});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForInfiniteAnimation(response, config) {
|
|
|
|
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
|
|
|
start,
|
|
|
|
handleRequests
|
|
|
|
};
|