from pystray import Icon as icon, Menu as menu, MenuItem as item from PIL import Image import sys import subprocess import time import pathlib import os import getpass __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) CREATE_NO_WINDOW = 0x08000000 startupState = False useBlackState = False notifyState = True pingFrequency = 5 runDisabled = False previousPing = True if pathlib.Path(os.path.join(__location__, 'settings.txt')).is_file(): settings = open(os.path.join(__location__, 'settings.txt'), "r") data = settings.read().split(",") useBlackState = eval(data[0]) pingFrequency = int(data[1]) loggingState = eval(data[2]) notifyState = eval(data[3]) settings.close() bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\start-wifiChecker.bat' % getpass.getuser() if os.path.exists(bat_path): runDisabled = True def ping(): ping_response = subprocess.Popen(["ping", "-a", "google.com"], stdout=subprocess.PIPE, creationflags=CREATE_NO_WINDOW).stdout.read() result = ping_response.decode('utf-8') if loggingState: log = open(os.path.join(__location__, 'log.log'), "a") log.write("\n\n ----------- \n\n"+result) log.close() if "Lost = 0" in result or "Lost = 1" in result: return [True, True, result.split("Average = ")[1]] elif "Lost = 2" in result or "Lost = 3" in result: return [True, False, result.split("Average = ")[1]] else: return [False] def quitScript(icon): icon.visible = False icon.stop() def saveSettings(icon): settings = open(os.path.join(__location__, 'settings.txt'), "w") settings.write(str(useBlackState) + "," + str(pingFrequency) + "," + str(loggingState) + "," + str(notifyState)) settings.close() def set_state(v): def inner(icon, item): global pingFrequency pingFrequency = v return inner def get_state(v): def inner(item): return pingFrequency == v return inner def startup(icon): runDisabled = True bat_path = r'C:\Users\%s\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup' % getpass.getuser() with open(bat_path + '\\' + "start-wifiChecker.bat", "w+") as bat_file: bat_file.write(r'start %s' % (__location__ + "\\wifiChecker.pyw")) bat_file.close() icon.notify("Added To Startup List For Current User\nTo remove: press WinKey+R, type 'shell:startup', and delete the file named 'start-wifiChecker.bat'.") def blackIcons(icon, item): global useBlackState useBlackState = not item.checked icon.notify("Will Change Icon Color On Next Ping") time.sleep(3) icon.remove_notification() def logging(icon, item): global loggingState loggingState = not item.checked def connectionNotify(icon, item): global notifyState notifyState = not item.checked def setup(icon): icon.visible = True while icon.visible: pingVal = ping() if pingVal[0] == False: icon.icon = Image.open(__location__+"\\noWifi" + ("_White" if not useBlackState else "") + ".png") icon.title = "WiFi Checker\nFailed To Ping" if previousPing and notifyState: icon.notify("Disconnected From Internet") time.sleep(3) icon.remove_notification() previousPing = False else: if pingVal[1]: icon.icon = Image.open(__location__+"\\fullWifi" + ("_White" if not useBlackState else "") + ".png") icon.title = "WiFi Checker\nPing: " + pingVal[2] else: icon.icon = Image.open(__location__+"\\wifiAvailable" + ("_White" if not useBlackState else "") + ".png") icon.title = "WiFi Checker\nPing: " + pingVal[2] + "Lost Pings" previousPing = True time.sleep(pingFrequency) def run(): if loggingState: log = open(os.path.join(__location__, 'log.log'), "w") log.write("") log.close() icon("Wifi Checker", Image.open(__location__+"\wifiLoading" + ("_White" if not useBlackState else "") + ".png"), menu = menu( item( "Ping Frequency", menu( lambda: ( item( "No Delay", set_state(0), checked=get_state(0), radio=True ), item( "Delay 3 seconds", set_state(3), checked=get_state(3), radio=True ), item( "Delay 5 seconds", set_state(5), checked=get_state(5), radio=True ), item( "Delay 10 seconds", set_state(10), checked=get_state(10), radio=True ), ) ) ), item( 'Use Black Icons', blackIcons, checked=lambda item: useBlackState ), item( 'Notify When Connection Lost', connectionNotify, checked=lambda item: notifyState ), item( 'Run On Login', startup, enabled=lambda item: not runDisabled ), item( 'Enable Logging To File', logging, checked=lambda item: loggingState ), item( 'Save Settings For Next Run', saveSettings ), item( 'Quit', quitScript, default = True ) ), title="Wifi Checker").run(setup) run()