Jump to content

Network meter


Recommended Posts

Hello guys, I have found this network meter for my father because his Internet provider billing him wrong. So i've taken this code and made some change.

My problem is when I get disconnected all the number goes negative ($amont, $aval, $totalstate) and when I get it back these number stay negative. I want to keep them like just before I lose the connection.

Anyone can help me with this ?

The code is attached.

Network Meter.au3

Link to comment
Share on other sites

Jayson, try this:

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <WinAPIEx.au3>
Opt("GUIOnEventMode", 1)

Global $intext, $outtext, $totalin, $newin, $newout

GUICreate("Connexion Internet", 280, 160, 150, 150, BitOR($WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_POPUP, $WS_GROUP, $WS_CLIPSIBLINGS), BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit");just to stop it

GUICtrlCreateGroup("Connexion", 5, 5, 270, 35)
GUICtrlCreateLabel("Status:", 25, 22, 35, 15)
$statusstate = GUICtrlCreateLabel("", 100, 22, 170, 15)

GUICtrlCreateGroup("Activité", 5, 45, 270, 65)
GUICtrlCreateLabel("Envoyés:", 15, 62, 50, 15)
$amont = GUICtrlCreateLabel("En attente de données...", 100, 62, 170, 15)
GUICtrlCreateLabel("Reçus:", 15, 77, 70, 15)
$aval = GUICtrlCreateLabel("En attente de données...", 100, 77, 170, 15)
GUICtrlCreateLabel("Activité totale:", 15, 92, 70, 15)
$totalstate = GUICtrlCreateLabel("", 100, 92, 170, 15)

GUICtrlCreateGroup("Time", 5, 115, 270, 35)
GUICtrlCreateLabel("Time connected:", 25, 132, 80, 15)
$timestate = GUICtrlCreateLabel("", 120, 132, 130, 15)

GUISetState()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""

$Output=""
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

$inmax = 0
$outmax = 0

$lastin = 0
$lastout = 0
$seconds = 0 * 60
Countdown()
AdlibRegister("Countdown", 1000)

While 1
    Sleep(1000)
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        $iNetworkConnected = _WinAPI_IsNetworkAlive()
       For $objItem In $colItems
            ;I want only to check for the wireless network adapter
            If StringInStr($objItem.Name,"Atheros")<>0 Then
              $newin = $objItem.BytesReceivedPersec
              $newout = $objItem.BytesSentPersec
              ;$total = $objItem.BytesTotalPersec
            EndIf
            If $lastin = 0 and $lastout = 0 Then
               $lastin = $newin
               $lastout = $newout
            Endif

            $in = $newin - $lastin
            $out = $newout - $lastout
            $lastin = $newin
            $lastout = $newout

            if $in <> 0 and $out <> 0 Then
                $inmax = $inmax + $in
                $outmax = $outmax + $out
                $inP = Round((($inmax / 1024) / 1024), 3)
                $outP = Round((($outmax / 1024) / 1024), 3)
                $totalP = Round(((($inmax + $outmax) /1024) / 1048576), 3)
                $intext = int($inmax) & " octets (" & $inP & " Mo)"
                $outtext = int($outmax) & " octets (" & $outP & " Mo)"
                $totalin = $inP + $outP & " Mo (" & $totalP & " Go)"
                GUICtrlSetData($amont,$intext)
                GUICtrlSetData($aval,$outtext)
                GUICtrlSetData($totalstate,$totalin)
            EndIf
            If $iNetworkConnected  Then 
                GUICtrlSetData($statusstate,"Connecté")
                AdlibRegister("Countdown", 1000)
            Else
                GUICtrlSetData($statusstate,"Non-connecté")
                GUICtrlSetData($amont,"")
                GUICtrlSetData($aval,"")
                GUICtrlSetData($totalstate,"")
                AdlibUnRegister("Countdown")
            EndIf
       Next
    Endif
WEnd

Func Countdown()
    Local $sec, $min, $hr
    $sec = Mod($seconds, 60)
    $min = Mod($seconds / 60, 60)
    $hr = Floor($seconds / 60 ^ 2)
    GUICtrlSetData($timestate, StringFormat("%02i:%02i:%02i", $hr, $min, $sec))
    $seconds += 1
EndFunc

Func _Exit()
    Exit
EndFunc

[EDIT] added network connection checking (credits to Yashied for his WinAPIEx UDF - I've used _WinAPI_IsNetworkAlive() for that)

Edited by taietel
Link to comment
Share on other sites

the script could use some work .. but this should do what you want ..

#include <GUIConstants.au3>
#include <WindowsConstants.au3>

GUICreate("Connexion Internet", 280, 140, 150, 150);, BitOR($WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_POPUP, $WS_GROUP, $WS_CLIPSIBLINGS), BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))

