Need Help with SmartApp HTTP GET

smartapp

(Drew) #1

I’m trying to create a SmartApp that displays text on my Amcrest ProHD 1080P (1920TVL) video feed. To do this, I need to send a HTTP GET to the camera that’s on the same local network as my Smartthings hub. I’ve tested in the browser by setting the address and it works, though from ST it’s not returning anything.

Can someone point out what I’m missing with the following?

private updateCameraDisplay(displayText){
try {
	def userpassascii = "usernamehere:passwordhere"
	def userpass = "Basic " + userpassascii.encodeAsBase64().toString()
	def headers = [:] 
	headers.put("HOST", "192.168.0.119:80")
	headers.put("Authorization", userpass)
    headers.put("Content-Type", "text/html")
    def result = new physicalgraph.device.HubAction(
        method: "GET",
        path: "/cgi-bin/configManager.cgi?action=setConfig&ChannelTitle[0].Name=$displayText",
        headers: headers
    )
    log.debug (result)
} catch (e) {
	log.error "something went wrong: $e"
}
return result
}

Any help is greatly appreciated!


(Dave Gutheinz) #2

I think you are missing some items required to send data and receive a response. Below is an example from working code:

void discoverDevices() {
   def headers = [:]
   def bridgePort = state.bridgePort
   def bridgeIP = state.bridgeIP
   headers.put("HOST", "$bridgeIP:$bridgePort")
   headers.put("command", "pollForDevices")
   sendHubCommand(new physicalgraph.device.HubAction(
	[headers: headers],
	state.bridgeDNI,
	[callback: parseDiscoveredDevices]
))

}

As you can see, the sendHubCommand actually causes the data to be sent. The callback is the function that will receive and parse the return. In basic form, the callback is also called the hubActionResponse. Since I do not know the format of the return data, a good example of what you are trying to do is not easy. see this example:

void parseDiscoveredDevices(response) {
   def cmdResponse = response.headers["cmd-response"]
   def foundDevices = parseJson(cmdResponse)
       log.debug foundDevices
}

Above, i extract the cmdResponse from the ‘response’ received - particularly the header field “cmdResponse”. data could also be in response.body or other fields.

Good luck.


(Drew) #3

Hmmm… It looks adding sendHubCommand(result) to my code should work, but I get the same result. Nothing.

I’ve also tried the following to no avail:

sendHubCommand(new physicalgraph.device.HubAction("""GET /cgi-bin/configManager.cgi?action=setConfig&ChannelTitle[0].Name=$displayText HTTP/1.1\r\nHOST: 192.168.0.119:80\r\nAuthorization: Basic $userpass\r\n\r\n""", physicalgraph.device.Protocol.LAN))

Anyone know how to send fire a simple HTTP GET with basic authentication from the hub for a local address?