Imports System.Web.UI
Imports HomeSeer.PluginSdk.Devices
Imports Scheduler.HSPI_HSTOUCH.FoscamFinder
Imports tenGlobalMethods

Public Class supporttest

    Public Sub Main(ByVal Parms As Object)
        Dim Interior_Light_State_On As Boolean = False
        Dim Interior_Light_Keypad_State_On As Boolean = False
        Dim Init As Boolean = False
        Dim ParmArray(10) As String
        '' Extract script parameters into ParmArray()
        ParmArray(0) = ""
        If Parms.ToString.Contains("|") Then
            ParmArray = Parms.ToString.Split(Convert.ToChar("|"))
        Else
            ParmArray(0) = Parms.ToString
        End If
        Dim retval As String = ""

        hs.WriteLog("Test", "Support Test")
        Exit Sub


        Try
            If Init = False Then
                If CBool(hs.PluginFunction("EasyTrigger", "", "AnyDevicesValueInThisGroupIsGreaterThan", {"Interior_Lights", 1})) Then
                    Interior_Light_State_On = True
                    Interior_Light_Keypad_State_On = True
                    retval = DeviceOn(hs, GetRefsByCategoryName(hs4, "Interior_Light_Keypad"))
                Else
                    retval = DeviceOff(hs, GetRefsByCategoryName(hs4, "Interior_Light_Keypad"))
                End If
                Init = True
            End If
            If Interior_Light_Keypad_State_On And Interior_Light_State_On And
               CBool(hs.PluginFunction("EasyTrigger", "", "AnyDevicesValueInThisGroupIsEqualTo", {"Interior_Keypad", 0})) Then 'Turn off interior lights and keypad switches
                retval = DeviceOff(hs, GetRefsByCategoryName(hs4, "Interior")) 'turn off Interior Lights
                retval = DeviceOff(hs, GetRefsByCategoryName(hs4, "Interior_Light_Keypad")) 'turn off keypad
                Interior_Light_State_On = False
                Interior_Light_Keypad_State_On = False
                hs.WriteLog("Interior Lights Control", "Interior Lights Off")

            ElseIf Not (Interior_Light_State_On) And Not (Interior_Light_Keypad_State_On) And
               CBool(hs.PluginFunction("EasyTrigger", "", "AnyDevicesValueInThisGroupIsGreaterThan", {"Interior_Lights", 1})) Then 'and any light on, turn on keypad
                retval = DeviceOn(hs, GetRefsByCategoryName(hs4, "Interior_Light_Keypad"))
                Interior_Light_State_On = True
                Interior_Light_Keypad_State_On = True
                hs.WriteLog("Interior Lights Control", "Interior Light On")

            ElseIf Not (Interior_Light_State_On) And Not (Interior_Light_Keypad_State_On) And
               CType(hs.CAPIGetStatus(GetFeatureRefByFullname(hs, "Bedroom Main Lights", "Bedroom Keypad - Button F")), HomeSeerAPI.CAPI.CAPIStatus).Status.ToString = "On" Then
                retval = DeviceOn(hs, GetRefsByCategoryName(hs4, "Interior")) 'turn on Interior Lights
                Interior_Light_State_On = True
                Interior_Light_Keypad_State_On = True
            End If

            If retval IsNot "" Then
                hs.WriteLog("Interior Light Control", "Error:  " + retval)
            End If
        Catch ex As Exception
            hs.WriteLog("Interior Light Control", "Exception: " & ex.Message)
        Finally
        End Try

    End Sub
End Class


Public Class miscflagset
    Public Sub Main(ByVal Parms As Object)
        Dim dev As HomeSeer.PluginSdk.Devices.HsFeature
        dev = hs4.GetFeatureByRef(69)
        'hs4.UpdatePropertyByRef(69, HomeSeer.PluginSdk.Devices.EProperty.Misc, dev.Misc Or HomeSeer.PluginSdk.Devices.EMiscFlag.Hidden)


        Dim i As Integer = Not CInt(HomeSeer.PluginSdk.Devices.EMiscFlag.Hidden)

        hs4.UpdatePropertyByRef(69, HomeSeer.PluginSdk.Devices.EProperty.Misc, dev.Misc And (Not CInt(HomeSeer.PluginSdk.Devices.EMiscFlag.Hidden)))

    End Sub
