From c56c6561937b3d97ef48cdf4283296bacf20c976 Mon Sep 17 00:00:00 2001 From: Vadym Stupakov Date: Wed, 5 Aug 2020 22:31:10 +0300 Subject: [PATCH] switching to psutil in order to monitor charger state (#68) * switching to psutil in order to monitor charger state --- requirements.txt | 1 - source/core.py | 49 ++++++++++++++++++++++++++---------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/requirements.txt b/requirements.txt index 5984983..5da3718 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ psutil click distro -power \ No newline at end of file diff --git a/source/core.py b/source/core.py index ba47504..6210b79 100644 --- a/source/core.py +++ b/source/core.py @@ -10,7 +10,6 @@ import sys import time from pathlib import Path -import power as pw import psutil as p # ToDo: @@ -54,6 +53,19 @@ def turbo(value: bool = None): return value +def charging(): + """ + get charge state: is battery charging or discharging + """ + bat_info = p.sensors_battery() + if bat_info is None: + state = True + else: + state = bat_info.power_plugged + + return state + + def get_avail_gov(): f = Path("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors") return f.read_text().strip().split(" ") @@ -79,10 +91,6 @@ def get_current_gov(): return s.getoutput("cpufreqctl --governor").strip().split(" ")[0] -def get_bat_state(): - return pw.PowerManagement().get_providing_power_source_type() - - # auto-cpufreq log file auto_cpufreq_log_file = Path("/var/log/auto-cpufreq.log") auto_cpufreq_log_file_snap = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.log") @@ -341,46 +349,43 @@ def mon_performance(): footer() -# set cpufreq based if device is charging def set_autofreq(): + """ + set cpufreq governor based if device is charging + """ print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") - # get battery state - bat_state = pw.PowerManagement().get_providing_power_source_type() - # determine which governor should be used - if bat_state == pw.POWER_TYPE_AC: + if charging(): print("Battery is: charging") set_performance() - elif bat_state == pw.POWER_TYPE_BATTERY: + else: print("Battery is: discharging") set_powersave() - else: - print("Couldn't determine the battery status. Please report this issue.") -# make cpufreq suggestions def mon_autofreq(): + """ + make cpufreq suggestions + :return: + """ print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") - # get battery state - bat_state = pw.PowerManagement().get_providing_power_source_type() - # determine which governor should be used - if bat_state == pw.POWER_TYPE_AC: + if charging(): print("Battery is: charging") print(f"Suggesting use of \"{get_avail_performance()}\" governor\nCurrently using:", get_current_gov()) mon_performance() - elif bat_state == pw.POWER_TYPE_BATTERY: + else: print("Battery is: discharging") print(f"Suggesting use of \"{get_avail_powersave()}\" governor\nCurrently using:", get_current_gov()) mon_powersave() - else: - print("Couldn't determine the battery status. Please report this issue.") -# get system information def sysinfo(): + """ + get system information + """ # added as a temp fix for issue: https://github.com/giampaolo/psutil/issues/1650 import warnings warnings.filterwarnings("ignore")