2022-03-01 22:14:36 +01:00
|
|
|
const config = require('../config.json');
|
|
|
|
const logger = require('./logger.js');
|
2022-03-03 20:59:12 +01:00
|
|
|
const controller = require('./controller.js');
|
2022-02-23 17:08:41 +01:00
|
|
|
const parser = require('./parser.js');
|
2022-03-01 22:14:36 +01:00
|
|
|
const constants = require('./constants.js');
|
|
|
|
const path = require('path');
|
2022-02-23 17:08:41 +01:00
|
|
|
|
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());
|
2022-03-04 00:02:07 +01:00
|
|
|
app.use(bodyparser.urlencoded({ extended: true }));
|
2021-04-06 16:41:49 +02:00
|
|
|
|
2022-02-23 17:08:41 +01:00
|
|
|
const html = require('./index.js').get();
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
// run the express http server and handle gets/posts
|
2022-02-24 10:41:50 +01:00
|
|
|
async function start() {
|
|
|
|
return await new Promise(function (resolve, reject) {
|
2021-04-06 16:41:49 +02:00
|
|
|
app.listen(config.server.port, config.server.listen)
|
2022-02-23 17:08:41 +01:00
|
|
|
.on('listening', function () {
|
2022-02-24 10:41:50 +01:00
|
|
|
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) {
|
2022-02-24 10:41:50 +01:00
|
|
|
reject('error starting server (' + err + ')');
|
2021-04-06 16:41:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:41:50 +01:00
|
|
|
function handleRequests() {
|
2022-02-23 17:08:41 +01:00
|
|
|
// 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'
|
2022-03-01 22:14:36 +01:00
|
|
|
app.post('/' + constants.MODE_SET, (request, response) => {
|
2022-02-21 00:48:29 +01:00
|
|
|
logger.http(request);
|
2022-03-01 22:14:36 +01:00
|
|
|
handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_SET), response);
|
2022-02-21 00:48:29 +01:00
|
|
|
});
|
2022-02-23 17:08:41 +01:00
|
|
|
// POST '/morph'
|
2022-03-01 22:14:36 +01:00
|
|
|
app.post('/' + constants.MODE_MORPH, (request, response) => {
|
2022-02-21 00:48:29 +01:00
|
|
|
logger.http(request);
|
2022-03-01 22:14:36 +01:00
|
|
|
handleSimpleAnimation(parser.parseRequest(request.body, constants.MODE_MORPH), response);
|
2021-04-06 16:41:49 +02:00
|
|
|
});
|
2022-02-23 17:08:41 +01:00
|
|
|
// POST '/blink'
|
2022-03-01 22:14:36 +01:00
|
|
|
app.post('/' + constants.MODE_BLINK, (request, response) => {
|
2022-02-23 17:08:41 +01:00
|
|
|
logger.http(request);
|
2022-03-01 22:14:36 +01:00
|
|
|
handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_BLINK), response);
|
2022-02-23 17:08:41 +01:00
|
|
|
});
|
|
|
|
// POST '/pulse'
|
2022-03-01 22:14:36 +01:00
|
|
|
app.post('/' + constants.MODE_PULSE, (request, response) => {
|
2022-02-23 17:08:41 +01:00
|
|
|
logger.http(request);
|
2022-03-01 22:14:36 +01:00
|
|
|
handleComplexAnimation(parser.parseRequest(request.body, constants.MODE_PULSE), response);
|
2022-02-23 17:08:41 +01:00
|
|
|
});
|
|
|
|
// POST '/poweroff'
|
2022-03-01 22:14:36 +01:00
|
|
|
app.post('/' + constants.MODE_POWEROFF, (request, response) => {
|
2022-02-23 17:08:41 +01:00
|
|
|
logger.http(request);
|
2022-03-01 22:14:36 +01:00
|
|
|
handlePowerOff(parser.parseRequest(request.body, constants.MODE_POWEROFF), response);
|
2022-02-23 17:08:41 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-24 11:33:53 +01:00
|
|
|
async function handleSimpleAnimation(config, response) {
|
|
|
|
try {
|
2022-03-03 20:59:12 +01:00
|
|
|
response.end(JSON.stringify(await controller.simple(config)));
|
2022-02-24 11:33:53 +01:00
|
|
|
} catch (err) {
|
2022-03-04 00:02:07 +01:00
|
|
|
if (err.message.includes('feature report')) {
|
|
|
|
await controller.resetBlinksticks();
|
|
|
|
}
|
2022-02-24 11:51:21 +01:00
|
|
|
logger.error(err);
|
2022-02-24 11:33:53 +01:00
|
|
|
response.status(500);
|
2022-03-04 00:02:07 +01:00
|
|
|
response.end(JSON.stringify({ status: 'error', error: err.message }));
|
2022-02-24 11:33:53 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-23 17:08:41 +01:00
|
|
|
|
2022-02-24 11:33:53 +01:00
|
|
|
async function handleComplexAnimation(config, response) {
|
2022-03-03 20:59:12 +01:00
|
|
|
if (controller.isInfiniteAnimation(config)) {
|
2022-03-04 00:02:07 +01:00
|
|
|
response.end(JSON.stringify({ status: 'ok', time: 'infinite' }));
|
2022-02-24 11:33:53 +01:00
|
|
|
response = undefined;
|
|
|
|
}
|
|
|
|
try {
|
2022-03-03 20:59:12 +01:00
|
|
|
const result = await controller.complex(config);
|
2022-02-24 11:33:53 +01:00
|
|
|
if (response === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
response.end(JSON.stringify(result));
|
|
|
|
} catch (err) {
|
|
|
|
if (response === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-24 11:51:21 +01:00
|
|
|
logger.error(err);
|
|
|
|
response.status(500);
|
2022-03-04 00:02:07 +01:00
|
|
|
response.end(JSON.stringify({ status: 'error', error: err.message }));
|
2022-02-24 11:51:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handlePowerOff(config, response) {
|
|
|
|
try {
|
2022-03-07 23:19:18 +01:00
|
|
|
if (config.blinkstick === undefined) {
|
|
|
|
config.blinkstick = constants.ALL;
|
|
|
|
}
|
2022-03-03 20:59:12 +01:00
|
|
|
response.end(JSON.stringify(await controller.powerOff(config)));
|
2022-02-24 11:51:21 +01:00
|
|
|
} catch (err) {
|
2022-02-24 11:33:53 +01:00
|
|
|
response.status(500);
|
2022-03-04 00:02:07 +01:00
|
|
|
response.end(JSON.stringify({ status: 'error', error: err.message }));
|
2022-02-24 11:33:53 +01:00
|
|
|
}
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// exports
|
|
|
|
module.exports = {
|
|
|
|
start,
|
|
|
|
handleRequests
|
|
|
|
};
|