Announcement

Collapse
No announcement yet.

Scripts with common subroutine

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

    Scripts with common subroutine

    Is it possible to use a second file in a vb.net script to call a common subroutine? For instance, the blow could be cut into 2 scripts

    Code:
    Sub Main(Parms As Object)
    '  hs.writelog("Parameter Test",CInt(Parms(0)))
    Dim newValue as Integer
    
    	newValue = CInt(Parms(0))
    
    	'Ceiling Fan
    	TestSet("Ceiling Fan Light Selector", "Ceiling Fan Light", newValue)
    
    	'Fireplace Outside Cans
    	TestSet("HSTouch -Fireplace Outside Cans Selector", "Fireplace Outside Lights", newValue)
    
    	'Fireplace Inside Cans
    	TestSet("Fireplace Inside Lights", "HSTouch -Fireplace Inside Cans Selector", newValue)
    
    	'Entrance Light
    	TestSet("Entrance Light @ Door", "HSTouch - Foyer Selector", newValue)
    
    	'Light Outside Powder Room
    	TestSet("Light Outside Powder Room", "HSTouch - Light Outside Powder Room Selector", newValue)
    
    	'Stairs Light
    	TestSet("HSTouch - Stairs Light Selector","Main Stairs Lights @ Top of Steps", newValue)
    
    
    End Sub
    And have it call the script below from a second file

    Code:
    Sub TestSet(byRef Selector as String, byRef Device as String, byRef , newValue as Integer)
    
    Dim SelectorRef As Integer
    Dim DevRef As Integer
    
    	'Get the Device References
    	SelectorRef = hs.GetDeviceRefByName(Selector)
    	DevRef = hs.GetDeviceRefByName(Device)
    
    	'Set the Device Value if the Device Selector is selected.
    	If hs.IsOn (SelectorRef) then
    		hs.CAPIControlHandler(hs.CAPIGetSingleControl(DevRef,false,newValue,false,true))
    	end if
    
    End Sub
    So that I can use the second part in other scripts as well. I will have at least 6 scripts using this same subroutine.

    Karl
    Karl S
    HS4Pro on Windows 10
    1070 Devices
    56 Z-Wave Nodes
    104 Events
    HSTouch Clients: 3 Android, 1 iOS
    Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

    #2
    Hi Karl,

    Yes you can. Just include something like this at the top of the script:

    Code:
    #Include includes/functions.vb
    
    Sub Main(Parms As Object)
    ...
    I do the same and put them in a directory called Includes under the Scripts directory.

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

    Comment


      #3
      GREAT! Thank you, Al! (I need a gif of the old Fat Albert gang cheering...)
      Karl S
      HS4Pro on Windows 10
      1070 Devices
      56 Z-Wave Nodes
      104 Events
      HSTouch Clients: 3 Android, 1 iOS
      Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

      Comment


        #4
        Hi Al;

        I am trying to use the include statement and am getting errors.

        Code:
        #Include includes/functions.vb
        	
         Sub Main(byVal args As String)
         
        	Logit("start",1)
        
        	Logit("stop",1)
        	
        End Sub
        Here is my Inclues\functions.vb

        Code:
        Sub Main(byVal Args As Object)
        
        	Public Sub LogIt(byVal Message As String, byVal Severity As Integer)
        		
        		Dim ID As String = "Info"
        		Dim strScript As String = "Test.vb"
        		
        		If Severity = 0 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#80800")	'Black
        		If Severity = 1 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#0000FF")	'Blue
        		If Severity = 2 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#ADD8E6")	'Light Blue
        		If Severity = 3 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#FF0000")	'Red
        		If Severity = 4 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#FFA500")	'Orange
        			
        	End Sub
        
        End Sub
        The HomeSeer Log error is
        PHP Code:
        Compiling script test.vb'Logit' is not declaredIt may be inaccessible due to its protection level
        Am I using this incorrectly?

        Thanks;
        Don

        Originally posted by sparkman View Post

        Code:
        #Include includes/functions.vb
        
        Sub Main(Parms As Object)
        ...
        Don

        Comment


          #5
          Been a while since I have coded for HomeSeer. Are the function calls case sensitive? You are calling the function with a lower case I while your declare it with an uppercase I logit vs logIt. Try using logIt in your test file:

          Code:
          #Include includes/functions.vb
          	
           Sub Main(byVal args As String)
           
          	LogIt("start",1)
          
          	LogIt("stop",1)
          	
          End Sub
          Karl S
          HS4Pro on Windows 10
          1070 Devices
          56 Z-Wave Nodes
          104 Events
          HSTouch Clients: 3 Android, 1 iOS
          Google Home: 3 Mini units, 1 Pair Audios, 2 Displays

          Comment


            #6
            I changed the case to duplicate the method call, and still errors.

            Thanks for looking.
            Don

            Comment


              #7
              In the included file you are nesting functions. The Main function there isn't necessary, as you already have one in the top-level file. (The included file is inserted "in-line" as if you typed it in there).

              So in your functions.vb, if you remove the 1st line
              Sub Main(byVal Args As Object)

              and the last line
              End Sub

              it should work.

              Comment


                #8
                If I understand you correctly, you don't need a main sub in the functions.vb? I tried that and still get the same error. I'll do a google on the #include statement and go from there.

                Thanks
                Don

                Comment


                  #9
                  I tried it here and seems to work:

                  includes\functions.vb

                  Code:
                          Public Sub LogIt(byVal Message As String, byVal Severity As Integer)
                  		
                  		Dim ID As String = "Info"
                  		Dim strScript As String = "Test.vb"
                  		
                  		If Severity = 0 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#80800")	'Black
                  		If Severity = 1 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#0000FF")	'Blue
                  		If Severity = 2 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#ADD8E6")	'Light Blue
                  		If Severity = 3 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#FF0000")	'Red
                  		If Severity = 4 Then hs.WriteLogEx(ID," -- " & strScript &  " -- " & Message,"#FFA500")	'Orange
                  			
                  	End Sub
                  test.vb

                  Code:
                  #Include includes/functions.vb
                  
                  Sub Main(ByVal Parm As Object)
                  
                  	Logit("start",1)
                  
                  	Logit("stop",1)
                  
                  End Sub
                  Jon

                  Comment


                    #10
                    Wierd. I've got to be doing something wrong.

                    I am getting this error dealing with the includes statement. Does this make any sense to anyone?

                    Thanks

                    PHP Code:
                    Compiling script test.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'use any aliases
                    Last edited by donstephens; July 16, 2017, 04:17 PM.
                    Don

                    Comment


                      #11
                      dont forget that functions.vb needs to be inside the directory c:/...homeseer hs3/scripts/includes
                      HS4 Pro on Shuttle NC10U, Win10; Z-NET
                      Number of Devices: 1005
                      Number of Events: 293

                      Plug-Ins: BLLock, DirecTv, EasyTrigger, Honeywell WiFi Thermostat, Marquis monoprice Amp, MeiHarmonyHub, PHLocation2, Pushover 3P, UltraM1G3, rnbWeather, Worx Landroid, Z-Wave

                      External applications: Homebridge-homeseer, Geofency, EgiGeoZone.

                      Comment


                        #12
                        Good point, double check the pathname spellings. I noticed that in your initial post you typed "Here is my Inclues\functions.vb". Might be just a typo there, but worth a second look.

                        Comment


                          #13
                          In my path, the Includes folder is capitalized and under the script folder. Will keep hammering on this.

                          Originally posted by zwolfpack View Post
                          Good point, double check the pathname spellings. I noticed that in your initial post you typed "Here is my Inclues\functions.vb". Might be just a typo there, but worth a second look.
                          Don

                          Comment


                            #14
                            I just keep my subs in the same vb file as my parent sub.

                            Code:
                            Public sub main(...)
                            End sub
                            
                            Public sub run_me(...)
                                Do_this(..)
                            End sub
                            
                            Public sub run_me_too(...)
                                Do_this(..)
                            End sub
                            
                            Function Do_this(..)
                                 Return x
                            End function

                            Comment


                              #15
                              Since you're including the code, the functions sub does not need to be public. I tried it with test.vb in the scripts folder and functions.vb in the includes folder. It works fine when the sub is not declared as public, but errors when it is made public.
                              Fred

                              HomeSeer Pro 3.0.0.548, HS3Touch, Zwave 3.0.1.252, Envisalink DSC 3.0.0.40, WeatherXML, Z-stick, HS phone, Way2Call

                              Comment

                              Working...
                              X