power: restrict lines to 80 chars, remove trailing whitespace

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-07-29 17:46:27 -04:00
parent 31bebdd59c
commit 92d7d7c2b7
2 changed files with 34 additions and 19 deletions

View File

@ -215,7 +215,7 @@ gcode:
### Power Control Plugin ### Power Control Plugin
``` ```
[moonraker_plugin power] [moonraker_plugin power]
devices: printer, led devices: printer, led
# A comma separated list of devices you wish to control. Do not use spaces in # A comma separated list of devices you wish to control. Do not use spaces in
# the device's name here # the device's name here
#{dev}_name: Friendly Name #{dev}_name: Friendly Name

View File

@ -49,7 +49,8 @@ class PrinterPower:
result[dev] = "device_not_found" result[dev] = "device_not_found"
continue 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": if path == "/printer/power/on":
GPIO.set_pin_value(self.devices[dev]["pin"], 1) GPIO.set_pin_value(self.devices[dev]["pin"], 1)
elif path == "/printer/power/off": elif path == "/printer/power/off":
@ -57,7 +58,8 @@ class PrinterPower:
elif path != "/printer/power/status": elif path != "/printer/power/status":
raise self.server.error("Unsupported power request") 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"] result[dev] = self.devices[dev]["status"]
return result return result
@ -72,22 +74,33 @@ class PrinterPower:
for dev in devices: for dev in devices:
dev = dev.strip() dev = dev.strip()
if dev + "_pin" not in config.keys(): 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 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] = { self.devices[dev] = {
"name": config[dev + "_name"] if dev + "_name" in config.keys() else dev, "name": name,
"pin": int(config[dev + "_pin"]), "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 "status": None
} }
try: try:
logging.debug("Attempting to configure pin GPIO" + str(self.devices[dev]["pin"])) logging.debug(
GPIO.setup_pin(self.devices[dev]["pin"], self.devices[dev]["active_low"]) "Attempting to configure pin GPIO%d"
self.devices[dev]["status"] = GPIO.is_pin_on(self.devices[dev]["pin"]) % (self.devices[dev]["pin"]))
except: GPIO.setup_pin(
logging.info("Power plugin: ERR Problem configuring the output pin for device " + dev + ". Removing device") 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) self.devices.pop(dev, None)
continue continue
@ -97,13 +110,14 @@ class GPIO:
@staticmethod @staticmethod
def _set_gpio_option(gpio, option, value): def _set_gpio_option(gpio, option, value):
GPIO._write( GPIO._write(
os.path.join(GPIO.gpio_root, "gpio"+str(gpio), option), os.path.join(GPIO.gpio_root, "gpio%d" % (gpio), option),
value value
) )
@staticmethod
def _get_gpio_option(pin, option): def _get_gpio_option(pin, option):
return GPIO._read( return GPIO._read(
os.path.join(GPIO.gpio_root, "gpio"+str(pin), option) os.path.join(GPIO.gpio_root, "gpio%d" % (pin), option)
) )
@staticmethod @staticmethod
@ -120,9 +134,9 @@ class GPIO:
@staticmethod @staticmethod
def verify_pin(pin, active_low=1): 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): 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) GPIO.setup_pin(pin, active_low)
return return
@ -137,18 +151,19 @@ class GPIO:
pin = int(pin) pin = int(pin)
active_low = 1 if active_low == 1 else 0 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): if not os.path.exists(gpiopath):
GPIO._write( GPIO._write(
os.path.join(GPIO.gpio_root, "export"), os.path.join(GPIO.gpio_root, "export"),
pin) pin)
logging.info("Waiting for GPIO"+str(pin)+" to initialize") logging.info("Waiting for GPIO%d to initialize" % (pin))
while os.stat(os.path.join(GPIO.gpio_root, "gpio"+str(pin),"active_low")).st_gid == 0: while os.stat(os.path.join(
GPIO.gpio_root, "gpio%dactive_low") % (pin)).st_gid == 0:
time.sleep(.1) time.sleep(.1)
if GPIO._get_gpio_option(pin, "active_low").strip() != str(active_low): if GPIO._get_gpio_option(pin, "active_low").strip() != str(active_low):
GPIO._set_gpio_option(pin, "active_low", active_low) GPIO._set_gpio_option(pin, "active_low", active_low)
if GPIO._get_gpio_option(pin, "direction").strip() != "out": if GPIO._get_gpio_option(pin, "direction").strip() != "out":
GPIO._set_gpio_option(pin, "direction", "out") GPIO._set_gpio_option(pin, "direction", "out")