Response from sendHubCommand


(Convinced ST will never be unbroken…) #1

Could someone here please provide a quick and dirty example on how to parse a response from something like the following GET within a SmartApp?

sendHubCommand(new physicalgraph.device.HubAction("""GET /?${settings.params} HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
}

Thanks,

-sip


(Patrick Stuart [@pstuart]) #2

I have no idea if this is officially supported, but what I was able to glean from various code samples was this will trap the response async:

def locationHandler(evt) {
    def description = evt.description
    def hub = evt?.hubId    
    log.debug "cp desc: " + description
}

Hope that helps. Would love to see a more robust way to get the message, but splitting out the description isn’t that hard to get responses. Most likely your responses will be in base64 and will have to decode it.


(Convinced ST will never be unbroken…) #3

Is this actually working in an app for you… I ask 'cause it isn’t here. My send command looks like:

def eventHandler(evt) {
	sendHttp()
}

def sendHttp() {
def ip = "${settings.server}:${settings.port}"
def deviceNetworkId = "1234"
sendHubCommand(new physicalgraph.device.HubAction("""GET /${settings.HAMBcommand} HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
}

But with your handler, all I see in the console is:

5:32:22 PM: debug Executing GET /query/info HTTP/1.1 
HOST: 10.0.0.31:80 

on Home Hub via sendHubCommand

(Patrick Stuart [@pstuart]) #4

Yes, I am using that code to get the event response and parse out the response from my server, which sends back the status code in base64.

    def sendHttp() {
def ip = "${settings.server}:(insertPort#)"
def deviceNetworkId = "(InsertHexIPHere)"

sendHubCommand(new physicalgraph.device.HubAction("""GET /${settings.command} HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))

log.debug("sent test command")
log.debug location.hubs*.firmwareVersionString.findAll { it }
}


def locationHandler(evt) {
    def description = evt.description
    def hub = evt?.hubId
    
    log.debug "cp desc: " + description
    if (description.count(",") > 4)
    {
def bodyString = new String(description.split(',')[5].split(":")[1].decodeBase64())
log.debug(bodyString)
}

This is the exact code I am using and I do get a response back. I parse out the ones that have less than 4 count which are more likely system events, and get the one I am looking for which the 6th item contains the body response in base64 and have to decode it.

It could be the fact that you are not properly HEX converting the destination IP?


(Convinced ST will never be unbroken…) #5

Ok… here is the code:

def locationHandler(evt) {
	log.debug "We got to location handler"
    def description = evt.description
    def hub = evt?.hubId    
    log.debug "cp desc: " + description
}

def eventHandler(evt) {
	sendHttp()
}

def sendHttp() {
def ip = "${settings.server}:${settings.port}"
def deviceNetworkId = "0A00000C:1F90"
sendHubCommand(new physicalgraph.device.HubAction("""GET /${settings.HAMBcommand} HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
}

Server address is 10.0.0.12:8080 so think the Hex is correct. Request shows up on the server and response is a simple text string, but I don’t even get into the locationhandler (nothing in the log).


(Patrick Stuart [@pstuart]) #6

stupid question, but have you confirmed a response outside of smartthings?


(Convinced ST will never be unbroken…) #7

Yes. When I:

curl http://10.0.0.12:8080/?allOFF

I get back:

AllOFF command received. 

And in this test, HAMBcommand does in fact = “?allOFF”


(Patrick Stuart [@pstuart]) #8

not exactly the same, curl doesn’t send headers, to simulate smartthings, do the request from a web browser and see if you get a response.

i think you are really needing raw socket support, but that is just a guess.

let me know if u get a response from a web browser…


(Convinced ST will never be unbroken…) #9

Yes, I always get that exact response from a browser.


(Patrick Stuart [@pstuart]) #10

ok, so does the command register on the device and you aren’t getting the response then?

if so, its possible that smartthings isn’t keeping the connection alive long enough for the response?

wish the documentation was much better on these commands.


(Convinced ST will never be unbroken…) #11

Yes, server dumps this to the log (10.0.0.29 is the ST hub):

Received from 10.0.0.29 at 6:03:47 PM on 6/30/14
GET /?allOFF HTTP/1.1



HOST: 10.0.0.12:8080

I dunno about SmartThings killing the connection. I built a device-type that is using hubaction to the same IP, and am getting back a response to the parse handler.