smartfritz-promise
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -9,2 +9,8 @@ /** | ||
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'); | ||
var Promise = require('bluebird'); | ||
var fritz = require('./smartfritz.js'); | ||
@@ -19,32 +25,33 @@ | ||
fritz.getDeviceListInfoArray(sid).then(function(res) { | ||
var ain = '109710195784'; | ||
fritz.getBatteryCharge(sid, ain).then(function(res) { | ||
console.log(res); | ||
}); | ||
// fritz.getGuestWlan(sid).then(function(res) { | ||
// console.log(res); | ||
// }); | ||
fritz.getGuestWlan(sid).then(function(res) { | ||
console.log(res); | ||
}); | ||
// display switch information | ||
// fritz.getSwitchList(sid).then(function(switches) { | ||
// console.log("Switches: " + switches); | ||
fritz.getSwitchList(sid).then(function(switches) { | ||
console.log("Switches: " + switches); | ||
// if (switches.length) { | ||
// fritz.getSwitchName(sid, switches[0]).then(function(name) { | ||
// console.log("Switch name [" + switches[0] + "]: " + name); | ||
if (switches.length) { | ||
fritz.getSwitchName(sid, switches[0]).then(function(name) { | ||
console.log("Switch name [" + switches[0] + "]: " + name); | ||
// fritz.getSwitchPresence(sid, switches[0]).then(function(presence) { | ||
// console.log("Switch presence [" + switches[0] + "]: " + presence); | ||
fritz.getSwitchPresence(sid, switches[0]).then(function(presence) { | ||
console.log("Switch presence [" + switches[0] + "]: " + presence); | ||
// fritz.getSwitchState(sid, switches[0]).then(function(state) { | ||
// console.log("Switch state [" + switches[0] + "]: " + state); | ||
// }); | ||
fritz.getSwitchState(sid, switches[0]).then(function(state) { | ||
console.log("Switch state [" + switches[0] + "]: " + state); | ||
}); | ||
// fritz.getTemperature(sid, switches[0]).then(function(temp) { | ||
// console.log("Switch temperature [" + switches[0] + "]: " + temp + "°C"); | ||
// }); | ||
// }); | ||
// }); | ||
// } | ||
// }); | ||
fritz.getTemperature(sid, switches[0]).then(function(temp) { | ||
console.log("Switch temperature [" + switches[0] + "]: " + temp + "°C"); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
@@ -51,0 +58,0 @@ // display thermostat information |
{ | ||
"name": "smartfritz-promise", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"description": "SmartHome for Fritz!Box and Dect!200 Node to communicate with AVM FritzBox over the aha-http-interface", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -24,16 +24,11 @@ /** | ||
// run command for selected device | ||
function executeCommand(sid, command, ain, options, path) | ||
/** | ||
* Execute HTTP request that honors failed/invalid login | ||
*/ | ||
function httpRequest(path, req, options) | ||
{ | ||
var req = extend({}, defaults, options || {}); | ||
req.url += path || '/webservices/homeautoswitch.lua?0=0'; | ||
return new Promise(function(resolve, reject) { | ||
req = extend({}, defaults, req, options); | ||
req.url += path; | ||
if (sid) | ||
req.url += '&sid=' + sid; | ||
if (command) | ||
req.url += '&switchcmd=' + command; | ||
if (ain) | ||
req.url += '&ain=' + ain; | ||
return new Promise(function(resolve, reject) { | ||
request(req, function(error, response, body) { | ||
@@ -59,2 +54,19 @@ if (error || !(/^2/.test('' + response.statusCode)) || /action=".?login.lua"/.test(body)) { | ||
/** | ||
* 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 | ||
@@ -280,3 +292,3 @@ */ | ||
{ | ||
return module.exports.getDeviceListInfo(sid, options).then(function(devices) { | ||
return module.exports.getDeviceList(sid, options).then(function(devices) { | ||
// get thermostats- right now they're only available via the XML api | ||
@@ -311,3 +323,3 @@ var thermostats = devices.filter(function(device) { | ||
// get nght temperature (Absenktemperatur) | ||
// get night temperature (Absenktemperatur) | ||
module.exports.getTempNight = function(sid, ain, options) | ||
@@ -328,3 +340,35 @@ { | ||
// get battery charge - not part of Fritz API | ||
module.exports.getBatteryCharge = function(sid, ain, options) | ||
{ | ||
return module.exports.getDeviceList(sid).then(function(devices) { | ||
var dev = devices.find(function(device) { | ||
return device.identifier.replace(/\s/g, '') == ain; | ||
}); | ||
if (dev === undefined) { | ||
return Promise.reject(); | ||
} | ||
var req = { | ||
method: 'POST', | ||
form: { | ||
sid: sid, | ||
xhr: 1, | ||
no_sidrenew: '', | ||
device: dev.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); | ||
}); | ||
}); | ||
} | ||
/* | ||
@@ -334,2 +378,3 @@ * WLAN | ||
// get guest WLAN settings - not part of Fritz API | ||
module.exports.getGuestWlan = function(sid, options) | ||
@@ -342,2 +387,3 @@ { | ||
// set guest WLAN settings - not part of Fritz API | ||
module.exports.setGuestWlan = function(sid, enable, options) | ||
@@ -358,36 +404,17 @@ { | ||
// additional settings to apply values | ||
settings = extend(settings, { | ||
'sid': sid, | ||
xhr: 1, | ||
apply: '', | ||
no_sidrenew: '', | ||
oldpage: '/wlan/guest_access.lua' | ||
}); | ||
var req = { | ||
method: 'POST', | ||
form: extend(settings, { | ||
sid: sid, | ||
xhr: 1, | ||
no_sidrenew: '', | ||
apply: '', | ||
oldpage: '/wlan/guest_access.lua' | ||
}) | ||
}; | ||
return new Promise(function(resolve, reject) { | ||
var req = extend({}, defaults, { | ||
method: 'POST', | ||
form: settings | ||
}, options || {}); | ||
req.url += '/data.lua'; | ||
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(parseHTML(body)); | ||
} | ||
}); | ||
return httpRequest('/data.lua', req, options).then(function(body) { | ||
return Promise.resolve(parseHTML(body)); | ||
}); | ||
}); | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21625
437