Imports HomeSeerAPI        '  @DONOTEXPORT
Class ToggleDeviceSample
    Public Sub Main(ByVal Parms As String)
        ''
        '' Toggle the Specified Device ON/OFF
        ''   Anything but OFF is considered ON
        ''
        '' Specify the Device Name, including Locations, as a Parm
        ''
        Dim theDevice As String
        Dim ccOFF As CAPIControl
        Dim ccON As CAPIControl
        Dim theOffLabel As String
        Dim devStatus As String
        Dim ccResponse As CAPIControlResponse
        '' Get Device from the Parms
        theDevice = Parms.ToString
        '' Get the CAPI ControlUse value for OFF
        ccOFF = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._Off)
        If IsNothing(ccOFF) Then
            hs.WriteLog("ToggleDevice", "ControlUse not define for device: " & theDevice)
            Exit Sub
        End If
        theOffLabel = ccOFF.Label
        '' Get the Device CAPIStatus
        devStatus = hs.CAPIGetStatus(hs.GetDeviceRefByName(theDevice)).Status
        '' Determine Current Status, and toggle it
        If theOffLabel.ToUpper.Trim = devStatus.ToUpper.Trim Then
            '' Currently OFF, turn it ON
            ccON = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._On)
            ccResponse = hs.CAPIControlHandler(ccON)
            If ccResponse <> CAPIControlResponse.All_Success Then
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " & ccResponse.ToString)
            End If
        Else
            '' NOT OFF, turn it OFF
            ccResponse = hs.CAPIControlHandler(ccOFF)
            If ccResponse <> CAPIControlResponse.All_Success Then
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " & ccResponse.ToString)
            End If
        End If
    End Sub
End Class
Class ScriptWithEmbeddedClassDefinition
    Public Sub Main(ByVal Parmsparms As String)
        Dim o As TestClass
        o = New TestClass("Hello World", hs)
        o.Logit()
    End Sub
    Class TestClass
        Private _LogMessage As String, _hs As HomeSeerAPI.IHSApplication
        Public Sub New(LogMessage As String, hs As HomeSeerAPI.IHSApplication)
            _LogMessage = LogMessage
            _hs = hs
        End Sub
        Public Sub Logit()
            _hs.WriteLog("Logit", _LogMessage)
        End Sub
    End Class
End Class
Class NonHsParmsSample
    Public Sub Main(ByVal parms() As Object)
        If parms.Length > 0 Then
            For i As Integer = 0 To parms.Length - 1
                hs.WriteLog("SampleScript", "Parameter" & i.ToString & "=" & parms(i).ToString)
            Next
        Else
            hs.WriteLog("SampleScript", "No Parameters Found")
        End If

    End Sub
End Class
Public Class TwoMethods
    Public Sub ToggleFrontPorch(ByVal Parms As String)
        ToggleDevice("Outside Front Porch")
    End Sub
    Public Sub ToggleBackPorch(ByVal Parms As String)
        ToggleDevice("Outside Side Porch")
    End Sub

    Private Sub ToggleDevice(ByVal Parms As String)
        ''
        '' Toggle the Specified Device ON/OFF
        ''   Anything but OFF is considered ON
        ''
        '' Specify the Device Name, including Locations, as a Parm
        ''
        Dim theDevice As String
        Dim ccOFF As CAPIControl
        Dim ccON As CAPIControl
        Dim theOffLabel As String
        Dim devStatus As String
        Dim ccResponse As CAPIControlResponse
        '' Get Device from the Parms
        theDevice = Parms.ToString
        '' Get the CAPI ControlUse value for OFF
        ccOFF = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._Off)
        If IsNothing(ccOFF) Then
            hs.WriteLog("ToggleDevice", "ControlUse not defined for device: " & theDevice)
            Exit Sub
        End If
        theOffLabel = ccOFF.Label
        '' Get the Device CAPIStatus
        devStatus = hs.CAPIGetStatus(hs.GetDeviceRefByName(theDevice)).Status
        '' Determine Current Status, and toggle it
        If theOffLabel.ToUpper.Trim = devStatus.ToUpper.Trim Then
            '' Currently OFF, turn it ON
            ccON = hs.CAPIGetSingleControlByUse(hs.GetDeviceRefByName(theDevice), ePairControlUse._On)
            ccResponse = hs.CAPIControlHandler(ccON)
            If ccResponse <> CAPIControlResponse.All_Success Then
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " & ccResponse.ToString)
            End If
        Else
            '' NOT OFF, turn it OFF
            ccResponse = hs.CAPIControlHandler(ccOFF)
            If ccResponse <> CAPIControlResponse.All_Success Then
                hs.WriteLog("ToggleDevice", "CapiControlHandler Failed, ccResponse: " & ccResponse.ToString)
            End If
        End If
    End Sub
End Class
