-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.js
executable file
·43 lines (38 loc) · 1.48 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const SprutHubHelper = require('./lib/SprutHubHelper.js');
var NODE_PATH = '/spruthub/';
module.exports = function(RED) {
RED.httpAdmin.get(NODE_PATH + 'static/*', function (req, res) {
var options = {
root: __dirname + '/static/',
dotfiles: 'deny'
};
res.sendFile(req.params[0], options);
});
RED.httpAdmin.get(NODE_PATH + 'checkConnection', function (req, res) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
if (controller && controller.constructor.name === "ServerNode") {
controller.checkConnection(config).then(function(response){
res.json(response);
}).catch(error => {
res.json({error:error});
});
} else {
res.status(404).end();
}
});
RED.httpAdmin.get(NODE_PATH + 'getDevices', function (req, res) {
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
var force = config.forceRefresh ? ['1', 'yes', 'true'].includes(config.forceRefresh.toLowerCase()) : false;
if (controller && controller.constructor.name === "ServerNode") {
controller.getAccessories(force).then(function(response){
res.json(SprutHubHelper.convertDevicesData(response));
}).catch(error => {
res.json(error);
});
} else {
res.status(404).end();
}
});
}