Httpget response is returning a null object?

I'm kind of new to working with Hubitat but am at the point I'm trying to use someone elses custom device driver that is having an issue and I'm trying to debug it. I'm using the Xbox One Smartglass Driver and the developer isn't using Hubitat anymore and they can't work on it, so I'm working on debugging the console off command that used to work, but possibly due to the Xbox REST server being newer now ??? Its not returning the device_status back to the driver as expected. Or something else changed since he wrote it.

The rest server is passing a json object back to the browser that is correct, but the line of code that reads it back into the Hubitat device driver is showing up as a null object.

httpGet([uri:"http://${restIp}:$restPORT/device?addr=$xboxIP"], { response ->
xboxStatus = response.data.devices[liveID].device_status // Storing Device Status in Variable

This is the call and xboxStatus should be set to "Available".

If I put the call directly into a browser: http://10.1.10.33:1234/device?addr=10.1.10.50

It returns this json object:

[{"liveid":"F4000D8D96E5A3F6","ip_address":"10.1.10.50","connection_state":"Disconnected","pairing_state":"NotPaired","device_status":"Available","last_error":0,"authenticated_users_allowed":true,"console_users_allowed":false,"anonymous_connection_allowed":true,"is_certificate_pending":false}]

But xboxStatus does not get set and if I add a debug log statement to add it to the log

log.debug response.data.devices[liveID].device_status

It logs an error that its a null object.

What should I be looking for here? Can someone point me at documentation on how the response variable should be written? Or if its something else point me in the right direction.

try the fx httpPostJSon instead

private authorizedHttpRequest(Map options = [:], String path, String method, Closure closure) {
def attempt = options.attempt ?: 0

log.debug "authorizedHttpRequest ${method} ${path} attempt ${attempt}"
try {
	def requestParameters = [
        uri: serverUrl,
        path: path,
        headers: [
            'User-Agent': userAgent,
            Authorization: "Bearer ${accessToken}"
        ]
    ]

	if (method == "GET") {
        httpGet(requestParameters) { resp -> closure(resp) }
    } else if (method == "POST") {
    	if (options.body) {
        	requestParameters["body"] = options.body
            log.debug "authorizedHttpRequest body: ${options.body}"
            httpPostJson(requestParameters) { resp -> closure(resp) }
        } else {
    		httpPost(requestParameters) { resp -> closure(resp) }
        }
    } else {
    	log.error "Invalid method ${method}"
    }
} catch (groovyx.net.http.HttpResponseException e) {
    if (e.response?.data?.status?.code == 14) {
    	if (attempt < 3) {
            refreshAccessToken()
            authorizedHttpRequest(path, mehod, closure, body: options.body, attempt: attempt++)
        } else {
        	log.error "Failed after 3 attempts to perform request: ${path}"
        }
    } else {
    	log.error "Request failed for path: ${path}.  ${e.response?.data}"
    }
}

}

Is that the literal string returned in the browser? If so, looks like an array of size 1.

This line seems wrong:
response.data.devices[liveID].device_status

Try this:

response.data[0].device_status

Awesome. Thanks Tom, that was what I needed. Works great now. This is in the Xbox One Smartglass Driver that someone wrote last year and I plugged it in as a user app last week and couldn't get the off function to work. He said it used to work, so I have no idea what may have happened. I'll let him know there's a fix for it. I was searching all over for how to use the response code.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.