forked from mapero/node-red-contrib-apcaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apcrequest.js
60 lines (52 loc) · 1.7 KB
/
apcrequest.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = function(RED) {
"use strict";
var ApcAccess = require('apcaccess');
var validCmds = [
"status",
"events",
"statusJson"
];
function ApcRequest(n) {
RED.nodes.createNode(this,n);
var node = this;
node.name = n.name;
node.server = RED.nodes.getNode(n.server);
node.server.on('status', function(status) {
node.status(status);
});
node.on('input', function(msg) {
if (validCmds.indexOf(msg.payload) > -1 && node.server.client.isConnected) {
switch(msg.payload) {
case 'status':
node.server.client.getStatus()
.then(function(result) {
msg.payload = result;
node.send(msg);
});
break;
case 'statusJson':
node.server.client.getStatusJson()
.then(function(result) {
msg.payload = result;
node.send(msg);
});
break;
case 'events':
node.server.client.getEvents()
.then(function(result) {
msg.payload = result;
node.send(msg);
});
break;
}
} else if (!node.server.client.isConnected) {
node.warn('Client is not connected to server. Check the configuration.');
} else {
node.warn('Invalid Command. Use: status, statusJson or events');
}
});
node.on("close", function() {
});
}
RED.nodes.registerType("apcrequest",ApcRequest);
};