#include <File.au3>
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
#include <Constants.au3>
#include <Array.au3>

#Region Options
Opt('MustDeclareVars', 1)
OnAutoItExitRegister("_MyExit")
#EndRegion Options

#Region Global Variablen
Global $sw_start = False, $a_ip, $a_config, $r, $t = 1, $sw_onoff, $sw_btnstartendis, $atemp, $temp, $timer, $msg, $timeout_sl, $intervall_sl, $schwelle_sl, $MarkAll, $Marked = False, $a

; extended Styles des ListView
Global $exStyles = BitOR($LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_CHECKBOXES)

; Einlesen der IP-Adressen aus ip_adressen.txt in Array $a_ip
_FileReadToArray(@ScriptDir & "\ip_adressen.txt", $a_ip)

; Texte für spätere Übersetzung in andere Sprachen ... evtl. Einlesen aus "language".ini
Global $aTexte[37] = ["IP-Pinger", "prüfe...", "warte...", "Online", "Offline", "-----", "Start", "Stop", "Alle an", "Alle aus", _; 0 - 9
        "IP-Adresse/Hostname", "Status", "in ms", "Ping", "IP-Adresse", "Zeiten einstellen", "Pingverhalten", "Timeout/Ping:", _; 10 - 17
        "Ping nächste IP nach:", "Schwellenwert Warnung:", "Optionen", "Listview scrollen an/aus", _; 18 - 21
        "Offline-Rechner nicht mehr pingen", "Alarmmeldung wenn Offline", "Anzeige bei Schwellenwert", _; 22 - 24
        "Ping beginnt bei Start immer oben", " - Alarm", "Der Rechner ", " ist nicht erreichbar", _;25 - 28
        "Die Adresse wird nicht weiter geprüft, bis Sie dieses Ziel wieder aktivieren!" & _
        @CRLF & "Diese Meldung wird nach 10 Sekunden ausgeblendet.", _; 29
        "Die Adresse wird weiterhin geprüft." & @CRLF & @CRLF & "Diese Meldung wird nach jedem Durchlauf erscheinen." & _
        @CRLF & "Diese Meldung wird nach 5 Sekunden ausgeblendet.", _; 30
        "Legende:", "= wird nicht geprüft", "= warte auf Start", "= Ziel nicht erreichbar", "= Ziel über Schwellenwert", "= Ziel erreichbar"]; 31 - 36

; Defaultwerte, wenn keine Config.ini vorhanden, oder Schlüssel nicht korrekt lesbar
Global $timeout = 3
Global $intervall = 3
Global $schwelle = 3

; [0]= Index, [1]= Status "ON/OFF", [2]= akt. Pingzeit, [3]= durchschnittl. Pingzeit, [4]= Summe Pingzeiten, [5]= Anzahl bisheriger Ping's, [6]= ListView-Items
Global $a_LV[$a_ip[0] + 1][7]
#EndRegion Global Variablen

#Region Gui
Global $gui = GUICreate($aTexte[0], 660, 390)
;Überschriften des ListView
GUICtrlCreateLabel($aTexte[10], 38, 10, 110, 20)
GUICtrlCreateLabel($aTexte[11], 235, 10, 40, 20)
GUICtrlCreateLabel($aTexte[12], 300, 10, 30, 20)
GUICtrlCreateLabel(Chr(216) & " " & $aTexte[12], 350, 10, 40, 20)

;Legende
GUICtrlCreateLabel($aTexte[31], 10, 346, 100, 20)
GUICtrlCreateLabel("", 10, 366, 16, 16)
GUICtrlSetBkColor(-1, 0x000000)
GUICtrlCreateLabel($aTexte[32], 30, 367, 100, 20)
GUICtrlCreateLabel("", 130, 366, 16, 16)
GUICtrlSetBkColor(-1, 0x0000FF)
GUICtrlCreateLabel($aTexte[33], 150, 367, 100, 20)
GUICtrlCreateLabel("", 255, 366, 16, 16)
GUICtrlSetBkColor(-1, 0xFF0000)
GUICtrlCreateLabel($aTexte[34], 275, 367, 120, 20)
GUICtrlCreateLabel("", 395, 366, 16, 16)
GUICtrlSetBkColor(-1, 0xFFFF00)
GUICtrlCreateLabel($aTexte[35], 415, 367, 130, 20)
GUICtrlCreateLabel("", 550, 366, 16, 16)
GUICtrlSetBkColor(-1, 0x00FF00)
GUICtrlCreateLabel($aTexte[36], 570, 367, 100, 20)

