Merge pull request #525 from Yaima/master

Better exception handling for Ecobee
This commit is contained in:
Yaima
2016-02-18 15:13:34 -08:00

View File

@@ -257,21 +257,16 @@ def getEcobeeThermostats() {
} }
} else { } else {
log.debug "http status: ${resp.status}" log.debug "http status: ${resp.status}"
//refresh the auth token }
if (resp.data.status.code == 14) { }
log.debug "Storing the failed action to try later" } catch (groovyx.net.http.HttpResponseException e) {
log.trace "Exception polling children: " + e.response.data.status
if (e.response.data.status.code == 14) {
atomicState.action = "getEcobeeThermostats" atomicState.action = "getEcobeeThermostats"
log.debug "Refreshing your auth_token!" log.debug "Refreshing your auth_token!"
refreshAuthToken() refreshAuthToken()
} else {
log.error "Authentication error, invalid authentication method, lack of credentials, etc."
} }
} }
}
} catch(Exception e) {
log.debug "___exception getEcobeeThermostats(): " + e
refreshAuthToken()
}
atomicState.thermostats = stats atomicState.thermostats = stats
return stats return stats
} }
@@ -450,23 +445,15 @@ def pollChildren(child = null) {
} }
result = true result = true
log.debug "updated ${atomicState.thermostats?.size()} stats: ${atomicState.thermostats}" log.debug "updated ${atomicState.thermostats?.size()} stats: ${atomicState.thermostats}"
} else { }
log.error "polling children & got http status ${resp.status}" }
} catch (groovyx.net.http.HttpResponseException e) {
//refresh the auth token log.trace "Exception polling children: " + e.response.data.status
if (resp.data.status.code == 14) { if (e.response.data.status.code == 14) {
atomicState.action = "pollChildren" atomicState.action = "pollChildren"
log.debug "Refreshing your auth_token!" log.debug "Refreshing your auth_token!"
refreshAuthToken() refreshAuthToken()
} }
else {
log.error "Authentication error, invalid authentication method, lack of credentials, etc."
}
}
}
} catch(Exception e) {
log.debug "___exception polling children: " + e
refreshAuthToken()
} }
return result return result
} }
@@ -642,8 +629,6 @@ private refreshAuthToken() {
} }
atomicState.action = "" atomicState.action = ""
} else {
log.debug "refresh failed ${resp.status} : ${resp.status.code}"
} }
} }
} catch (groovyx.net.http.HttpResponseException e) { } catch (groovyx.net.http.HttpResponseException e) {
@@ -651,7 +636,7 @@ private refreshAuthToken() {
def reAttemptPeriod = 300 // in sec def reAttemptPeriod = 300 // in sec
if (e.statusCode != 401) { // this issue might comes from exceed 20sec app execution, connectivity issue etc. if (e.statusCode != 401) { // this issue might comes from exceed 20sec app execution, connectivity issue etc.
runIn(reAttemptPeriod, "refreshAuthToken") runIn(reAttemptPeriod, "refreshAuthToken")
} else if (e.statusCode == 401) { //refresh token is expired } else if (e.statusCode == 401) { // unauthorized
atomicState.reAttempt = atomicState.reAttempt + 1 atomicState.reAttempt = atomicState.reAttempt + 1
log.warn "reAttempt refreshAuthToken to try = ${atomicState.reAttempt}" log.warn "reAttempt refreshAuthToken to try = ${atomicState.reAttempt}"
if (atomicState.reAttempt <= 3) { if (atomicState.reAttempt <= 3) {
@@ -724,29 +709,21 @@ def sendJson(child = null, String jsonBody) {
log.debug "Error return code = ${resp.data.status.code}" log.debug "Error return code = ${resp.data.status.code}"
debugEvent("Error return code = ${resp.data.status.code}") debugEvent("Error return code = ${resp.data.status.code}")
} }
} else { }
log.error "sent Json & got http status ${resp.status} - ${resp.status.code}" }
debugEvent ("sent Json & got http status ${resp.status} - ${resp.status.code}") } catch (groovyx.net.http.HttpResponseException e) {
log.trace "Exception Sending Json: " + e.response.data.status
//refresh the auth token debugEvent ("sent Json & got http status ${e.statusCode} - ${e.response.data.status.code}")
if (resp.status.code == 14) { if (e.response.data.status.code == 14) {
atomicState.action = "pollChildren"
log.debug "Refreshing your auth_token!" log.debug "Refreshing your auth_token!"
debugEvent ("Refreshing OAUTH Token")
refreshAuthToken() refreshAuthToken()
return false }
} else { else {
debugEvent("Authentication error, invalid authentication method, lack of credentials, etc.") debugEvent("Authentication error, invalid authentication method, lack of credentials, etc.")
log.error "Authentication error, invalid authentication method, lack of credentials, etc." log.error "Authentication error, invalid authentication method, lack of credentials, etc."
return false
} }
} }
}
} catch(Exception e) {
log.debug "Exception Sending Json: " + e
debugEvent ("Exception Sending JSON: " + e)
refreshAuthToken()
return false
}
if (returnStatus == 0) if (returnStatus == 0)
return true return true