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
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import sys
|
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from simple_pid import PID
|
from simple_pid import PID
|
||||||
|
|
||||||
@@ -14,11 +15,8 @@ def build_pwm_path(specific):
|
|||||||
|
|
||||||
|
|
||||||
def write_sysfs(path, value):
|
def write_sysfs(path, value):
|
||||||
try:
|
with open(build_pwm_path(path), 'w') as sysfs_f:
|
||||||
with open(build_pwm_path(path), 'w') as sysfs_f:
|
sysfs_f.write(str(value))
|
||||||
sysfs_f.write(str(value))
|
|
||||||
except OSError as err:
|
|
||||||
print("WARN:", err.strerror)
|
|
||||||
|
|
||||||
|
|
||||||
def read_sysfs(path):
|
def read_sysfs(path):
|
||||||
@@ -41,26 +39,35 @@ class ThermalZone:
|
|||||||
self.target = target
|
self.target = target
|
||||||
self.setup_pwm()
|
self.setup_pwm()
|
||||||
|
|
||||||
logging.info("[{zone}] Source={source} Fans={fans} Factor={factor} PID={pid}".format(zone=name,
|
logging.getLogger("pyfan").info(
|
||||||
source=temp_source,
|
"[{zone}] Source={source} Fans={fans} Factor={factor} PID={pid}".format(zone=name,
|
||||||
fans=fans,
|
source=temp_source,
|
||||||
factor=factor,
|
fans=fans,
|
||||||
pid=(
|
factor=factor,
|
||||||
p, i, d)))
|
pid=(
|
||||||
|
p, i, d)))
|
||||||
|
|
||||||
def eval(self):
|
def eval(self):
|
||||||
diff = self.target - self.get_temp()
|
diff = self.target - self.get_temp()
|
||||||
val = self.pid(diff)
|
val = self.pid(diff)
|
||||||
|
|
||||||
for target_fan in self.fans:
|
try:
|
||||||
if type(target_fan) is dict:
|
for target_fan in self.fans:
|
||||||
write_sysfs(list(target_fan.keys())[0], min(int(val), list(target_fan.values())[0]))
|
if type(target_fan) is dict:
|
||||||
else:
|
write_sysfs(list(target_fan.keys())[0], min(int(val), list(target_fan.values())[0]))
|
||||||
write_sysfs(target_fan, int(val))
|
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(
|
p, i, d = self.pid.components
|
||||||
"[{name}] {val}% ({diff}C/{temp}C)".format(name=self.name, val=int(val / 255 * 100), diff=diff,
|
|
||||||
temp=self.get_temp()))
|
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):
|
def get_temp(self):
|
||||||
return float(read_sysfs(self.temp_source)) * self.factor
|
return float(read_sysfs(self.temp_source)) * self.factor
|
||||||
@@ -79,7 +86,7 @@ class ThermalZone:
|
|||||||
class PyFan:
|
class PyFan:
|
||||||
def __init__(self, config="/etc/pyfan") -> None:
|
def __init__(self, config="/etc/pyfan") -> None:
|
||||||
self.config = self.__load_config(config)
|
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 = []
|
self.zones = []
|
||||||
|
|
||||||
for zone in self.config["thermalzones"]:
|
for zone in self.config["thermalzones"]:
|
||||||
@@ -88,7 +95,7 @@ class PyFan:
|
|||||||
zone["target"],
|
zone["target"],
|
||||||
zone["factor"], zone["name"]))
|
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):
|
def __enter__(self):
|
||||||
return self
|
return self
|
||||||
|
Reference in New Issue
Block a user