From 4b44460b0b5c572880a1b6de907817cf91d3dde4 Mon Sep 17 00:00:00 2001 From: marstorp Date: Tue, 11 Oct 2016 10:58:49 -0700 Subject: [PATCH] CHF-420 Add device health check to Bose DTH --- .../bose-soundtouch.groovy | 46 +++++++++++++++---- .../bose-soundtouch-connect.groovy | 10 ++-- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/devicetypes/smartthings/bose-soundtouch.src/bose-soundtouch.groovy b/devicetypes/smartthings/bose-soundtouch.src/bose-soundtouch.groovy index 38d624c..090be56 100644 --- a/devicetypes/smartthings/bose-soundtouch.src/bose-soundtouch.groovy +++ b/devicetypes/smartthings/bose-soundtouch.src/bose-soundtouch.groovy @@ -27,7 +27,7 @@ metadata { capability "Switch" capability "Refresh" capability "Music Player" - capability "Polling" + capability "Health Check" /** * Define all commands, ie, if you have a custom action not @@ -236,7 +236,33 @@ def parse(String event) { * @return action(s) to take or null */ def installed() { - onAction("refresh") + // Notify health check about this device with timeout interval 12 minutes + sendEvent(name: "checkInterval", value: 12 * 60, data: [protocol: "lan", hubHardwareId: device.hub.hardwareID], displayed: false) + startPoll() +} + +/** + * Called by health check if no events been generated in the last 12 minutes + * If device doesn't respond it will be marked offline (not available) + */ +def ping() { + TRACE("ping") + boseSendGetNowPlaying() +} + +/** + * Schedule a 2 minute poll of the device to refresh the + * tiles so the user gets the correct information. + */ +def startPoll() { + TRACE("startPoll") + unschedule() + // Schedule 2 minute polling of speaker status (song average length is 3-4 minutes) + def sec = Math.round(Math.floor(Math.random() * 60)) + //def cron = "$sec 0/5 * * * ?" // every 5 min + def cron = "$sec 0/2 * * * ?" // every 2 min + log.debug "schedule('$cron', boseSendGetNowPlaying)" + schedule(cron, boseSendGetNowPlaying) } /** @@ -316,14 +342,6 @@ def onAction(String user, data=null) { return actions } -/** - * Called every so often (every 5 minutes actually) to refresh the - * tiles so the user gets the correct information. - */ -def poll() { - return boseRefreshNowPlaying() -} - /** * Joins this speaker into the everywhere zone */ @@ -837,6 +855,10 @@ def boseRefreshNowPlaying(delay=0) { return boseGET("/now_playing") } +def boseSendGetNowPlaying() { + sendHubCommand(boseGET("/now_playing")) +} + /** * Requests the list of presets * @@ -1014,4 +1036,8 @@ def boseGetDeviceID() { */ def getDeviceIP() { return parent.resolveDNI2Address(device.deviceNetworkId) +} + +def TRACE(text) { + log.trace "${text}" } \ No newline at end of file diff --git a/smartapps/smartthings/bose-soundtouch-connect.src/bose-soundtouch-connect.groovy b/smartapps/smartthings/bose-soundtouch-connect.src/bose-soundtouch-connect.groovy index 2916215..883c6e8 100644 --- a/smartapps/smartthings/bose-soundtouch-connect.src/bose-soundtouch-connect.groovy +++ b/smartapps/smartthings/bose-soundtouch-connect.src/bose-soundtouch-connect.groovy @@ -19,9 +19,9 @@ author: "SmartThings", description: "Control your Bose SoundTouch speakers", category: "SmartThings Labs", - iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", - iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", - iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png", + iconUrl: "https://d3azp77rte0gip.cloudfront.net/smartapps/fcf1d93a-ba0b-4324-b96f-e5b5487dfaf5/images/BoseST_icon.png", + iconX2Url: "https://d3azp77rte0gip.cloudfront.net/smartapps/fcf1d93a-ba0b-4324-b96f-e5b5487dfaf5/images/BoseST_icon@2x.png", + iconX3Url: "https://d3azp77rte0gip.cloudfront.net/smartapps/fcf1d93a-ba0b-4324-b96f-e5b5487dfaf5/images/BoseST_icon@2x-1.png", singleInstance: true ) @@ -104,7 +104,7 @@ def deviceDiscovery() return dynamicPage(name:"deviceDiscovery", title:"Discovery Started!", nextPage:"", refreshInterval:refreshInterval, install:true, uninstall: true) { section("Please wait while we discover your ${getDeviceName()}. Discovery can take five minutes or more, so sit back and relax! Select your device below once discovered.") { - input "selecteddevice", "enum", required:false, title:"Select ${getDeviceName()} (${numFound} found)", multiple:true, options:devices + input "selecteddevice", "enum", required:false, title:"Select ${getDeviceName()} (${numFound} found)", multiple:true, options:devices, submitOnChange: true } } } @@ -196,6 +196,8 @@ def addDevice(){ d = addChildDevice(getNameSpace(), getDeviceName(), dni, newDevice?.value.hub, [label:"${deviceName}"]) d.boseSetDeviceID(newDevice.value.deviceID) log.trace "Created ${d.displayName} with id $dni" + // sync DTH with device, done here as it currently don't work from the DTH's installed() method + d.refresh() } else { log.trace "${d.displayName} with id $dni already exists" }