From 9e0e4bf7b60581402e17a624de724995642bb6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 19 Jul 2018 12:53:34 +0200 Subject: [PATCH] Pi Support: Allow configuration of vcgencmd --- src/octoprint/plugins/pi_support/__init__.py | 38 +++++++++++++------ .../templates/pi_support_settings.jinja2 | 21 +++++++++- tests/plugins/pi_support/__init__.py | 0 tests/plugins/pi_support/fakes/fake_model.txt | 1 + .../plugins/pi_support/fakes/fake_octopi.txt | 1 + .../plugins/pi_support/fakes/fake_vcgencmd.py | 2 + .../{ => pi_support}/test_pi_support.py | 0 7 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 tests/plugins/pi_support/__init__.py create mode 100644 tests/plugins/pi_support/fakes/fake_model.txt create mode 100644 tests/plugins/pi_support/fakes/fake_octopi.txt create mode 100644 tests/plugins/pi_support/fakes/fake_vcgencmd.py rename tests/plugins/{ => pi_support}/test_pi_support.py (100%) diff --git a/src/octoprint/plugins/pi_support/__init__.py b/src/octoprint/plugins/pi_support/__init__.py index 99af47dc8..e0bd9ac10 100644 --- a/src/octoprint/plugins/pi_support/__init__.py +++ b/src/octoprint/plugins/pi_support/__init__.py @@ -15,14 +15,18 @@ import octoprint.plugin _PROC_DT_MODEL_PATH = "/proc/device-tree/model" _OCTOPI_VERSION_PATH = "/etc/octopi_version" -_VCGENCMD = "/usr/bin/vcgencmd" +_VCGENCMD_THROTTLE = "/usr/bin/vcgencmd get_throttled" ### uncomment for local debugging +#import sys +#base = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "tests", "plugins", "pi_support", "fakes")) +#_PROC_DT_MODEL_PATH = os.path.join(base, "fake_model.txt") +#_OCTOPI_VERSION_PATH = os.path.join(base, "fake_octopi.txt") +#_VCGENCMD_THROTTLE = "{} {}".format(sys.executable, os.path.join(base, "fake_vcgencmd.py")) #import itertools -#_PROC_DT_MODEL_PATH = "fake_model.txt" -#_OCTOPI_VERSION_PATH = "fake_octopi.txt" #_VCGENCMD_OUTPUT = itertools.chain(iter(("0x0", "0x0", "0x50005", "0x50000", "0x70007")), itertools.repeat("0x70000")) + # see https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=147781&start=50#p972790 _FLAG_UNDERVOLTAGE = 1 << 0 _FLAG_FREQ_CAPPED = 1 << 1 @@ -126,11 +130,11 @@ def get_proc_dt_model(): return _proc_dt_model -def get_vcgencmd_throttled_state(): - output = sarge.get_stdout([_VCGENCMD, "get_throttled"]) +def get_vcgencmd_throttled_state(command): + output = sarge.get_stdout(command) #output = "throttled={}".format(next(_VCGENCMD_OUTPUT)) # for local debugging if not "throttled=0x" in output: - raise ValueError("cannot parse vcgencmd get_throttled output: {}".format(output)) + raise ValueError("cannot parse \"{}\" output: {}".format(command, output)) value = output[len("throttled="):].strip(" \t\r\n\0") value = int(value, 0) @@ -165,6 +169,7 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, self._throttle_check = None self._throttle_undervoltage = False self._throttle_overheat = False + self._throttle_functional = True #~~ EnvironmentDetectionPlugin @@ -210,13 +215,18 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, if self._settings.get_boolean(["vcgencmd_throttle_check_enabled"]): self._check_throttled_state() self._throttle_check = RepeatedTimer(self._check_throttled_state_interval, - self._check_throttled_state) + self._check_throttled_state, + condition=self._check_throttled_state_condition) self._throttle_check.start() #~~ SettingsPlugin def get_settings_defaults(self): - return dict(vcgencmd_throttle_check_enabled=True) + return dict(vcgencmd_throttle_check_enabled=True, + vcgencmd_throttle_check_command=_VCGENCMD_THROTTLE) + + def get_settings_restricted_paths(self): + return dict(admin=[["vcgencmd_throttle_check_enabled"], ["vcgencmd_throttle_check_command"]]) #~~ Helpers @@ -228,12 +238,18 @@ class PiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin, # check state every 5min if nothing's currently amiss return 300 + def _check_throttled_state_condition(self): + return self._throttle_functional + def _check_throttled_state(self): - self._logger.debug("Retrieving throttle state via {}".format(_VCGENCMD)) + command = self._settings.get(["vcgencmd_throttle_check_command"]) + + self._logger.debug("Retrieving throttle state via \"{}\"".format(command)) try: - state = get_vcgencmd_throttled_state() + state = get_vcgencmd_throttled_state(command) except: - self._logger.exception("Got an error while trying to fetch the current throttle state via {}".format(_VCGENCMD)) + self._logger.exception("Got an error while trying to fetch the current throttle state via \"{}\"".format(command)) + self._throttle_functional = False return if self._throttle_state == state: diff --git a/src/octoprint/plugins/pi_support/templates/pi_support_settings.jinja2 b/src/octoprint/plugins/pi_support/templates/pi_support_settings.jinja2 index e1d8ff135..83cd07fea 100644 --- a/src/octoprint/plugins/pi_support/templates/pi_support_settings.jinja2 +++ b/src/octoprint/plugins/pi_support/templates/pi_support_settings.jinja2 @@ -1,12 +1,31 @@

{{ _('Pi Support') }}

+

{{ _('Please restart OctoPrint after changing any of this plugin\'s settings.') }}

+
- {{ _('This will regularly check the output of vcgencmd get_throttled for indications by the Pi that something is amiss either with power regulation or CPU/GPU temperature. If you change this, you\'ll need to restart OctoPrint for the change to take effect.') }} + {{ _('This will regularly check with the Pi if something is amiss either with power regulation or CPU/GPU temperature.') }} +
+
+ +
+
{{ _('Advanced options') }}
+
+ +
+ +
+ + {{ _('Defaults to /usr/bin/vcgencmd get_throttled. You usually shouldn\'t have to change this.') }} +
+
+
+ + diff --git a/tests/plugins/pi_support/__init__.py b/tests/plugins/pi_support/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/plugins/pi_support/fakes/fake_model.txt b/tests/plugins/pi_support/fakes/fake_model.txt new file mode 100644 index 000000000..686e67ebc --- /dev/null +++ b/tests/plugins/pi_support/fakes/fake_model.txt @@ -0,0 +1 @@ +Raspberry Pi Model F Rev 1.1 diff --git a/tests/plugins/pi_support/fakes/fake_octopi.txt b/tests/plugins/pi_support/fakes/fake_octopi.txt new file mode 100644 index 000000000..e815b861f --- /dev/null +++ b/tests/plugins/pi_support/fakes/fake_octopi.txt @@ -0,0 +1 @@ +0.15.1 diff --git a/tests/plugins/pi_support/fakes/fake_vcgencmd.py b/tests/plugins/pi_support/fakes/fake_vcgencmd.py new file mode 100644 index 000000000..3dc7084cf --- /dev/null +++ b/tests/plugins/pi_support/fakes/fake_vcgencmd.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python2 +print("throttled=0x50005") diff --git a/tests/plugins/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py similarity index 100% rename from tests/plugins/test_pi_support.py rename to tests/plugins/pi_support/test_pi_support.py