Skip to content

Commit

Permalink
v0.9 Added some defensive code for rescheduling when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
yracine committed Oct 24, 2015
1 parent 486ae54 commit 171fcc3
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions smartapps/myAutomaticServiceMgr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def about() {
section("About") {
paragraph "MyAutomaticServiceMgr, the smartapp that connects your Automatic connected vehicle(s) to SmartThings via cloud-to-cloud integration" +
" and polls your Automatic device's events on a regular interval"
paragraph "Version 0.8.9"
paragraph "Version 0.9"
paragraph "If you like this smartapp, please support the developer via PayPal and click on the Paypal link below "
href url: "https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=yracine%40yahoo%2ecom&lc=US&item_name=Maisons%20ecomatiq&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest",
title:"Paypal donation..."
Expand Down Expand Up @@ -368,21 +368,63 @@ def initialize() {
create_child_devices()


state?.poll = [ last: 0, rescheduled: now() ]
Integer delay = givenInterval ?: 30 // By default, do it every 30 min.
if ((delay < 5) || (delay>59)) {
state?.msg= "MyAutomaticServiceMgr>scheduling interval not in range (${delay} min), exiting..."
log.debug state.msg
runIn(30, "sendMsgWithDelay")
runIn(30,"sendMsgWithDelay")
return
}
schedule("0 0/${delay} * * * ?", takeAction)
//Subscribe to different events (ex. sunrise and sunset events) to trigger rescheduling if needed
subscribe(location, "sunrise", rescheduleIfNeeded)
subscribe(location, "sunset", rescheduleIfNeeded)
subscribe(location, "mode", rescheduleIfNeeded)
subscribe(location, "sunriseTime", rescheduleIfNeeded)
subscribe(location, "sunsetTime", rescheduleIfNeeded)

log.trace "initialize>polling delay= ${delay}..."
rescheduleIfNeeded()
}


def rescheduleIfNeeded() {
Integer delay = givenInterval ?: 30 // By default, do poll every 30 min.
BigDecimal currentTime = now()
BigDecimal lastPollTime = (currentTime - (state?.poll["last"]?:0))
if (lastPollTime != currentTime) {
BigDecimal lastPollInMinutes= (lastPollTime/60000).round(1)
log.info "rescheduleIfNeeded>last poll was ${lastPollInMinutes.toString()} minutes ago"
}
if (((state?.poll["last"]?:0) + (delay * 60000) < currentTime) && canSchedule()) {
log.info "rescheduleIfNeeded>scheduling takeAction in ${delay} minutes.."
schedule("0 0/${delay} * * * ?", takeAction)
}

takeAction()

// Update rescheduled state

if (!evt) state.poll["rescheduled"] = now()
}

def takeAction() {
log.trace "takeAction>begin"
def MAX_EXCEPTION_COUNT=5
def msg
String exceptionCheck
Integer delay = givenInterval ?: 30 // By default, do poll every 30 min.
state?.poll["last"] = now()

//schedule the scheduleIfNeeded() function

if (((state?.poll["rescheduled"]?:0) + (delay * 60000)) < now()) {
log.info "takeAction>scheduling rescheduleIfNeeded() in ${delay} minutes.."
schedule("0 0/${delay} * * * ?", rescheduleIfNeeded)
// Update rescheduled state
state?.poll["rescheduled"] = now()
}


def devices = AutomaticDevices.collect { dni ->

Expand Down Expand Up @@ -650,6 +692,6 @@ def getChildName() { "My Automatic Device" }

def getServerUrl() { return getApiServerUrl() }

def getSmartThingsClientId() { "insert your public key here!" }
f getSmartThingsClientId() { "insert your public key here!" }

def getSmartThingsPrivateKey() { "insert your private key here!" }

0 comments on commit 171fcc3

Please sign in to comment.