Skip to content

Snippet: Retrofitting IP addressing via bindings

Justin Parker edited this page Oct 14, 2022 · 5 revisions

Many older recipes don't allow for dynamic IP addressing. Instead they only have a param_IPAddress parameter.

This snippet shows how the recipe can be easily converted.

Near the top of the recipe:

local_event_IPAddress = LocalEvent({ 'schema': { 'type': 'string' }})

def remote_event_IPAddress(arg):
  if is_blank(param_ipAddress) and local_event_IPAddress.getArg() != arg:
      local_event_IPAddress.emit(arg)
      dest = '%s:%s' % (ipAddr, param_port or DEFAULT_PORT)
      console.info('IP address updated! Will connect to TCP %s' % dest)
      tcp.setDest(dest)

Wherever main is called:

  ipAddr = None
  
  if is_blank(param_ipAddress):
    ipAddr = local_event_IPAddress.getArg()
  else:
    ipAddr = param_ipAddress
    local_event_IPAddress.emit(ipAddr)
    
  if is_blank(ipAddr):
    console.warn('No IP address configured or from binding; nothing to do or will wait.')
  else:
    dest = '%s:%s' % (ipAddr, param_port or DEFAULT_PORT)
    console.info('Will connect to TCP %s' % dest)
    tcp.setDest(dest)

Change to suit!