Untitled

 avatar
unknown
plain_text
2 years ago
14 kB
5
Indexable
import ctypes
import struct
import psutil
import json
import urllib.request
import tkinter as tk
import time
import os
import speedtest

class DeviceInformation:
    def __init__(self):
        self.battery_info = self.get_battery_info()
        self.location_data = self.get_device_location()
        self.transfer_speed = self.get_disk_transfer_speed()
        self.estimated_transfer_time = self.get_estimated_transfer_time(1024 * 1024 * 1024)  # 1 GB
        self.download_speed, self.upload_speed = self.get_internet_speed()

    def get_internet_speed(self):
        st = speedtest.Speedtest()
        download_speed = st.download() / 1024 / 1024  # Convert to Mbps
        upload_speed = st.upload() / 1024 / 1024  # Convert to Mbps
        return download_speed, upload_speed

    def get_battery_info(self):
        # Utilizarea Windows API pentru a obține informații despre baterie
        battery_info = {}

        # Definirea structurii pentru rezultatul informațiilor despre baterie
        class SYSTEM_POWER_STATUS(ctypes.Structure):
            _fields_ = [("ACLineStatus", ctypes.c_byte),
                        ("BatteryFlag", ctypes.c_byte),
                        ("BatteryLifePercent", ctypes.c_byte),
                        ("Reserved1", ctypes.c_byte),
                        ("BatteryLifeTime", ctypes.c_ulong),
                        ("BatteryFullLifeTime", ctypes.c_ulong)]

        # Obținerea informațiilor despre baterie utilizând funcția GetSystemPowerStatus
        system_power_status = SYSTEM_POWER_STATUS()
        result = ctypes.windll.kernel32.GetSystemPowerStatus(ctypes.byref(system_power_status))
        if result != 0:
            battery_info["ACLineStatus"] = system_power_status.ACLineStatus
            battery_info["BatteryFlag"] = system_power_status.BatteryFlag
            battery_info["BatteryLifePercent"] = system_power_status.BatteryLifePercent

        return battery_info

    def get_device_location(self):
        url = "https://ipinfo.io/json"
        response = urllib.request.urlopen(url)
        location_data = json.load(response)
        return location_data

    def get_disk_transfer_speed(self):
        # Obținerea vitezei de transfer a hard disk-ului
        disk_speed = 0.0
        try:
            start_time = time.time()
            file_path = "test_file.txt"
            file_size = 100 * 1024 * 1024  # 100 MB
            with open(file_path, "wb") as file:
                file.write(os.urandom(file_size))
            end_time = time.time()
            transfer_time = end_time - start_time
            disk_speed = file_size / transfer_time / 1024 / 1024  # MB/s
            os.remove(file_path)
        except Exception as e:
            print(f"Error occurred while measuring disk transfer speed: {e}")

        return disk_speed

    def get_estimated_transfer_time(self, file_size):
        # Obținerea timpului estimat pentru transferul unui fișier de mărime dată
        transfer_speed = self.transfer_speed
        if transfer_speed > 0:
            estimated_time = file_size / (transfer_speed * 1024 * 1024)  # În secunde
            return estimated_time
        else:
            return 0

    def get_cpu_usage(self):
        # Obținerea nivelului de utilizare a procesorului
        cpu_usage = psutil.cpu_percent()
        return cpu_usage


