power: make the module more modular
Signed-off-by: Pawel Zubrycki <paw.zubr@gmail.com>
This commit is contained in:
parent
0fada7ffc2
commit
a0e23eb22a
|
@ -34,15 +34,7 @@ class PrinterPower:
|
||||||
logging.info("Power plugin loading devices: " + str(dev_names))
|
logging.info("Power plugin loading devices: " + str(dev_names))
|
||||||
devices = {}
|
devices = {}
|
||||||
for dev in dev_names:
|
for dev in dev_names:
|
||||||
pin = config.getint(dev + "_pin")
|
devices[dev] = GpioDevice(dev, config)
|
||||||
name = config.get(dev + "_name", dev)
|
|
||||||
active_low = config.getboolean(dev + "_active_low", False)
|
|
||||||
devices[dev] = {
|
|
||||||
"name": name,
|
|
||||||
"pin": pin,
|
|
||||||
"active_low": int(active_low),
|
|
||||||
"status": None
|
|
||||||
}
|
|
||||||
ioloop = IOLoop.current()
|
ioloop = IOLoop.current()
|
||||||
ioloop.spawn_callback(self.initialize_devices, devices)
|
ioloop.spawn_callback(self.initialize_devices, devices)
|
||||||
|
|
||||||
|
@ -50,7 +42,7 @@ class PrinterPower:
|
||||||
output = {"devices": []}
|
output = {"devices": []}
|
||||||
for dev in self.devices:
|
for dev in self.devices:
|
||||||
output['devices'].append({
|
output['devices'].append({
|
||||||
"name": self.devices[dev]["name"],
|
"name": self.devices[dev].name,
|
||||||
"id": dev
|
"id": dev
|
||||||
})
|
})
|
||||||
return output
|
return output
|
||||||
|
@ -65,25 +57,21 @@ class PrinterPower:
|
||||||
result = {}
|
result = {}
|
||||||
req = path.split("/")[-1]
|
req = path.split("/")[-1]
|
||||||
for dev in args:
|
for dev in args:
|
||||||
if path.startswith("/machine/gpio_power/"):
|
if req not in ("on", "off", "status"):
|
||||||
res = await self._power_dev(dev, req)
|
|
||||||
if res:
|
|
||||||
result[dev] = self.devices[dev]["status"]
|
|
||||||
else:
|
|
||||||
result[dev] = "device_not_found"
|
|
||||||
else:
|
|
||||||
raise self.server.error("Unsupported power request")
|
raise self.server.error("Unsupported power request")
|
||||||
|
if (await self._power_dev(dev, req)):
|
||||||
|
result[dev] = self.devices[dev].status
|
||||||
|
else:
|
||||||
|
result[dev] = "device_not_found"
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def _power_dev(self, dev, req):
|
async def _power_dev(self, dev, req):
|
||||||
if dev not in self.devices:
|
if dev not in self.devices:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
await GPIO.verify_pin(self.devices[dev]["pin"],
|
|
||||||
self.devices[dev]["active_low"])
|
|
||||||
if req in ["on", "off"]:
|
if req in ["on", "off"]:
|
||||||
val = 1 if req == "on" else 0
|
await self.devices[dev].power(req)
|
||||||
GPIO.set_pin_value(self.devices[dev]["pin"], val)
|
|
||||||
self.server.send_event("gpio_power:power_changed", {
|
self.server.send_event("gpio_power:power_changed", {
|
||||||
"device": dev,
|
"device": dev,
|
||||||
"status": req
|
"status": req
|
||||||
|
@ -91,17 +79,13 @@ class PrinterPower:
|
||||||
elif req != "status":
|
elif req != "status":
|
||||||
raise self.server.error("Unsupported power request")
|
raise self.server.error("Unsupported power request")
|
||||||
|
|
||||||
self.devices[dev]["status"] = GPIO.is_pin_on(
|
await self.devices[dev].refresh_status()
|
||||||
self.devices[dev]["pin"])
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
async def initialize_devices(self, devices):
|
async def initialize_devices(self, devices):
|
||||||
for name, device in devices.items():
|
for name, device in devices.items():
|
||||||
try:
|
try:
|
||||||
logging.debug(
|
await device.initialize()
|
||||||
f"Attempting to configure pin GPIO{device['pin']}")
|
|
||||||
await GPIO.setup_pin(device["pin"], device["active_low"])
|
|
||||||
device["status"] = GPIO.is_pin_on(device["pin"])
|
|
||||||
except Exception:
|
except Exception:
|
||||||
logging.exception(
|
logging.exception(
|
||||||
f"Power plugin: ERR Problem configuring the output pin for"
|
f"Power plugin: ERR Problem configuring the output pin for"
|
||||||
|
@ -123,6 +107,11 @@ class PrinterPower:
|
||||||
ioloop = IOLoop.current()
|
ioloop = IOLoop.current()
|
||||||
ioloop.spawn_callback(self._power_dev, device, status)
|
ioloop.spawn_callback(self._power_dev, device, status)
|
||||||
|
|
||||||
|
async def add_device(self, dev, device):
|
||||||
|
await device.initialize()
|
||||||
|
self.devices[dev] = device
|
||||||
|
|
||||||
|
|
||||||
class GPIO:
|
class GPIO:
|
||||||
gpio_root = "/sys/class/gpio"
|
gpio_root = "/sys/class/gpio"
|
||||||
|
|
||||||
|
@ -187,7 +176,6 @@ class GPIO:
|
||||||
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")
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_pin_on(pin):
|
def is_pin_on(pin):
|
||||||
return "on" if int(GPIO._get_gpio_option(pin, "value")) else "off"
|
return "on" if int(GPIO._get_gpio_option(pin, "value")) else "off"
|
||||||
|
@ -198,5 +186,25 @@ class GPIO:
|
||||||
GPIO._set_gpio_option(pin, "value", value)
|
GPIO._set_gpio_option(pin, "value", value)
|
||||||
|
|
||||||
|
|
||||||
|
class GpioDevice:
|
||||||
|
def __init__(self, dev, config):
|
||||||
|
self.name = config.get(dev + "_name", dev)
|
||||||
|
self.status = None
|
||||||
|
self.pin = config.getint(dev + "_pin")
|
||||||
|
self.active_low = int(config.getboolean(dev + "_active_low", False))
|
||||||
|
|
||||||
|
async def initialize(self):
|
||||||
|
logging.debug(f"Attempting to configure pin GPIO{self.pin}")
|
||||||
|
await GPIO.setup_pin(self.pin, self.active_low)
|
||||||
|
await self.refresh_status()
|
||||||
|
|
||||||
|
async def refresh_status(self):
|
||||||
|
self.status = GPIO.is_pin_on(self.pin)
|
||||||
|
|
||||||
|
async def power(self, status):
|
||||||
|
await GPIO.verify_pin(self.pin, self.active_low)
|
||||||
|
GPIO.set_pin_value(self.pin, int(status == "on"))
|
||||||
|
|
||||||
|
|
||||||
def load_plugin(config):
|
def load_plugin(config):
|
||||||
return PrinterPower(config)
|
return PrinterPower(config)
|
||||||
|
|
Loading…
Reference in New Issue