Http HubAction in devicetype isn't calling parse()


(Gavin Mogan) #1

Because raw socket hubactions won’t actually return results, I’ve started writing a http based middle layer. I can’t figure out how actually retrieve the response. I thought it should come back in the parse() function but as far as I can tell its not being called.

I’ve tried both methods of HubAction

def poll() {
    def pollAction = new physicalgraph.device.HubAction(
    		method: "POST",
    		body: body
    		path: "/send/<ip>/10002?timeout=1001",
    		headers: [HOST: "<ip>:54231", 'Content-type': 'application/json']
    )
    pollaction
}

and

def poll() {
    def pollAction = new physicalgraph.device.HubAction("""POST /send/<ip>/10002?timeout=1002 HTTP/1.1\r\nContent-Length: ${bodyLength}\r\nContent-type: application/json\r\nHOST: <ip>:54231\r\n\r\n${body}\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}")
    pollAction
}

Looking at a network dump (using ngrep)

  POST /send/<ip>/10002?timeout=1002
  HTTP/1.1
  Content-Length: 40
  Content-type: application/json
  HOST: <ip>:54231
  ["<username>\r", "<password>\r", "POWR?   \r"]..

Response:

  HTTP/1.1 200 OK
  X-Powered-By: Express
  Content-Type: application/json; charset=utf-8
  Content-Length: 43
  X-Response-Time: 1005.299ms
  Date: Thu, 18 Sep 2014 04:32:28 GMT
  Connection: keep-alive

  ["Login:","\r\nPassword:","\r\n1\r","\r\n"]

(Tony Gutierrez) #2

I’m stuck as well. I see people using parse, but I cant get it to fire.


(Geko) #3

To receive HTTP response your deviceNetworkId must be set properly.


(Tony Gutierrez) #4

You’re the man. I also had to convert the ip to hex, and then copy that hex into the device properties screen for the networkid.


(Tony Gutierrez) #5

Any advice on how to let parse know which request it is parsing? I am assuming this is asynchronous, so how can I send a tag or identifier with the request that I can read in parse() to know what request I am parsing?


(Geko) #6

Implement a state machine or just save your last request in the state variable.


(Scott G) #7

@geko you can use ‘state’ variable in a DeviceType? I only saw it as part of smartapp documentation. any examples?


(Mike Maxwell) #8

Easy peasy, state.foo = "bar"
Then later if (state.foo == “bar”) punt…


(Scott G) #9

Haha, thanks Mike! Don’t I feel foolish…


(Mike Maxwell) #10

Shouldn’t, I’ve spent hours trolling device code trying to understand and remember these tid-bits…


(Tony Gutierrez) #11

I dont see how that would help:

Req1, state = req1
req2, state = req2
req1 response comes in, state = req2