class MenuApp:
    def __init__(self):
        self.device_info = DeviceInformation()

        self.root = tk.Tk()
        self.root.title("Device Menu")
        self.root.geometry("300x200")

        self.main_menu_frame = tk.Frame(self.root)
        self.main_menu_frame.pack(pady=20)

        self.internet_menu_frame = tk.Frame(self.root)
        self.battery_menu_frame = tk.Frame(self.root)
        self.location_menu_frame = tk.Frame(self.root)
        self.transfer_speed_menu_frame = tk.Frame(self.root)
        self.cpu_usage_menu_frame = tk.Frame(self.root)
        self.tests_menu_frame = tk.Frame(self.root)

        self.create_main_menu()
        self.create_internet_menu()
        self.create_battery_menu()
        self.create_location_menu()
        self.create_transfer_speed_menu()
        self.create_cpu_usage_menu()
        self.create_tests_menu()

    def create_main_menu(self):
        main_menu_label = tk.Label(self.main_menu_frame, text="Main Menu", font=("Helvetica", 16))
        main_menu_label.pack(pady=10)

        internet_button = tk.Button(self.main_menu_frame, text="Internet", command=self.show_internet_menu)
        internet_button.pack(pady=5)

        battery_button = tk.Button(self.main_menu_frame, text="Battery", command=self.show_battery_menu)
        battery_button.pack(pady=5)

        location_button = tk.Button(self.main_menu_frame, text="Location", command=self.show_location_menu)
        location_button.pack(pady=5)

        transfer_speed_button = tk.Button(self.main_menu_frame, text="Transfer Speed", command=self.show_transfer_speed_menu)
        transfer_speed_button.pack(pady=5)

        cpu_usage_button = tk.Button(self.main_menu_frame, text="CPU Usage", command=self.show_cpu_usage_menu)
        cpu_usage_button.pack(pady=5)

        tests_button = tk.Button(self.main_menu_frame, text="Tests", command=self.show_tests_menu)
        tests_button.pack(pady=5)

    def create_internet_menu(self):
        download_speed_label = tk.Label(self.internet_menu_frame,
                                        text=f"Download Speed: {self.device_info.download_speed:.2f} Mbps",
                                        font=("Helvetica", 12))
        download_speed_label.pack(pady=5)

        upload_speed_label = tk.Label(self.internet_menu_frame,
                                      text=f"Upload Speed: {self.device_info.upload_speed:.2f} Mbps",
                                      font=("Helvetica", 12))
        upload_speed_label.pack(pady=5)

        back_button = tk.Button(self.internet_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def create_battery_menu(self):
        ac_line_status = "Plugged in" if self.device_info.battery_info.get("ACLineStatus") == 1 else "Not plugged in"
        battery_life_percent = self.device_info.battery_info.get("BatteryLifePercent", 0)
        battery_info_label = tk.Label(self.battery_menu_frame,
                                      text=f"AC Line Status: {ac_line_status}\n"
                                           f"Battery Life Percent: {battery_life_percent}%",
                                      font=("Helvetica", 12))
        battery_info_label.pack(pady=5)

        back_button = tk.Button(self.battery_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def create_location_menu(self):
        country = self.device_info.location_data.get("country", "")
        city = self.device_info.location_data.get("city", "")
        ip = self.device_info.location_data.get("ip", "")
        location_info_label = tk.Label(self.location_menu_frame,
                                       text=f"Country: {country}\n"
                                            f"City: {city}\n"
                                            f"IP: {ip}",
                                       font=("Helvetica", 12))
        location_info_label.pack(pady=5)

        back_button = tk.Button(self.location_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def create_transfer_speed_menu(self):
        transfer_speed_label = tk.Label(self.transfer_speed_menu_frame,
                                        text=f"Transfer Speed: {self.device_info.transfer_speed:.2f} MB/s",
                                        font=("Helvetica", 12))
        transfer_speed_label.pack(pady=5)

        estimated_time_label = tk.Label(self.transfer_speed_menu_frame,
                                        text=f"Estimated Transfer Time for 1GB: {self.device_info.estimated_transfer_time:.2f} seconds",
                                        font=("Helvetica", 12))
        estimated_time_label.pack(pady=5)

        back_button = tk.Button(self.transfer_speed_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def create_cpu_usage_menu(self):
        cpu_usage_label = tk.Label(self.cpu_usage_menu_frame,
                                   text=f"CPU Usage: {self.device_info.get_cpu_usage()}%",
                                   font=("Helvetica", 12))
        cpu_usage_label.pack(pady=5)

        back_button = tk.Button(self.cpu_usage_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def create_tests_menu(self):
        download_speed_label = tk.Label(self.tests_menu_frame,
                                        text=f"Download Speed: {self.device_info.download_speed:.2f} Mbps",
                                        font=("Helvetica", 12))
        download_speed_label.pack(pady=5)

        video_size_480p = 1.1  # Size of the 480p YouTube video in MB (approximate value)
        video_size_720p = 2.5  # Size of the 720p YouTube video in MB (approximate value)
        video_size_1080p = 5  # Size of the 1080p YouTube video in MB (approximate value)

        video_consumption_480p = video_size_480p   # Mbps
        video_consumption_720p = video_size_720p  # Mbps
        video_consumption_1080p = video_size_1080p  # Mbps

        video_consumption_label_480p = tk.Label(self.tests_menu_frame,
                                                text=f"Video Consumption (480p): {video_consumption_480p:.2f} Mbps",
                                                font=("Helvetica", 12))
        video_consumption_label_480p.pack(pady=5)

        video_consumption_label_720p = tk.Label(self.tests_menu_frame,
                                                text=f"Video Consumption (720p): {video_consumption_720p:.2f} Mbps",
                                                font=("Helvetica", 12))
        video_consumption_label_720p.pack(pady=5)

        video_consumption_label_1080p = tk.Label(self.tests_menu_frame,
                                                 text=f"Video Consumption (1080p): {video_consumption_1080p:.2f} Mbps",
                                                 font=("Helvetica", 12))
        video_consumption_label_1080p.pack(pady=5)

        if self.device_info.download_speed >= video_consumption_1080p:
            num_videos_1080p = int(self.device_info.download_speed / video_consumption_1080p)
            result_label_text = f"Your internet speed is sufficient to watch {num_videos_1080p} videos in 1080p smoothly."
        elif self.device_info.download_speed >= video_consumption_720p:
            num_videos_720p = int(self.device_info.download_speed / video_consumption_720p)
            result_label_text = f"Your internet speed is sufficient to watch {num_videos_720p} videos in 720p smoothly."
        elif self.device_info.download_speed >= video_consumption_480p:
            num_videos_480p = int(self.device_info.download_speed / video_consumption_480p)
            result_label_text = f"Your internet speed is sufficient to watch {num_videos_480p} videos in 480p smoothly."
        else:
            result_label_text = "Your internet speed may not be sufficient to watch the video smoothly."

        result_label = tk.Label(self.tests_menu_frame, text=result_label_text, font=("Helvetica", 12))
        result_label.pack(pady=5)

        back_button = tk.Button(self.tests_menu_frame, text="Back to Main Menu", command=self.show_main_menu)
        back_button.pack(pady=10)

    def show_main_menu(self):
        self.battery_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.main_menu_frame.pack()

    def show_internet_menu(self):
        self.main_menu_frame.pack_forget()
        self.battery_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.internet_menu_frame.pack()

    def show_battery_menu(self):
        self.main_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.battery_menu_frame.pack()

    def show_location_menu(self):
        self.main_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.battery_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.location_menu_frame.pack()

    def show_transfer_speed_menu(self):
        self.main_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.battery_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack()

    def show_cpu_usage_menu(self):
        self.main_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.battery_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.tests_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack()

    def show_tests_menu(self):
        self.main_menu_frame.pack_forget()
        self.internet_menu_frame.pack_forget()
        self.battery_menu_frame.pack_forget()
        self.location_menu_frame.pack_forget()
        self.transfer_speed_menu_frame.pack_forget()
        self.cpu_usage_menu_frame.pack_forget()
        self.tests_menu_frame.pack()


app = MenuApp()
app.root.mainloop()
Editor is loading...