from itertools import count
from logging import root
import socket
import ipaddress
import ctypes
import time
import os
import tkinter as tk
from tkinter import *
from tkinter.font import BOLD
lib = ctypes.windll.kernel32
t = lib.GetTickCount64()
t = int(str(t)[:-3])
mins, sec = divmod(t, 60)
hour, mins = divmod(mins, 60)
days, hour = divmod(hour, 24)
UPTIME = f"{days} days, {hour:02}:{mins:02}:{sec:02}"
UPTIME_DAYS = f"{days}"
NUMBER_OF_DAYS = 7
COUNTDOWN = 60
UPTIME_LONG = days >= NUMBER_OF_DAYS
TEXT = "Restartujte prosím co nejdříve své zařízení!"
DEVICE_IP = ipaddress.ip_address(socket.gethostbyname(socket.gethostname()))
LAN_VINICE = ipaddress.ip_network('10.100.0.0/16')
WIFI_VINICE = ipaddress.ip_network('10.64.4.0/22')
VINICE_NETWORK = (DEVICE_IP in LAN_VINICE) or (DEVICE_IP in WIFI_VINICE)
USERNAME = os.environ.get('USERNAME')
#print("IP ADDRESS:", DEVICE_IP)
#print("UPTIME:", UPTIME)
#print("UPTIME TOO LONG:", UPTIME_LONG)
#print("VINICE NETWORK:", VINICE_NETWORK)
#def Close():
# root.destroy()
def Restart():
os.system(f"shutdown -t {COUNTDOWN} -r -f")
#restart_button["state"] = DISABLED
#cancel_restart_button["state"] = NORMAL
restart_button.pack_forget()
cancel_restart_button.pack(padx=10, pady=10)
countdown(COUNTDOWN)
def Cancel_Restart():
os.system("shutdown -a")
#restart_button["state"] = NORMAL
#cancel_restart_button["state"] = DISABLED
cancel_restart_button.pack_forget()
restart_button.pack(padx=10, pady=10)
root.after_cancel(job)
T3['text'] = f"{TEXT}"
def No_Close():
pass
def countdown(count):
# change text in label
T3['text'] = f"Odpočet do restartu: {count}"
if count > 0:
global job
# call countdown again after 1000ms (1s)
job = root.after(1000, countdown, count-1)
#if UPTIME_LONG and VINICE_NETWORK:
if DEVICE_IP:
root = tk.Tk()
root.title("")
root.geometry("380x180")
#root.iconbitmap(r'c:\Windows\System32\WindowsUpdateIcon.ico')
#root.eval('tk::PlaceWindow . center')
root.call('wm', 'attributes', '.', '-topmost', '1')
#root.overrideredirect(1)
root.resizable(0,0)
root.attributes('-toolwindow', True)
root.option_add( "*font", "Consolas 11" )
#root.attributes("-alpha", 0.5)
root.protocol("WM_DELETE_WINDOW", No_Close)
top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)
job = None
T1 = tk.Label(root, text=f"Uživatel: {USERNAME}\nIP adresa: {DEVICE_IP}", padx=6, pady=6)
T1.pack()
T2 = tk.Label(root, text=f"Počet dnů bez restartu: {UPTIME_DAYS}", padx=6, pady=6, fg="red")
T2.pack()
T3 = tk.Label(root, text=f"{TEXT}", padx=6, pady=6, font=('Consolas', 11, BOLD))
T3.pack()
restart_button = Button(root, text="RESTARTOVAT", command=Restart, bg="red", fg="white", width=20)
#restart_button.pack(padx=10, pady=10, in_=bottom, side=LEFT)
restart_button.pack(padx=10, pady=10)
cancel_restart_button = Button(root, text="ODLOŽIT", command=Cancel_Restart, width=20, bg="green", fg="white")
#cancel_restart_button["state"] = DISABLED
root.mainloop()
#else:
# print("NOTHING")