-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
323 lines (287 loc) · 11.5 KB
/
main.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
// Load your modules here, e.g.:
// const fs = require("fs");
const axios = require('axios');
/**
* The adapter instance
* @type {ioBroker.Adapter}
*/
// current poll period: 5min (300000ms)
const UPDATE_PERIOD=300000;
let adapter;
let authToken='';
let gw='';
let currentComfortTemp=0;
let boilerInterval;
// Update state variable
function updateState(StateID, value) {
adapter.getState(StateID, function(err,state) {
// only update state upon change
if (state==null || state.val!=value) {
adapter.setStateAsync(StateID, {val: value, ack: true});
}
});
}
// Main loop, fetch data from boiler
function updateBoiler() {
adapter.log.debug('updateBoiler()');
if (authToken=='') {
adapter.log.debug('Not logged in yet, attempting login');
loginBoiler();
return;
}
if (gw=='') {
adapter.log.debug('Gateway not set yet, waiting...');
return;
}
const url='https://www.ariston-net.remotethermo.com/api/v2/velis/slpPlantData/' +
gw + '?umSys=si';
axios.get(url, {
headers: {
'Ar.authtoken': authToken
}
}).then(function (response) {
if (response.status==200) {
adapter.log.debug('Got waterTemp from ariston-net: ' + response.data.waterTemp);
// update state with values received from API
updateState('ariston-remotethermo.0.boiler.waterTemp', response.data.waterTemp);
updateState('ariston-remotethermo.0.boiler.comfortTemp', response.data.comfortTemp);
updateState('ariston-remotethermo.0.boiler.mode', response.data.mode);
updateState('ariston-remotethermo.0.boiler.opMode', response.data.opMode);
updateState('ariston-remotethermo.0.boiler.boostOn', response.data.boostOn);
updateState('ariston-remotethermo.0.boiler.hpState', response.data.hpState);
updateState('ariston-remotethermo.0.boiler.on', response.data.on);
} else if (response.status==405) {
adapter.log.info('updateBoiler(): Authentication expired, re-login');
loginBoiler();
} else {
adapter.log.error('updateBoiler(): Unexpected HTTP response code: ' + response.status);
}
})
.catch(function (error) {
if (error.response.status==429) {
const secs=error.response.data.match(/\d+/)[0];
adapter.log.info(`updateBoiler(): Got throttled ${secs} seconds`);
adapter.log.debug(`updateBoiler(): Rescheduling updateBoiler ${secs} seconds`);
setTimeout(() => { updateBoiler(); },(parseInt(secs)+1)*1000);
} else if (error.response.status==405) {
adapter.log.info('updateBoiler(): Authentication expired, re-login 2');
loginBoiler();
} else {
adapter.log.error('updateBoiler(): Unexpected HTTP response code: ' + error.response.status);
}
});
}
function getGateway() {
adapter.log.debug('getGateway()');
axios.get('https://www.ariston-net.remotethermo.com/api/v2/velis/plants', {
headers: {
'Ar.authtoken': authToken
}
}).then(function (response) {
if (response.status==200) {
gw=response.data[0].gw;
adapter.log.info(`Got GatewayID: ${gw}`);
adapter.setStateAsync('ariston-remotethermo.0.boiler.gw',
{val: response.data[0].gw, ack: true});
// everything's ready now, fetch status from API
updateBoiler();
} else {
adapter.log.error('getGateway(): Unexpected HTTP return code ' + response.status);
}
}).catch(function (error) {
if (error.response.status==429) {
const secs=error.response.data.match(/\d+/)[0];
adapter.log.info(`getGateway(): Got throttled ${secs} seconds`);
adapter.log.debug(`Rescheduling getGateway ${secs} seconds`);
setTimeout(() => { getGateway(); },(parseInt(secs)+1)*1000);
} else {
adapter.log.error('getGateway(): Unexpected HTTP return code ' + error.response.status);
}
});
}
function updateComfortTemp(newTemp) {
if (gw=='') return;
const update={
'new': {
'comfort': newTemp,
'reduced': 0.0
},
'old': {
'comfort': currentComfortTemp,
'reduced': 0.0
}
};
const url='https://www.ariston-net.remotethermo.com/api/v2/velis/slpPlantData/' +
gw + '/temperatures?umSys=si';
axios.post(url, update, {headers: {'Ar.authtoken': authToken}})
.then(function (response) {
if (response.status==200) {
adapter.log.info('Update comfortTemp successful');
adapter.setStateAsync('ariston-remotethermo.0.boiler.comfortTemp',
{ack: true});
currentComfortTemp=newTemp;
}
})
.catch(function (error) {
if (error.response.status==429) {
const secs=error.response.data.match(/\d+/)[0];
adapter.log.info(`updateComfortTemp(): Got throttled ${secs} seconds`);
adapter.log.debug(`updateComfortTemp(): Rescheduling ${secs} seconds`);
setTimeout(() => { updateComfortTemp(newTemp);},(parseInt(secs)+1) * 1000);
} else {
adapter.log.error('updateComfortTemp(): Unexpected HTTP return code ' + error.response.status);
}
});
}
function onOff(target) {
if (gw=='') return;
adapter.log.debug('OnOff: ' + target.toString());
const url='https://www.ariston-net.remotethermo.com/api/v2/velis/slpPlantData/' +
gw + '/switch';
axios.post(url, target.toString(), {headers: {'Ar.authtoken': authToken, 'Content-Type': 'application/json'}})
.then(function (response) {
if (response.status==200) {
if (target) {
adapter.log.info('Boiler switched on');
} else {
adapter.log.info('Boiler switched off');
adapter.setStateAsync('ariston-remotethermo.0.boiler.on',
{ack: true});
}
}
})
.catch(function (error) {
if (error.response.status==429) {
const secs=error.response.data.match(/\d+/)[0];
adapter.log.info(`onOff(): Got throttled ${secs} seconds`);
adapter.log.debug(`onOff(): Rescheduling ${secs} seconds`);
setTimeout(() => { onOff(target); },(parseInt(secs)+1) * 1000);
} else {
adapter.log.error('onOff(): Unexpected HTTP return code ' + error.response.status);
}
});
}
function boost(target) {
if (gw=='') return;
let state='false';
if (target==true) state='true';
const url='https://www.ariston-net.remotethermo.com/api/v2/velis/slpPlantData/' +
gw + '/boost';
axios.post(url, state, {headers: {'Ar.authtoken': authToken}})
.then(function (response) {
if (response.status==200) {
if (target)
adapter.log.info('Boiler boost on');
else
adapter.log.info('Boiler boost off');
adapter.setStateAsync('ariston-remotethermo.0.boiler.boostOn',
{ack: true});
}
})
.catch(function (error) {
if (error.response.status==429) {
const secs=error.response.data.match(/\d+/)[0];
adapter.log.info(`boost(): Got throttled ${secs} seconds`);
adapter.log.debug(`boost(): Rescheduling ${secs} seconds`);
setTimeout(() => {boost(target);},(parseInt(secs)+1) * 1000);
} else {
adapter.log.error('boost(): Unexpected HTTP return code ' + error.response.status);
}
});
}
function loginBoiler() {
if (adapter.config.password=='' || adapter.config.email=='') {
adapter.log.error('Login Data missing...');
return;
}
axios.post('https://www.ariston-net.remotethermo.com/api/v2/accounts/login', {
usr: adapter.config.email,
pwd: adapter.config.password,
imp: false,
notTrack: true,
appInfo: {
os: 2,
appVer: '5.6.772.40151',
appId: 'com.remotethermo.aristonnet'
}
})
.then(function (response) {
if (response.status==200 && 'token' in response.data) {
adapter.log.info('Login Successful, got token');
authToken=response.data.token;
// now fetch gateway ID
getGateway();
}
})
.catch(function (error) {
adapter.log.error('boost(): Unexpected HTTP return code ' + error.response.status);
});
}
/**
* Starts the adapter instance
* @param {Partial<utils.AdapterOptions>} [options]
*/
function startAdapter(options) {
// Create the adapter and define its methods
return adapter = utils.adapter(Object.assign({}, options, {
name: 'ariston-remotethermo',
// The ready callback is called when databases are connected and adapter received configuration.
// start here!
ready: main, // Main method defined below for readability
// is called when adapter shuts down - callback has to be called under any circumstances!
unload: (callback) => {
try {
// Here you must clear all timeouts or intervals that may still be active
clearInterval(boilerInterval);
callback();
} catch (e) {
adapter.log && adapter.log.warn('[END 7 catch] adapter stopped ' + e);
callback();
}
},
// is called if a subscribed state changes
stateChange: (id, state) => {
if (state) {
// The state was changed
adapter.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
if (id == 'ariston-remotethermo.0.boiler.comfortTemp' && state.ack == false) {
adapter.log.info(`New comfort temp requested: ${state.val}`);
updateComfortTemp(state.val);
}
if (id == 'ariston-remotethermo.0.boiler.on' && state.ack == false) {
onOff(state.val);
}
if (id == 'ariston-remotethermo.0.boiler.boostOn' && state.ack == false) {
boost(state.val);
}
} else {
// The state was deleted
adapter.log.info(`state ${id} deleted`);
}
},
}));
}
async function main() {
// subscribe changes to the following states
adapter.subscribeStates('ariston-remotethermo.0.boiler.comfortTemp');
adapter.subscribeStates('ariston-remotethermo.0.boiler.on');
adapter.subscribeStates('ariston-remotethermo.0.boiler.boostOn');
// trigger initial update before scheduling the periodic one
updateBoiler();
//
boilerInterval=setInterval(updateBoiler,UPDATE_PERIOD);
}
if (require.main !== module) {
// Export startAdapter in compact mode
module.exports = startAdapter;
} else {
// otherwise start the instance directly
startAdapter();
}