Everything is split into modules and put into classes where inheritance is needed

This commit is contained in:
2025-06-05 18:37:02 -04:00
parent af2fe80ce5
commit 32f60194d0
4 changed files with 66 additions and 53 deletions

View File

@ -1,26 +1,35 @@
import subprocess
import main
import ntfy
import time
import re
from typing import Optional
from ntfy import Ntfy
last_cpu_check: float = time.time()
last_cpu_check_unix: float = time.time()
last_cpu_check: int = 60 # Seconds
def temperature() -> Optional[float]:
sensors_out = subprocess.check_output(["sensors"]).decode()
for line in sensors_out.splitlines():
if "Tctl" in line:
match = re.search(r"(\d+\.\d+)°C", line)
if match:
return float(match.group(1))
return None
class Tempature:
cpu_temp_crtitical_message: str = "🔥 CPU tempature is at critical tempatures!"
cpu_temp_warning_message: str = "🌡️ CPU tempature is at a high tempature."
def temperature_check():
cpu_temp = temperature()
if cpu_temp and (time.time() - last_cpu_check) > main.last_check_debounce * 1000:
if cpu_temp >= main.cpu_critical_temp:
ntfy.send(f"🔥 CPU tempature is at critical tempatures! {cpu_temp}")
elif cpu_temp >= main.cpu_warning_temp:
ntfy.send(f"🌡️ CPU tempature is at a high tempature. {cpu_temp}")
def __init__(self, ntfy_instance: Ntfy, cpu_critical_temp: int, cpu_warning_temp: int):
self.cpu_critical_temp = cpu_critical_temp
self.cpu_warning_temp = cpu_warning_temp
self.ntfy = ntfy_instance
def get(self) -> Optional[float]:
sensors_out = subprocess.check_output(["sensors"]).decode()
for line in sensors_out.splitlines():
if "Tctl" in line:
match = re.search(r"(\d+\.\d+)°C", line)
if match:
return float(match.group(1))
return None
def ntfy_check(self):
cpu_temp = self.get()
if cpu_temp and (time.time() - last_cpu_check_unix) > last_cpu_check * 1000:
if cpu_temp >= self.cpu_critical_temp:
self.ntfy.send(f"{Tempature.cpu_temp_crtitical_message} {cpu_temp}")
elif cpu_temp >= self.cpu_warning_temp:
self.ntfy.send(f"{Tempature.cpu_temp_warning_message} {cpu_temp}")