-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecoflow-connect.js
73 lines (39 loc) · 1.52 KB
/
ecoflow-connect.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
61
62
63
64
65
66
67
68
69
70
71
72
73
var https = require('https'),
urllib = require("url");
const { Ecoflow } = require("./ecoflow.js");
module.exports = function(RED) {
function ecoflowCredentials(n) {
RED.nodes.createNode(this, n);
var node = this;
this.serial_number = n.serial_number;
this.app_key = n.app_key;
this.secret_key = n.secret_key;
}
RED.nodes.registerType("ecoflow-credential", ecoflowCredentials);
function ecoflowConnectNode(n) {
RED.nodes.createNode(this, n);
var node = this;
node.credentials = RED.nodes.getNode(n.server);
node.serial_number = node.credentials.serial_number;
node.app_key = node.credentials.app_key;
node.secret_key = node.credentials.secret_key;
function fetchData() {
var ecoflow = new Ecoflow(node.serial_number,node.app_key, node.secret_key);
ecoflow.getDeviceInfo()
.then(deviceInfo => {
node.send(deviceInfo);
node.status({});
}).catch(err => {
node.error(msg.error);
node.status({ fill: "red", shape: "dot", text: "error" });
return;
});
}
node.on("close", function(){
});
node.on("input", function(){
fetchData();
});
}
RED.nodes.registerType("ecoflow-connect", ecoflowConnectNode);
};