End Class

Public Class Insteon
    Public Sub Main(p As Object)
        Dim rc As Boolean
        Dim ref = 89
        ref = 80

        log("start")

        rc = hs4.SendControlForFeatureByValue(ref, 100)
        log("100 " & rc.ToString)

        rc = hs4.SendControlForFeatureByValue(ref, 0)
        log("0 " & rc.ToString)

        rc = hs4.SendControlForFeatureByValue(ref, 101)
        log("101 " & rc.ToString)

        rc = hs4.SendControlForFeatureByValue(ref, 102)
        log("102 " & rc.ToString)

        log("end")

    End Sub
    Friend Sub log(s As String)
        Dim ref As Integer = 89
        ref = 80
        Dim value As Double
        Dim status As String
        hs.WaitSecs(2)
        value = CType(hs4.GetPropertyByRef(ref, HomeSeer.PluginSdk.Devices.EProperty.Value), Double)
        status = hs4.GetPropertyByRef(77, HomeSeer.PluginSdk.Devices.EProperty.DisplayedStatus).ToString
        Console.WriteLine(s & "   Value: " & value.ToString & "  Status: " & status)
    End Sub

End Class

Public Class GetOnOffControl
    Public Sub Main(ByVal Parms As Object)
        ''
        '' Extract Parms --> ParmArray
        ''
        Dim ParmArray(0) As String
        '' Determine if Parms is a single String, or an Array of Strings
        If Parms.GetType().ToString = "System.String" Then
            '' Single String from HS scripting engine. Extract parms
            ParmArray(0) = ""
            If Parms.ToString.Contains("|") Then
                ParmArray = Parms.ToString.Split(Convert.ToChar("|"))
            Else
                ParmArray(0) = Parms.ToString
            End If
        Else
            '' Array of strings from HsTouch
            ParmArray = CType(Parms, String())
        End If
        '' ParmArray() now contains one or more passed parameters

        Dim FeatureRef As Integer = CInt(ParmArray(0))   'get the Ref from the script parameter
        Dim aControl As HomeSeer.PluginSdk.Devices.Controls.StatusControl
        '' ON
        aControl = GetControl(hs4, FeatureRef, True, True)   'ON
        If IsNothing(aControl) Then
            Console.WriteLine("GetControl ON Failed")
        Else
            Console.WriteLine("GetControl ON returned Value: " & aControl.TargetValue.ToString)
        End If
        '' OFF
        aControl = GetControl(hs4, FeatureRef, False, True)   'OFF
        If IsNothing(aControl) Then
            Console.WriteLine("GetControl OFF Failed")
        Else
            Console.WriteLine("GetControl OFF returned Value: " & aControl.TargetValue.ToString)
        End If
    End Sub
    Friend Function GetControl(hs4 As HomeSeer.PluginSdk.IHsController, FeatureRef As Integer, TurnOn As Boolean, Optional TryControlLabel As Boolean = False) As HomeSeer.PluginSdk.Devices.Controls.StatusControl
        ''
        '' Helper function to find the correct Control for DeviceOn and DeviceOff
        ''
        Dim aControl As HomeSeer.PluginSdk.Devices.Controls.StatusControl
        Dim theFeature As HomeSeer.PluginSdk.Devices.HsFeature
        Dim SCs As List(Of HomeSeer.PluginSdk.Devices.Controls.StatusControl)
        Dim s1, s2, s3 As String
        '' Validate the hs4 object
        If IsNothing(hs4) Then
            Return Nothing
        End If
        '' Get the Feature to be controlled
        Try
            theFeature = hs4.GetFeatureByRef(FeatureRef)
        Catch ex As Exception
            theFeature = Nothing
        End Try
        If IsNothing(theFeature) Then
            '' Must be invalid DevRef
            hs4.WriteLog(HomeSeer.PluginSdk.Logging.ELogType.Error, FeatureRef.ToString & " is an invalid FeatureRef", "tenGlobalMethods.GetControl")
            Return Nothing
        End If
        '' Try to get ControlUse
        Try
            If TurnOn Then
                '' Get Control for ON
                If Not theFeature.HasControlForUse(HomeSeer.PluginSdk.Devices.Controls.EControlUse.On) Then
                    aControl = Nothing
                Else
                    aControl = theFeature.GetFirstControlForUse(HomeSeer.PluginSdk.Devices.Controls.EControlUse.On)
                End If
            Else
                '' Get Control for OFF
                If Not theFeature.HasControlForUse(HomeSeer.PluginSdk.Devices.Controls.EControlUse.Off) Then
                    aControl = Nothing
                Else
                    aControl = theFeature.GetFirstControlForUse(HomeSeer.PluginSdk.Devices.Controls.EControlUse.Off)
                End If
            End If
        Catch ex As Exception
            aControl = Nothing
        End Try
        If Not IsNothing(aControl) Then Return aControl   'found Control, Return it
        If Not TryControlLabel Then Return Nothing   'no ControlUse and search not requested, return error
        '' Try searching for a matching Control
        If TurnOn Then s1 = " ON " Else s1 = " OFF "   'what we are looking for
        SCs = hs4.GetStatusControlsByRef(FeatureRef)   'get all controls for this Ref
        If SCs.Count < 1 Then Return Nothing
        For Each sc As HomeSeer.PluginSdk.Devices.Controls.StatusControl In SCs
            '' loop through all defined controls
            If Not sc.IsRange Then
                '' Not a Range of Values
                s2 = sc.GetLabelForValue(sc.TargetValue)   'get a Control Label
                s3 = " " & s2.ToUpper & " "   'pad this label with spaces
                If s3.Contains(s1) Then
                    '' Found the correct Control
                    Return sc
                End If
            End If
        Next sc
        Return Nothing
    End Function
