
Public Class tenRunScript
    Public Sub Main(ByVal Parms As Object)
        ''
        '' This Script will run a second script within tenScripting4
        '' 
        ''   A single parameter, with the following parameters is passed to tenRunScript
        ''
        ''     ScriptName|ScriptMethod|Your Parm1|Your Parm2|etc.
        ''
        ''       ScriptName:  Name of Script to be tested. Do not include the file extension  (.vb nor .cs)
        ''       ScriptMethod:  The Method to be invoked.  Usually Main, but must be specified
        ''       Parmn:  The parameters to be passed to your script
        ''
        ''     Event Example, Event Entries:
        ''
        ''       Script:  c:/Program Files (x86)/HomeSeer HS3/scripts/tenRunScript.vb
        ''       Sub or Function:  Main
        ''       Parameters:  SpeakAndLogMultipleParms|Main|Speak This|Log This
        ''
        Dim plugin As HomeSeerAPI.PluginAccess
        Dim s, ss(), sp As String
        '' Called from an Event or HsTouch???
        If Parms.GetType().ToString = "System.String" Then
            '' single string from an Event
            s = CType(Parms, String)
        Else
            '' array of strings from HsTouch 
            s = CType(Parms, String())(0)
        End If
        ss = s.Split(Convert.ToChar("|"))   'extract individual parameters
        If ss.Count < 2 Then
            hs.WriteLog("tenRunScript", "You must specify at least 2 parameters, ScriptName and ScriptMethod")
            hs.WriteLog("tenRunScript", "  Parms: " & Parms.ToString)
            Exit Sub
        End If
        '' Extract Parameters
        sp = ""   'target script parameters
        If ss.Count > 2 Then
            For i As Integer = 2 To ss.Count - 1
                sp = sp & ss(i) & "|"   'add in each parameters
            Next
            sp = sp.Substring(0, sp.Length - 1)   'strip out last |
        End If
        hs.WriteLog("tenRunScript", "Script: " & ss(0) & "       Method: " & ss(1))
        If ss.Count > 2 Then
            hs.WriteLog("tenRunScript", "    Parms: " & sp)
        End If
        Try
            plugin = New HomeSeerAPI.PluginAccess(hs, "tenScripting4", "")
        Catch ex As Exception
            hs.WriteLog("tenRunScript", "Exception Trying to access tenScripting4:" & ex.Message)
            Exit Sub
        End Try
        If IsNothing(plugin) Then
            hs.WriteLog("tenRunScript", "Could not connect to tenScripting4.  Make sure it is running.")
            Exit Sub
        End If
        If CType(plugin.Connected, Boolean) Then
            '' Connected to tenScripting4
            hs.WriteLog("tenRunScript", "Connected to tenScripting4.  Will now run " & ss(0))
        Else
            hs.WriteLog("tenRunScript", "Failed to connect to tenScripting4. Make sure the tenScripting4 VS project is running.")
            Exit Sub
        End If
        '' Call in to tenScripting to run the script
        Try
            plugin.PluginFunction("AutoRunScript", {Parms.ToString})
        Catch ex As Exception
            hs.WriteLog("tenRunScript", "Exception calling tenScripting4.AutoRunScript:" & ex.Message)
        End Try
    End Sub

End Class

Public Class tenRunScriptButton
    Public Sub Main(ByVal Parms() As Object)
        ''
        '' This Script will run a second script within tenScripting4
        ''
        ''   This version is the same as tenRunScript, but is used by the DeviceButton function.
        ''       The parameters passed to it by HS when a device button runs a script is different than
        ''       when an event runs a script.  The script parameter is an array of type Object.  The first
        ''       parameter is an integer that contains the DevRef of the button device.  The second parameter
        ''       is a string that contains the same parameters as passed to tenRunScript,  SEE BELOW
        '' 
        ''   Your device button must pass the following string to tenRunScriptButton:
        ''
        ''     ScriptName|ScriptMethod|Your Parm1|Your Parm2|etc.
        ''
        ''       ScriptName:  Name of Script to be tested. Do not include the file extension  (.vb nor .cs)
        ''       ScriptMethod:  The Method to be invoked.  Usually Main, but must be specified
        ''       Parmn:  The parameters to be passed to your script
        ''
        ''     DeviceButton Example:
        ''
        ''       Script:  c:/Program Files (x86)/HomeSeer HS3/scripts/tenRunScriptButton.vb
        ''       Sub or Function:  Main
        ''       Parameters:  SpeakAndLogMultipleParms|Main|Speak This|Log This
        ''
        Dim plugin As HomeSeerAPI.PluginAccess
        Dim s, sp, ss() As String
        hs.WriteLog("tenRunScriptButton", "DeviceButton Ref:" & CInt(Parms(0)))
        s = Parms(1).ToString   'get the parameters for the script in tenScripting
        ss = s.Split(Convert.ToChar("|"))   'extract individual parameters
        If ss.Count < 2 Then
            hs.WriteLog("tenRunScriptButton", "You must specify at least 2 parameters, ScriptName and ScriptMethod")
            hs.WriteLog("tenRunScriptButton", "  Parms: " & s.ToString)
            Exit Sub
        End If
        '' Extract Parameters
        sp = ""   'target script parameters
        If ss.Count > 2 Then
            For i As Integer = 2 To ss.Count - 1
                sp = sp & ss(i) & "|"   'add in each parameters
            Next
            sp = sp.Substring(0, sp.Length - 1)   'strip out last |
        End If
        hs.WriteLog("tenRunScriptButton", "Script: " & ss(0) & "       Method: " & ss(1))
        If ss.Count > 2 Then
            hs.WriteLog("tenRunScriptButton", "    Parms: " & sp)
        End If
        Try
            plugin = New HomeSeerAPI.PluginAccess(hs, "tenScripting4", "")
        Catch ex As Exception
            hs.WriteLog("tenRunScriptButton", "Exception Trying to access tenScripting4:" & ex.Message)
            Exit Sub
        End Try
        If IsNothing(plugin) Then
            hs.WriteLog("tenRunScriptButton", "Could not connect to tenScripting4.  Make sure it is running.")
            Exit Sub
        End If
        If CType(plugin.Connected, Boolean) Then
            '' Connected to tenScripting4
            hs.WriteLog("tenRunScriptButton", "Connected to tenScripting4.  Will now run " & ss(0))
        Else
            hs.WriteLog("tenRunScriptButton", "Failed to connect to tenScripting4. Make sure the tenScripting4 VS project is running.")
            Exit Sub
        End If
        '' Call in to tenScripting to run the script
        Try
            plugin.PluginFunction("AutoRunScript", {s})
        Catch ex As Exception
            hs.WriteLog("tenRunScriptButton", "Exception calling tenScripting4.AutoRunScript:" & ex.Message)
        End Try
    End Sub

End Class