-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecoflow-api-node.js
66 lines (55 loc) · 2.1 KB
/
ecoflow-api-node.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
module.exports = function(RED) {
function queryEcoflowApi(config) {
RED.nodes.createNode(this,config);
let node = this;
let fn = config.function;
let sn = config.serial_number;
if (fn == "msg") {
fn = null;
}
let server = RED.nodes.getNode(config.remote);
node.on('input', async function(msg, send, done) {
let func = fn ? fn : msg.function;
let serialNumber = sn ? sn : msg.sn ? msg.sn : msg.payload;
let quotaTypes = msg.payload;
let data, error;
switch(func) {
case 'queryQuotaAll':
[data,error] = await server.queryQuotaAll(serialNumber);
break;
case 'deviceList':
[data,error] = await server.queryDeviceList();
break;
case 'queryQuotaSelective':
node.debug(quotaTypes);
if (getType(quotaTypes) != 'Array') {
error = "msg.payload is not an array!";
} else {
[data,error] = await server.queryQuotaSelective(serialNumber,quotaTypes);
}
break;
case 'setQuota':
node.debug(quotaTypes);
if (getType(quotaTypes) != 'Object') {
error = "msg.payload is not an object!";
} else {
[data,error] = await server.setQuotaSelective(serialNumber,quotaTypes);
}
break;
case 'queryMqttCert':
[data,error] = await server.queryMqttCert();
break;
}
if (data) {
msg.payload = data;
send(msg);
done();
} else {
//send(msg);
done(error);
}
});
}
RED.nodes.registerType("ecoflow-api", queryEcoflowApi);
const getType = obj => Object.prototype.toString.call(obj).slice(8, -1);
}