End Class

Public Class forumtest

    ' This script Turns off Kitchen Lights
    '
    '
    Public Sub Main(ByVal Parms As Object)

        hs.SetDeviceValueByRef(2560, 37.0, False)


    End Sub
End Class


Public Class sonostest
    Public Sub Main(ByVal Parms As Object)
        ''
        '' Extract Parms --> ParmArray
        ''
        Dim ParmArray(0) As String
        '' Determine if Parms is a single String, or an Array of Strings
        If Parms.GetType().ToString = "System.String" Then
            '' Single String from HS scripting engine. Extract parms
            ParmArray(0) = ""
            If Parms.ToString.Contains("|") Then
                ParmArray = Parms.ToString.Split(Convert.ToChar("|"))
            Else
                ParmArray(0) = Parms.ToString
            End If
        Else
            '' Array of strings from HsTouch
            ParmArray = CType(Parms, String())
        End If
        '' ParmArray() now contains one or more passed parameters
        Dim result As Object
        result = hs.PluginFunction("Sonos4", "", "PlayMusic", {"RINCON_48A6B885868201400", "", "", "Bach", "", "", True, 0})



    End Sub
End Class



Public Class GetRootRef
    Public Sub Main(ByVal Parms As Object)
        ''
        '' Extract Parms --> ParmArray
        ''
        Dim ParmArray(0) As String
        '' Determine if Parms is a single String, or an Array of Strings
        If Parms.GetType().ToString = "System.String" Then
            '' Single String from HS scripting engine. Extract parms
            ParmArray(0) = ""
            If Parms.ToString.Contains("|") Then
                ParmArray = Parms.ToString.Split(Convert.ToChar("|"))
            Else
                ParmArray(0) = Parms.ToString
            End If
        Else
            '' Array of strings from HsTouch
            ParmArray = CType(Parms, String())
        End If
        '' ParmArray() now contains one or more passed parameters

        '' Get Ref of Device
        Dim iRef As Integer
        Dim iResult As Integer
        iRef = CInt(ParmArray(0))   'Get RefId from Script parameter
        iResult = FindRoot(iRef)
        hs4.WriteLog(HomeSeer.PluginSdk.Logging.ELogType.Info, iResult.ToString & " is the root of " & iRef.ToString, "GetRootRef", "blue")
    End Sub
    Friend Function FindRoot(theRef As Integer) As Integer
        Dim aDevice As HsDevice
        aDevice = hs4.GetDeviceByRef(theRef)   'Get the Device
        If aDevice.TypeInfo.ApiType = Identification.EApiType.Device Then
            ''  The specified Ref is the root, return it
            Return theRef
        Else
            '' Not the root, return the root
            Return aDevice.AssociatedDevices(0)
        End If
    End Function
