Update main.js
This commit is contained in:
parent
61d8435261
commit
9305b4aad3
143
main.js
143
main.js
@ -36,6 +36,17 @@
|
||||
// you have to require the utils module and call adapter function
|
||||
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
|
||||
|
||||
// load additional libraries
|
||||
var express = require('express'); // call express
|
||||
var request = null; // will be initialized later if polling enabled
|
||||
|
||||
// REST server
|
||||
var webServer = null;
|
||||
var app = null;
|
||||
var router = null;
|
||||
var timer = null;
|
||||
|
||||
|
||||
// you have to call the adapter function and pass a options object
|
||||
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
|
||||
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.doorgate.0
|
||||
@ -92,6 +103,138 @@ adapter.on('ready', function () {
|
||||
main();
|
||||
});
|
||||
|
||||
|
||||
function addRoutes(_router) {
|
||||
// test route to make sure everything is working (accessed at GET http://localhost:9090/api)
|
||||
_router.get('/', function (req, res) {
|
||||
res.json({message: 'Welcome to our JSON REST api!'});
|
||||
});
|
||||
|
||||
_router.get('/plain/', function(req, res) {
|
||||
res.send('Welcome to our text REST api!');
|
||||
});
|
||||
|
||||
// on routes that end in /plain/:id
|
||||
// ----------------------------------------------------
|
||||
_router.route('/plain/:id')
|
||||
// get the bear with that id (accessed at GET http://localhost:8080/api/plain/:id)
|
||||
.get(function (req, res) {
|
||||
adapter.getForeignState(req.params.id, {user: req.user}, function (err, state) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
} else if (!state) {
|
||||
res.status(500).send('not found');
|
||||
} else {
|
||||
res.send('Value: ' + state.val);
|
||||
}
|
||||
});
|
||||
})// create a handler for post (accessed at POST http://localhost:8080/api/bears)
|
||||
.post(function (req, res) {
|
||||
adapter.setForeignState(req.params.id, req.body, {user: req.user}, function (err, state) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
} else if (!state) {
|
||||
res.status(500).send('not found');
|
||||
} else {
|
||||
res.send('Value used');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// handler for get over JSON
|
||||
_router.route('/:id')
|
||||
// get the bear with that id (accessed at GET http://localhost:8080/api/plain/:id)
|
||||
.get(function (req, res) {
|
||||
adapter.getForeignState(req.params.id, {user: req.user}, function (err, state) {
|
||||
if (err) {
|
||||
res.status(500).send({error: err});
|
||||
} else {
|
||||
res.json(state);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initWebServer(settings) {
|
||||
app = express();
|
||||
router = express.Router();
|
||||
|
||||
// install authentication
|
||||
app.get('/', function (req, res) {
|
||||
if (settings.auth) {
|
||||
var b64auth = (req.headers.authorization || '').split(' ')[1] || '';
|
||||
var loginPass = new Buffer(b64auth, 'base64').toString().split(':');
|
||||
var login = loginPass[0];
|
||||
var password = loginPass[1];
|
||||
|
||||
// Check in yunkong2 user and password
|
||||
adapter.checkPassword(login, password, function (result) {
|
||||
if (!result) {
|
||||
adapter.log.error('Wrong user or password: ' + login);
|
||||
res.set('WWW-Authenticate', 'Basic realm="nope"');
|
||||
res.status(401).send('You shall not pass.');
|
||||
} else {
|
||||
req.user = login;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
req.user = settings.defaultUser;
|
||||
}
|
||||
});
|
||||
|
||||
// add route cases
|
||||
addRoutes(router);
|
||||
|
||||
// REGISTER OUR ROUTES -------------------------------
|
||||
// all of our routes will be prefixed with /api
|
||||
app.use('/api', router);
|
||||
|
||||
if (settings.port) {
|
||||
if (settings.secure) {
|
||||
if (!adapter.config.certificates) {
|
||||
adapter.log.error('certificates missing');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
webServer = LE.createServer(app, adapter.config, adapter.config.certificates, adapter.config.leConfig, adapter.log);
|
||||
|
||||
adapter.getPort(settings.port, function (port) {
|
||||
if (port != settings.port && !adapter.config.findNextPort) {
|
||||
adapter.log.error('port ' + settings.port + ' already in use');
|
||||
process.exit(1);
|
||||
}
|
||||
webServer.listen(port, settings.bind, function() {
|
||||
adapter.log.info('Server listening on http' + (settings.secure ? 's' : '') + '://' + settings.bind + ':' + port);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
adapter.log.error('port missing');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function pollData() {
|
||||
// you can read about module "request" here: https://www.npmjs.com/package/request
|
||||
request = request || require('request'); // load library
|
||||
request(adapter.config.pollURL, function (error, response, body) {
|
||||
if (error || response.statusCode !== 200) {
|
||||
adapter.log.error(error || response.statusCode);
|
||||
} else {
|
||||
// try to parse answer
|
||||
try {
|
||||
var data = JSON.parse(body);
|
||||
// do something with data
|
||||
adapter.log.info(JSON.parse(data));
|
||||
|
||||
} catch (e) {
|
||||
adapter.log.error('Cannot parse answer');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function main() {
|
||||
|
||||
// The adapters config (in the instance object everything under the attribute "native") is accessible via
|
||||
|
Loading…
Reference in New Issue
Block a user