Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
smartthings_lan_discovery.groovy
/**
* LANtest App
*
* Author: m@mattmartz.com
* Date: 2014-02-02
*/
preferences {
section("Local IP address...") {
input "ip", "text"
}
section("Port of local...") {
input "port", "number"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(location, null, locationHandler, [filterEvents:false])
subscribe(app, changeMode)
}
//urn:schemas-upnp-org:device:DigitalSecurityCamera:2 //2 is pan-tilt, 1 is fixed?
private discoverPanTilt()
{
//consider using other discovery methods
sendHubCommand(new physicalgraph.device.HubAction("lan discovery urn:schemas-upnp-org:device:DigitalSecurityCamera:2", physicalgraph.device.Protocol.LAN))
log.debug "sent discoverPanTilt"
}
private discoverFixed()
{
//consider using other discovery methods
sendHubCommand(new physicalgraph.device.HubAction("lan discovery urn:schemas-upnp-org:device:DigitalSecurityCamera:1", physicalgraph.device.Protocol.LAN))
log.debug "sent discoverFixed"
}
def changeMode(evt) {
def deviceNetworkId = "0A000005:0BB8" // "10.0.0.5:3000"
sendHubCommand(new physicalgraph.device.HubAction("""GET / HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
log.debug location.hubs*.firmwareVersionString.findAll { it }
discoverPanTilt()
discoverFixed()
}
def locationHandler(evt) {
def description = evt.description
def hub = evt?.hubId
def parsedEvent = parseEventMessage(description)
parsedEvent << ["hub":hub]
log.debug "parsedEvent " + parsedEvent
if (parsedEvent?.ssdpTerm?.contains("urn:schemas-upnp-org"))
{
log.debug "upnp device" + parsedEvent?.ssdpTerm
}
else if (parsedEvent.headers && parsedEvent.body)
{
def headerString = new String(parsedEvent.headers.decodeBase64())
def bodyString = new String(parsedEvent.body.decodeBase64())
log.debug "bodyString " + bodyString
def type = (headerString =~ /Content-Type:.*/) ? (headerString =~ /Content-Type:.*/)[0] : null
}
else {
log.debug "cp desc: " + description
//log.debug description
}
//log.debug description
}
private def parseEventMessage(Map event) {
//handles sonos attribute events
return event
}
private def parseEventMessage(String description) {
def event = [:]
def parts = description.split(',')
parts.each { part ->
part = part.trim()
if (part.startsWith('devicetype:')) {
def valueString = part.split(":")[1].trim()
event.devicetype = valueString
}
else if (part.startsWith('mac:')) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.mac = valueString
}
}
else if (part.startsWith('networkAddress:')) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.ip = valueString
}
}
else if (part.startsWith('deviceAddress:')) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.port = valueString
}
}
else if (part.startsWith('ssdpPath:')) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.ssdpPath = valueString
}
}
else if (part.startsWith('ssdpUSN:')) {
part -= "ssdpUSN:"
def valueString = part.trim()
if (valueString) {
event.ssdpUSN = valueString
}
}
else if (part.startsWith('ssdpTerm:')) {
part -= "ssdpTerm:"
def valueString = part.trim()
if (valueString) {
event.ssdpTerm = valueString
}
}
else if (part.startsWith('headers')) {
part -= "headers:"
def valueString = part.trim()
if (valueString) {
event.headers = valueString
}
}
else if (part.startsWith('body')) {
part -= "body:"
def valueString = part.trim()
if (valueString) {
event.body = valueString
}
}
}
event
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment