• Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular

    Controlling global variable and timer from Groovy

    General Discussion
    script groovy timer
    2
    9
    3044
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      benya last edited by jkandasa

      I am planning to connect a multi-position switch with a few PIR sensors.

      When PIR1 gets tripped and switch is OFF, switch should be set to position1.
      Switch should be turned OFF after 3 minutes.

      When PIR1 gets tripped and switch is not OFF, switch should be kept in this state extra 3 minutes and then turned OFF.
      When PIR2 gets tripped, switch should be set to position2 for 3 minutes.

      To implement this scenario, I plan to define global variable to hold switch state and timer to turn switch off when timeout expires.

      Now questions:

      Should I use "Utilities->Variables repository" to define global variable?
      Could someone post a code snippet to show how to read/write global variable in the Groovy script?

      I guess timer should be "normal" (not recurring)?
      Timer should be connected to the "Turn OFF" operation?
      How could I control timer in the Groovy script (e.g. change time when it should trigger)?

      I need Groovy sample that runs some command line tool or shell script.

      jkandasa 1 Reply Last reply Reply Quote 0
      • jkandasa
        jkandasa @benya last edited by

        @benya

        Should I use "Utilities->Variables repository" to define global variable?
        Could someone post a code snippet to show how to read/write global variable in the Groovy script?

        You can get follow http://forum.mycontroller.org/category/24/scripts have a lot of examples.

        def myVar = mcApi.variable().get("my-variable")
        //Read values
        myVar.getId()  //Returns internal id(for end user not useful)
        myVar.getKey() //Key(here: my-variable)
        myVar.getValue() //value as String
        myVar.getValue2() //value2 as String
        myVar.getValue3() //value3 as String
        
        //Change value
        myVar.setValue("my-new-value") //Should be in the form of String
        
        //to save
        myVar.save()
        //or
        mcApi.variable().update(myVar)
        

        I guess timer should be "normal" (not recurring)?
        Timer should be connected to the "Turn OFF" operation?
        How could I control timer in the Groovy script (e.g. change time when it should trigger)?

        You can control your timer. Even you can control your operations. I will add example soon.

        I need Groovy sample that runs some command line tool or shell script.

        MyController scripts designed to run on MyController itself. For now, we cannot run outside (command line).

        B 2 Replies Last reply Reply Quote 0
        • B
          benya @jkandasa last edited by

          @jkandasa said:

          @benya

          I need Groovy sample that runs some command line tool or shell script.

          MyController scripts designed to run on MyController itself. For now, we cannot run outside (command line).

          Do you mean I cannot do "ps".execute() from Groovy or system("ps") from Perl?

          B 1 Reply Last reply Reply Quote 0
          • B
            benya @benya last edited by

            @benya I verified, Groovy could execute external tools. Tested with Demo system:

            return "ls".execute().text.split("\n")
            

            Result:

            {
              "mcResult": [
                "Publicbroker-wstestmosquittoorg8080mqtt",
                "h2",
                "start.bat",
                "start.sh",
                "start_.sh",
                "stop.sh",
                "test_19Kz0-tcpwantsoftru1883",
                "test_48n0f-tcpwantsoftru1883",
                "test_FzOqu-tcpwantsoftru1883",
                "test_epPgp-tcpwantsoftru1883",
                "tmp"
              ]
            }
            jkandasa 1 Reply Last reply Reply Quote 1
            • jkandasa
              jkandasa @benya last edited by

              @benya I thought you asked to execute script itself from command line.

              1 Reply Last reply Reply Quote 0
              • B
                benya @jkandasa last edited by

                @jkandasa said:

                You can get follow http://forum.mycontroller.org/category/24/scripts have a lot of examples.

                I saw only 9 topics in this list.

                Did you have a chance to prepare sample to show how to control timer?

                jkandasa 1 Reply Last reply Reply Quote 0
                • jkandasa
                  jkandasa @benya last edited by

                  @benya

                  def filters = [:];
                  //Add filter to get your timer
                  filters.put("name", "Disable-PIR-rules");
                  //your timer
                  def myTimer = mcApi.timer().get(filters);
                  
                  //Do actions
                  myTimer.setEnabled(false);
                  
                  //Update this timer
                  mcApi.timer().update(myTimer);
                  
                  B 1 Reply Last reply Reply Quote 0
                  • B
                    benya @jkandasa last edited by benya

                    @jkandasa Seems mcApi.timer().update(myTimer); causes timer to loose references to the operations and doesn't trigger operation when enabled again.

                    I had better luck with the following code:

                    def filters = [:];
                    filters.put("name", "Timer1");
                    def myTimer = mcApi.timer().get(filters);
                    
                    def timerIds = [myTimer.getId()];
                    mcApi.timer().disable(timerIds)
                    
                    mcApi.timer().enable(timerIds)
                    jkandasa 1 Reply Last reply Reply Quote 1
                    • jkandasa
                      jkandasa @benya last edited by

                      @benya Thanks for the update, let me check mcApi.timer().update(myTimer)

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post

                      0
                      Online

                      647
                      Users

                      506
                      Topics

                      3.3k
                      Posts

                      Copyright © 2015-2023 MyController.org | Contributors | Localization