-
Notifications
You must be signed in to change notification settings - Fork 1
/
ecoflow.js
90 lines (59 loc) · 1.99 KB
/
ecoflow.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Ecoflow.js
var https = require('https'),
urllib = require("url");
// constructor function for the Cat class
class Ecoflow {
constructor(param_serial_number, param_app_key, param_api_secret_key) {
this.serial_number = param_serial_number;
this.app_key = param_app_key;
this.secret_key = param_api_secret_key;
}
getDeviceInfo() {
var self=this;
return new Promise((resolve, reject) => {
// https://api.ecoflow.com/iot-service/open/api/device/queryDeviceQuota?sn=R331ZEB4ZE8Q0464
var url = "/iot-service/open/api/device/queryDeviceQuota?sn=" + this.serial_number;
var options = {
method: 'GET',
host: "api.ecoflow.com",
port: 443,
path: url,
headers: {
"Content-Type": "application/json",
"appKey": this.app_key,
"secretKey": this.secret_key
}
};
var msg = {};
var request = https.request(options, function(res) {
res.setEncoding('utf8');
msg.statusCode = res.statusCode;
msg.payload = "";
res.on("data", function(chunk) {
msg.payload += chunk;
});
res.on("end",function() {
try {
msg.payload = JSON.parse(msg.payload);
msg.error = "";
resolve(msg);
// cb_ok(context, msg);
}
catch(e) {
// cb_error(msg);
msg.error= e;
reject(msg);
}
});
});
request.on("error", function(err) {
msg.error = err;
reject(msg);
});
request.end();
})
}
}
module.exports = {
Ecoflow
};