PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/smartapps/smartthings/mdns-discovery-example.groovy

https://github.com/rappleg/SmartThings
Groovy | 169 lines | 138 code | 23 blank | 8 comment | 32 complexity | 3fbd243318da4061767b172f604b72e4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * mDNS Discovery Example
  3. *
  4. * Author: smartthings
  5. * Date: 2014-04-02
  6. */
  7. preferences {
  8. section("Title") {
  9. page(name:"firstPage", title:"mDNS Device Setup", content:"firstPage")
  10. }
  11. }
  12. def installed() {
  13. log.debug "Installed with settings: ${settings}"
  14. initialize()
  15. }
  16. def updated() {
  17. log.debug "Updated with settings: ${settings}"
  18. unsubscribe()
  19. initialize()
  20. }
  21. def initialize() {
  22. // TODO: subscribe to attributes, devices, locations, etc.
  23. }
  24. def discover() {
  25. sendHubCommand(new physicalgraph.device.HubAction("lan discover mdns/dns-sd ._smartthings._tcp._site", physicalgraph.device.Protocol.LAN))
  26. }
  27. def firstPage()
  28. {
  29. int refreshCount = !state.refreshCount ? 0 : state.refreshCount as int
  30. state.refreshCount = refreshCount + 1
  31. def refreshInterval = 5
  32. //log.debug "REFRESH COUNT :: ${refreshCount}"
  33. if(!state.subscribe) {
  34. subscribe(location, null, locationHandler, [filterEvents:false])
  35. state.subscribe = true
  36. }
  37. if((refreshCount % 3) == 0) {
  38. discover()
  39. }
  40. def devicesDiscovered = devicesDiscovered()
  41. return dynamicPage(name:"firstPage", title:"Discovery Started!", nextPage:"", refreshInterval: refreshInterval, install:true, uninstall: true) {
  42. section("Please wait while we discover your device. Select your device below once discovered.") {
  43. input "selectedDevices", "enum", required:false, title:"Select Devices \n(${devicesDiscovered.size() ?: 0} found)", multiple:true, options:devicesDiscovered
  44. }
  45. }
  46. }
  47. def devicesDiscovered() {
  48. def devices = getDevices()
  49. def map = [:]
  50. devices.each {
  51. def value = it.value.name ?: "mDNS Device: ${it.value.mac}"
  52. def key = it.value.ip + ":" + it.value.port
  53. map["${key}"] = value
  54. }
  55. map
  56. }
  57. def getDevices()
  58. {
  59. if (!state.devices) { state.devices = [:] }
  60. state.devices
  61. }
  62. def locationHandler(evt) {
  63. def description = evt.description
  64. def hub = evt?.hubId
  65. def parsedEvent = parseEventMessage(description)
  66. parsedEvent << ["hub":hub]
  67. if (parsedEvent?.mdnsPath)
  68. {
  69. def devices = getDevices()
  70. if (!(devices."${parsedEvent?.mac?.toString()}"))
  71. { //device does not exist
  72. devices << ["${parsedEvent.mac.toString()}":parsedEvent]
  73. }
  74. else
  75. { // update the values
  76. log.debug "Device was already found in state..."
  77. def d = device."${parsedEvent.mac.toString()}"
  78. boolean deviceChangedValues = false
  79. if(d.ip != parsedEvent.ip || d.port != parsedEvent.port) {
  80. d.ip = parsedEvent.ip
  81. d.port = parsedEvent.port
  82. deviceChangedValues = true
  83. log.debug "mdns device's port or ip changed..."
  84. }
  85. if (deviceChangedValues) {
  86. def children = getChildDevices()
  87. children.each {
  88. if (it.getDeviceDataByName("mac") == parsedEvent.mac) {
  89. log.debug "updating dni for device ${it} with mac ${parsedEvent.mac}"
  90. it.setDeviceNetworkId((parsedEvent.ip + ":" + parsedEvent.port))
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. private def parseEventMessage(String description) {
  98. def event = [:]
  99. def parts = description.split(',')
  100. parts.each { part ->
  101. part = part.trim()
  102. if (part.startsWith('devicetype:')) {
  103. def valueString = part.split(":")[1].trim()
  104. event.devicetype = valueString
  105. }
  106. else if (part.startsWith('mac:')) {
  107. def valueString = part.split(":")[1].trim()
  108. if (valueString) {
  109. event.mac = valueString
  110. }
  111. }
  112. else if (part.startsWith('networkAddress:')) {
  113. def valueString = part.split(":")[1].trim()
  114. if (valueString) {
  115. event.ip = valueString
  116. }
  117. }
  118. else if (part.startsWith('deviceAddress:')) {
  119. def valueString = part.split(":")[1].trim()
  120. if (valueString) {
  121. event.port = valueString
  122. }
  123. }
  124. else if (part.startsWith('mdnsPath:')) {
  125. def valueString = part.split(":")[1].trim()
  126. if (valueString) {
  127. event.mdnsPath = valueString
  128. }
  129. }
  130. else if (part.startsWith('headers')) {
  131. part -= "headers:"
  132. def valueString = part.trim()
  133. if (valueString) {
  134. event.headers = valueString
  135. }
  136. }
  137. else if (part.startsWith('body')) {
  138. part -= "body:"
  139. def valueString = part.trim()
  140. if (valueString) {
  141. event.body = valueString
  142. }
  143. }
  144. }
  145. event
  146. }