Ja, es funktioniert jetzt 🙂 Ich sehe die Geräte in der dS App nicht, aber das spielt eigentlich keine Rolle, da ich das WLAN über benutzerdefinierte Handlungen steuere. Werde noch benutzerdefinierte Zustände erstellen um den Status anzuzeigen.
mainscript
// ***************************************************************************
// FRITZ!Box Integration Global Code - START
// ***************************************************************************
global function fritzBoxGet(location, uri, action) {
fritzBoxSet(location, uri, action, 'nil', 'nil')
}
global function fritzBoxSet(location, uri, action, tag, value) {
log(5, format('fritzBoxSet invoked with location: %s, uri: %s, action: %s, tag: %s, value: %s', location, uri, action, tag, value))
// xml tag string - if value provided
var xmlTag
if (tag == 'nil') {
xmlTag = ''
} else {
xmlTag = '<' + tag + '>' + value + '</' + tag + '>'
}
// build request
var request = {
'headers' : {
'Content-Type' : 'text/xml; charset="utf-8"',
'SoapAction' : '',
},
'url' : '',
'method' : 'POST',
'withmeta' : true
}
request.url = fritzBoxBaseURL + location
request.headers.SoapAction = '"' + uri + '#' + action + '"'
request.data = "<?xml version='1.0' encoding='utf-8'?><s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><u:" + action + " xmlns:u='" + uri + "'>" + xmlTag + "</u:" + action + "></s:Body></s:Envelope>"
log(5, format('fritzBox request for location: %s, uri: %s, action: %s, tag: %s, value: %s = %s', location, uri, action, tag, value, request))
// execute request
var fritzBoxResponse = httprequest(request)
if (fritzBoxResponse.status != 200) {
log(format('ERROR - FRITZ!Box returned HTTP status %s. Error: %s', fritzBoxResponse.status, fritzBoxResponse.data))
}
log (5, format('fritzBox response for location: %s, uri: %s, action: %s, tag: %s, value: %s = %s', location, uri, action, tag, value, fritzBoxResponse))
fritzBoxResponse
}
global function getTagValueFromXml(xml, tag) {
var tag_start = find(xml, '<' + tag + '>')
log(5, format('getTagValueFromXml: tag start %s found at %s', tag, tag_start))
var tag_end = find(xml, '</' + tag + '>', tag_start)
log(5, format('getTagValueFromXml: tag end %s found at %s', tag, tag_end))
var substr_from = tag_start + strlen(tag) + 2
var substr_len = tag_end - tag_start - strlen(tag) - 2
log(5, format('getTagValueFromXml: substr_from %s, substr_len %s', substr_from, substr_len))
substr(xml, substr_from, substr_len)
}
// FritzBox connection information
var fritzBox = {
'protocol': 'https',
'ip': 'fritz.box',
'user': 'user',
'password': 'password'
}
// build fritzBoxBaseURL
var fritzBoxPort
if (fritzBox.protocol=='https') fritzBoxPort = 49443 else fritzBoxPort = 49000
global fritzBoxBaseURL
fritzBoxBaseURL = '!' + fritzBox.protocol + '://' + fritzBox.user + ':' + fritzBox.password + '@' + fritzBox.ip + ':' + fritzBoxPort
var nil = 'nil' // prevent logging of last result (if this is the last line in the global script)
// ***************************************************************************
// FRITZ!Box Integration Global Code - END
// ***************************************************************************
Device für 2.4G WLAN:
// ***************************************************************************
// FRITZ!Box Device WLAN2G
// ***************************************************************************
// init with input
{
'message': 'init',
'output': 'basic',
'colorclass': 8, /* dS Joker */
'group': 8, /* dS Joker */
'inputs':[ {
'inputtype': 0,
'usage': 1,
'group': 8, /* dS Joker */
'hardwarename': 'WLAN2G',
'updateinterval': 300,
'alivesigninterval': 900,
}]
}
// implementation
function updatestate()
{
var action = 'GetInfo'
var fritzBoxResponse = fritzBoxGet(location, uri, action)
var status = getTagValueFromXml(fritzBoxResponse.data, tag)
// update state
var msg = { 'message':'input', 'index':0 }
msg.value = status;
log(format("status of WLAN2G is %d", status))
message(msg)
}
var location = "/upnp/control/wlanconfig1"
var uri = "urn:dslforum-org:service:WLANConfiguration:1"
var action = 'SetEnable'
var tag = 'NewEnable'
// when the input value changes
on (message()) as m {
if (m.message=="channel" && m.index==0) {
var newValue = if(m.value>50, 1, 0)
log(format("setting WLAN2G to %d", newValue))
var fritzBoxResponse = fritzBoxSet(location, uri, action, tag, newValue)
updatestate()
}
}
// every 5 minutes
on (every(0:05)) {
// get status
updatestate()
}
// get state on startup
updatestate()
return true // end of script definition
Device für 5G WLAN:
// init with input
{
'message': 'init',
'output': 'basic',
'colorclass': 8, /* dS Joker */
'group': 8, /* dS Joker */
'inputs':[ {
'inputtype': 0,
'usage': 1,
'group': 8, /* dS Joker */
'hardwarename': 'WLAN5G',
'updateinterval': 300,
'alivesigninterval': 900,
}]
}
// implementation
function updatestate()
{
var action = 'GetInfo'
var fritzBoxResponse = fritzBoxGet(location, uri, action)
var status = getTagValueFromXml(fritzBoxResponse.data, tag)
// update state
var msg = { 'message':'input', 'index':0 }
msg.value = status;
log(format("status of WLAN5G is %d", status))
message(msg)
}
var location = "/upnp/control/wlanconfig2"
var uri = "urn:dslforum-org:service:WLANConfiguration:2"
var action = 'SetEnable'
var tag = 'NewEnable'
// when the input value changes
on (message()) as m {
if (m.message=="channel" && m.index==0) {
var newValue = if(m.value>50, 1, 0)
log(format("setting WLAN5G to %d", newValue))
var fritzBoxResponse = fritzBoxSet(location, uri, action, tag, newValue)
updatestate()
}
}
// every 5 minutes
on (every(0:05)) {
// get status
updatestate()
}
// get state on startup
updatestate()
return true // end of script definition
Device für Gast WLAN:
// init with input
{
'message': 'init',
'output': 'basic',
'colorclass': 8, /* dS Joker */
'group': 8, /* dS Joker */
'inputs':[ {
'inputtype': 0,
'usage': 1,
'group': 8, /* dS Joker */
'hardwarename': 'WLANGUEST',
'updateinterval': 300,
'alivesigninterval': 900,
}]
}
// implementation
function updatestate()
{
var action = 'GetInfo'
var fritzBoxResponse = fritzBoxGet(location, uri, action)
var status = getTagValueFromXml(fritzBoxResponse.data, tag)
// update state
var msg = { 'message':'input', 'index':0 }
msg.value = status;
log(format("status of WLANGUEST is %d", status))
message(msg)
}
var location = "/upnp/control/wlanconfig3"
var uri = "urn:dslforum-org:service:WLANConfiguration:3"
var action = 'SetEnable'
var tag = 'NewEnable'
// when the input value changes
on (message()) as m {
if (m.message=="channel" && m.index==0) {
var newValue = if(m.value>50, 1, 0)
log(format("setting WLANGUEST to %d", newValue))
var fritzBoxResponse = fritzBoxSet(location, uri, action, tag, newValue)
updatestate()
}
}
// every 5 minutes
on (every(0:05)) {
// get status
updatestate()
}
// get state on startup
updatestate()
return true // end of script definition
Verbesserungsvorschläge willkommen!
Besten Dank und Gruss,
Chris