forked from rlindner/node-red-contrib-sma-webconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sma-webconnect.js
242 lines (207 loc) · 6.11 KB
/
sma-webconnect.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
module.exports = function (RED) {
const retry = require("requestretry");
const functions = require("./functions.js");
const parseResult = functions.parseResult;
const devicePresets = require('./device-presets/');
// read presets on init
devicePresets.readPresets();
var deviceConfigs = devicePresets.getConfigs();
var message = {};
function request(uri, body, callback) {
retry({
method: "POST",
uri,
body,
json: true,
strictSSL: false,
timeout: 1500,
maxAttempts: 3,
retryDelay: 100
}, callback);
}
function buildUrl(https, host, path) {
return (https ? "https" : "http") + "://" + host + path;
}
function login(node, callback) {
const url = buildUrl(node.use_tls, node.ip_address, "/dyn/login.json");
node.debug("requesting " + url);
request(
url,
{
right: node.right,
pass: node.credentials.password
}, (error, response, body) => {
node.debug("response to " + url + ": " + JSON.stringify(response));
var result;
if (error) {
node.error(error);
} else if (response && response.statusCode == 301 && response.headers && response.headers.location) {
if (response.headers.location.substring(0, 5) != response.request.uri.protocol) {
node.error("detected an redirect to HTTPS, please change your configuration to use HTTPS");
}
} else if (body) {
if (body.err) {
node.error(body);
} else if (body.result) {
result = body.result.sid;
node.sid = body.result.sid;
node.log("session created: " + node.ip_address + " : " + body.result.sid);
}
}
if (callback) {
callback(result);
}
}
);
}
function getValues(node, callback, onSessionTimeout) {
const url = buildUrl(node.use_tls, node.ip_address, "/dyn/getValues.json?sid=" + node.sid);
// set default message according to device type
if (node.use_custom_config) {
message = node.custom_config;
}
else {
message = eval(deviceConfigs[node.device_selection]);
}
const value_keys = Object.keys(message.values);
node.debug("requesting " + url);
request(
url,
{
"destDev": [],
"keys": value_keys
}, (error, response, body) => {
node.debug("response to " + url + ": " + JSON.stringify(response));
if (error) {
node.error(error);
} else if (body) {
if (body.err) {
if (body.err == "401") {
if (onSessionTimeout) {
onSessionTimeout();
}
} else {
node.error(body);
}
} else if (body.result) {
if (callback) {
const result = parseResult(body.result, message);
callback(result);
}
}
}
}
);
}
function getFreeSessionsCount(node, callback) {
const url = buildUrl(node.use_tls, node.ip_address, "/dyn/sessionCheck.json");
node.debug("requesting " + url);
request(
url,
{},
(error, response, body) => {
node.debug("response to " + url + ": " + JSON.stringify(response));
if (error) {
node.error(error);
} else if (body) {
if (body.result && body.result.cntFreeSess != null) {
if (callback) {
callback(body.result.cntFreeSess);
}
} else {
node.log(body);
}
}
}
);
}
function logout(node, callback) {
if (node.ip_address) {
const url = buildUrl(node.use_tls, node.ip_address, "/dyn/logout.json?sid=" + node.sid);
node.log(url);
request(
url,
{},
(error, response, body) => {
node.debug("response to " + url + ": " + JSON.stringify(response));
var result = false;
if (error) {
node.error(error);
} else if (body) {
if (body.result && body.result.login != null) {
if (body.result.isLogin == false) {
result = true;
node.log("session closed: " + node.sid);
node.sid = null;
}
} else {
node.log(body);
}
}
if (callback) {
callback(result);
}
}
);
}
}
function query(node, retries, completion) {
if (retries > 0) {
if (node.sid) {
getValues(node, (result) => {
getFreeSessionsCount(node, (count) => {
result.available_sessions = count;
completion(result);
})
}, () => {
login(node, (sid) => {
query(node, retries - 1, completion);
});
})
} else {
login(node, (sid) => {
query(node, retries - 1, completion);
});
}
}
}
function SMAWebconnectNode(config) {
RED.nodes.createNode(this, config);
this.ip_address = config.ip_address;
this.use_tls = config.use_tls;
this.right = config.right;
this.device_selection = config.device_selection;
this.custom_config = {};
this.use_custom_config = false;
this.sid = null;
// change device names to new format till refactoring has been done
/** REMOVE AFTER REFACTOR */
if (this.device_selection === 'sb_tripower') {
this.device_selection = 'sunny_tripower';
} else if (this.device_selection === 'sb_storage') {
this.device_selection = 'sunny_boy_storage'
}
/** END */
var node = this;
node.on("input", function(msg) {
if (msg.payload.hasOwnProperty('sma_config')) {
node.use_custom_config = true;
node.custom_config = msg.payload.sma_config;
}
query(node, 3, (result) => {
msg.payload = result;
node.send(msg);
});
});
node.on("close", function(done) {
logout(node, (result) => {
done();
});
});
}
RED.nodes.registerType("sma-webconnect", SMAWebconnectNode, {
credentials: {
password: { type: "password" }
}
});
};