power: introduce "force" argument for remote method calls

The "force" argument may be used turn on a device during a print
that is otherwise locked for normal requests.

Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2022-12-23 10:36:10 -05:00
parent a6b8a8a3e9
commit aeea9a2efd
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 12 additions and 8 deletions

View File

@ -169,7 +169,9 @@ class PrinterPower:
result[name] = "device_not_found" result[name] = "device_not_found"
return result return result
def set_device_power(self, device: str, state: Union[bool, str]) -> None: def set_device_power(
self, device: str, state: Union[bool, str], force: bool = False
) -> None:
request: str = "" request: str = ""
if isinstance(state, bool): if isinstance(state, bool):
request = "on" if state else "off" request = "on" if state else "off"
@ -185,7 +187,8 @@ class PrinterPower:
return return
event_loop = self.server.get_event_loop() event_loop = self.server.get_event_loop()
event_loop.register_callback( event_loop.register_callback(
self.devices[device].process_request, request) self.devices[device].process_request, request, force=force
)
async def add_device(self, name: str, device: PowerDevice) -> None: async def add_device(self, name: str, device: PowerDevice) -> None:
if name in self.devices: if name in self.devices:
@ -361,7 +364,7 @@ class PowerDevice:
self.init_task = eventloop.create_task(ret) self.init_task = eventloop.create_task(ret)
return self.state != "error" return self.state != "error"
async def process_request(self, req: str) -> str: async def process_request(self, req: str, force: bool = False) -> str:
if self.state == "init" and self.request_lock.locked(): if self.state == "init" and self.request_lock.locked():
# return immediately if the device is initializing, # return immediately if the device is initializing,
# otherwise its possible for this to block indefinitely # otherwise its possible for this to block indefinitely
@ -381,6 +384,7 @@ class PowerDevice:
if base_state != cur_state: if base_state != cur_state:
self.notify_power_changed() self.notify_power_changed()
return cur_state return cur_state
if not force:
printing = await self._check_klippy_printing() printing = await self._check_klippy_printing()
if self.locked_while_printing and printing: if self.locked_while_printing and printing:
raise self.server.error( raise self.server.error(