Facility Control

 View Only
Expand all | Collapse all

Scheduler tasks triggering

  • 1.  Scheduler tasks triggering

    Posted 01-09-2019 12:43
    Hello all,

    I want to trigger 3 task on same time, each task assigned to button. Each button have an trigger id - so i can fireGPI locally to trigger that button. I want to trigger then in same time each day - so i need to trigger it from timer task , timer should be 'simple clock' as i understand. The problem is that i don't understand how to translate the value of 'simple timer' to time. I got something like this:
    event.getCurrentValue(); i got: 1.547037143131E12 on parameter 'text field' , or 1547037143131 on simple label.

    How can i do it right way?

    Thank you,

    Alex.


  • 2.  RE: Scheduler tasks triggering

    Posted 01-09-2019 18:03
    Ok, after a little research i understand that this value (1547037143131) is UTC time in milliseconds.

    Anyway, question about scedulere still relevant😀
    #DashBoard


  • 3.  RE: Scheduler tasks triggering

    Posted 01-09-2019 18:33

    There are javascript methods to get the day, month, year, etc from a UTC time.

    BUT, if your goal is to run something at a specific time each day, then using a timer is overkill. It will run a task every minute (or every second) to simply check if it's the right time, and most of the time do nothing.

    You can use asyncexec to schedule something to run at a specific time. Or more precisely, asyncexec runs something after a specific time interval. You just need to calculate what that interval is based on the current time and the time at which you want to run something. I've attached a panel to this post that I made for someone else. It runs something every day at 11:05:00.

    In that panel, I have the following api code:

    function runAtTime(time)
    {
    function recallMem()
    {
    ogscript.debug("DO MY TASK");
    scheduleTomorrow(time); //Reschedule
    }

    var now = new Date();
    var delay = time.getTime() - now.getTime();
    if (delay < 0)
    {
    delay = 0;
    }

    ogscript.asyncExec(recallMem, delay);
    }

    This function runs the task at the scheduled time. The task is within the function, and it’s a function called recallMem. All it does it do a debug statement, and reschedule itself for tomorrow. You would replace that debug statement with something that calls your 3 tasks.

    What we do, is we get the current time (called now). We calculate the difference between the time when we want to run it, and the current time. If it’s less than 0 (i.e. in the past), we make it 0.

    Then we call asyncExec. This method runs a function, after a specific delay.

    We also have the following 2 functions:

    function scheduleToday(time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    if (newTime.getTime() - today.getTime() < 0) //IF we are too late for today
    {
    ogscript.debug("Too late for today.");
    scheduleTomorrow(time);
    }
    else
    {
    ogscript.debug("Scheduled task for today: " + newTime);
    runAtTime(newTime);
    }
    }

    function scheduleTomorrow(time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    ogscript.debug("Scheduling for " + newTime);
    runAtTime(newTime);
    }

    scheduleToday checks if the time we specified has already passed for today. If it’s in the future, we do the schedule. If it’s already passed, then we call scheduleTomorrow.
    scheduleTomorrow calculates a date with the specified time, but for tomorrow, and schedules that task at that time.

    Finally, we have something that schedules the task at a specific time (11:05:00.000):

    var task1Time = new Date()
    task1Time.setHours(11);
    task1Time.setMinutes(5);
    task1Time.setSeconds(0);
    task1Time.setMilliseconds(0);
    scheduleToday(task1Time);

    You can change the 11 5 and 0 to be the exact time you want. You could also get those values from a parameter instead.

    It’s a little more complex than the timers, but it’s much more precise and efficient.

    If you still want to use timers, let me know, and I can cook up an example with timers. But the above is probably a better way to do it.

    AtTime2.grid


    #DashBoard


  • 4.  RE: Scheduler tasks triggering

    Posted 01-10-2019 11:25
    Hello Bgatien,

    Thank you for that expanded answers, this is as tricky as cool!

    I'll try to integrate this script to my needs, the only differences is that i need three or more triggers each day, instead one fixed in your script. I believe i will success with that (maybe just copy whole script for each trigger time task?)

    Thanks!!
    #DashBoard


  • 5.  RE: Scheduler tasks triggering

    Posted 01-10-2019 14:43
    If you need multiple things triggered, you can write multiple "runAtTimes" function that do different things. Or you could pass in an argument to runAtTimes that says which OGP triggers to trigger.

    You can then call scheduleToday multiple times with different times and task lists.

    #DashBoard


  • 6.  RE: Scheduler tasks triggering

    Posted 01-10-2019 18:10
    If you need multiple things triggered, you can write multiple "runAtTimes" function that do different things. Or you could pass in an argument to runAtTimes that says which OGP triggers to trigger.

    You can then call scheduleToday multiple times with different times and task lists.


    Lets say i need to trigger 3 tasks (using fireGPI), 1 on 09:00, 2 on 12:00, and third on 15:00.
    How i can do it using argument ?
    #DashBoard


  • 7.  RE: Scheduler tasks triggering

    Posted 01-10-2019 18:52

    This works for me:

    function runAtTime(functionName, time)
    {
    function runGPI(gpi) {
    return function() {
    ogscript.debug("running gpi " + gpi);
    }
    }

    var now = new Date();
    var delay = time.getTime() - now.getTime();

    if (delay > 0) {
    ogscript.asyncExec(runGPI(functionName), delay);
    }
    }

    function scheduleToday(functionName, time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    if (newTime.getTime() - today.getTime() < 0) //IF we are too late for today
    {
    ogscript.debug("Too late for today.");
    scheduleTomorrow(functionName, time);
    }
    else
    {
    ogscript.debug("Scheduled task for today: " + newTime);
    runAtTime(functionName, newTime);
    }
    }

    function scheduleTomorrow(functionName, time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    ogscript.debug("Scheduling for " + newTime);
    runAtTime(functionName, newTime);
    }

    var task1Time = new Date()
    task1Time.setHours(13);
    task1Time.setMinutes(49);
    task1Time.setSeconds(0);
    task1Time.setMilliseconds(0);
    scheduleToday("trigger1", task1Time);

    task1Time.setSeconds(10);
    scheduleToday("trigger2", task1Time);
    task1Time.setSeconds(15);
    scheduleToday("trigger3", task1Time);

    It runs "trigger1" at 13:49:00. It runs "trigger2" at 13:49:10 and "trigger3" at 13:49:15. I did not use your sample times because I wanted something I could test within the next minute.

    Change the bolded line above (ogscript.debug("running gpi " + gpi);) to something that fires the right GPI.


    #DashBoard


  • 8.  RE: Scheduler tasks triggering

    Posted 01-10-2019 20:22
    I've got the concept. Thank you very much for your help, this is very helpful information.

    Alex.
    #DashBoard


  • 9.  RE: Scheduler tasks triggering

    Posted 01-14-2019 16:37

    I accidentally removed a line from the following function:

    function runGPI(gpi) {
    return function() {
    ogscript.debug("running gpi " + gpi);
    }
    }

    After you trigger your gpis, you need to schedule the task for tomorrow. Something like:
    scheduleTomorrow(gpi, time);

    You are scheduling multiple "asyncExec" tasks simultaneouly. You can have many. So you can just run runAtTime and that's it.


    #DashBoard


  • 10.  RE: Scheduler tasks triggering

    Posted 01-14-2019 18:12
    Ohh, ok. That's makes sence!

    Thank you, i will check it !
    #DashBoard


  • 11.  RE: Scheduler tasks triggering

    Posted 01-30-2020 10:49

    Hey Alex and Ben.

    Any chance that grid is still available, would very much like to have a look at it?

     


    #DashBoard


  • 12.  RE: Scheduler tasks triggering

    Posted 01-30-2020 15:04

    Hi Tom,

    It appears we cannot attach the grid file anymore in this forum since non-images are not allowed for upload; however, I can share the source code with you if that works. You can create a custom panel and paste the code below into your 'Source' tab. Hope it helps!

     

    <abs contexttype="opengear" id="_top" style="">
    <meta>
    <api>function runAtTime(functionName, time)
    {
    function recallMem1()
    {
    ogscript.debug("DO MY TASK 1");
    scheduleTomorrow(time); //Reschedule
    }

    function recallMem2()
    {
    ogscript.debug("DO MY TASK 2");
    scheduleTomorrow(time); //Reschedule
    }

    function recallMem3()
    {
    ogscript.debug("DO MY TASK 3");
    scheduleTomorrow(time); //Reschedule
    }

    var now = new Date();
    var delay = time.getTime() - now.getTime();
    ogscript.debug("delay is " + delay);

    if (delay &lt; 0)
    {
    delay = 0;
    }
    if (delay &gt; 0) {
    ogscript.debug("Schedulling " + functionName + " for " + delay);
    switch (functionName) {
    case "recallMem1": {ogscript.asyncExec(recallMem1, delay); break;}
    case "recallMem2": {ogscript.asyncExec(recallMem2, delay); break;}
    case "recallMem3": {ogscript.asyncExec(recallMem3, delay); break;}
    }
    }
    }

    function scheduleToday(functionName, time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    if (newTime.getTime() - today.getTime() &lt; 0) //IF we are too late for today
    {
    ogscript.debug("Too late for today.");
    scheduleTomorrow(functionName, time);
    }
    else
    {
    ogscript.debug("Scheduled task for today: " + newTime);
    runAtTime(functionName, newTime);
    }
    }

    function scheduleTomorrow(functionName, time)
    {
    var today = new Date();
    var newTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1, time.getHours(), time.getMinutes(), time.getSeconds(), time.getMilliseconds());
    ogscript.debug("Scheduling for " + newTime);
    runAtTime(functionName, newTime);
    }

    var task1Time = new Date()
    task1Time.setHours(13);
    task1Time.setMinutes(38);
    task1Time.setMilliseconds(0);
    scheduleToday("recallMem1", task1Time);
    </api>
    </meta>
    </abs>

    #DashBoard