power: add websocket notification

Signed-off-by: Jordan Ruthe <jordanruthe@gmail.com>
This commit is contained in:
Jordan Ruthe 2020-10-19 22:22:39 -04:00 committed by Arksine
parent d582d76346
commit b63d192df7
2 changed files with 24 additions and 2 deletions

View File

@ -69,9 +69,9 @@ class PrinterPower:
await GPIO.verify_pin(self.devices[dev]["pin"],
self.devices[dev]["active_low"])
if path == "/machine/gpio_power/on":
GPIO.set_pin_value(self.devices[dev]["pin"], 1)
await self._power_dev(dev, "on")
elif path == "/machine/gpio_power/off":
GPIO.set_pin_value(self.devices[dev]["pin"], 0)
await self._power_dev(dev, "off")
elif path != "/machine/gpio_power/status":
raise self.server.error("Unsupported power request")
@ -81,6 +81,23 @@ class PrinterPower:
result[dev] = self.devices[dev]["status"]
return result
async def _power_dev(self, dev, status):
if dev not in self.devices:
return
if (status == "on"):
GPIO.set_pin_value(self.devices[dev]["pin"], 1)
self.server.send_event("gpio_power:power_changed", {
"device": dev,
"status": "on"
})
elif (status == "off"):
GPIO.set_pin_value(self.devices[dev]["pin"], 0)
self.server.send_event("gpio_power:power_changed", {
"device": dev,
"status": "off"
})
async def initialize_devices(self, devices):
for name, device in devices.items():
try:

View File

@ -117,6 +117,8 @@ class WebsocketManager:
"file_manager:filelist_changed", self._handle_filelist_changed)
self.server.register_event_handler(
"file_manager:metadata_update", self._handle_metadata_update)
self.server.register_event_handler(
"gpio_power:power_changed", self._handle_power_changed)
async def _handle_klippy_disconnect(self):
await self.notify_websockets("klippy_disconnected")
@ -133,6 +135,9 @@ class WebsocketManager:
async def _handle_metadata_update(self, metadata):
await self.notify_websockets("metadata_update", metadata)
async def _handle_power_changed(self, pstatus):
await self.notify_websockets("power_changed", pstatus)
def register_local_handler(self, api_def, callback):
for ws_method, req_method in \
zip(api_def.ws_methods, api_def.request_methods):