forked from nischelwitzer/smartfritz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
smartfritz.js
577 lines (480 loc) · 16.4 KB
/
smartfritz.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/**
* smartFritz - Fritz goes smartHome
*
* AVM SmartHome nodeJS Control - for AVM Fritz!Box and Dect200 Devices
*
* @author Andreas Goetz <cpuidle@gmx.de>
*
* Forked from: https://github.com/nischelwitzer/smartfritz
* nischi - first version: July 2014
*
* based on: Node2Fritz by steffen.timm on 05.12.13 for Fritz!OS > 05.50
* and thk4711 (code https://github.com/pimatic/pimatic/issues/38)
* Documentation is at http://www.avm.de/de/Extern/files/session_id/AHA-HTTP-Interface.pdf
*/
/* jshint esversion: 6, -W079 */
var Promise = require('bluebird');
var request = require('request').defaults({ strictSSL: false }); // be less strict about SSL errors
var cheerio = require('cheerio');
var parser = require('xml2json-light');
var extend = require('extend');
/*
* Object-oriented API
*/
module.exports.Fritz = Fritz;
function Fritz(username, password, uri) {
this.sid = null;
this.username = username;
this.password = password;
this.options = { url: uri || 'http://fritz.box' };
}
Fritz.prototype = {
call: function(func) {
var originalSID = this.sid;
/* jshint laxbreak:true */
var promise = this.sid
? Promise.resolve(this.sid)
: module.exports.getSessionID(this.username, this.password, this.options);
// function arguments beyond func parameter
var args = Array.from(arguments).slice(1).concat(this.options);
return promise.then(function(sid) {
this.sid = sid;
return func.apply(null, [this.sid].concat(args)).catch(function(error) {
if (error.response && error.response.statusCode == 403) {
// this.sid has not been updated or is invalid - get a new SID
if (this.sid === null || this.sid === originalSID) {
this.sid = null;
return fritz.getSessionID(this.username, this.password, this.options).then(function(sid) {
// this session id is the most current one - so use it from now on
this.sid = sid;
return func.apply(null, [this.sid].concat(args));
}.bind(this));
}
// this.sid has already been updated during the func() call - assume this is a valid SID now
else {
return func.apply(null, [this.sid].concat(args));
}
}
throw error;
}.bind(this));
}.bind(this));
},
getSID: function() {
return this.sid;
},
getDeviceListInfo: function() {
return this.call(module.exports.getDeviceListInfo);
},
getDeviceList: function() {
return this.call(module.exports.getDeviceList);
},
getDevice: function(ain) {
return this.call(module.exports.getDevice, ain);
},
getTemperature: function(ain) {
return this.call(module.exports.getTemperature, ain);
},
getSwitchList: function() {
return this.call(module.exports.getSwitchList);
},
getSwitchState: function(ain) {
return this.call(module.exports.getSwitchState, ain);
},
setSwitchOn: function(ain) {
return this.call(module.exports.setSwitchOn, ain);
},
setSwitchOff: function(ain) {
return this.call(module.exports.setSwitchOff, ain);
},
getSwitchEnergy: function(ain) {
return this.call(module.exports.getSwitchEnergy, ain);
},
getSwitchPower: function(ain) {
return this.call(module.exports.getSwitchPower, ain);
},
getSwitchPresence: function(ain) {
return this.call(module.exports.getSwitchPresence, ain);
},
getSwitchName: function(ain) {
return this.call(module.exports.getSwitchName, ain);
},
getThermostatList: function() {
return this.call(module.exports.getThermostatList);
},
setTempTarget: function(ain, temp) {
return this.call(module.exports.setTempTarget, ain, temp);
},
getTempTarget: function(ain) {
return this.call(module.exports.getTempTarget, ain);
},
getTempNight: function(ain) {
return this.call(module.exports.getTempNight, ain);
},
getTempComfort: function(ain) {
return this.call(module.exports.getTempComfort, ain);
},
getBatteryCharge: function(ain) {
return this.call(module.exports.getBatteryCharge, ain);
},
getGuestWlan: function() {
return this.call(module.exports.getGuestWlan);
},
setGuestWlan: function(enable) {
return this.call(module.exports.setGuestWlan, enable);
},
};
/*
* Functional API
*/
var defaults = { url: 'http://fritz.box' };
/**
* Execute HTTP request that honors failed/invalid login
*/
function httpRequest(path, req, options)
{
return new Promise(function(resolve, reject) {
req = extend({}, defaults, req, options);
req.url += path;
request(req, function(error, response, body) {
if (error || !(/^2/.test('' + response.statusCode)) || /action=".?login.lua"/.test(body)) {
if (/action=".?login.lua"/.test(body)) {
// fake failed login if redirected to login page without HTTP 403
response.statusCode = 403;
}
reject({
error: error,
response: response,
options: req
});
}
else {
resolve(body.trim());
}
});
});
}
/**
* Execute Fritz API command for device specified by AIN
*/
function executeCommand(sid, command, ain, options, path)
{
path = path || '/webservices/homeautoswitch.lua?0=0';
if (sid)
path += '&sid=' + sid;
if (command)
path += '&switchcmd=' + command;
if (ain)
path += '&ain=' + ain;
return httpRequest(path, {}, options);
}
/**
* Parse guest WLAN form settings
*/
function parseHTML(html)
{
$ = cheerio.load(html);
var form = $('form');
var settings = {};
$('input', form).each(function(i, elem) {
var val;
var name = $(elem).attr('name');
if (!name) return;
switch ($(elem).attr('type')) {
case 'checkbox':
val = $(elem).attr('checked') == 'checked';
break;
default:
val = $(elem).val();
}
settings[name] = val;
});
$('select option[selected=selected]', form).each(function(i, elem) {
var val = $(elem).val();
var name = $(elem).parent().attr('name');
settings[name] = val;
});
return settings;
}
/*
* Temperature conversion
*/
const MIN_TEMP = 8;
const MAX_TEMP = 28;
function temp2api(temp)
{
var res;
if (temp == 'on' || temp === true)
res = 254;
else if (temp == 'off' || temp === false)
res = 253;
else {
// 0.5C accuracy
res = Math.round((Math.min(Math.max(temp, MIN_TEMP), MAX_TEMP) - 8) * 2) + 16;
}
return res;
}
function api2temp(param)
{
if (param == 254)
return 'on';
else if (param == 253)
return 'off';
else {
// 0.5C accuracy
return (parseFloat(param) - 16) / 2 + 8;
}
}
// #############################################################################
// run command for selected device
module.exports.executeCommand = executeCommand;
// supported temperature range
module.exports.MIN_TEMP = MIN_TEMP;
module.exports.MAX_TEMP = MAX_TEMP;
// functions bitmask
module.exports.FUNCTION_THERMOSTAT = 1 << 6; // Comet DECT, Heizkostenregler
module.exports.FUNCTION_ENERGYMETER = 1 << 7; // Energie Messgerät
module.exports.FUNCTION_TEMPERATURESENSOR = 1 << 8; // Temperatursensor
module.exports.FUNCTION_OUTLET = 1 << 9; // Schaltsteckdose
module.exports.FUNCTION_DECTREPEATER = 1 << 10; // AVM DECT Repeater
/*
* Session handling
*/
// get session id
module.exports.getSessionID = function(username, password, options)
{
if (typeof username !== 'string') throw new Error('Invalid username');
if (typeof password !== 'string') throw new Error('Invalid password');
return executeCommand(null, null, null, options, '/login_sid.lua').then(function(body) {
var challenge = body.match("<Challenge>(.*?)</Challenge>")[1];
var challengeResponse = challenge +'-'+
require('crypto').createHash('md5').update(Buffer(challenge+'-'+password, 'UTF-16LE')).digest('hex');
var url = "/login_sid.lua?username=" + username + "&response=" + challengeResponse;
return executeCommand(null, null, null, options, url).then(function(body) {
sessionID = body.match("<SID>(.*?)</SID>")[1];
return Promise.resolve(sessionID);
});
});
};
// check if session id is OK
module.exports.checkSession = function(sid, options)
{
return executeCommand(sid, null, null, options, '/login_sid.lua').then(function(body) {
sessionID = body.match("<SID>(.*?)</SID>")[1];
return Promise.resolve(sessionID);
});
};
/*
* General functions
*/
// get detailed device information (XML)
module.exports.getDeviceListInfo = function(sid, options)
{
return executeCommand(sid, 'getdevicelistinfos', null, options);
};
// get device list
module.exports.getDeviceList = function(sid, options)
{
return module.exports.getDeviceListInfo(sid, options).then(function(devicelistinfo) {
var devices = parser.xml2json(devicelistinfo);
// extract devices as array
devices = [].concat((devices.devicelist || {}).device || []);
return Promise.resolve(devices);
});
};
// get single device
module.exports.getDevice = function(sid, ain, options)
{
/* jshint laxbreak:true */
var deviceList = options && options.deviceList
? Promise.resolve(options.deviceList)
: module.exports.getDeviceList(sid, options);
return deviceList.then(function(devices) {
var dev = devices.find(function(device) {
return device.identifier.replace(/\s/g, '') == ain;
});
return dev || Promise.reject();
});
};
// get temperature- both switches and thermostats are supported, but not powerline modules
module.exports.getTemperature = function(sid, ain, options)
{
return executeCommand(sid, 'gettemperature', ain, options).then(function(body) {
return Promise.resolve(parseFloat(body) / 10); // °C
});
};
/*
* Switches
*/
// get switch list
module.exports.getSwitchList = function(sid, options)
{
return executeCommand(sid, 'getswitchlist', null, options).then(function(res) {
// force empty array on empty result
return Promise.resolve(res === "" ? [] : res.split(','));
});
};
// get switch state
module.exports.getSwitchState = function(sid, ain, options)
{
return executeCommand(sid, 'getswitchstate', ain, options).then(function(body) {
return Promise.resolve(/^1/.test(body)); // true if on
});
};
// turn an outlet on. returns the state the outlet was set to
module.exports.setSwitchOn = function(sid, ain, options)
{
return executeCommand(sid, 'setswitchon', ain, options).then(function(body) {
return Promise.resolve(/^1/.test(body)); // true if on
});
};
// turn an outlet off. returns the state the outlet was set to
module.exports.setSwitchOff = function(sid, ain, options)
{
return executeCommand(sid, 'setswitchoff', ain, options).then(function(body) {
return Promise.resolve(/^1/.test(body)); // false if off
});
};
// get the total enery consumption. returns the value in Wh
module.exports.getSwitchEnergy = function(sid, ain, options)
{
return executeCommand(sid, 'getswitchenergy', ain, options).then(function(body) {
return Promise.resolve(parseFloat(body)); // Wh
});
};
// get the current enery consumption of an outlet. returns the value in mW
module.exports.getSwitchPower = function(sid, ain, options)
{
return executeCommand(sid, 'getswitchpower', ain, options).then(function(body) {
return Promise.resolve(parseFloat(body) / 1000); // W
});
};
// get the outet presence status
module.exports.getSwitchPresence = function(sid, ain, options)
{
return executeCommand(sid, 'getswitchpresent', ain, options).then(function(body) {
return Promise.resolve(/^1/.test(body)); // true if present
});
};
// get switch name
module.exports.getSwitchName = function(sid, ain, options)
{
return executeCommand(sid, 'getswitchname', ain, options).then(function(body) {
return Promise.resolve(body.trim());
});
};
/*
* Thermostats
*/
// get the switch list
module.exports.getThermostatList = function(sid, options)
{
/* jshint laxbreak:true */
var deviceList = options && options.deviceList
? Promise.resolve(options.deviceList)
: module.exports.getDeviceList(sid, options);
return deviceList.then(function(devices) {
// get thermostats- right now they're only available via the XML api
var thermostats = devices.filter(function(device) {
return device.productname == 'Comet DECT';
}).map(function(device) {
// fix ain
return device.identifier.replace(/\s/g, '');
});
return Promise.resolve(thermostats);
});
};
// set target temperature (Solltemperatur)
module.exports.setTempTarget = function(sid, ain, temp, options)
{
return executeCommand(sid, 'sethkrtsoll¶m=' + temp2api(temp), ain, options).then(function(body) {
// api does not return a value
return Promise.resolve(temp);
});
};
// get target temperature (Solltemperatur)
module.exports.getTempTarget = function(sid, ain, options)
{
return executeCommand(sid, 'gethkrtsoll', ain, options).then(function(body) {
return Promise.resolve(api2temp(body));
});
};
// get night temperature (Absenktemperatur)
module.exports.getTempNight = function(sid, ain, options)
{
return executeCommand(sid, 'gethkrabsenk', ain, options).then(function(body) {
return Promise.resolve(api2temp(body));
});
};
// get comfort temperature (Komforttemperatur)
module.exports.getTempComfort = function(sid, ain, options)
{
return executeCommand(sid, 'gethkrkomfort', ain, options).then(function(body) {
return Promise.resolve(api2temp(body));
});
};
// get battery charge - not part of Fritz API
module.exports.getBatteryCharge = function(sid, ain, options)
{
return module.exports.getDevice(sid, ain, options).then(function(device) {
var req = {
method: 'POST',
form: {
sid: sid,
xhr: 1,
no_sidrenew: '',
device: device.id,
oldpage: '/net/home_auto_hkr_edit.lua',
back_to_page: '/net/network.lua'
}
};
return httpRequest('/data.lua', req, options).then(function(body) {
$ = cheerio.load(body);
var res = $('div>label:contains(Batterie)+span').first().text().replace(/[\s%]/g, '');
return Promise.resolve(res);
});
});
};
/*
* WLAN
*/
// get guest WLAN settings - not part of Fritz API
module.exports.getGuestWlan = function(sid, options)
{
return executeCommand(sid, null, null, options, '/wlan/guest_access.lua?0=0').then(function(body) {
return Promise.resolve(parseHTML(body));
});
};
// set guest WLAN settings - not part of Fritz API
module.exports.setGuestWlan = function(sid, enable, options)
{
/* jshint laxbreak:true */
var settings = enable instanceof Object
? Promise.resolve(enable)
: executeCommand(sid, null, null, options, '/wlan/guest_access.lua?0=0').then(function(body) {
return extend(parseHTML(body), {
activate_guest_access: enable
});
});
return settings.then(function(settings) {
// convert boolean to checkbox
for (var property in settings) {
if (settings[property] === true)
settings[property] = 'on';
else if (settings[property] === false)
delete settings[property];
}
var req = {
method: 'POST',
form: extend(settings, {
sid: sid,
xhr: 1,
no_sidrenew: '',
apply: '',
oldpage: '/wlan/guest_access.lua'
})
};
return httpRequest('/data.lua', req, options).then(function(body) {
return Promise.resolve(parseHTML(body));
});
});
};