;GUICtrlCreateGroup("Connexion", 5, 5, 290, 35)
GUICtrlCreateLabel("Status:", 15, 22, 35, 15)
$statusstate = GUICtrlCreateLabel("", 120, 22, 170, 15)

;GUICtrlCreateGroup("Activité", 5, 45, 210, 65)
GUICtrlCreateLabel("Envoyés:", 15, 42, 50, 15)
$amont = GUICtrlCreateLabel("En attente de données...", 120, 42, 170, 15)
GUICtrlCreateLabel("Reçus:", 15, 57, 70, 15)
$aval = GUICtrlCreateLabel("En attente de données...", 120, 57, 170, 15)
GUICtrlCreateLabel("Activité totale:", 15, 80, 70, 15)
$totalstate = GUICtrlCreateLabel("", 120, 80, 170, 15)

;GUICtrlCreateGroup("Time", 5, 115, 290, 35)
GUICtrlCreateLabel("Time connected:", 15, 100, 80, 15)
$timestate = GUICtrlCreateLabel("", 120, 100, 170, 15)

GUISetState()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

$inmax = 0
$outmax = 0

$lastin = 0
$lastout = 0
$seconds = 0 * 60; <--- huh?
Countdown()
AdlibRegister("Countdown", 1000)

While 1
    Sleep(1000)
    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", 0x30)
    If Not IsObj($colItems) Then ContinueLoop
    For $objItem In $colItems
        $newin = $objItem.BytesReceivedPersec
        $newout = $objItem.BytesSentPersec
        If $lastin = 0 And $lastout = 0 Then
            $lastin = $newin
            $lastout = $newout
        EndIf
        $in = $newin - $lastin
        $out = $newout - $lastout
        $lastin = $newin
        $lastout = $newout
        If $in > 0 And $out > 0 Then
            $inmax = $inmax + $in
            $outmax = $outmax + $out
            $inP = Round((($inmax / 1024) / 1024), 3)
            $outP = Round((($outmax / 1024) / 1024), 3)
            $totalP = Round(((($inmax + $outmax) / 1024) / 1048576), 3)
            $intext = Int($inmax) & " octets (" & $inP & " Mo)"
            $outtext = Int($outmax) & " octets (" & $outP & " Mo)"
            $totalin = $inP + $outP & " Mo (" & $totalP & " Go)"
            GUICtrlSetData($amont, $intext)
            GUICtrlSetData($aval, $outtext)
            GUICtrlSetData($totalstate, $totalin)
        EndIf
        ExitLoop
    Next
    If @IPAddress1 <> "127.0.0.1" Then
        GUICtrlSetData($statusstate, "Connecté")
        AdlibRegister("Countdown", 1000)
    Else
        GUICtrlSetData($statusstate, "Non-connecté")
        ;GUICtrlSetData($amont, "")
        ;GUICtrlSetData($aval, "")
        ;GUICtrlSetData($totalstate, "")
        AdlibUnRegister("Countdown")
    EndIf
WEnd

Func Countdown()
    Local $sec, $min, $hr
    $sec = Mod($seconds, 60)
    $min = Mod($seconds / 60, 60)
    $hr = Floor($seconds / 60 ^ 2)
    GUICtrlSetData($timestate, StringFormat("%02i:%02i:%02i", $hr, $min, $sec))
    $seconds += 1
EndFunc

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...