Facility Control

 View Only
  • 1.  running an action periodically

    Posted 10-18-2020 22:35

    Hi, I have a dashboard panel that that has a button that changes color every second because it is connected to a timer. I was wondering how I could change the frequency that it changes colors.

    Thank you!



  • 2.  RE: running an action periodically

    Posted 10-19-2020 14:40

    You can edit your timer and change the Repeat Rate:

    In PanelBuilder Mode, Click on Timers:

     

    Pick the timer you want and ajust its repeat rate:

     

     


    #DashBoard


  • 3.  RE: running an action periodically

    Posted 10-19-2020 17:56

    How would I turn that into a widget? Right now I'm using the "pause" function in order to make a delay but it only works for the first few seconds of starting the timer.


    #DashBoard


  • 4.  RE: running an action periodically

    Posted 10-19-2020 18:46

    You should not require widgets for this.   

    What are you trying to do?   

    -Are you trying to run the same thing every 100ms.   For that you use the timer.

    Or

    -Are you trying to run something, wait X ms, run something else, wait Y ms, run something else, wait Z ms, run something else, etc.     For that, you use the pause function.   

    You can either run a task, pause, run a second task, pause, run a third task, pause, etc.

    Or you can use the "ogscript.pause" method inside your tasks.   BUT that method does not work in a normal task, because we do NOT want the user to be able to pause the UI so that DashBoard freezes.   Instead, you have to run it in an asyncexec block.   Like this:

     

    function runTask() {
      ogscript.debug("Do the first thing");
      ogscript.pause(1000);
      ogscript.debug("Do the second thing");
      ogscript.pause(3000);
      ogscript.debug("Do the third thing");
      ogscript.pause(500);
      ogscript.debug("Do the fourth thing");
    }

    // Run the runTask function in the background in 1 milliseconds
    ogscript.asyncExec(runTask, 1);

     

     

     

    Here is the whole panel with that code in a button:

    <abs contexttype="opengear" id="_top" keepalive="false">
    <button buttontype="push" height="61" left="137" name="Run 4 things with timing" top="167" width="209">
    <task tasktype="ogscript">function runTask() {
    ogscript.debug("Do the first thing");
    ogscript.pause(1000);
    ogscript.debug("Do the second thing");
    ogscript.pause(3000);
    ogscript.debug("Do the third thing");
    ogscript.pause(500);
    ogscript.debug("Do the fourth thing");
    }

    // Run the task in 1 milliseconds
    ogscript.asyncExec(runTask, 1);</task>
    </button>
    </abs>

     

     


    #DashBoard


  • 5.  RE: running an action periodically

    Posted 10-20-2020 00:55

    I have a timer that's linked to an array of buttons. Every second it changes from red to blue by linking this code to a timer :

    if (params.getValue('delayTime', 0)%2 == 0 )
    {
    changeButtons("style:button_Style2")
    } else {
    changeButtons("style:button_Style1")
    }

    What I want to do is change the frequency to change the buttons faster or slower given a certain input. 

     


    #DashBoard


  • 6.  RE: running an action periodically

    Posted 10-20-2020 14:07

    I unfortunately don't think you can change the frequency of a timer once the timer has been created.   I don't see any methods in DashBoard to do that.

    But you can still acheive what you want using a recursive function and asyncexec (which calls a function after a specific delay).   

    Here is a panel that does that:

     

    <abs contexttype="opengear" id="_top" keepalive="false">
    <meta>
    <params>
    <param access="1" constraint="0.0;2000.0;0.0;2000.0;1" constrainttype="INT_STEP_RANGE" name="interval" oid="interval" precision="0" type="INT16" value="0" widget="fader"/>
    </params>
    </meta>
    <button buttontype="push" height="69" id="button" left="140" name="Start" top="289" width="203">
    <task tasktype="ogscript">function flashLights() {

    color = ogscript.getObject("nextcolor");
    if (color != null &amp;&amp; color == "red") {
    ogscript.setStyle("button", "bg#ff0000");
    ogscript.putObject("nextcolor", "blue");
    } else {
    ogscript.setStyle("button", "bg#0000ff");
    ogscript.putObject("nextcolor", "red");
    }

     

    var interval = params.getValue("interval",0);
    if (interval != null &amp;&amp; interval &gt; 0) {
    // Run the task in 1 milliseconds
    ogscript.asyncExec(flashLights, interval);
    }
    }


    flashLights();</task>
    </button>
    <param expand="true" height="489" left="431" oid="interval" showlabel="false" top="128" width="171"/>
    </abs>

     

     

     

    If has a fader bar that controls how fast or slow the button changes styles.  BTW, If it goes all the way down to 0, it stops it completely, and you have to click on start again to get it going.   You can change that if you want, perhaps using a second parameter to decide if the button is blinking or not, and changing the range on the interval parameter so that it can't go down below a certain threshold, like 10.

     

    The important part is this:

    Lines 3-10 just decide what color the button should be next and sets the style.   

    Line 13 gets the interval between flashes (the fader bar value)

    Line 14-17 - if the interval is more than 0, then schedule a call flashLights again after the interval.

    Line 21 starts the whole process off.


    #DashBoard