From 92d7d7c2b7e3c716d33b03d4be55fb1721d7805d Mon Sep 17 00:00:00 2001 From: Arksine Date: Wed, 29 Jul 2020 17:46:27 -0400 Subject: [PATCH] power: restrict lines to 80 chars, remove trailing whitespace Signed-off-by: Eric Callahan --- docs/installation.md | 2 +- moonraker/plugins/power.py | 51 ++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/docs/installation.md b/docs/installation.md index fa97a04..e2ac244 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -215,7 +215,7 @@ gcode: ### Power Control Plugin ``` [moonraker_plugin power] -devices: printer, led +devices: printer, led # A comma separated list of devices you wish to control. Do not use spaces in # the device's name here #{dev}_name: Friendly Name diff --git a/moonraker/plugins/power.py b/moonraker/plugins/power.py index d8d9700..5729b37 100644 --- a/moonraker/plugins/power.py +++ b/moonraker/plugins/power.py @@ -49,7 +49,8 @@ class PrinterPower: result[dev] = "device_not_found" continue - GPIO.verify_pin(self.devices[dev]["pin"], self.devices[dev]["active_low"]) + GPIO.verify_pin(self.devices[dev]["pin"], + self.devices[dev]["active_low"]) if path == "/printer/power/on": GPIO.set_pin_value(self.devices[dev]["pin"], 1) elif path == "/printer/power/off": @@ -57,7 +58,8 @@ class PrinterPower: elif path != "/printer/power/status": raise self.server.error("Unsupported power request") - self.devices[dev]["status"] = GPIO.is_pin_on(self.devices[dev]["pin"]) + self.devices[dev]["status"] = GPIO.is_pin_on( + self.devices[dev]["pin"]) result[dev] = self.devices[dev]["status"] return result @@ -72,22 +74,33 @@ class PrinterPower: for dev in devices: dev = dev.strip() if dev + "_pin" not in config.keys(): - logging.info("Power plugin: ERR " + dev + " does not have a pin defined") + logging.info( + "Power plugin: ERR " + dev + " does not have a pin defined") continue + name = config[dev + "_name"] if dev + "_name" in config.keys() \ + else dev + active_low = 1 if dev+"_active_low" in config.keys() and \ + config[dev+"_active_low"] == "True" else 0 self.devices[dev] = { - "name": config[dev + "_name"] if dev + "_name" in config.keys() else dev, + "name": name, "pin": int(config[dev + "_pin"]), - "active_low": 1 if dev+"_active_low" in config.keys() and config[dev+"_active_low"] == "True" else 0, + "active_low": active_low, "status": None } try: - logging.debug("Attempting to configure pin GPIO" + str(self.devices[dev]["pin"])) - GPIO.setup_pin(self.devices[dev]["pin"], self.devices[dev]["active_low"]) - self.devices[dev]["status"] = GPIO.is_pin_on(self.devices[dev]["pin"]) - except: - logging.info("Power plugin: ERR Problem configuring the output pin for device " + dev + ". Removing device") + logging.debug( + "Attempting to configure pin GPIO%d" + % (self.devices[dev]["pin"])) + GPIO.setup_pin( + self.devices[dev]["pin"], self.devices[dev]["active_low"]) + self.devices[dev]["status"] = GPIO.is_pin_on( + self.devices[dev]["pin"]) + except Exception: + logging.info( + "Power plugin: ERR Problem configuring the output pin for" + " device %s. Removing device" % (dev)) self.devices.pop(dev, None) continue @@ -97,13 +110,14 @@ class GPIO: @staticmethod def _set_gpio_option(gpio, option, value): GPIO._write( - os.path.join(GPIO.gpio_root, "gpio"+str(gpio), option), + os.path.join(GPIO.gpio_root, "gpio%d" % (gpio), option), value ) + @staticmethod def _get_gpio_option(pin, option): return GPIO._read( - os.path.join(GPIO.gpio_root, "gpio"+str(pin), option) + os.path.join(GPIO.gpio_root, "gpio%d" % (pin), option) ) @staticmethod @@ -120,9 +134,9 @@ class GPIO: @staticmethod def verify_pin(pin, active_low=1): - gpiopath = os.path.join(GPIO.gpio_root, "gpio"+str(pin)) + gpiopath = os.path.join(GPIO.gpio_root, "gpio%d" % (pin)) if not os.path.exists(gpiopath): - logging.info("Re-intializing GPIO"+str(pin)) + logging.info("Re-intializing GPIO%d" % (pin)) GPIO.setup_pin(pin, active_low) return @@ -137,18 +151,19 @@ class GPIO: pin = int(pin) active_low = 1 if active_low == 1 else 0 - gpiopath = os.path.join(GPIO.gpio_root, "gpio"+str(pin)) + gpiopath = os.path.join(GPIO.gpio_root, "gpio%d" % (pin)) if not os.path.exists(gpiopath): GPIO._write( os.path.join(GPIO.gpio_root, "export"), pin) - logging.info("Waiting for GPIO"+str(pin)+" to initialize") - while os.stat(os.path.join(GPIO.gpio_root, "gpio"+str(pin),"active_low")).st_gid == 0: + logging.info("Waiting for GPIO%d to initialize" % (pin)) + while os.stat(os.path.join( + GPIO.gpio_root, "gpio%dactive_low") % (pin)).st_gid == 0: time.sleep(.1) if GPIO._get_gpio_option(pin, "active_low").strip() != str(active_low): GPIO._set_gpio_option(pin, "active_low", active_low) - + if GPIO._get_gpio_option(pin, "direction").strip() != "out": GPIO._set_gpio_option(pin, "direction", "out")