Imports tenGlobalMethods
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices

''
'' Sample test script for various tenGlobalMethods functions
''    This is a single script (testGlobalMethods) with multiple entry points 
''
Public Class testGlobalMethods
    ''    
    '' The following constants are used throughout these tests, and can be changed for your installation
    ''     Rename them and change their values
    ''
    '' Device Refs
    Friend Const refOfficeCeiling As Integer = 174   'Office ceiling light
    Friend Const refOfficeDesk As Integer = 69   'Office desk light
    Friend Const refHallwayLight As Integer = 172   'Hallway light
    Friend Const refFoyer As Integer = 168   'Foyer light
    '' Categories
    Friend Const catOffice As String = "Office"
    Friend Const catLights As String = "Lights"
    Friend Const catHallway As String = "Hallway"
    ''
    Friend rc As String
    Public Sub testDeviceOnOff(ByVal Parms As Object)
        rc = DeviceOn(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOn for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
        rc = DeviceOff(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOff for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
        rc = DeviceOn(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOn for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
    End Sub
    Public Sub testDevicesOnOff(ByVal Parms As Object)
        rc = DeviceOn(hs4, {catOffice, catHallway})
        If rc <> "" Then wl("DeviceOn for multiple Categories failed," & " RC=" & rc)
        rc = DeviceOff(hs4, {catOffice, catHallway})
        If rc <> "" Then wl("DeviceOff for multiple Categories failed," & " RC=" & rc)
        rc = DeviceOn(hs4, {catOffice, catHallway})
        If rc <> "" Then wl("DeviceOn for multiple Categories failed," & " RC=" & rc)
    End Sub
    Public Sub testToggleDeviceOnOff(ByVal Parms As Object)
        rc = ToggleDeviceOnOff(hs4, refOfficeCeiling)
        If rc <> "" Then wl("ToggleDeviceOnOff failed," & " RC=" & rc)
        hs.WaitSecs(1)
        rc = ToggleDeviceOnOff(hs4, refOfficeCeiling)
        If rc <> "" Then wl("ToggleDeviceOnOff failed," & " RC=" & rc)
    End Sub
    Public Sub testToggleDevice(ByVal Parms As Object)
        rc = ToggleDevice(hs4, refOfficeCeiling, 100, 0)
        If rc <> "" Then wl("ToggleDevice failed," & " RC=" & rc)
        hs.WaitSecs(1)
        rc = ToggleDevice(hs4, refOfficeCeiling, 100, 0)
        If rc <> "" Then wl("ToggleDevice failed," & " RC=" & rc)
    End Sub
    Public Sub testIsOff(ByVal Parms As Object)
        wl("Starting testIsOff")
        rc = DeviceOn(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOn for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
        hs.WaitSecs(1)
        If IsOff(hs4, refOfficeCeiling) Then wl("IsOff failed for Ref " & refOfficeCeiling.ToString)
        rc = DeviceOff(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOff for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
        hs.WaitSecs(1)
        If Not IsOff(hs4, refOfficeCeiling) Then wl("IsOff failed for Ref " & refOfficeCeiling.ToString)
        wl("Completed testIsOff")
    End Sub
    Public Sub testAreAllOff(ByVal Parms As Object)
        wl("Starting testAreAllOff")
        rc = DeviceOff(hs4, {catOffice, catHallway})
        If rc <> "" Then wl("DeviceOff for multiple Categories failed," & " RC=" & rc)
        hs.WaitSecs(5)   'need to wait for OFF to complete
        If Not AreAllOff(hs4, {catOffice, catHallway}) Then wl("First AreAllOff failed")
        '
        rc = DeviceOn(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOn for Ref " & refOfficeCeiling.ToString & " RC=" & rc)
        hs.WaitSecs(1)
        If AreAllOff(hs4, {catOffice, catHallway}) Then wl("Second AreAllOff failed")
        wl("Completed testAreAllOff")
    End Sub
    Public Sub testFullnameFunctions(ByVal Parms As Object)
        Dim s() As String, i As Integer
        wl("Ref = " & refOfficeCeiling.ToString)
        s = GetFullnameByRef(hs4, refOfficeCeiling)
        wl("Found Root = " & s(0))
        wl("Found Child = " & s(1))
        If IsNothing(s) Then wl("GetFullnameByRef failed") : Exit Sub
        i = GetFeatureRefByFullname(hs, s(0), s(1))
        wl("Found Ref = " & i.ToString)
        If i = refOfficeCeiling Then
            wl("Success")
        Else
            wl("Failure")
        End If
    End Sub
    Public Sub testCategories(ByVal Parms As Object)
        Dim cats As List(Of String)
        Dim refs As List(Of Integer)
        Dim found As Boolean
        '' Get categories for a ref
        cats = GetCategoriesByRef(hs4, refOfficeCeiling)   'get the categories for this ref
        wl("Categories for Ref " & refOfficeCeiling.ToString & ":")
        '' Print these categories
        For Each s As String In cats
            wl("  " & s)
        Next s
        '' For each category, make sure the ref is in it
        For Each s As String In cats
            refs = GetRefsByCategoryName(hs4, s)   'get the refs in this category
            found = False
            wl("Category " & s)
            For Each i As Integer In refs
                wl("     Found Ref " & i.ToString)
                If i = refOfficeCeiling Then found = True
            Next i
            If Not found Then
                wl("Did not find Ref " & refOfficeCeiling.ToString & " in category " & s)
            End If
        Next s
    End Sub
    Public Sub testLists(ByVal Parms As Object)
        Dim listOffice As List(Of Integer)
        Dim listLights As List(Of Integer)
        Dim listBoth As List(Of Integer)
        Dim listEither As List(Of Integer)
        listOffice = GetRefsByCategoryName(hs4, catOffice)
        wl("Refs in Category " & catOffice)
        For Each i As Integer In listOffice
            wl("   " & i.ToString)
        Next
        listLights = GetRefsByCategoryName(hs4, catLights)
        wl("Refs in Category " & catLights)
        For Each i As Integer In listLights
            wl("   " & i.ToString)
        Next
        listBoth = GetRefsInBothLists(listOffice, listLights)
        wl("Refs in Both Lists")
        For Each i As Integer In listBoth
            wl("   " & i.ToString)
        Next
        listEither = GetRefsInEitherList(listOffice, listLights)
        wl("Refs in Either List ")
        For Each i As Integer In listEither
            wl("   " & i.ToString)
        Next
    End Sub
    Public Sub testSaveRestore(Parms As Object)
        '' Turn all OFF
        rc = DeviceOff(hs4, catOffice)   'turn them all off
        If rc <> "" Then wl("DeviceOff for Category " & catOffice & ", RC=" & rc)
        hs.WaitSecs(3)
        '' Save State, all are OFF
        rc = SaveDeviceStates(hs4, "GlobalMethodsTest", catOffice)
        If rc <> "" Then wl("SaveDeviceStates Failed," & " RC=" & rc)
        '' Turn one light ON
        rc = DeviceOn(hs4, refOfficeCeiling)
        If rc <> "" Then wl("DeviceOn for REf " & refOfficeCeiling & ", RC=" & rc)
        '' Restore saved state, light should go back off
        rc = RestoreDeviceStates(hs4, "GlobalMethodsTest")
        If rc <> "" Then wl("RestoreDeviceStates Failed")
    End Sub
    Public Sub testIfDistinctRefs(Parms As Object)
        '' HS Log should only contain one ON and OFF for each Ref, no duplicates
        '' INI file should not contain and duplicated Refs
        DeviceOn(hs4, {catOffice, catHallway, catLights})
        SaveDeviceStates(hs4, "xyz", {catOffice, catHallway, catLights})
        DeviceOff(hs4, {catOffice, catHallway, catLights})
        RestoreDeviceStates(hs4, "xyz")
    End Sub
    Public Sub testCheckFirst(parms As Object)
        wl("ON") : DeviceOn(hs4, catOffice) : hs.WaitSecs(2)
        wl("ON true") : DeviceOn(hs4, catOffice, True) : hs.WaitSecs(2)
        wl("OFF true") : DeviceOff(hs4, catOffice, True) : hs.WaitSecs(2)
        wl("OFF true") : DeviceOff(hs4, catOffice, True) : hs.WaitSecs(2)
        wl("OFF") : DeviceOff(hs4, catOffice)
    End Sub
    Public Sub testRemoveDeviceStates(parms As Object)
        Dim rc As String
        rc = SaveDeviceStates(hs4, "OfficeStates", "Office")
        rc = RestoreDeviceStates(hs4, "OfficeStates")
        rc = RestoreDeviceStates(hs4, "OfficeStates", True)
        rc = SaveDeviceStates(hs4, "OfficeStates", "Office")
        rc = RemoveDeviceStates(hs4, "OfficeStates")
    End Sub
    Public Sub testDeviceDim(p As Object)
        Dim rc As String
        rc = ten.DeviceDim(hs4, catOffice, 55.0)
        Console.WriteLine(rc)
    End Sub
    Friend Sub wl(s As String)
        '' helper for WriteLog, or change to Console.Writeline
        hs4.WriteLog(HomeSeer.PluginSdk.Logging.ELogType.Info, s, "tenGlobalMethods Test")   'for running from HS
        'Console.WriteLine(s)   'for running from tenScripting
    End Sub
End Class
