From 7c3c93f59215b4ea9959505729f2ab1a7394a67b Mon Sep 17 00:00:00 2001 From: Keir Rice Date: Sat, 24 Sep 2022 22:59:42 +1200 Subject: [PATCH] Having trouble getting the script to run in the enclosure framework. Trying a differant aproach to popen. --- octoprint_enclosure/__init__.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 1799ec4..b4f02ec 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1143,14 +1143,23 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP def read_shtc3_temp(self, address, i2cbus): try: script = os.path.dirname(os.path.realpath(__file__)) + "/SHTC3.py" - args = ["python", script, str(i2cbus), str(address)] + cmd = [sys.executable, script, str(i2cbus), str(address)] + if self._settings.get(["use_sudo"]): + cmd.insert(0, "sudo") + if self._settings.get(["debug_temperature_log"]) is True: - self._logger.debug("Temperature SHTC3 cmd: %s", " ".join(args)) - proc = Popen(args, stdout=PIPE) - stdout, _ = proc.communicate() - if self._settings.get(["debug_temperature_log"]) is True: - self._logger.debug("SHTC3 result: %s", stdout) - temp, hum = stdout.decode("utf-8").split("|") + self._logger.debug("Temperature SHTC3 cmd: %s", " ".join(cmd)) + + stdout = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True) + output, errors = stdout.communicate() + + if self._settings.get(["debug_temperature_log"]) is True: + if len(errors) > 0: + self._logger.error("SHTC3 error: %s", errors) + else: + self._logger.debug("SHTC3 result: %s", output) + + temp, hum = output.split("|") return self.to_float(temp.strip()), self.to_float(hum.strip()) except Exception as ex: