Sequence to sendHubMessage from a LAN DeviceHandler


(Kelvin) #1

Does anyone have an example of a LAN device handler sending http via sendHubMessage and then getting the return response. In particular, if I call sendHubMessage in the DeviceHandler code, where is the appropriate locationHandler to catch the response? Is it the one in the service manager side of the LAN device or should I add a locationHandler to the DeviceHandler code.

I have tried a child method call into the service manager to invoke the service manager’s sendHubMessage on behalf of the child device and I see the http server catch and respond but I don’t see a response in the locationHandler. I know that invoking sendHubMessage and getting responses in the locationHandler works for ssdp discovery which is done from the service-manager.


(Geko) #2

I think this is a good example by @pstuart:

https://github.com/pstuart/smartthings/blob/master/generic_camera.groovy


(Kelvin) #3

It looks like take() is doing the setup for the http transaction sent to the hub. But normally, we would see sendHubCommand() but this is not available at the DeviceHandler level. I am missing a step. How is the equivalent of sendHubCommand effected? It looks like take() returns “hubAction”. Who invokes take() and then does the sendHubCommand. Is that magically happening in the tile invocation?

Thanks


(swanny) #4

I believe the one line that says “hubAction” actual returns the hubAction object. When the code that handles device types see that type of object returned, it automatically sends it to the hub. I think you can create an event and send it in the same way (by returning it from the function). I don’t really like that groovy allows you to return an object just by putting it alone on a line. It really obfuscates the code in cases like this.


(Kelvin) #5

Thank you all, that is what I suspected. I’ll try it and see. I’m then guessing, based on the code that the parse() function will get called with the return http response.


(Geko) #6

take() is “Image Capture” device command. It’s actually documented here:
https://graph.api.smartthings.com/ide/doc/deviceType

Look in Methods defined by SmartDevice Type Handlers

  HubAction <command name>()

The device framework will call sendHubCommand() for you.


(Kelvin) #7

Thank you, that is spot on. I missed that since I was used to sendHubCommand() from the normal app side.
More arcane SmartThings knowledge for me to process.
Thank goodness for this blog.

Hmm, the actual wording of that area definitely could use some work. I don’t think it is clear or even close to obvious what is happening/supposed to happen there.