Concurrent dataport.wait() calls in Lua


#1

Is there a way to have multiple calls to dataport.wait() running concurrently? I have a script that processes an array (or table in Lua-speak, I suppose … I’m new to Lua) of dataports to check for certain events and generate email notifications off of those events. Here is the code:

-- This script watches under/overflow events and reports them to
-- an email address
local my_device = alias['device_alias']
local useremail = 'user@example.com'
local my_timezone = 'America/Chicago'
-- End of configuration variables

local flowEvent = {}
local lastFlowEventTS = {}

settimezone(my_timezone)

debug('Script started')

for i=1, 16 do
  local overflowName = string.format("zone%02doverflow", i)
  local overflow = my_device.alias[overflowName]
  flowEvent[i] = overflow
  if overflow.timestamp == nil then
    overflow.value = 0
  end
  lastFlowEventTS[i] = overflow.timestamp
end

debug('Overflow initialized')

for i=17, 32 do
  local underflowName = string.format("zone%02dunderflow", i - 16)
  local underflow = my_device.alias[underflowName]
  flowEvent[i] = underflow
  if underflow.timestamp == nil then
    underflow.value = 0
  end
  lastFlowEventTS[i] = underflow.timestamp
end

debug('Underflow initialized')

while true do
  for i=1, 32 do
    local TS = flowEvent[i].timestamp
    if TS > lastFlowEventTS[i] then
      if 1 == flowEvent[i][TS] then
        local subject = string.format('%s - %s',my_device.name, flowEvent[i].name)
        local message = string.format('Flow event detected\n\nDevice: %s\nEvent: %s\nTime: %s',my_device.name, flowEvent[i].name, date('%a, %d %b %Y %T', TS))
        debug(subject)
        local status,reason = email(useremail, subject, message)
        if status == false then debug('Email failed, reason: ' .. reason) end
      end
      lastFlowEventTS[i] = TS
    end
  end
end

The code seems to work just fine, but it feels wasteful to just have it continuously spinning through all 32 dataports. So I was wondering if there was a way to run wait() on all 32 dataports concurrently without having to set up a script or event/alert per dataport


#2

Hi Adam,

The script engine Exosite created does not support asynchronous behavior or multithreading. With this in mind there is no way to wait on multiple dataports concurrently.

You are correct, the way to achieve this would be to create a number of individual event/alert pairs. As a side affect of that setup, you would see a performance increase as the script above goes through many steps in a loop before completion.