mirror of
https://github.com/mtan93/SmartThingsPublic.git
synced 2026-03-08 05:31:56 +00:00
INSIDE-787: Improve Wemo (Connect) event subscriptions
This commit is contained in:
@@ -39,7 +39,7 @@ private getFriendlyName(String deviceNetworkId) {
|
||||
sendHubCommand(new physicalgraph.device.HubAction("""GET /setup.xml HTTP/1.1
|
||||
HOST: ${deviceNetworkId}
|
||||
|
||||
""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
|
||||
""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}", [callback: "setupHandler"]))
|
||||
}
|
||||
|
||||
private verifyDevices() {
|
||||
@@ -52,6 +52,13 @@ private verifyDevices() {
|
||||
}
|
||||
}
|
||||
|
||||
void ssdpSubscribe() {
|
||||
subscribe(location, "ssdpTerm.urn:Belkin:device:insight:1", ssdpSwitchHandler)
|
||||
subscribe(location, "ssdpTerm.urn:Belkin:device:controllee:1", ssdpSwitchHandler)
|
||||
subscribe(location, "ssdpTerm.urn:Belkin:device:sensor:1", ssdpMotionHandler)
|
||||
subscribe(location, "ssdpTerm.urn:Belkin:device:lightswitch:1", ssdpLightSwitchHandler)
|
||||
}
|
||||
|
||||
def firstPage()
|
||||
{
|
||||
if(canInstallLabs())
|
||||
@@ -62,7 +69,7 @@ def firstPage()
|
||||
|
||||
log.debug "REFRESH COUNT :: ${refreshCount}"
|
||||
|
||||
subscribe(location, null, locationHandler, [filterEvents:false])
|
||||
ssdpSubscribe()
|
||||
|
||||
//ssdp request every 25 seconds
|
||||
if((refreshCount % 5) == 0) {
|
||||
@@ -105,9 +112,7 @@ def devicesDiscovered() {
|
||||
def motions = getWemoMotions()
|
||||
def lightSwitches = getWemoLightSwitches()
|
||||
def devices = switches + motions + lightSwitches
|
||||
def list = []
|
||||
|
||||
list = devices?.collect{ [app.id, it.ssdpUSN].join('.') }
|
||||
devices?.collect{ [app.id, it.ssdpUSN].join('.') }
|
||||
}
|
||||
|
||||
def switchesDiscovered() {
|
||||
@@ -176,7 +181,8 @@ def updated() {
|
||||
def initialize() {
|
||||
unsubscribe()
|
||||
unschedule()
|
||||
subscribe(location, null, locationHandler, [filterEvents:false])
|
||||
|
||||
ssdpSubscribe()
|
||||
|
||||
if (selectedSwitches)
|
||||
addSwitches()
|
||||
@@ -313,6 +319,127 @@ def addLightSwitches() {
|
||||
}
|
||||
}
|
||||
|
||||
def ssdpSwitchHandler(evt) {
|
||||
def description = evt.description
|
||||
def hub = evt?.hubId
|
||||
def parsedEvent = parseDiscoveryMessage(description)
|
||||
parsedEvent << ["hub":hub]
|
||||
log.debug parsedEvent
|
||||
|
||||
def switches = getWemoSwitches()
|
||||
if (!(switches."${parsedEvent.ssdpUSN.toString()}")) {
|
||||
//if it doesn't already exist
|
||||
switches << ["${parsedEvent.ssdpUSN.toString()}":parsedEvent]
|
||||
} else {
|
||||
log.debug "Device was already found in state..."
|
||||
def d = switches."${parsedEvent.ssdpUSN.toString()}"
|
||||
boolean deviceChangedValues = false
|
||||
log.debug "$d.ip <==> $parsedEvent.ip"
|
||||
if(d.ip != parsedEvent.ip || d.port != parsedEvent.port) {
|
||||
d.ip = parsedEvent.ip
|
||||
d.port = parsedEvent.port
|
||||
deviceChangedValues = true
|
||||
log.debug "Device's port or ip changed..."
|
||||
def child = getChildDevice(parsedEvent.mac)
|
||||
child.subscribe(parsedEvent.ip, parsedEvent.port)
|
||||
child.poll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def ssdpMotionHandler(evt) {
|
||||
log.info("ssdpMotionHandler")
|
||||
def description = evt.description
|
||||
def hub = evt?.hubId
|
||||
def parsedEvent = parseDiscoveryMessage(description)
|
||||
parsedEvent << ["hub":hub]
|
||||
log.debug parsedEvent
|
||||
|
||||
def motions = getWemoMotions()
|
||||
if (!(motions."${parsedEvent.ssdpUSN.toString()}")) {
|
||||
//if it doesn't already exist
|
||||
motions << ["${parsedEvent.ssdpUSN.toString()}":parsedEvent]
|
||||
} else { // just update the values
|
||||
log.debug "Device was already found in state..."
|
||||
|
||||
def d = motions."${parsedEvent.ssdpUSN.toString()}"
|
||||
boolean deviceChangedValues = false
|
||||
|
||||
if(d.ip != parsedEvent.ip || d.port != parsedEvent.port) {
|
||||
d.ip = parsedEvent.ip
|
||||
d.port = parsedEvent.port
|
||||
deviceChangedValues = true
|
||||
log.debug "Device's port or ip changed..."
|
||||
}
|
||||
|
||||
if (deviceChangedValues) {
|
||||
def children = getChildDevices()
|
||||
log.debug "Found children ${children}"
|
||||
children.each {
|
||||
if (it.getDeviceDataByName("mac") == parsedEvent.mac) {
|
||||
log.debug "updating ip and port, and resubscribing, for device ${it} with mac ${parsedEvent.mac}"
|
||||
it.subscribe(parsedEvent.ip, parsedEvent.port)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def ssdpLightSwitchHandler(evt) {
|
||||
log.info("ssdpLightSwitchHandler")
|
||||
def description = evt.description
|
||||
def hub = evt?.hubId
|
||||
def parsedEvent = parseDiscoveryMessage(description)
|
||||
parsedEvent << ["hub":hub]
|
||||
log.debug parsedEvent
|
||||
|
||||
def lightSwitches = getWemoLightSwitches()
|
||||
|
||||
if (!(lightSwitches."${parsedEvent.ssdpUSN.toString()}")) {
|
||||
//if it doesn't already exist
|
||||
lightSwitches << ["${parsedEvent.ssdpUSN.toString()}":parsedEvent]
|
||||
} else {
|
||||
log.debug "Device was already found in state..."
|
||||
|
||||
def d = lightSwitches."${parsedEvent.ssdpUSN.toString()}"
|
||||
boolean deviceChangedValues = false
|
||||
|
||||
if(d.ip != parsedEvent.ip || d.port != parsedEvent.port) {
|
||||
d.ip = parsedEvent.ip
|
||||
d.port = parsedEvent.port
|
||||
deviceChangedValues = true
|
||||
log.debug "Device's port or ip changed..."
|
||||
def child = getChildDevice(parsedEvent.mac)
|
||||
log.debug "updating ip and port, and resubscribing, for device with mac ${parsedEvent.mac}"
|
||||
child.subscribe(parsedEvent.ip, parsedEvent.port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setupHandler(hubResponse) {
|
||||
String contentType = hubResponse?.headers['Content-Type']
|
||||
if (contentType != null && contentType == 'text/xml') {
|
||||
def body = hubResponse.xml
|
||||
def wemoDevices = []
|
||||
String deviceType = body?.device?.deviceType?.text() ?: ""
|
||||
if (deviceType.startsWith("urn:Belkin:device:controllee:1") || deviceType.startsWith("urn:Belkin:device:insight:1")) {
|
||||
wemoDevices = getWemoSwitches()
|
||||
} else if (deviceType.startsWith("urn:Belkin:device:sensor")) {
|
||||
wemoDevices = getWemoMotions()
|
||||
} else if (deviceType.startsWith("urn:Belkin:device:lightswitch")) {
|
||||
wemoDevices = getWemoLightSwitches()
|
||||
}
|
||||
|
||||
def wemoDevice = wemoDevices.find {it?.key?.contains(body?.device?.UDN?.text())}
|
||||
if (wemoDevice) {
|
||||
wemoDevice.value << [name:body?.device?.friendlyName?.text(), verified: true]
|
||||
} else {
|
||||
log.error "/setup.xml returned a wemo device that didn't exist"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
def locationHandler(evt) {
|
||||
def description = evt.description
|
||||
def hub = evt?.hubId
|
||||
@@ -459,6 +586,7 @@ def locationHandler(evt) {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private def parseXmlBody(def body) {
|
||||
def decodedBytes = body.decodeBase64()
|
||||
def bodyString
|
||||
|
||||
Reference in New Issue
Block a user