-
-
Save CosmicPuppy/ff2b3017f9f423788f26 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Busywait Test | |
* | |
* Author: Terry Gauchat | |
* Date: 2014-04-11 | |
*/ | |
definition( | |
name: "BusyWait Test", | |
namespace: "CosmicPuppy", | |
author: "Terry Gauchat", | |
description: "BusyWait Test v0.001", | |
category: "My Apps", | |
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", | |
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png", | |
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png" | |
) | |
preferences { | |
section("Preferences"){ | |
input "mySwitch", "capability.switch", title: "Switch to flash:", required: true, multiple: true | |
input "pauseMillis", "number", title: "Pause duration in Milliseconds:", required: true | |
} | |
} | |
def installed() { | |
subscribe(app, appTouch) | |
log.debug "Installed with settings: ${settings}" | |
} | |
def updated() { | |
unsubscribe() | |
subscribe(app, appTouch) | |
log.debug "Updated with settings: ${settings}" | |
} | |
def appTouch(evt) { | |
log.debug "appTouch with Event: $evt" | |
switchesOff() | |
pause( pauseMillis ) | |
switchesOn() | |
pause( pauseMillis ) | |
switchesOff() | |
pause( pauseMillis ) | |
switchesOn() | |
log.debug "Exec complete." | |
} | |
def pause(millis) { | |
def passed = 0 | |
def now = new Date().time | |
log.debug "pausing... at Now: $now" | |
/* This loop is an impolite busywait. We need to be given a true sleep() method, please. */ | |
while ( passed < millis ) { | |
passed = new Date().time - now | |
} | |
log.debug "... DONE pausing." | |
} | |
def switchesOn() { | |
mySwitch.on() | |
def t = new Date() | |
log.debug "ON at time $t" | |
} | |
def switchesOff() { | |
mySwitch.off() | |
def t = new Date() | |
log.debug "OFF at time $t" | |
} | |
/* =========== */ | |
/* End of File */ | |
/* =========== */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment