Skip to content

Instantly share code, notes, and snippets.

@MZachmann
Created December 4, 2016 22:49
Show Gist options
  • Save MZachmann/9275306e1d2453529b074e7924ba8d8c to your computer and use it in GitHub Desktop.
Save MZachmann/9275306e1d2453529b074e7924ba8d8c to your computer and use it in GitHub Desktop.
A chunk of the ISY994 <-> SmartThings code
// Fourth GUI Page
// This page has no data entry. Its just a placeholder where we wait for the Nodes to be polled
// and then enumerated
def nodePage() {
printDebug "*****Running node page"
def nodes = getNodes()
// turn off the ssdp searcher here
stopSubscribing()
// subscribe to the generic guy that reads REST results
if(!state.subscribed) {
printDebug('Subscribing to more updates')
// subscribe to answers from HUB, all responses will go here
subscribe(location, null, locationHandler, [filterEvents:false])
state.subscribed = true
}
// we only run this when we have no prebuilt nodes
// so really only after an uninstall or on a clean install
// not on a rerun
if(nodes.size() == 0)
{
// define the IP by hardcode or ISY device
if(isuseip)
{
state.ipaddress = ipaddress
state.port = ipport
}
else
{
def selDev = getSelectedDevice()
state.ipaddress = selDev.value.ip
state.port = selDev.value.port
}
// send the Query Nodes REST command
def path = "/rest/nodes"
sendHubCommand(getRequest(state.ipaddress, state.port, path))
}
def refreshInterval = 5;
return dynamicPage(name:"nodePage", title:"ISY Node Reading (Page 3 of 4)", nextPage:"entryPage", install:false, refreshInterval:refreshInterval) {
section("Waiting for nodes to be found") {
paragraph "Wait for the next line to fill in with a set of non-zero nodes. Usually this will take 5 seconds. Then click Next at the top of the page."
paragraph "Finding Nodes... \n(${nodes.size() ?: 0} found)"
}
}
}
// Last GUI Page
// Node selection preference page - choose which Insteon lights to control
def entryPage() {
printDebug "*****Running entry page"
stopSubscribing()
def nodes = getNodes()
// sort the light names alphabetically
def rooms = nodes.collect {it.value.name}.sort()
return dynamicPage(name:"entryPage", title:"ISY Node Selection (Page 4 of 4)", nextPage:"", install:true, uninstall: true) {
section("Select nodes...") {
paragraph "Click below to get a list of devices (lights). Pick which lights to add to the SmartThings database. That will fill the list below. Then click Done to add them."
input "selectedRooms", "enum", required:false, title:"Select Nodes \n(${nodes.size() ?: 0} found)", multiple:true, options:rooms
}
}
}
// --------------------------------------------------------------------------
// Utility Methods
// --------------------------------------------------------------------------
// stop subscriber
def stopSubscribing()
{
if(state.subscribed)
{
unsubscribe()
state.subscribed = false;
}
}
// Returns a map of ISYs for the preference page
def getDevicesForDialog() {
def devices = getDevices()
def map = [:]
devices.each {
def value = it.value.ip
def key = it.value.mac
map["${key}"] = value
}
map
}
// Returns the ISY map
def getDevices() {
if (!state.devices) { state.devices = [:] }
printDebug("There are ${state.devices.size()} devices at this time")
state.devices
}
// Return the selected ISY device based on mac address
def getSelectedDevice() {
def selDev
selectedISY.each { dni ->
def devices = getDevices()
printDebug("Looking for ${dni}")
selDev = devices.find { it.value.mac == dni }
}
if(selDev)
printDebug "found device ${selDev.value.mac}"
else
printDebug "did not find device(s)"
selDev
}
// Returns a map of Insteon nodes for internal use
def getNodes() {
if (!state.nodes) {
state.nodes = [:]
}
printDebug("There are ${state.nodes.size()} nodes at this time")
state.nodes
}
// this updates all of the levels of all of the switches
// we run this every 7 minutes (see schedule call in initialize())
def sendStatusRequest()
{
if(!state.subscribed)
{
// since we do not know which preferences pages were run when and some unsubscribe, make sure we turn it back on
printDebug('Scheduler has to resubscribe')
subscribe(location, null, locationHandler, [filterEvents:false])
state.subscribed = true
}
// now send the status request to find out levels of every switch
def path = "/rest/status"
sendHubCommand(getRequest(state.ipaddress, state.port, path))
}
// handle the response to a /rest/nodes request and enumerate all of the nodes
// put the node list into state.nodes
def enumerateNodes(msg)
{
def xmlNodes = msg.xml.node
printDebug "Found ${xmlNodes.size()} nodes."
// here we clear things out for real since we are about to reread them
state.nodes = [:]
// parse the individual nodes from the rest result
xmlNodes.each {
def addr = it.address.text()
def name = it.name.text()
def type = it.@nodeDefId.text()
if(addr && name && type)
{
// show the nodes found for debugging
printDebug "${addr} => ${name}.${type}"
// create a persistent node entry with name, type
state.nodes[addr] = [name:name, type:type]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment