-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
235 lines (211 loc) · 9.17 KB
/
index.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
var request = require('request');
var request = request.defaults({ jar: true });
var cheerio = require('cheerio');
module.exports = function (RED) {
function GetIDsalusit500(config) {
RED.nodes.createNode(this, config);
var postdata = {
url: 'https://salus-it500.com/public/#.php',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
form:
{
IDemail: this.credentials.username,
password: this.credentials.password,
login: 'Login'
}
}
var flowContext = this.context().flow;
var node = this;
node.on('input', function (msg) {
request.post(postdata, (err, response, body) => {
if (err) {
return console.error('[node-red-contrib-salus-it500] login failed:', err);
}
console.log('[node-red-contrib-salus-it500] Logged in')
request.get('https://salus-it500.com/public/devices.php', (err, getresponse, html) => {
var $ = cheerio.load(html)
var currentToken = $('#token').attr('value')
var devId = $('input[name="devId"]').attr('value')
flowContext.set('token', currentToken);
flowContext.set('devId', devId);
this.status({ fill: "green", shape: "dot", text: currentToken });
msg.payload = { token: currentToken, id: devId }
node.send(msg);
})
});
});
}
RED.nodes.registerType("Retrieve ID and Token", GetIDsalusit500, {
credentials: {
username: { type: "text" },
password: { type: "password" }
}
});
function SetTempsalusit500(config) {
RED.nodes.createNode(this, config);
var flowContext = this.context().flow;
var node = this;
node.on('input', function (msg) {
var cToken = flowContext.get('token') || 0;
var cdevId = flowContext.get('devId') || 0;
if (cToken == 0 || cdevId == 0) {
msg.payload = 'No token or device id found.';
node.send([null, msg])
} else {
var postdata = {
url: 'https://salus-it500.com/includes/set.php',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
form:
{
token: cToken,
tempUnit: 0,
devId: cdevId,
current_tempZ1_set: 1,
current_tempZ1: msg.payload
}
}
request.post(postdata, (err, response, body) => {
if (err) {
msg.payload = msg;
node.warn(msg);
return console.error('[node-red-contrib-salus-it500] Failed:', err);
}
var body2json = JSON.parse(body);
if (body2json.errorMsg) {
msg.payload = body2json.errorMsg;
node.send([null, msg]);
} else {
this.status({ fill: "green", shape: "dot", text: msg.payload });
flowContext.set("setTemp", msg.payload);
node.send([msg, null]);
}
});
}
});
}
RED.nodes.registerType("Set temperature", SetTempsalusit500);
function GetTempsalusit500(config) {
RED.nodes.createNode(this, config);
var flowContext = this.context().flow;
var node = this;
node.on('input', function (msg) {
var cToken = flowContext.get('token') || 0;
var cdevId = flowContext.get('devId') || 0;
if (cToken == 0 || cdevId == 0) {
msg.payload = 'No token or device id found.';
node.send([null, msg])
} else {
var endpoint = 'https://salus-it500.com/public/ajax_device_values.php?devId=' + cdevId + '&token=' + cToken
request.get(endpoint, (err, response, body) => {
if (err) {
return console.error('[node-red-contrib-salus-it500] Failed:', err);
}
var body2json = JSON.parse(body);
if (body2json.frost == 32) {
node.send([null, msg])
} else {
var currentTemp = parseFloat(body2json.CH1currentRoomTemp);
var setTemp = parseFloat(body2json.CH1currentSetPoint);
var auto = parseInt(body2json.CH1autoMode) == 0;
var salusheatstatus = parseInt(body2json.CH1heatOnOffStatus);
this.status({ fill: "green", shape: "dot", text: currentTemp });
msg.payload = { Current: currentTemp, Set: setTemp, HeatStatus: salusheatstatus };
flowContext.set("currentTemp", currentTemp);
flowContext.set("setTemp", setTemp);
flowContext.set("auto", auto);
flowContext.set("heatStatus", salusheatstatus);
node.send([msg, null]);
}
});
}
});
}
RED.nodes.registerType("Current temperature", GetTempsalusit500);
function SetAutosalusit500(config) {
RED.nodes.createNode(this, config);
var flowContext = this.context().flow;
var node = this;
node.on('input', function (msg) {
var cToken = flowContext.get('token') || 0;
var cdevId = flowContext.get('devId') || 0;
var desiredState;
if (cToken == 0 || cdevId == 0) {
msg.payload = 'No token or device id found.';
node.send([null, msg])
} else {
if (msg.payload === "AUTO" || msg.payload === true){
desiredState = 0
} else if (msg.payload === "OFF" || msg.payload === false){
desiredState = 1
} else {
desiredState = 0
}
var postdata = {
url: 'https://salus-it500.com/includes/set.php',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
form:
{
token: cToken,
devId: cdevId,
auto: desiredState,
auto_setZ1: 1
}
}
request.post(postdata, (err, response, body) => {
if (err) {
msg.payload = msg;
node.warn(msg);
return console.error('[node-red-contrib-salus-it500] Failed:', err);
}
var body2json = JSON.parse(body);
if (body2json.errorMsg) {
msg.payload = body2json.errorMsg;
node.send([null, msg]);
} else {
this.status({ fill: "green", shape: "dot", text: msg.payload });
flowContext.set("auto", desiredState == 0);
node.send([msg, null]);
}
});
}
});
}
RED.nodes.registerType("Set Auto", SetAutosalusit500);
function GetHeatStatussalusit500(config) {
RED.nodes.createNode(this, config);
var flowContext = this.context().flow;
var node = this;
node.on('input', function (msg) {
var cToken = flowContext.get('token') || 0;
var cdevId = flowContext.get('devId') || 0;
if (cToken == 0 || cdevId == 0) {
msg.payload = 'No token or device id found.';
node.send([null, msg])
} else {
var endpoint = 'https://salus-it500.com/public/ajax_device_values.php?devId=' + cdevId + '&token=' + cToken
request.get(endpoint, (err, response, body) => {
if (err) {
return console.error('[node-red-contrib-salus-it500] Failed:', err);
}
var body2json = JSON.parse(body);
if (body2json.frost == 32) {
node.send([null, msg])
} else {
var salusheatstatus = parseInt(body2json.CH1heatOnOffStatus);
this.status({ fill: "green", shape: "dot", text: salusheatstatus });
msg.payload = { Current: salusheatstatus };
flowContext.set("heatStatus", salusheatstatus);
node.send([msg, null]);
}
});
}
});
}
RED.nodes.registerType("Heat Status", GetHeatStatussalusit500);
}