Best way of polling a device

Sorry if this was asked before, but I could not find something in the forum within the context of edge drivers.

I am writing an edge driver for a LED strip which can be controlled over wifi or infrared. Unfortunately, the device does not support sending an event whenever the properties change over infrared, so I will have to implement some kind of polling. Since the device is not battery powered, this is not such a major concern.

My question is which is the best way to implement polling. For the moment I have create a periodic timer in the cosock thread during device initialization and it appears to be working properly. The timer is stopped on device removal. Is this the correct way to implement this? Do you think that device initialization and removal are the best places to set up the timer? Are there any recommendations for an update interval?

Thank you in advance.

I’m no expert, but my experience with edge zigbee drivers, the timers need to relaunch them:

  • when the hub reboots due to a firmware update or power failure and there are no ups.
  • if the driver version is updated
  • If a driver change is made on the device.

I put the code to relaunch the timer in the lifecycle init, which runs in the three previous cases.

As it is to poll the status of the device, it is not necessary to check if it was already running before relaunching it, since whenever init is called, it is sure that it is already stopped.

In case you have multiple timers and they may or may not be running, what I do is save a variable in permanent memory to know which timers were running, to relaunch only those.

set_field(field, value, addtional_params)
get_field(field)

https://developer-preview.smartthings.com/edge-device-drivers/device.html#st.Device.set_field

For example

  lifecycle_handlers = {
  init = do_init,
  infoChanged = do_Preferences,
  removed = do_removed
  },


local function do_init (self, device)

 --- ReStart Timer color changing function if active -----
 if device:get_field("colorChanging") == "Active" then 
   device:set_field("colorChanging", "Continue", {persist = true})
   driver_handler.color_Changing_handler(self,device,"Continue")
 else
  device:emit_event(color_Changing.colorChanging("Inactive"))
 end

  ---- restart random on-off timer if active -----
  if device:get_field("random_state") == "Active" then  
    driver_handler.random_on_off_handler(self,device,"Active")
  end

end