-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.js
174 lines (146 loc) · 5.34 KB
/
api.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const fetch = require('node-fetch');
const baseUrl = 'https://intl.fusionsolar.huawei.com/thirdData';
let token;
class LunaApi {
constructor(username, password) {
this.username = username;
this.password = password;
}
async initializeSession() {
const accountUrl = `${baseUrl}/login`;
const requestBody = JSON.stringify({
"userName": this.username,
"systemCode": this.password
});
const apiData = await this.apiRequest(accountUrl, 'POST', requestBody);
return apiData;
}
async apiRequest(url, methodContent, requestBody) {
const apiResponse = await fetch(url, {
method: methodContent,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: requestBody,
});
const apiData = await apiResponse;
const bodyData = await apiData.text();
token = apiData.headers.get('xsrf-token');
if (apiData.statusText === 'OK' && bodyData.length > 0) {
return true;
} else {
return false;
}
}
async getSystems() {
const systemsUrl = `${baseUrl}/getStationList`;
const response = await fetch(systemsUrl, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
"XSRF-TOKEN": token,
}
});
const apiData = await response.json();
return apiData.data;
}
async getBasicStats(stationCode) {
const systemsUrl = `${baseUrl}/getStationRealKpi`;
let bodyData = JSON.stringify({
"stationCodes": stationCode
});
const response = await fetch(systemsUrl, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
"XSRF-TOKEN": token,
},
body: bodyData
});
const apiData = await response.json();
if (apiData.data !== null && apiData.success) {
return apiData.data[0].dataItemMap;
} else {
return null;
}
}
async getDevList(stationCode) {
const systemsUrl = `${baseUrl}/getDevList`;
let battery = "";
let inverter = "";
let powerSensor = "";
let bodyData = JSON.stringify({
"stationCodes": stationCode
});
const response = await fetch(systemsUrl, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
"XSRF-TOKEN": token,
},
body: bodyData
});
const apiData = await response.json();
// console.log('apiData getDevList values: ', apiData);
if (apiData.success) {
try {
for (let index = 0; index < apiData.data.length; index++) {
if (apiData.data[index]["devName"] !== null && apiData.data[index]["devName"].includes('Battery')) {
battery = apiData.data[index];
}
if (apiData.data[index]["devName"] !== null && apiData.data[index]["devName"].includes('meter')) {
powerSensor = apiData.data[index];
}
if (apiData.data[index]["devName"] !== null && apiData.data[index]["devName"].includes('Power Sensor')) {
powerSensor = apiData.data[index];
}
if ((apiData.data[index]["devName"] !== null && apiData.data[index]["invType"] !== null) && apiData.data[index]["invType"].includes("SUN2000-")) {
inverter = apiData.data[index];
}
}
} catch (error) {
console.log('getDevListData failed with: ', error);
}
} else {
console.log('devListData failed: ', apiData.data);
}
return { battery, inverter, powerSensor };
}
async getDevRealKpi(devIds, devTypeId, server) {
const systemsUrl = `https://${server}.fusionsolar.huawei.com:31942/thirdData/getDevRealKpi`;
let bodyData = JSON.stringify({
"devIds": devIds,
"devTypeId": devTypeId
});
const response = await fetch(systemsUrl, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
"XSRF-TOKEN": token,
},
body: bodyData
});
try {
const apiData = await response.json();
// console.log('apiData getDevRealKpi values: ', apiData);
if (apiData.errorCode !== "undefined" && apiData.success) {
if (apiData.data !== 'undefined') {
return apiData.data[0].dataItemMap;;
} else {
return null;
}
}
else {
return null;
}
} catch (error) {
console.log('getDevRealKpi failed with: ', error);
}
}
}
module.exports = { LunaApi };