Redisgned the way CPU tempature is detected and the start up notification

This commit is contained in:
2025-06-12 01:44:21 -04:00
parent 97e3348bd4
commit 3dbc105ae3
5 changed files with 75 additions and 53 deletions

View File

@ -7,21 +7,25 @@ from print_t import print_t
from typing import Optional
from ntfy import Ntfy
_time_now = time.time()
_init_run_critical: bool = True
_init_run_warning: bool = True
_time_now: float = time.time()
last_cpu_check_warning: float = _time_now
last_cpu_warning_set: float = _time_now
last_warning_temp: int = 0
last_cpu_check_warning: float = _time_now
last_cpu_check_crticial: float = _time_now
def timeout_expired(check: float, timeout: int) -> bool:
return (time.time() - check) > timeout * 1000
return (time.time() - check) > timeout
class Tempature:
warning_message: str = "🌡️ CPU is at a high tempature."
timeout_check: int = 60 # Seconds
timeout_check_critical: int = 120 # Seconds
timeout_check_warn: int = 300 # Seconds
critical_message: str = "🔥 CPU is at a very high tempature."
warning_message: str = "🌡️ CPU is at a high tempature."
thermal_critical_c: int = 80
thermal_warn_c: int = 70
def __init__(self, ntfy_instance: Ntfy, warning_temp: int):
self.warning_temp = warning_temp
def __init__(self, ntfy_instance: Ntfy):
self.ntfy = ntfy_instance
def get(self) -> Optional[float]:
@ -33,27 +37,22 @@ class Tempature:
return float(match.group(1))
return None
def __warn(self, cpu_temp: int):
pad = 0
if timeout_expired(last_cpu_warning_set, Tempature.timeout_check * 5):
#If the timeout expired, send a notify immediately
global last_warning_temp
last_warning_temp = 0
else:
#If the timeout is still active, apply padding to the current CPU tempature so its less spammy
pad = 5
if cpu_temp + pad > last_warning_temp:
message = f"{cpu_temp} C."
if last_warning_temp != 0:
message += f" The tempature has risen by: {cpu_temp - last_warning_temp} C. 📈"
last_warning_temp = cpu_temp
self.ntfy.send(message=message, title=Tempature.warning_message)
def ntfy_check(self):
cpu_temp = self.get()
if cpu_temp:
global _init_run_warning
global _init_run_critical
global last_cpu_check_warning
global last_cpu_check_crticial
cpu_temp = math.floor(cpu_temp)
if cpu_temp >= self.warning_temp and timeout_expired(last_cpu_check_warning, Tempature.timeout_check):
self.__warn(cpu_temp)
if cpu_temp >= Tempature.thermal_warn_c and (_init_run_warning or timeout_expired(last_cpu_check_warning, Tempature.timeout_check_warn)):
_init_run_warning = False
last_cpu_check_warning = time.time()
self.ntfy.send(message=f"{cpu_temp} C", title=Tempature.warning_message)
if cpu_temp >= Tempature.thermal_critical_c and (_init_run_critical or timeout_expired(last_cpu_check_crticial, Tempature.timeout_check_critical)):
_init_run_critical = False
last_cpu_check_crticial = time.time()
self.ntfy.send(message=f"{cpu_temp} C", title=Tempature.critical_message)
else:
print_t("\033[31mCannot get a feasible tempature value for the CPU. (lm-sensors)\033[0m")