End Class

Public Class MyCalendarEventScripts_III
    '
    ' New AK Google Calendar - Condense TODAY (5x) Children Device(s) into a single summary VD in order to create Key text searchable (parsable) event(s)
    '

    Public Sub Main(parm As Object)
        ''''''''''''''''  a type should be declared for these variables.  You are mixing integers and strings all over the place.  This will get you in trouble.
        '''''''''''''''    if you do not declare type, they are just generic objects and VB will try to guess what you meant.  As distributer, tenScripting has
        '''''''''''''''    Option Strict On, so you should have received errors for all of the attempts to implicitly convert from one type to another.
        'Dim CalEntry
        'Dim posn
        'Dim calTime
        'Dim CalResult

        Dim sCalEntry As String
        Dim iposn As Integer
        Dim sCalTime As String
        Dim sCalResult As String = ""       ''''''''''' I've also defualted this to empty string, as you are accumulating stuff into it and the first time
        ''''''''''''''''''''''''''''''''''''''''''''''' you will be using it without having set it to anything

        ' Change the HS.DeviceString(XXX) to reflect your latest AK Google TODAY children (5x) devices ID numbers (8490-8494)

        sCalEntry = hs.DeviceString(8490)
        If Len(sCalEntry) > 0 Then
            iposn = InStr(1, sCalEntry, "(") - 1
            If InStr(sCalEntry, "00:00") = 0 Then
                sCalTime = Mid(sCalEntry, iposn + 2, 5)
                sCalResult = sCalResult & sCalTime & " " & Left(sCalEntry, iposn)
            Else
                sCalResult = sCalResult & Left(sCalEntry, iposn)
            End If
        End If

        sCalEntry = hs.DeviceString(8491)
        If Len(sCalEntry) > 0 Then
            iposn = InStr(1, sCalEntry, "(") - 1
            If InStr(sCalEntry, "00:00") = 0 Then
                sCalTime = Mid(sCalEntry, iposn + 2, 5)
                sCalResult = sCalResult & sCalTime & " " & Left(sCalEntry, iposn)
            Else
                sCalResult = sCalResult & Left(sCalEntry, iposn)
            End If
        End If

        sCalEntry = hs.DeviceString(8492)
        If Len(sCalEntry) > 0 Then
            iposn = InStr(1, sCalEntry, "(") - 1
            If InStr(sCalEntry, "00:00") = 0 Then
                sCalTime = Mid(sCalEntry, iposn + 2, 5)
                sCalResult = sCalResult & sCalTime & " " & Left(sCalEntry, iposn)
            Else
                sCalResult = sCalResult & Left(sCalEntry, iposn)
            End If
        End If

        sCalEntry = hs.DeviceString(8493)
        If Len(sCalEntry) > 0 Then
            iposn = InStr(1, sCalEntry, "(") - 1
            If InStr(sCalEntry, "00:00") = 0 Then
                sCalTime = Mid(sCalEntry, iposn + 2, 5)
                sCalResult = sCalResult & sCalTime & " " & Left(sCalEntry, iposn)
            Else
                sCalResult = sCalResult & Left(sCalEntry, iposn)
            End If
        End If

        sCalEntry = hs.DeviceString(8494)
        If Len(sCalEntry) > 0 Then
            iposn = InStr(1, sCalEntry, "(") - 1
            If InStr(sCalEntry, "00:00") = 0 Then
                sCalTime = Mid(sCalEntry, iposn + 2, 5)
                sCalResult = sCalResult & sCalTime & " " & Left(sCalEntry, iposn)
            Else
                sCalResult = sCalResult & Left(sCalEntry, iposn)
            End If
        End If

        ' Change the HS.DeviceString(XXX) to your reflect your single "key text searchable" Event Virtual Device (1x) ID numbers 8519

        hs.SetDeviceString(8519, sCalResult, True)

    End Sub
End Class


Public Class MySonosScript

    Public Sub Main(ByVal Parms As Object)

        Dim rc As Boolean

        Dim devOfficeSonos As Integer = GetFeatureRefByFullname(hs, "Sonos4 Office Office", "Sonos4 Office Controls")

        rc = hs4.SendControlForFeatureByValue(devOfficeSonos, 1001)

    End Sub
End Class