blinky/libs/server.js

143 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-04-06 16:41:49 +02:00
// requirements
const path = require('path');
const config = require('../config');
const packageJSON = require('../package.json');
const logger = require('./logger');
const blinkstick = require('./blinkstick');
// third party requirements
const express = require('express');
2022-02-21 11:56:49 +01:00
const favicon = require('serve-favicon');
2021-04-06 16:41:49 +02:00
const parser = require('body-parser');
// setup express, blinkstick and other stuff
const app = express();
app.use(favicon(path.join(path.dirname(__dirname), "public", "favicon.ico")));
app.use(parser.json());
app.use(parser.urlencoded({ extended: true }));
// get the html content for get requests
// TODO: replace with template engine (vue.js)
function getHTML() {
let welcomeMessage =
"<html>" +
"<head>" +
"<title>" + packageJSON.name + " " + packageJSON.version + "</title>" +
"<style>" +
"body { background: #2a2a2a; color: #f1f1f1; margin: 1.25% }" +
"th, td { border: 1px solid #919191; padding: 6px; text-align: left }" +
"</style>" +
"</head>" +
"<body>" +
"<div>" +
"<h1>" + packageJSON.name + " " + packageJSON.version + "</h1>" +
"</div>" +
"<hr>";
welcomeMessage +=
"<div>" +
"<h2>get:</h2>" +
"<p>" + config.api.get.description + "</p>" +
"</div>";
welcomeMessage +=
"<div>" +
"<h2>post:</h2>" +
"<table style=\"width:100%\">" +
"<th>argument</th>" +
"<th>available values</th>" +
"<th>default</th>" +
2022-02-21 12:20:18 +01:00
"<th>restrictions</th>" +
2021-04-06 16:41:49 +02:00
"<th>description</th>";
Object.keys(config.api.post).forEach(function (argument) {
2022-02-21 12:20:18 +01:00
let restrictions = config.api.post[argument].restrictions || "";
2021-04-06 16:41:49 +02:00
welcomeMessage +=
"<tr>" +
"<td>" + argument + "</td>" +
"<td>" + config.api.post[argument].available + "</td>" +
"<td>" + config.api.post[argument].default + "</td>" +
2022-02-21 12:20:18 +01:00
"<td>" + restrictions + "</td>" +
2021-04-06 16:41:49 +02:00
"<td>" + config.api.post[argument].description + "</td>" +
"</tr>";
});
welcomeMessage +=
"</table>" +
"</div>" +
"</body>" +
2022-02-21 11:56:49 +01:00
"</html>";
2021-04-06 16:41:49 +02:00
return welcomeMessage;
}
// 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)
.on("listening", function () {
return resolve("server listening on " + config.server.listen + ":" + config.server.port + "...")
})
.on("error", function (err) {
return reject("error starting server (" + err + ")");
});
});
}
function handleRequests() {
// GET methods
2022-02-21 00:48:29 +01:00
app.get('*', (request, response) => {
logger.http(request);
2021-04-06 16:41:49 +02:00
response.send(getHTML());
response.end();
});
2022-02-21 00:48:29 +01:00
app.post('/off', (request, response) => {
logger.http(request);
response.end();
blinkstick.powerOff(parseInt(request.body.index));
2022-02-21 00:48:29 +01:00
});
2021-04-06 16:41:49 +02:00
// POST methods
2022-02-21 00:48:29 +01:00
app.post('*', (request, response) => {
logger.http(request);
response.end();
blinkstick.illuminate(parseRequest(request.body))
.then(logger.info)
.catch(logger.error);
2021-04-06 16:41:49 +02:00
});
}
// parse the request and return an object with sane defaults
function parseRequest(data) {
2021-04-07 12:03:07 +02:00
let blinkstickConfig = {
2021-04-06 16:41:49 +02:00
"id": Math.random(),
2022-02-21 00:48:29 +01:00
"mode": data.mode || config.api.post.mode.default,
"color": blinkstick.parseColor(data.color),
2021-04-06 16:41:49 +02:00
"options": {
"index": blinkstick.LEDS_ALL,
2022-02-21 00:48:29 +01:00
"steps": data.steps,
"duration": data.duration || config.api.post.duration.default,
2021-04-06 16:41:49 +02:00
"pulse": {
2022-02-22 17:00:30 +01:00
"done": 0,
"max": data.pulses || 0
2021-04-06 16:41:49 +02:00
}
}
};
2022-02-21 11:56:49 +01:00
if (data.index !== undefined) {
2022-02-21 01:14:42 +01:00
blinkstickConfig.options.index = parseInt(data.index);
if (blinkstickConfig.options.index < 0) {
blinkstickConfig.options.index = 0;
} else if (blinkstickConfig.options.index > 7) {
blinkstickConfig.options.index = 7;
}
2022-02-21 00:48:29 +01:00
}
2021-04-06 16:41:49 +02:00
if (blinkstickConfig.options.duration < 100) {
blinkstickConfig.options.duration = 100;
}
2022-02-22 17:00:30 +01:00
if (blinkstickConfig.options.index === blinkstick.LEDS_ALL && blinkstickConfig.mode === blinkstick.MODE_MORPH) {
blinkstickConfig.options.duration = blinkstickConfig.options.duration / 8;
}
2022-02-21 11:56:49 +01:00
if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) {
2022-02-21 00:48:29 +01:00
blinkstickConfig.options.steps = blinkstickConfig.options.duration / 10;
}
2021-04-06 16:41:49 +02:00
return blinkstickConfig;
}
// exports
module.exports = {
start,
handleRequests
};