this lookup method thanks to @thebearmay
def nsCallback(resp, data)
{
logDebug("in callback")
// test change
def jSlurp = new JsonSlurper()
Map ipData = (Map)jSlurp.parseText((String)resp.data)
def String newIP = ipData.Answer.data[0]
sendEvent(name:"dynamicIPResult", value:ipData.Answer.data[0])
// now compare ip to our own and if different reset and log
if ((newIP != null) && (newIP != ""))
{
def String currentIP = settings.macAddress
logInfo("Comparing resolved IP: $newIP to $currentIP")
if (currentIP != newIP)
{
logInfo("IP address has Changed !!! Resetting DNI !")
Map dni = dniIsValid(newIP);
// Update Device Network ID
logDebug("got back dni = $dni")
if (dni)
{
device.updateSetting("macAddress", [type: "string", value: dni.canonical]);
dniUpdate();
resyncSensors();
}
}
}
}
void DNSCheckCallback()
{
logInfo("Dns Update Check Callback Startup")
updated()
}
void updated() {
//
// Called everytime the user saves the driver preferences
//
try {
logDebug("updated()");
// Clear previous states
state.clear();
// Unschedule possible previous runIn() calls
unschedule();
// lgk if ddns name resolve this first and do ip check before dniupdatE.. ALSO schedule the re-check.
def String ddnsname = settings.DDNSName
def Number ddnsupdatetime = settings.DDNSRefreshTime
logDebug("DDNS Name = $ddnsname")
logDebug("DDNS Refresh Time = $ddnsupdatetime")
if ((ddnsname != null) && (ddnsname != ""))
{
logDebug("Got ddns name $ddnsname")
// now resolve
Map params = [
uri: "https://8.8.8.8/resolve?name=$ddnsname&type=A",
contentType: "text/plain",
timeout: 20
]
logDebug("calling dns Update url = $params")
asynchttpGet("nsCallback", params)
}
// now schedule next run of update
if ((ddnsupdatetime != null) && (ddnsupdatetime != 00))
{
def thesecs = ddnsupdatetime * 3600
logInfo("Rescheduling IP Address Check to run again in $thesecs seconds.")
runIn(thesecs, "DNSCheckCallback");
}
// Update Device Network ID
String error = dniUpdate();
if (error == null) {
// The gateway dni hasn't changed: we set OK only if a resync sensors is not pending
if (device.getDataValue("sensorResync")) ztatus("Sensor sync pending", "blue");
else ztatus("OK", "green");
}
else if (error != "") ztatus(error, "red");
else resyncSensors();
// Update driver version now and every Sunday @ 2am
// versionUpdate();
// schedule("0 0 2 ? * 1 *", versionUpdate);
// Turn off debug log in 30 minutes
if (logGetLevel() > 2) runIn(1800, logDebugOff);
// lgk get rid of now unused time attribute
device.deleteCurrentState("time")
}
catch (Exception e) {
logError("Exception in updated(): ${e}");
}
}
// ------------------------------------------------------------