This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Cancel event-scripts
#9
Here's a complete example with two sequences - one for "on" event, another for "off". 32/1/1 is the object that triggers execution. Delay between each command is fixed at 0.5 seconds. This can be made adjustable via sequence table, but it must not be 0, otherwise sequence thread won't be able to pass control to localbus monitor thread.

Code:
if not client then
  require('copas')

  seqon = {
    { address = '1/1/1', value = true },
    { address = '32/1/2', value = 42 },
    { address = '32/1/3', value = 43 },
    { address = '32/1/4', value = 44 },
    { address = '32/1/5', value = 45 },
  }

  seqoff = {
    { address = '1/1/1', value = false },
    { address = '32/1/2', value = 53 },
    { address = '32/1/3', value = 54 },
    { address = '32/1/4', value = 55 },
    { address = '32/1/5', value = 56 },
  }

  function eventhandler(event)
    if event.dst == '32/1/1' then
      local val = busdatatype.decode(event.datahex, dt.bool)

      sequence = val and seqon or seqoff
      seqpos = 1

      copas.wakeup(seqthread)
    end
  end

  client = require('localbus').new()
  client:sethandler('groupwrite', eventhandler)

  copas.addserver(client.sock, function()
    client.sock = copas.wrap(client.sock)
    while true do
      client:step()
    end
  end, 1)

  seqthread = copas.addthread(function()
    copas.sleep(-1) -- put thread to sleep until wakeup is called

    while true do
      local curr = sequence[ seqpos ]

      grp.write(curr.address, curr.value)
      seqpos = seqpos + 1

      -- end of sequence
      if seqpos > #sequence then
        copas.sleep(-1)
      else
        copas.sleep(0.5)
      end
    end
  end)
end

copas.step()
Reply


Messages In This Thread
Cancel event-scripts - by buuuudzik - 08.02.2019, 09:23
RE: Cancel event-scripts - by Daniel - 08.02.2019, 09:53
RE: Cancel event-scripts - by admin - 08.02.2019, 10:24
RE: Cancel event-scripts - by buuuudzik - 08.02.2019, 14:52
RE: Cancel event-scripts - by Daniel - 08.02.2019, 14:56
RE: Cancel event-scripts - by buuuudzik - 08.02.2019, 15:11
RE: Cancel event-scripts - by admin - 10.02.2019, 11:32
RE: Cancel event-scripts - by buuuudzik - 10.02.2019, 12:18
RE: Cancel event-scripts - by admin - 11.02.2019, 12:45
RE: Cancel event-scripts - by JASK - 07.01.2023, 00:14
RE: Cancel event-scripts - by buuuudzik - 11.02.2019, 14:14
RE: Cancel event-scripts - by khalil - 08.01.2023, 07:48
RE: Cancel event-scripts - by admin - 11.02.2019, 15:40
RE: Cancel event-scripts - by apkotelnikov - 12.02.2019, 23:26
RE: Cancel event-scripts - by buuuudzik - 13.02.2019, 07:37
RE: Cancel event-scripts - by admin - 13.02.2019, 07:49
RE: Cancel event-scripts - by admin - 09.01.2023, 07:49
RE: Cancel event-scripts - by JASK - 30.01.2023, 22:56
RE: Cancel event-scripts - by admin - 31.01.2023, 07:50

Forum Jump: