Logic Machine Forum
Create Schedule by Script - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Create Schedule by Script (/showthread.php?tid=3786)



Create Schedule by Script - DGrandes - 04.01.2022

Hi,

I need to create a lot of schedules and I have not found to do it by script in the forum. Is that possible?

Thanks!!


RE: Create Schedule by Script - admin - 04.01.2022

It's possible to do via db queries. The easiest approach is to duplicate existing schedule and change the mapped object. I can provide an example if this works for you.


RE: Create Schedule by Script - DGrandes - 05.01.2022

(04.01.2022, 16:21)admin Wrote: It's possible to do via db queries. The easiest approach is to duplicate existing schedule and change the mapped object. I can provide an example if this works for you.

Ok thanks!! Could you provide me the example?


RE: Create Schedule by Script - admin - 05.01.2022

Use this function to duplicate a scheduler.
Arguments:
1. id - source scheduler ID
2. address - object to map the new scheduler to
3. name - new scheduler name (optional)
Code:
function copyscheduler(id, address, name)
  local scheduler = db:getrow('SELECT * FROM schedulers WHERE id=?', id)
  local events = db:getall('SELECT * FROM scheduler_events WHERE scheduler=?', id)

  if scheduler then
    scheduler.id = nil
    scheduler.name = name or (scheduler.name .. ' copy')
    scheduler.object = buslib.encodega(address)

    db:insert('schedulers', scheduler)
    local newid = db:getlastautoid()
    for _, event in ipairs(events) do
      event.id = nil
      event.scheduler = newid
      db:insert('scheduler_events', event)
    end
  end

  io.writefile('/tmp/lm-scheduler-clear', '')
end

-- duplicate scheduler ID 1, map it to 1/1/1 and set name to 'test2'
copyscheduler(1, '1/1/1', 'test2')