better error handling
fixed resume reset better logging messages Signed-off-by: Giovanni Harting <539@idlegandalf.com>
This commit is contained in:
51
pyfan.py
51
pyfan.py
@@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
import logging
|
||||
import sys
|
||||
from time import sleep
|
||||
|
||||
import yaml
|
||||
from simple_pid import PID
|
||||
|
||||
@@ -14,11 +15,8 @@ def build_pwm_path(specific):
|
||||
|
||||
|
||||
def write_sysfs(path, value):
|
||||
try:
|
||||
with open(build_pwm_path(path), 'w') as sysfs_f:
|
||||
sysfs_f.write(str(value))
|
||||
except OSError as err:
|
||||
print("WARN:", err.strerror)
|
||||
with open(build_pwm_path(path), 'w') as sysfs_f:
|
||||
sysfs_f.write(str(value))
|
||||
|
||||
|
||||
def read_sysfs(path):
|
||||
@@ -41,26 +39,35 @@ class ThermalZone:
|
||||
self.target = target
|
||||
self.setup_pwm()
|
||||
|
||||
logging.info("[{zone}] Source={source} Fans={fans} Factor={factor} PID={pid}".format(zone=name,
|
||||
source=temp_source,
|
||||
fans=fans,
|
||||
factor=factor,
|
||||
pid=(
|
||||
p, i, d)))
|
||||
logging.getLogger("pyfan").info(
|
||||
"[{zone}] Source={source} Fans={fans} Factor={factor} PID={pid}".format(zone=name,
|
||||
source=temp_source,
|
||||
fans=fans,
|
||||
factor=factor,
|
||||
pid=(
|
||||
p, i, d)))
|
||||
|
||||
def eval(self):
|
||||
diff = self.target - self.get_temp()
|
||||
val = self.pid(diff)
|
||||
|
||||
for target_fan in self.fans:
|
||||
if type(target_fan) is dict:
|
||||
write_sysfs(list(target_fan.keys())[0], min(int(val), list(target_fan.values())[0]))
|
||||
else:
|
||||
write_sysfs(target_fan, int(val))
|
||||
try:
|
||||
for target_fan in self.fans:
|
||||
if type(target_fan) is dict:
|
||||
write_sysfs(list(target_fan.keys())[0], min(int(val), list(target_fan.values())[0]))
|
||||
else:
|
||||
write_sysfs(target_fan, int(val))
|
||||
except OSError as err:
|
||||
logging.getLogger("pyfan").warning("Failed to set pwm, trying to reset it. (%s)" % err.strerror)
|
||||
self.setup_pwm(1)
|
||||
|
||||
logging.debug(
|
||||
"[{name}] {val}% ({diff}C/{temp}C)".format(name=self.name, val=int(val / 255 * 100), diff=diff,
|
||||
temp=self.get_temp()))
|
||||
p, i, d = self.pid.components
|
||||
|
||||
logging.getLogger("pyfan").debug(
|
||||
"[{name}] {val}% ({diff}C/{temp}C) ({p}|{i}|{d})".format(name=self.name, val=int(val / 255 * 100),
|
||||
diff=diff,
|
||||
temp=self.get_temp(), p=int(p), i=int(i),
|
||||
d=int(d)))
|
||||
|
||||
def get_temp(self):
|
||||
return float(read_sysfs(self.temp_source)) * self.factor
|
||||
@@ -79,7 +86,7 @@ class ThermalZone:
|
||||
class PyFan:
|
||||
def __init__(self, config="/etc/pyfan") -> None:
|
||||
self.config = self.__load_config(config)
|
||||
logging.basicConfig(level=logging.getLevelName(self.config["loglevel"]), style='{')
|
||||
logging.basicConfig(level=logging.getLevelName(self.config["loglevel"]))
|
||||
self.zones = []
|
||||
|
||||
for zone in self.config["thermalzones"]:
|
||||
@@ -88,7 +95,7 @@ class PyFan:
|
||||
zone["target"],
|
||||
zone["factor"], zone["name"]))
|
||||
|
||||
logging.info("Finished creating %d thermal zones." % len(self.zones))
|
||||
logging.getLogger("pyfan").info("Finished creating %d thermal zones." % len(self.zones))
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
Reference in New Issue
Block a user