-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiscovergyMetersList.js
40 lines (33 loc) · 1.32 KB
/
DiscovergyMetersList.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
module.exports = function(RED) {
const axios = require("axios");
function metersNode(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', async function(msg) {
if((typeof node.context().global.get("discovergy_username") !== 'undefined')&&( node.context().global.get("discovergy_username") !== null)) {
config.username = node.context().global.get("discovergy_username");
config.password = node.context().global.get("discovergy_password");
}
if(typeof msg.payload.username !== 'undefined') {
config.username = msg.payload.username;
config.password = msg.payload.password;
}
let meters = await axios.get("https://api.discovergy.com/public/v1/meters",{
auth: {
username: config.username,
password: config.password
}});
meters = meters.data;
let options = [];
for(let i=0;i<meters.length;i++) {
let option = {};
option[""+meters[i].fullSerialNumber] = meters[i].meterId;
options.push(option);
}
msg.options = options;
msg.payload = options;
node.send(msg);
});
}
RED.nodes.registerType("Available Meters",metersNode);
};