This guide on LAN drivers indicates that you can access the IP and port of a device through the device object itself:
local ip = device.ip -- found previously via discovery
local port = device.port -- found previously via discovery
How did they populate this in the device? How can it be updated? I know these during discovery of my devices, but am currently storing them somewhere else. I tried passing them in during device creation, but no luck. It would be great if they followed the device object as they do in this guide.
Does it? Or does it just use
device.port as placeholders in the sample code to represent the IP address and port of the device? It isn’t clear.
I don’t really know anything about the various discovery protocols but I formed the impression is that the idea is for the discovery process to identify devices by a persistent unique ID and then for the ‘init’ process to be used to figure out anything transient like the IP address. That may, of course, be easier said than done.
I think the driver guide is merely suggesting what YOU can do. There is NO inherent network logic built into a smart edge driver, only whatever you put in. For example, to go with the statement above, you may get an IP/Port within your discovery process. In my case I’m using multicast UDP so I have the discovery routine use udp:sendto(…) to a multicast address that my devices listens for, and it then responds to the sendto and THEN I can capture my devices IP address from the response. BTW, this is an example - you can’t use an IP address used during discovery for very long - the device could reboot at some time and get a new IP address. I my case I’m also getting the address during init (when the hub or driver restarts) and in a refresh process that I’ve got tied to a timer.
In terms of storing things with the device - you can use persistence with the driver to store additional data. Look at transient_store for example in the device object (Device — SmartThings Edge Device Drivers documentation). There is also longer term persistent storage, but an IP address is something I’d treat as transient.
Ok. That is essentially what I am doing right now. I find the IP address in the device_init and store it using
device:set_field(). Then I use it elsewhere after that point by using
device:get_field(). I just got the impression that I could use some sort of built in support for those fields.