sendHubCommand result parsing

I know this question has been asked many times before as I have searched the forum for answers to this before posting and found lots of things touching on the subject but not been able to work out my exact issue.

I have this function:

def String getAuthToken(dni) {
def challenge = challengeGenerator()
def commandText = "{\"challenge\":\"$challenge\"}"    
def httpRequest = [
    path: "/xled/v1/login",
  	method: "POST",
    headers: [
		HOST: "$deviceIP:80",
		"Content-Type":	"application/json"
	],
    body: "$commandText"
]
try {
	def hubAction = new physicalgraph.device.HubAction(httpRequest, null, [callback: parse])
    log.debug "hub action: $hubAction"
    return sendHubCommand(hubAction)
}
catch (Exception e) {
	log.debug "Hit Exception $e on $hubAction"
}
}

Then this function to parse the result:

def parse(output) {
log.debug "Starting response parsing on ${output}"

def headers = ""
def parsedHeaders = ""

def msg = output

def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers      // => headers as a Map
def body = msg.body              // => request body as a string
def status = msg.status          // => http status code of the response
def json = msg.json              // => any JSON included in response body, as a data structure of lists and maps
def xml = msg.xml                // => any XML included in response body, as a document tree structure
def data = msg.data              // => either JSON or XML in response body (whichever is specified by content-type header in response)

log.debug "headers: ${headerMap}, status: ${status}, body: ${body}, data: ${data}"

if (status == 200) {
	if (body.contains("authentication_token")) {
		def authToken = null
        body = new groovy.json.JsonSlurper().parseText(body)
        log.debug "$body"
        authToken = body['authentication_token']

        if (authToken != null && authToken != "") {
            log.debug "Auth Token: $authToken"
            sendEvent(name: "authToken", value: "$authToken")
            verify()
		}
    }
}
else {
	log.debug "Unable to locate device on your network"
}
}

It works fine in one device handler and one SmartApp, but I just created a new device handler and copied and pasted it and nothing! It’s doing my head in, as it didn’t work in the SmartApp until I deleted it and rebuilt it from the code.

PLEASE HELP.

What is not working? What can you see in the live logging? What error message is there?

Those debug lines give you a good indication in the live logging where things go wrong. And groovy error messages too…

More details please!

Sorry I should have said, the debug works fine to sending the command. However it never makes it into the parse function.

76336a5b-8af7-4e13-b21a-3ed3bc432990 14:59:19: debug hub action: POST /xled/v1/login HTTP/1.1
Accept: */*
User-Agent: Linux UPnP/1.0 SmartThings
HOST: AN+IP+ADDRESS:80
Content-Type: application/json
Content-Length: 60

{"challenge":"RDh6ZGhtbU92TEJNZllmbUg4ZUR5cXJUMnkxUXpFRVM="}
76336a5b-8af7-4e13-b21a-3ed3bc432990 14:59:19: debug Encoded challenge: RDh6ZGhtbU92TEJNZllmbUg4ZUR5cXJUMnkxUXpFRVM=
76336a5b-8af7-4e13-b21a-3ed3bc432990 14:59:19: debug Created challenge: D8zdhmmOvLBMfYfmH8eDyqrT2y1QzEES

Yet it does work on another device handler and a SmartApp.

  1. Is the device on the same lan as the hub?
  2. Is the device responding to the request?
1 Like

Device and hub are on the same network.

The hub uses the same code there in order to ping the device as part of a SmartApp before creating it as a device to check it exists.

I’ve copied and pasted the code from the SmartApp to a custom device handler, and now no response. I don’t have the kit to check the network for the packets though.

Have you confirmed if live logging (with the default “All” selected) is showing an error?

I have, the log debug events I have posted above is all I get.

It’s like the command is built and either the hub doesn’t send it, doesn’t get a response, or just ignores the response. I don’t know how to check that the send and receive has actually occured without somehow using a MITM setup on the hub.

My guess is that it isn’t being sent. What is calling the getAuthToken() method and what are you doing with the return value?

Every action calls it as you need to be authenticated for them, so rather than try to keep it alive on a timer it’s called on each action.

Is there an easy way to get between the hub and the device to monitor traffic? Neither has a proxy option :frowning:

Just out of curiosity. Are you running the same time the SmartApp and the DH with the same code? Are you sure that you are trying to communicate with the right device?

No, SmartApp is run once to create the device then never used again.

Definitely the right device, if I call the same IP and path with the challenge from a REST browser add-on I get the expected return.

I’d like to thank all who responded for your time trying to solve this noobs question. I seem to have managed to resolve it.

Turns out deviceNetworkId MUST be all caps for IP addresses! My hex had lowercase letters, changed to uppercase and it’s getting a return.

1 Like

Sorry back again. I thought it was the Device Network Id which was the issue. Turns out I also set the “Hub” at the same time, and this was truly what solved the issue.

So I have added hub selection to my installer SmartApp, this should solve the problem. I can now gladly say I am talking to my lights.

Good to hear that. I hope you will continue the Twinkly development. :wink:

1 Like