-
-
Notifications
You must be signed in to change notification settings - Fork 336
fix: start/stop thresholds not being set because of initial values #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CasperVM
wants to merge
6
commits into
AdnanHodzic:master
Choose a base branch
from
CasperVM:battery-write-invalid-argument
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f4b9c3
fix: (WIP) start/stop thresholds not being set because of initial values
CasperVM 6648502
refactor: rewrite battery scripts more cleanly + fix bug where start/…
CasperVM 7a12d9c
fix: battery start/stop pr: small typos, pr comments..
CasperVM fe1dd2d
fix: actually validate start/stop, and enforce both to be present
CasperVM 58670c3
fix: start/stop threshold value check + add more docs + add override …
CasperVM 8edcc3c
add: debug prints for testers
CasperVM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,35 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| from subprocess import check_output | ||
|
|
||
| from auto_cpufreq.battery_scripts.shared import BatteryDevice | ||
| from auto_cpufreq.config.config import config | ||
| from auto_cpufreq.globals import POWER_SUPPLY_DIR | ||
|
|
||
| def set_battery(value, mode, bat): | ||
| path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold" | ||
| fallback_mode = "start" if mode == "start" else "end" | ||
| fallback_path = f"{POWER_SUPPLY_DIR}{bat}/charge_control_{fallback_mode}_threshold" | ||
| if os.path.isfile(path): check_output(f"echo {value} | tee {path}", shell=True, text=True) | ||
| elif os.path.isfile(fallback_path): check_output(f"echo {value} | tee {fallback_path}", shell=True, text=True) | ||
| else: print(f"WARNING: {path} does NOT exist") | ||
|
|
||
| def get_threshold_value(mode): | ||
| conf = config.get_config() | ||
| return conf["battery"][f"{mode}_threshold"] if conf.has_option("battery", f"{mode}_threshold") else (0 if mode == "start" else 100) | ||
|
|
||
| def asus_setup(): | ||
| conf = config.get_config() | ||
|
|
||
| if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return | ||
|
|
||
| if os.path.exists(POWER_SUPPLY_DIR): | ||
| batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')] | ||
|
|
||
| for bat in batteries: | ||
| set_battery(get_threshold_value("start"), "start", bat) | ||
| set_battery(get_threshold_value("stop"), "stop", bat) | ||
| else: print(f"WARNING {POWER_SUPPLY_DIR} does NOT esixt") | ||
|
|
||
|
|
||
| def asus_print_thresholds(): | ||
| batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')] | ||
| print("\n-------------------------------- Battery Info ---------------------------------\n") | ||
| print(f"battery count = {len(batteries)}") | ||
| for bat in batteries: | ||
| try: | ||
| primary_start = f"{POWER_SUPPLY_DIR}{bat}/charge_start_threshold" | ||
| primary_stop = f"{POWER_SUPPLY_DIR}{bat}/charge_stop_threshold" | ||
| fallback_start = f"{POWER_SUPPLY_DIR}{bat}/charge_control_start_threshold" | ||
| fallback_stop = f"{POWER_SUPPLY_DIR}{bat}/charge_control_end_threshold" | ||
| if os.path.isfile(primary_start): print(bat, "start threshold =", check_output(["cat", primary_start])) | ||
| elif os.path.isfile(fallback_start): print(bat, "start threshold =", check_output(["cat", fallback_start])) | ||
| else: print(f"{bat} start threshold: file not found") | ||
| if os.path.isfile(primary_stop): print(bat, "stop threshold =", check_output(["cat", primary_stop])) | ||
| elif os.path.isfile(fallback_stop): print(bat, "stop threshold =", check_output(["cat", fallback_stop])) | ||
| else: print(f"{bat} stop threshold: file not found") | ||
| except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e)) | ||
| class AsusBatteryDevice(BatteryDevice): | ||
| def __init__(self): | ||
| self.config = config.get_config() | ||
| self.batteries = [ | ||
| name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT") | ||
| ] | ||
| self.start_paths = { | ||
| bat: [ | ||
| os.path.join(POWER_SUPPLY_DIR, bat, "charge_start_threshold"), | ||
| os.path.join( | ||
| POWER_SUPPLY_DIR, bat, "charge_control_start_threshold" | ||
| ), # Fallback | ||
| ] | ||
| for bat in self.batteries | ||
| } | ||
| self.stop_paths = { | ||
| bat: [ | ||
| os.path.join(POWER_SUPPLY_DIR, bat, "charge_stop_threshold"), | ||
| os.path.join( | ||
| POWER_SUPPLY_DIR, bat, "charge_control_end_threshold" | ||
| ), # Fallback | ||
| ] | ||
| for bat in self.batteries | ||
| } | ||
|
|
||
| self.start_config_value = self.get_threshold_config_value("start") | ||
| self.stop_config_value = self.get_threshold_config_value("stop") | ||
CasperVM marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,38 @@ | ||
| #!/usr/bin/env python3 | ||
| from subprocess import PIPE, run | ||
|
|
||
| from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_print_thresholds, ideapad_acpi_setup | ||
| from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_print_thresholds, ideapad_laptop_setup | ||
| from auto_cpufreq.battery_scripts.thinkpad import thinkpad_print_thresholds, thinkpad_setup | ||
| from auto_cpufreq.battery_scripts.asus import asus_print_thresholds, asus_setup | ||
| from auto_cpufreq.battery_scripts.asus import AsusBatteryDevice | ||
| from auto_cpufreq.battery_scripts.ideapad_laptop import IdeapadBatteryDevice | ||
| from auto_cpufreq.battery_scripts.shared import BatteryDevice | ||
|
|
||
|
|
||
| def lsmod(module): | ||
| return ( | ||
| module in run(["lsmod"], stdout=PIPE, stderr=PIPE, text=True, shell=True).stdout | ||
| ) | ||
|
|
||
| def lsmod(module): return module in run(['lsmod'], stdout=PIPE, stderr=PIPE, text=True, shell=True).stdout | ||
|
|
||
| def battery_get_thresholds(): | ||
| if lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds() | ||
| elif lsmod("ideapad_laptop"): ideapad_laptop_print_thresholds() | ||
| elif lsmod("thinkpad_acpi"): thinkpad_print_thresholds() | ||
| elif lsmod("asus_wmi"): asus_print_thresholds() | ||
| else: return | ||
| if lsmod("ideapad_acpi"): | ||
| BatteryDevice().print_thresholds() | ||
| elif lsmod("ideapad_laptop"): | ||
| IdeapadBatteryDevice().print_thresholds() | ||
| elif lsmod("thinkpad_acpi"): | ||
| BatteryDevice().print_thresholds() | ||
| elif lsmod("asus_wmi"): | ||
| AsusBatteryDevice.print_thresholds() | ||
CasperVM marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| return | ||
|
|
||
|
|
||
| def battery_setup(): | ||
| if lsmod("ideapad_acpi"): ideapad_acpi_setup() | ||
| elif lsmod("ideapad_laptop"): ideapad_laptop_setup() | ||
| elif lsmod("thinkpad_acpi"): thinkpad_setup() | ||
| elif lsmod("asus_wmi"): asus_setup() | ||
| else: return | ||
| if lsmod("ideapad_acpi"): | ||
| BatteryDevice().setup() | ||
| elif lsmod("ideapad_laptop"): | ||
| IdeapadBatteryDevice().setup() | ||
| elif lsmod("thinkpad_acpi"): | ||
| BatteryDevice().setup() | ||
| elif lsmod("asus_wmi"): | ||
| AsusBatteryDevice.setup() | ||
CasperVM marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| return | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,69 +1,75 @@ | ||
| #!/usr/bin/env python3 | ||
| import os | ||
| from subprocess import check_output | ||
| from subprocess import CalledProcessError, check_output | ||
| import time | ||
|
|
||
| from auto_cpufreq.config.config import config | ||
| from auto_cpufreq.battery_scripts.shared import BatteryDevice | ||
| from auto_cpufreq.globals import CONSERVATION_MODE_FILE, POWER_SUPPLY_DIR | ||
|
|
||
| def set_battery(value, mode, bat): | ||
| path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold" | ||
| if os.path.exists(path): | ||
| check_output(f"echo {value} | tee {POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold", shell=True, text=True) | ||
| else: print(f"WARNING: {path} does NOT exist") | ||
|
|
||
| def get_threshold_value(mode): | ||
| conf = config.get_config() | ||
| return conf["battery"][f"{mode}_threshold"] if conf.has_option("battery", f"{mode}_threshold") else (0 if mode == "start" else 100) | ||
|
|
||
| def conservation_mode(value): | ||
| try: | ||
| check_output(f"echo {value} | tee {CONSERVATION_MODE_FILE}", shell=True, text=True) | ||
| print(f"conservation_mode is {value}") | ||
| except: print("unable to set conservation mode") | ||
| return | ||
|
|
||
| def check_conservation_mode(): | ||
| try: | ||
| value = check_output(["cat", CONSERVATION_MODE_FILE], text=True).rstrip() | ||
| if value == "1": return True | ||
| elif value == "0": return False | ||
| else: | ||
| print("could not get value from conservation mode") | ||
| return None | ||
| except: | ||
| print("could not get the value from conservation mode") | ||
| return False | ||
| class IdeapadBatteryDevice(BatteryDevice): | ||
| def check_conservation_mode(self): | ||
| try: | ||
| value = check_output(["cat", CONSERVATION_MODE_FILE], text=True).rstrip() | ||
| if value == "1": | ||
| return True | ||
| elif value == "0": | ||
| return False | ||
| else: | ||
| print( | ||
| f"WARNING: could not get value from conservation mode, unexpected value!: {value}" | ||
| ) | ||
| return None | ||
| except CalledProcessError as e: | ||
| print(f"WARNING: could not get value from conservation mode: {e.output}") | ||
| return False | ||
|
|
||
| def ideapad_laptop_setup(): | ||
| conf = config.get_config() | ||
| def set_conservation_mode(value): | ||
CasperVM marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try: | ||
| check_output( | ||
| f"echo {value} | tee {CONSERVATION_MODE_FILE}", shell=True, text=True | ||
CasperVM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
| print(f"conservation_mode is {value}") | ||
| except CalledProcessError as e: | ||
| print(f"WARNING: unable to set conservation mode: {e.output}") | ||
| return | ||
|
|
||
| if not (conf.has_option("battery", "enable_thresholds") and conf["battery"]["enable_thresholds"] == "true"): return | ||
| def setup(self): | ||
| if not ( | ||
| self.config.has_option("battery", "enable_thresholds") | ||
| and self.config["battery"]["enable_thresholds"] == "true" | ||
| ): | ||
| return | ||
|
|
||
| batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")] | ||
| if self.config.has_option("battery", "ideapad_laptop_conservation_mode"): | ||
| if self.config["battery"]["ideapad_laptop_conservation_mode"] == "true": | ||
| self.conservation_mode(1) | ||
| return | ||
| if self.config["battery"]["ideapad_laptop_conservation_mode"] == "false": | ||
| self.conservation_mode(0) | ||
|
|
||
| if conf.has_option("battery", "ideapad_laptop_conservation_mode"): | ||
| if conf["battery"]["ideapad_laptop_conservation_mode"] == "true": | ||
| conservation_mode(1) | ||
| if self.check_conservation_mode(): | ||
| print("WARNING: conservation mode is enabled unable to set thresholds") | ||
| return | ||
| if conf["battery"]["ideapad_laptop_conservation_mode"] == "false": conservation_mode(0) | ||
|
|
||
| if not check_conservation_mode(): | ||
| for bat in batteries: | ||
| set_battery(get_threshold_value("start"), "start", bat) | ||
| set_battery(get_threshold_value("stop"), "stop", bat) | ||
| else: print("conservation mode is enabled unable to set thresholds") | ||
| if not os.path.exists(POWER_SUPPLY_DIR): | ||
| print(f"WARNING: {POWER_SUPPLY_DIR} does NOT exist") | ||
| return | ||
|
|
||
| def ideapad_laptop_print_thresholds(): | ||
| if check_conservation_mode(): | ||
| print("conservation mode is on") | ||
| return | ||
| batteries = [ | ||
| name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT") | ||
| ] | ||
|
|
||
| batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith("BAT")] | ||
| for bat in batteries: | ||
| # First set 0 for start and 100 for stop, sometimes we can have conflicting values otherwise which will result in an 'invalid argument' upon writing. | ||
| self.set_battery(bat, "start", 0) | ||
| self.set_battery(bat, "stop", 100) | ||
| time.sleep(0.1) | ||
| self.set_battery(bat, "start", self.start_config_value) | ||
| self.set_battery(bat, "stop", self.stop_config_value) | ||
|
|
||
| print("\n-------------------------------- Battery Info ---------------------------------\n") | ||
| print(f"battery count = {len(batteries)}") | ||
| for bat in batteries: | ||
| try: | ||
| print(bat, "start threshold =", check_output(["cat", POWER_SUPPLY_DIR+bat+"/charge_start_threshold"])) | ||
| print(bat, "stop threshold =", check_output(["cat", POWER_SUPPLY_DIR+bat+"/charge_stop_threshold"])) | ||
| except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e)) | ||
| def print_thresholds(self): | ||
| if self.check_conservation_mode(): | ||
| print("WARNING: conservation mode is on") | ||
| return | ||
| super().print_thresholds() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.