power: refactor power requests
Do not allow on, off, or status requests without arguments. Pass device objects to "power_device()" rather than device names. Signed-off-by: Eric Callahan <arkine.code@gmail.com>
This commit is contained in:
parent
6738aa8c68
commit
f6fa093369
|
@ -39,46 +39,40 @@ class PrinterPower:
|
||||||
self.devices[dev.get_name()] = dev
|
self.devices[dev.get_name()] = dev
|
||||||
|
|
||||||
async def _handle_list_devices(self, web_request):
|
async def _handle_list_devices(self, web_request):
|
||||||
output = {"devices": list(self.devices.keys())}
|
dev_list = [d.get_device_info() for d in self.devices.values()]
|
||||||
|
output = {"devices": dev_list}
|
||||||
return output
|
return output
|
||||||
|
|
||||||
async def _handle_power_request(self, web_request):
|
async def _handle_power_request(self, web_request):
|
||||||
args = web_request.get_args()
|
args = web_request.get_args()
|
||||||
ep = web_request.get_endpoint()
|
ep = web_request.get_endpoint()
|
||||||
if len(args) == 0:
|
if not args:
|
||||||
if ep == "/machine/gpio_power/status":
|
raise self.server.error("No arguments provided")
|
||||||
args = self.devices
|
requsted_devs = {k: self.devices.get(k, None) for k in args}
|
||||||
else:
|
|
||||||
return "no_devices"
|
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
req = ep.split("/")[-1]
|
req = ep.split("/")[-1]
|
||||||
for dev in args:
|
for name, device in requsted_devs.items():
|
||||||
if req not in ("on", "off", "status"):
|
if device is not None:
|
||||||
raise self.server.error("Unsupported power request")
|
result[name] = await self._process_request(device, req)
|
||||||
if (await self._power_dev(dev, req)):
|
|
||||||
result[dev] = self.devices[dev].get_state()
|
|
||||||
else:
|
else:
|
||||||
result[dev] = "device_not_found"
|
result[name] = "device_not_found"
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def _power_dev(self, dev, req):
|
async def _process_request(self, device, req):
|
||||||
if dev not in self.devices:
|
|
||||||
return False
|
|
||||||
if req in ["on", "off"]:
|
if req in ["on", "off"]:
|
||||||
ret = self.devices[dev].set_power(req)
|
ret = device.set_power(req)
|
||||||
if asyncio.iscoroutine(ret):
|
if asyncio.iscoroutine(ret):
|
||||||
await ret
|
await ret
|
||||||
self.server.send_event("gpio_power:power_changed", {
|
dev_info = device.get_device_info()
|
||||||
"device": dev,
|
self.server.send_event("gpio_power:power_changed", dev_info)
|
||||||
"status": req
|
elif req == "status":
|
||||||
})
|
ret = device.refresh_status()
|
||||||
elif req != "status":
|
if asyncio.iscoroutine(ret):
|
||||||
raise self.server.error("Unsupported power request")
|
await ret
|
||||||
ret = self.devices[dev].refresh_status()
|
dev_info = device.get_device_info()
|
||||||
if asyncio.iscoroutine(ret):
|
else:
|
||||||
await ret
|
raise self.server.error(f"Unsupported power request: {req}")
|
||||||
return True
|
return dev_info['status']
|
||||||
|
|
||||||
def set_device_power(self, device, state):
|
def set_device_power(self, device, state):
|
||||||
status = None
|
status = None
|
||||||
|
@ -91,8 +85,12 @@ class PrinterPower:
|
||||||
if status not in ["on", "off"]:
|
if status not in ["on", "off"]:
|
||||||
logging.info(f"Invalid state received: {state}")
|
logging.info(f"Invalid state received: {state}")
|
||||||
return
|
return
|
||||||
|
if device not in self.devices:
|
||||||
|
logging.info(f"No device found: {device}")
|
||||||
|
return
|
||||||
ioloop = IOLoop.current()
|
ioloop = IOLoop.current()
|
||||||
ioloop.spawn_callback(self._power_dev, device, status)
|
ioloop.spawn_callback(
|
||||||
|
self._process_request, self.devices[device], status)
|
||||||
|
|
||||||
async def add_device(self, name, device):
|
async def add_device(self, name, device):
|
||||||
if name in self.devices:
|
if name in self.devices:
|
||||||
|
@ -181,8 +179,12 @@ class GpioDevice:
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_state(self):
|
def get_device_info(self):
|
||||||
return self.state
|
return {
|
||||||
|
'device': self.name,
|
||||||
|
'status': self.state,
|
||||||
|
'type': "gpio"
|
||||||
|
}
|
||||||
|
|
||||||
def refresh_status(self):
|
def refresh_status(self):
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue