Announcement

Collapse
No announcement yet.

Does HS support custom library functions (reusable functions)?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Does HS support custom library functions (reusable functions)?

    Hello all,

    I have about 10-15 scripts working fine, and I noticed that a lot of the code is the same across scripts. So, I thought about maintaining a library of functions that I can call from within individual scripts, and just pass the appropriate parameters and/or receive the return data.

    Does HS3 support this? If so, how do I implement it?

    thanks,

    Marco

    #2
    Have you looked at this scripting command? (see HomeSeer Help file)
    If I understand what you are asking, I think it will accomplish what you want. (Put your functions as subs of a master script, then call them with this command.)
    Attached Files
    Mike____________________________________________________________ __________________
    HS3 Pro Edition 3.0.0.548, NUC i3

    HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

    Comment


      #3
      Originally posted by Uncle Michael View Post
      Have you looked at this scripting command? (see HomeSeer Help file)
      If I understand what you are asking, I think it will accomplish what you want. (Put your functions as subs of a master script, then call them with this command.)
      This is EXACTLY what I was looking for.

      thank you!

      Comment


        #4
        You can also "include" a script file with your functions into other scripts and then call the functions directly without using the hs.scriptfunc command. The syntax for doing so is:

        Code:
        #Include functions.vb
        I put my functions in a file in a sub-directory and the syntax to include them that way is:

        Code:
        #Include folder/functions.vb
        Put this as your first line in your script and call the functions the same way as you would if they were directly in your script file.

        Cheers
        Al
        HS 4.2.8.0: 2134 Devices 1252 Events
        Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

        Comment


          #5
          This is even more EXACTLY what I was looking for. The good ol' simple #include!

          a big thanks.

          Originally posted by sparkman View Post
          You can also "include" a script file with your functions into other scripts and then call the functions directly without using the hs.scriptfunc command. The syntax for doing so is:

          Code:
          #Include functions.vb
          I put my functions in a file in a sub-directory and the syntax to include them that way is:

          Code:
          #Include folder/functions.vb
          Put this as your first line in your script and call the functions the same way as you would if they were directly in your script file.

          Cheers
          Al

          Comment


            #6
            Originally posted by sparkman View Post
            You can also "include" a script file with your functions into other scripts and then call the functions directly without using the hs.scriptfunc command. The syntax for doing so is:

            Code:
            #Include functions.vb
            I put my functions in a file in a sub-directory and the syntax to include them that way is:

            Code:
            #Include folder/functions.vb
            Put this as your first line in your script and call the functions the same way as you would if they were directly in your script file.

            Cheers
            Al
            This approach works for me when I use it from a script which is then called from a HomeSeer event.
            However, if I try to use this #include approach in my HomeSeer Shutdown vb script, the script doesn't run. Any reason why #include doesn't work on the Shutdown script?

            Comment


              #7
              Originally posted by randman View Post

              This approach works for me when I use it from a script which is then called from a HomeSeer event.
              However, if I try to use this #include approach in my HomeSeer Shutdown vb script, the script doesn't run. Any reason why #include doesn't work on the Shutdown script?
              No idea, you'd likely have to ask HST that question. Does it work in the startup script? There's typically very little time between HS starting its shutdown and finishing its shutdown, so you really can't do much in a shutdown script anyways.
              HS 4.2.8.0: 2134 Devices 1252 Events
              Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

              Comment


                #8
                Originally posted by sparkman View Post

                No idea, you'd likely have to ask HST that question. Does it work in the startup script? There's typically very little time between HS starting its shutdown and finishing its shutdown, so you really can't do much in a shutdown script anyways.
                There should be enough time to do what I need during shutdown. The problem is compilation.

                This is my test script which works just fine when called from a HomeSeer event:

                #include includes/Notify.vb
                Public Sub Main(ByVal param As Object)
                Notify("HomeSeer - Test", "Hello World!", 1)
                End Sub

                The Notify function above is defined as a "Public Sub" in includes/Notify.vb.

                However, if I do the same thing from my startup and shutdown vb scripts (which I've defined to be called in Tools->Setup->Custom), the
                startup and shutdown scripts fail to compile. Below is the HomeSeer log from the startup script:


                Apr-11 12:25:06 PM Error Compiling script RRM-Startup.vb: 'Notify' is not declared. It may be inaccessible due to its protection level.
                Apr-11 12:25:06 PM Error Compiling script RRM-Startup.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

                Comment


                  #9
                  Rather than placing your code in the startup/shutdown scripts, have you tried running another script (with includes) from these using the hs.runscript or hs.runscriptfunc ?
                  Jon

                  Comment


                    #10
                    Originally posted by randman View Post

                    There should be enough time to do what I need during shutdown. The problem is compilation.
                    Take out the token "Public" on your Main sub declare. For some reason, that seems to confuse the compiler. (Public is required on other subroutines that are called from outside though.)

                    Code:
                    #include includes/Notify.vb
                    Sub Main(param As Object)
                    Notify("HomeSeer - Test", "Hello World!", 1)
                    End Sub
                    If the problem persists after doing the above, then it might be a compilation problem during shutdown. Try to invoke the routine through a dummy event (to get it compiled), and then try the shutdown again.

                    Comment


                      #11
                      Originally posted by jon00 View Post
                      Rather than placing your code in the startup/shutdown scripts, have you tried running another script (with includes) from these using the hs.runscript or hs.runscriptfunc ?
                      Here's are two tests that I did from my Startup script:

                      1. In my Startup script, use hs.RunScriptFunc("test1.vb", ...). test1.vb uses #include and then test1.vb calls a function named 'Notify' that is defined in the included file.

                      This test fails. HomeSeer log says:

                      "Compiling script test1.vb: 'Notify' is not declared. It may be inaccessible due to its protection level".

                      So, somehow, the function 'Notify', which is defined in the #included file, is not recognized.

                      2. In my Startup script, use hs.RunScriptFunc("test2.vb",...). test2.vb does not use #include. test2.vb has the function that the Startup script calls via hs.RunScriptFunc().

                      This test succeeds.


                      Note that the #include method works for me in other scripts. But somehow, #include doesn't work from Startup or Shutdown scripts.

                      But at least I can do what I need using #2 above.




                      Comment


                        #12
                        You might try an absolute path to the include rather than a relative one.

                        Following may or may not be relevant ...

                        The #include directive in Visual Basic seems to be a HomeSeer specific extension, and (surprise) it's behavior is inconsistent between Windows and Linux platforms.

                        On Windows this includes a file in the standard scripts directory
                        Code:
                        #include Zodiac2.vb
                        On Linux this includes the same file
                        Code:
                        #include ./scripts/Zodiac2.vb
                        File not found doesn't generate an error or warning message. So a top-level file can be made portable between the two platforms by including both forms.

                        Comment


                          #13
                          Just a note - I was having the same problem, as discussed on another thread. To the best of my understanding, the compiler when launched from an event will honor the #include statement, and a compiler launched via an hs.runscriptfunc call will not, no matter what syntax or tweaks I've tried. The only workaround I can find is to hand-launch all scripts with #include statements first from an event, after which point the p code is created, and hs.runscriptfunc will work... until you run your shutdown.vb script, at which point it will stop working on-relaunch, until . . . etc etc.

                          Comment

                          Working...
                          X