@patrick or @chuck.schwer thoughts?
Hello all,
So I found this code to see if I can use it as an example to help educate me on mDNS discovery. I have adapted the code to work in Hubitat BUT I have no idea why this will not discover any devices on the network. My eventual goal in this learning is to query a single known IP address for mDNS results. Does HubAction not support this type of querying of the network? Or am I just off on my coding below? Any and ALL help is appreciated.
definition(
name:"mDNS Discovery",
namespace: "Aaron Ward",
author: "Aaron Ward",
description: "mDNS",
category: "tool",
iconUrl: "",
iconX2Url: "",
iconX3Url: ""
)
preferences {
section("Title") {
page(name:"firstPage", title:"mDNS Device Setup", content:"firstPage")
}
}
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.
}
def discover() {
sendHubCommand(new hubitat.device.HubAction("lan discover mdns/dns-sd .hubitat._tcp._site", hubitat.device.Protocol.LAN))
}
def firstPage()
{
int refreshCount = !state.refreshCount ? 0 : state.refreshCount as int
state.refreshCount = refreshCount + 1
def refreshInterval = 5
//log.debug "REFRESH COUNT :: ${refreshCount}"
if(!state.subscribe) {
subscribe(location, null, locationHandler, [filterEvents:false])
state.subscribe = true
}
if((refreshCount % 3) == 0) {
discover()
}
def devicesDiscovered = devicesDiscovered()
return dynamicPage(name:"firstPage", title:"Discovery Started!", nextPage:"", refreshInterval: refreshInterval, install:true, uninstall: true) {
section("Please wait while we discover your device. Select your device below once discovered.") {
input "selectedDevices", "enum", required:false, title:"Select Devices \n(${devicesDiscovered.size() ?: 0} found)", multiple:true, options:devicesDiscovered
}
}
}
def devicesDiscovered() {
def devices = getDevices()
def map = [:]
devices.each {
def value = it.value.name ?: "mDNS Device: ${it.value.mac}"
def key = it.value.ip + ":" + it.value.port
map["${key}"] = value
}
map
}
def getDevices()
{
if (!state.devices) { state.devices = [:] }
state.devices
}
def locationHandler(evt) {
def description = evt.description
def hub = evt?.hubId
def parsedEvent = parseEventMessage(description)
parsedEvent << ["hub":hub]
if (parsedEvent?.mdnsPath)
{
def devices = getDevices()
if (!(devices."${parsedEvent?.mac?.toString()}"))
{ //device does not exist
devices << ["${parsedEvent.mac.toString()}":parsedEvent]
}
else
{ // update the values
log.debug "Device was already found in state..."
def d = device."${parsedEvent.mac.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 "mdns device's port or ip changed..."
}
if (deviceChangedValues) {
def children = getChildDevices()
children.each {
if (it.getDeviceDataByName("mac") == parsedEvent.mac) {
log.debug "updating dni for device ${it} with mac ${parsedEvent.mac}"
it.setDeviceNetworkId((parsedEvent.ip + ":" + parsedEvent.port))
}
}
}
}
}
}
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('mdnsPath:')) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.mdnsPath = 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
}
Event log errors:
[app:2314](http://10.0.2.38/logs#app2314)2019-04-03 08:24:35.930 pm [warn](http://10.0.2.38/installedapp/configure/2314)Cannot get property 'host' on null object