;ListView
Global $hListView = GUICtrlCreateListView("| " & $aTexte[14] & " | " & $aTexte[15] & "     | " & $aTexte[13] & " " & $aTexte[12] & " | " & Chr(216) & " " & $aTexte[13] & " " & $aTexte[12], 10, 30, 403, 260, $LVS_NOCOLUMNHEADER)
_GUICtrlListView_SetExtendedListViewStyle($hListView, $exStyles)
_GUICtrlListView_JustifyColumn($hListView, 2, 2); Spalte 2,3,4 zentrieren
_GUICtrlListView_JustifyColumn($hListView, 3, 2)
_GUICtrlListView_JustifyColumn($hListView, 4, 2)
_GUICtrlListView_SetColumnWidth($hListView, 0, 20) ;Breite der Spalten festlegen !!! nicht änderbar, da Spaltenkopf ausgeblendet !!!
_GUICtrlListView_SetColumnWidth($hListView, 1, 180)
_GUICtrlListView_SetColumnWidth($hListView, 2, 70)
_GUICtrlListView_SetColumnWidth($hListView, 3, 60)
_GUICtrlListView_SetColumnWidth($hListView, 4, 50)

Global $hImage = _GUIImageList_Create(16, 16, 4, 0, 6)
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFF0000, 16, 16)); 0 rot
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x00FF00, 16, 16)); 1 grün
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFFFF00, 16, 16)); 2 gelb
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x0000FF, 16, 16)); 3 blau
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0x000000, 16, 16)); 4 schwarz
_GUIImageList_Add($hImage, _GUICtrlListView_CreateSolidBitMap($hListView, 0xFFFFFF, 16, 16)); 5 weiß
_GUICtrlListView_SetImageList($hListView, $hImage, 1)

;Funktionsbutton
Global $btn_start = GUICtrlCreateButton($aTexte[6], 10, 300, 120, 40)
GUICtrlSetFont(-1, 14, 900)
Global $btn_allon = GUICtrlCreateButton($aTexte[8], 151, 300, 120, 40)
GUICtrlSetFont(-1, 14, 900)
Global $btn_alloff = GUICtrlCreateButton($aTexte[9], 292, 300, 120, 40)
GUICtrlSetFont(-1, 14, 900)

GUICtrlCreateGroup($aTexte[20], 440, 23, 200, 266)
GUICtrlCreateLabel($aTexte[18], 450, 40, 130, 20)
Global $l_intervall = GUICtrlCreateLabel("", 580, 40, 50, 20)
Global $sl_intervall = GUICtrlCreateSlider(450, 60, 180, 20)
GUICtrlSetLimit(-1, 20, 1)
GUICtrlSetData(-1, 2)
GUICtrlCreateLabel($aTexte[19], 450, 90, 130, 20)
Global $l_schwelle = GUICtrlCreateLabel("", 580, 90, 50, 20)
Global $sl_schwelle = GUICtrlCreateSlider(450, 110, 180, 20)
GUICtrlSetLimit(-1, 20, 1)
GUICtrlSetData(-1, 2)
Global $cb_scrollen = GUICtrlCreateCheckbox($aTexte[21], 450, 160, 180, 20)
If $a_ip[0] < 16 Then GUICtrlSetState(-1, $GUI_DISABLE)
Global $cb_offpingen = GUICtrlCreateCheckbox($aTexte[22], 450, 180, 180, 20)
GUICtrlSetState(-1, $GUI_UNCHECKED)
Global $cb_alarm = GUICtrlCreateCheckbox($aTexte[23], 450, 200, 180, 20)
GUICtrlSetState(-1, $GUI_UNCHECKED)
Global $cb_schwelle = GUICtrlCreateCheckbox($aTexte[24], 450, 220, 180, 20)
GUICtrlSetState(-1, $GUI_UNCHECKED)
Global $cb_start0 = GUICtrlCreateCheckbox($aTexte[25], 450, 240, 180, 20)
GUICtrlSetState(-1, $GUI_UNCHECKED)
GUICtrlCreateGroup("", -99, -99, 1, 1)

For $i = 1 To $a_ip[0]
    $a_LV[$i][0] = $a_ip[$i]
    $a_LV[$i][1] = 0
    $a_LV[$i][2] = 0
    $a_LV[$i][3] = GUICtrlCreateListViewItem("|" & $a_LV[$i][0], $hListView)
    $a_LV[$i][4] = True
    _GUICtrlListView_SetItemChecked($hListView, $i - 1, True)
    _GUICtrlListView_SetItemImage($hListView, $i - 1, 5, 2)
    _GUICtrlListView_SetItemImage($hListView, $i - 1, 5, 3)
    GUICtrlSetData($a_LV[$i][3], "||" & $aTexte[2])
