// 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'); const favicon = require('serve-favicon'); 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 = "" + "" + "" + packageJSON.name + " " + packageJSON.version + "" + "" + "" + "" + "
" + "

" + packageJSON.name + " " + packageJSON.version + "

" + "
" + "
"; welcomeMessage += "
" + "

get:

" + "

" + config.api.get.description + "

" + "
"; welcomeMessage += "
" + "

post:

" + "" + "" + "" + "" + "" + ""; Object.keys(config.api.post).forEach(function (argument) { let restrictions = config.api.post[argument].restrictions || ""; welcomeMessage += "" + "" + "" + "" + "" + "" + ""; }); welcomeMessage += "
argumentavailable valuesdefaultrestrictionsdescription
" + argument + "" + config.api.post[argument].available + "" + config.api.post[argument].default + "" + restrictions + "" + config.api.post[argument].description + "
" + "
" + "" + ""; 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 app.get('*', (request, response) => { logger.http(request); response.send(getHTML()); response.end(); }); app.post('/off', (request, response) => { logger.http(request); response.end(); blinkstick.powerOff(); }); // POST methods app.post('*', (request, response) => { logger.http(request); response.end(); blinkstick.illuminate(parseRequest(request.body)) .then(logger.info) .catch(logger.error); }); } // parse the request and return an object with sane defaults function parseRequest(data) { let blinkstickConfig = { "id": Math.random(), "mode": data.mode || config.api.post.mode.default, "color": blinkstick.parseColor(data.color), "options": { "steps": data.steps, "duration": data.duration || config.api.post.duration.default, "pulse": { "max": data.pulses || 0, "done": 0 } } }; if (data.index !== undefined) { 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; } } if (blinkstickConfig.options.duration < 100) { blinkstickConfig.options.duration = 100; } if (blinkstickConfig.options.steps === undefined || blinkstickConfig.options.steps === 0) { blinkstickConfig.options.steps = blinkstickConfig.options.duration / 10; } return blinkstickConfig; } // exports module.exports = { start, handleRequests };