const bodyParser = require('body-parser');
const express = require('express');
const app = express();
/**
* The web server module.
*
* @module Server
*/
module.exports = (device) => {
const sp = device.ip.split(':');
let port;
if (sp.length >= 0) {
port = sp[sp.length - 1];
} else {
port = '80'
console.log("Port not defined in file, using default as 80");
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function (req, res) {
res.send(device)
})
app.post('/action', function (req, res) {
res.send(device.executeAction(req.body.label, req.body.input));
})
app.get('/action', function (req, res) {
res.send(device.device_actions)
})
device.measurements.forEach((measurement) => {
setInterval(measurement.callback, measurement.interval);
});
app.listen(port, function () {
console.log(`Hive Device Web Server started on port: ${port}!`)
})
};