Next

$MarkAll = GUICtrlCreateCheckbox("", 16, 7, 20, 20)
GUICtrlSetTip(-1, "Alle aus/abwählen")

_SetSlider() ; Setzen der Defaultwerte in den Slidern

GUISetState()
#EndRegion Gui

$timer = TimerInit()
#Region Programmschleife
While 1
    Sleep(10)
    $msg = GUIGetMsg()
    $sw_onoff = 0
    Switch $msg
        Case -3
            Exit
        Case $sl_intervall
            $intervall = GUICtrlRead($sl_intervall)
            _SetSlider()
        Case $sl_schwelle
            $schwelle = GUICtrlRead($sl_schwelle)
            _SetSlider()
        Case $btn_start
            $sw_start = Not $sw_start
            If $sw_start = True Then ; Prüfung aktiviert
                GUICtrlSetData($btn_start, $aTexte[7])
            Else
                GUICtrlSetData($btn_start, $aTexte[6])
                _GUICtrlListView_SetItemImage($hListView, $t - 1, 3, 2)
                GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[2])
                If BitAND(GUICtrlRead($cb_start0), $GUI_CHECKED) Then $t = 1
            EndIf
        Case $btn_allon
            $sw_onoff = 1
            GUICtrlSetState($btn_alloff, $GUI_ENABLE)
        Case $btn_alloff
            $sw_onoff = 2
            $sw_start = False
            GUICtrlSetData($btn_start, $aTexte[6])
            GUICtrlSetData($a_LV[$t - 1][3], "||" & $aTexte[2])
            GUICtrlSetState($btn_alloff, $GUI_DISABLE)
        Case $MarkAll

            Switch BitAND(GUICtrlRead($MarkAll), $GUI_CHECKED)
                Case True
                    _GUICtrlListView_SetItemChecked($hListView, -1)
                Case False
                    _GUICtrlListView_SetItemChecked($hListView, -1, False)
            EndSwitch
    EndSwitch
    $sw_btnstartendis = False
    For $i = 0 To _GUICtrlListView_GetItemCount($hListView) - 1
        Switch $sw_onoff
            Case 1
                _GUICtrlListView_SetItemChecked($hListView, $i, True)
            Case 2
                _GUICtrlListView_SetItemChecked($hListView, $i, False)
                _GUICtrlListView_SetItemImage($hListView, $i, 4, 2)
                _GUICtrlListView_SetItemImage($hListView, $i, 5, 3)
                _GUICtrlListView_SetItemImage($hListView, $i, 5, 4)
                GUICtrlSetData($a_LV[$i][3], "||" & $aTexte[5] & "| | ")
        EndSwitch
        If _GUICtrlListView_GetItemChecked($hListView, $i) Then
            $sw_btnstartendis = True
            If $a_LV[$i + 1][4] = False Then
                GUICtrlSetState($btn_alloff, $GUI_ENABLE)
                $a_LV[$i + 1][4] = True
                _GUICtrlListView_SetItemImage($hListView, $i, 3, 2)
                GUICtrlSetData($a_LV[$i + 1][3], "||" & $aTexte[2])
            EndIf
        Else
            If $a_LV[$i + 1][4] = True Then
                $a_LV[$i + 1][4] = False
                _GUICtrlListView_SetItemImage($hListView, $i, 4, 2)
                GUICtrlSetData($a_LV[$i + 1][3], "||" & $aTexte[5])
            EndIf
        EndIf
    Next
    $a = GUIGetCursorInfo($gui)
    If IsArray($a) Then
        If Not (36 > $a[0] And 16 < $a[0] + 20 And 27 > $a[1] And 7 < $a[1]) Then
            $Marked = False
            For $x = 0 To _GUICtrlListView_GetItemCount($hListView) - 1
                If Not _GUICtrlListView_GetItemChecked($hListView, $x) Then
                    $Marked = True
                    ExitLoop
                EndIf
            Next
            Switch $Marked
                Case True
                    GUICtrlSetState($MarkAll, $GUI_UNCHECKED)
                Case False
                    GUICtrlSetState($MarkAll, $GUI_CHECKED)
            EndSwitch
        EndIf
    EndIf

    If $sw_btnstartendis = False And BitAND(GUICtrlGetState($btn_start), $GUI_ENABLE) Then
        GUICtrlSetState($btn_start, $GUI_DISABLE)
    ElseIf $sw_btnstartendis = True And BitAND(GUICtrlGetState($btn_start), $GUI_DISABLE) Then
        GUICtrlSetState($btn_start, $GUI_ENABLE)
    EndIf
    If $sw_start = True Then
        If TimerDiff($timer) > $intervall * 100 Then
            If _GUICtrlListView_GetItemChecked($hListView, $t - 1) = True Then
                $temp = Ping($a_LV[$t][0], $timeout * 50)
                If $temp > 0 Then
                    $a_LV[$t][1] += $temp
                    $a_LV[$t][2] += 1
                    GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[3])
                    GUICtrlSetData($a_LV[$t][3], "|||" & $temp)
                    GUICtrlSetData($a_LV[$t][3], "||||" & Round($a_LV[$t][1] / $a_LV[$t][2], 0))
                    _GUICtrlListView_SetItemImage($hListView, $t - 1, 1, 2)
                    If $temp > $schwelle * 10 And BitAND(GUICtrlRead($cb_schwelle), $GUI_CHECKED) Then
                        _GUICtrlListView_SetItemImage($hListView, $t - 1, 2, 3)
                    Else
                        _GUICtrlListView_SetItemImage($hListView, $t - 1, 5, 3)
                    EndIf
                Else
                    GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[4])
                    GUICtrlSetData($a_LV[$t][3], "||| ")
                    GUICtrlSetData($a_LV[$t][3], "|||| ")
                    _GUICtrlListView_SetItemImage($hListView, $t - 1, 0, 2)
                    Select
                        Case BitAND(GUICtrlRead($cb_alarm), $GUI_CHECKED) And BitAND(GUICtrlRead($cb_offpingen), $GUI_CHECKED)
                            MsgBox(262160, $aTexte[0] & $aTexte[26], $aTexte[27] & $a_ip[$t] & $aTexte[28] & @CRLF & @CRLF & $aTexte[29], 10)
                            _GUICtrlListView_SetItemChecked($hListView, $t - 1, False)
                            _GUICtrlListView_SetItemImage($hListView, $t - 1, 4, 2)
                            GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[5])
                            $a_LV[$t][4] = False
                        Case BitAND(GUICtrlRead($cb_alarm), $GUI_UNCHECKED) And BitAND(GUICtrlRead($cb_offpingen), $GUI_CHECKED)
                            _GUICtrlListView_SetItemChecked($hListView, $t - 1, False)
                            _GUICtrlListView_SetItemImage($hListView, $t - 1, 4, 2)
                            GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[5])
                            $a_LV[$t][4] = False
                        Case BitAND(GUICtrlRead($cb_alarm), $GUI_CHECKED) And BitAND(GUICtrlRead($cb_offpingen), $GUI_UNCHECKED)
                            MsgBox(262192, $aTexte[0] & $aTexte[26], $aTexte[27] & $a_ip[$t] & $aTexte[28] & @CRLF & $aTexte[30], 5)
                    EndSelect
                EndIf
                $timer = TimerInit()
            EndIf
            $t += 1
            If $t > $a_ip[0] Then $t = 1
            If _GUICtrlListView_GetItemChecked($hListView, $t - 1) = True Then
                _GUICtrlListView_SetItemImage($hListView, $t - 1, 5, 2)
                GUICtrlSetData($a_LV[$t][3], "||" & $aTexte[1])
            EndIf
            If BitAND(GUICtrlRead($cb_scrollen), $GUI_CHECKED) Then
                Select
                    Case $t > 6 And $t < $a_ip[0] - 5
                        _GUICtrlListView_Scroll($hListView, 0, 10)
                    Case $t = 1
                        _GUICtrlListView_Scroll($hListView, 0, -10000)
                EndSelect
            EndIf
        EndIf
    EndIf
WEnd
#EndRegion Programmschleife

#Region Funktionen
Func _SetSlider()
    GUICtrlSetData($sl_intervall, $intervall)
    GUICtrlSetData($sl_schwelle, $schwelle)
    GUICtrlSetData($l_intervall, $intervall * 100 & " ms")
    GUICtrlSetData($l_schwelle, $schwelle * 10 & " ms")
EndFunc   ;==>_SetSlider

Func _MyExit()
    GUIDelete()
EndFunc   ;==>_MyExit
