Help me - want to change http to asynchttp

Standard prefix - not a programmer and I don't know Java or Groovy

One of the apps I use with HE is SleepIQ, which creates presence sensors from my Sleep Number bed. Work pretty well, when I have it poll every 5 minutes. But I know from past experiments that when it polled every minute, other devices on my HE would respond slowly.

I was looking at the code yesterday and there's a section where httpGet and httpPut are used. How difficult would it be to convert these to use asynchttp? I have cut and paste the section that uses httpGet as an example:

try {
def statusParams = [
uri: ApiUriBase() + '/rest/bed/familyStatus?_k=' + state.session?.key,
headers: [
'Content-Type': 'application/json;charset=UTF-8',
'Host': ApiHost(),
'User-Agent': ApiUserAgent(),
'Cookie': state.session?.cookies,
'DNT': '1',
],
]
httpGet(statusParams) { response ->
if (response.status == 200){
// log.trace "doStatus() Success - Request was successful: ($response.status) $response.data"
state.requestData = response.data
} else {
// log.trace "doStatus() Failure - Request was unsuccessful: ($response.status) $response.data"
state.session = null
state.requestData = [:]
}
}
} catch(Exception e) {
if (alreadyLoggedIn) {
// log.error "doStatus() Error ($e)"
} else {
// log.trace "doStatus() Error ($e)"
doLogin()
}
}
}

I was reading this post. But I'm not understanding how to change things, because whatever I try doesn't give an error, but the status doesn't update. So, obviously, I don't know what I'm doing.

I am not an expert by any means, but something like this should do the trick. I don't have the complete code, nor do I have that bed to test so you may need to play with the code.

  try {
def statusParams = [
    uri: ApiUriBase() + '/rest/bed/familyStatus?_k=' + state.session?.key,
    headers: [
        'Content-Type': 'application/json;charset=UTF-8',
        'Host': ApiHost(),
        'User-Agent': ApiUserAgent(),
        'Cookie': state.session?.cookies,
        'DNT': '1' ]
    ]
asynchttpGet("processCallBack", statusParams)
 } catch(Exception e) {
if (alreadyLoggedIn) {
    // log.error "doStatus() Error ($e)"
} else {
    // log.trace "doStatus() Error ($e)"
    doLogin()
}
}
def processCallBack(response, data) {
if (response.status == 200){
    // log.trace "doStatus() Success - Request was successful: ($response.status) $response.data"
    state.requestData = response.data
} else {
    // log.trace "doStatus() Failure - Request was unsuccessful: ($response.status) $response.data"
    state.session = null
    state.requestData = [:]
}
}

Let me know if that works.

1 Like

Thanks!! I'll give this a shot tonight.