power: move process_request to the device class
Perform the entire request within a lock to prevent rentry until the request completes. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
0d93cf2c39
commit
970c8a4181
|
@ -145,7 +145,7 @@ class PrinterPower:
|
|||
fname = queue[0].get("filename", "unknown")
|
||||
logging.debug(
|
||||
f"Job '{fname}' queued, powering on device [{name}]")
|
||||
await self._process_request(dev, "on")
|
||||
await dev.process_request("on")
|
||||
|
||||
async def _handle_list_devices(self,
|
||||
web_request: WebRequest
|
||||
|
@ -169,7 +169,7 @@ class PrinterPower:
|
|||
if action not in ["on", "off", "toggle"]:
|
||||
raise self.server.error(
|
||||
f"Invalid requested action '{action}'")
|
||||
result = await self._process_request(dev, action)
|
||||
result = await dev.process_request(action)
|
||||
return {dev_name: result}
|
||||
|
||||
async def _handle_batch_power_request(self,
|
||||
|
@ -184,44 +184,11 @@ class PrinterPower:
|
|||
req = ep.split("/")[-1]
|
||||
for name, device in requested_devs.items():
|
||||
if device is not None:
|
||||
result[name] = await self._process_request(device, req)
|
||||
result[name] = await device.process_request(req)
|
||||
else:
|
||||
result[name] = "device_not_found"
|
||||
return result
|
||||
|
||||
async def _process_request(self,
|
||||
device: PowerDevice,
|
||||
req: str
|
||||
) -> str:
|
||||
base_state: str = device.get_state()
|
||||
ret = device.refresh_status()
|
||||
if ret is not None:
|
||||
await ret
|
||||
cur_state: str = device.get_state()
|
||||
if req == "toggle":
|
||||
req = "on" if cur_state == "off" else "off"
|
||||
if req in ["on", "off"]:
|
||||
if req == cur_state:
|
||||
# device is already in requested state, do nothing
|
||||
if base_state != cur_state:
|
||||
device.notify_power_changed()
|
||||
return cur_state
|
||||
printing = await self._check_klippy_printing()
|
||||
if device.get_locked_while_printing() and printing:
|
||||
raise self.server.error(
|
||||
f"Unable to change power for {device.get_name()} "
|
||||
"while printing")
|
||||
ret = device.set_power(req)
|
||||
if ret is not None:
|
||||
await ret
|
||||
cur_state = device.get_state()
|
||||
await device.process_power_changed()
|
||||
elif req != "status":
|
||||
raise self.server.error(f"Unsupported power request: {req}")
|
||||
elif base_state != cur_state:
|
||||
device.notify_power_changed()
|
||||
return cur_state
|
||||
|
||||
def set_device_power(self, device: str, state: Union[bool, str]) -> None:
|
||||
request: str = ""
|
||||
if isinstance(state, bool):
|
||||
|
@ -238,7 +205,7 @@ class PrinterPower:
|
|||
return
|
||||
event_loop = self.server.get_event_loop()
|
||||
event_loop.register_callback(
|
||||
self._process_request, self.devices[device], request)
|
||||
self.devices[device].process_request, request)
|
||||
|
||||
async def add_device(self, name: str, device: PowerDevice) -> None:
|
||||
if name in self.devices:
|
||||
|
@ -265,6 +232,7 @@ class PowerDevice:
|
|||
self.name = name_parts[1]
|
||||
self.type: str = config.get('type')
|
||||
self.state: str = "init"
|
||||
self.request_lock = asyncio.Lock()
|
||||
self.locked_while_printing = config.getboolean(
|
||||
'locked_while_printing', False)
|
||||
self.off_when_shutdown = config.getboolean('off_when_shutdown', False)
|
||||
|
@ -287,6 +255,13 @@ class PowerDevice:
|
|||
self.on_when_queued = config.getboolean('on_when_upload_queued',
|
||||
False, deprecate=True)
|
||||
|
||||
async def _check_klippy_printing(self) -> bool:
|
||||
kapis: APIComp = self.server.lookup_component('klippy_apis')
|
||||
result: Dict[str, Any] = await kapis.query_objects(
|
||||
{'print_stats': None}, default={})
|
||||
pstate = result.get('print_stats', {}).get('state', "").lower()
|
||||
return pstate == "printing"
|
||||
|
||||
def _is_bound_to_klipper(self):
|
||||
return (
|
||||
self.bound_service is not None and
|
||||
|
@ -391,6 +366,37 @@ class PowerDevice:
|
|||
)
|
||||
return None
|
||||
|
||||
async def process_request(self, req: str) -> str:
|
||||
async with self.request_lock:
|
||||
base_state: str = self.state
|
||||
ret = self.refresh_status()
|
||||
if ret is not None:
|
||||
await ret
|
||||
cur_state: str = self.state
|
||||
if req == "toggle":
|
||||
req = "on" if cur_state == "off" else "off"
|
||||
if req in ["on", "off"]:
|
||||
if req == cur_state:
|
||||
# device is already in requested state, do nothing
|
||||
if base_state != cur_state:
|
||||
self.notify_power_changed()
|
||||
return cur_state
|
||||
printing = await self._check_klippy_printing()
|
||||
if self.locked_while_printing and printing:
|
||||
raise self.server.error(
|
||||
f"Unable to change power for {self.name} "
|
||||
"while printing")
|
||||
ret = self.set_power(req)
|
||||
if ret is not None:
|
||||
await ret
|
||||
cur_state = self.state
|
||||
await self.process_power_changed()
|
||||
elif req != "status":
|
||||
raise self.server.error(f"Unsupported power request: {req}")
|
||||
elif base_state != cur_state:
|
||||
self.notify_power_changed()
|
||||
return cur_state
|
||||
|
||||
def refresh_status(self) -> Optional[Coroutine]:
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -410,7 +416,6 @@ class HTTPDevice(PowerDevice):
|
|||
) -> None:
|
||||
super().__init__(config)
|
||||
self.client = AsyncHTTPClient()
|
||||
self.request_mutex = asyncio.Lock()
|
||||
self.addr: str = config.get("address")
|
||||
self.port = config.getint("port", default_port)
|
||||
self.user = config.load_template("user", default_user).render()
|
||||
|
@ -419,6 +424,7 @@ class HTTPDevice(PowerDevice):
|
|||
self.protocol = config.get("protocol", default_protocol)
|
||||
|
||||
async def initialize(self) -> None:
|
||||
async with self.request_lock:
|
||||
super().initialize()
|
||||
await self.refresh_status()
|
||||
|
||||
|
@ -450,7 +456,6 @@ class HTTPDevice(PowerDevice):
|
|||
"_send_status_request must be implemented by children")
|
||||
|
||||
async def refresh_status(self) -> None:
|
||||
async with self.request_mutex:
|
||||
try:
|
||||
state = await self._send_status_request()
|
||||
except Exception:
|
||||
|
@ -461,7 +466,6 @@ class HTTPDevice(PowerDevice):
|
|||
self.state = state
|
||||
|
||||
async def set_power(self, state):
|
||||
async with self.request_mutex:
|
||||
try:
|
||||
state = await self._send_power_request(state)
|
||||
except Exception:
|
||||
|
@ -544,7 +548,6 @@ class KlipperDevice(PowerDevice):
|
|||
f" for 'klipper_device' [{config.get_name()}]")
|
||||
self.is_shutdown: bool = False
|
||||
self.update_fut: Optional[asyncio.Future] = None
|
||||
self.request_mutex = asyncio.Lock()
|
||||
self.timer: Optional[float] = config.getfloat(
|
||||
'timer', None, above=0.000001)
|
||||
self.timer_handle: Optional[asyncio.TimerHandle] = None
|
||||
|
@ -596,7 +599,6 @@ class KlipperDevice(PowerDevice):
|
|||
async def refresh_status(self) -> None:
|
||||
if self.is_shutdown or self.state in ["on", "off", "init"]:
|
||||
return
|
||||
async with self.request_mutex:
|
||||
kapis: APIComp = self.server.lookup_component('klippy_apis')
|
||||
req: Dict[str, Optional[List[str]]] = {self.object_name: None}
|
||||
data: Optional[Dict[str, Any]]
|
||||
|
@ -612,7 +614,6 @@ class KlipperDevice(PowerDevice):
|
|||
raise self.server.error(
|
||||
f"Power Device {self.name}: Cannot set power for device "
|
||||
f"when Klipper is shutdown")
|
||||
async with self.request_mutex:
|
||||
self._reset_timer()
|
||||
eventloop = self.server.get_event_loop()
|
||||
self.update_fut = eventloop.create_future()
|
||||
|
@ -742,7 +743,6 @@ class TPLinkSmartPlug(PowerDevice):
|
|||
def __init__(self, config: ConfigHelper) -> None:
|
||||
super().__init__(config)
|
||||
self.timer = config.get("timer", "")
|
||||
self.request_mutex = asyncio.Lock()
|
||||
addr_and_output_id = config.get("address").split('/')
|
||||
self.addr = addr_and_output_id[0]
|
||||
if (len(addr_and_output_id) > 1):
|
||||
|
@ -826,11 +826,11 @@ class TPLinkSmartPlug(PowerDevice):
|
|||
return res
|
||||
|
||||
async def initialize(self) -> None:
|
||||
async with self.request_lock:
|
||||
super().initialize()
|
||||
await self.refresh_status()
|
||||
|
||||
async def refresh_status(self) -> None:
|
||||
async with self.request_mutex:
|
||||
try:
|
||||
state: str
|
||||
res = await self._send_tplink_command("info")
|
||||
|
@ -849,7 +849,6 @@ class TPLinkSmartPlug(PowerDevice):
|
|||
self.state = "on" if state else "off"
|
||||
|
||||
async def set_power(self, state) -> None:
|
||||
async with self.request_mutex:
|
||||
err: int
|
||||
try:
|
||||
if self.timer != "" and state == "off":
|
||||
|
@ -1143,7 +1142,6 @@ class MQTTDevice(PowerDevice):
|
|||
self.mqtt.subscribe_topic(
|
||||
self.state_topic, self._on_state_update, self.qos)
|
||||
self.query_response: Optional[asyncio.Future] = None
|
||||
self.request_mutex = asyncio.Lock()
|
||||
self.server.register_event_handler(
|
||||
"mqtt:connected", self._on_mqtt_connected)
|
||||
self.server.register_event_handler(
|
||||
|
@ -1151,7 +1149,7 @@ class MQTTDevice(PowerDevice):
|
|||
|
||||
def _on_state_update(self, payload: bytes) -> None:
|
||||
last_state = self.state
|
||||
in_request = self.request_mutex.locked()
|
||||
in_request = self.request_lock.locked()
|
||||
err: Optional[Exception] = None
|
||||
context = {
|
||||
'payload': payload.decode()
|
||||
|
@ -1185,7 +1183,7 @@ class MQTTDevice(PowerDevice):
|
|||
self.query_response.set_result(response)
|
||||
|
||||
async def _on_mqtt_connected(self) -> None:
|
||||
async with self.request_mutex:
|
||||
async with self.request_lock:
|
||||
if self.state in ["on", "off"]:
|
||||
return
|
||||
self.state = "init"
|
||||
|
@ -1226,7 +1224,7 @@ class MQTTDevice(PowerDevice):
|
|||
):
|
||||
self.query_response.set_exception(
|
||||
self.server.error("MQTT Disconnected", 503))
|
||||
async with self.request_mutex:
|
||||
async with self.request_lock:
|
||||
self.state = "error"
|
||||
self.notify_power_changed()
|
||||
|
||||
|
@ -1239,7 +1237,6 @@ class MQTTDevice(PowerDevice):
|
|||
raise self.server.error(
|
||||
f"MQTT Power Device {self.name}: "
|
||||
"MQTT Not Connected", 503)
|
||||
async with self.request_mutex:
|
||||
self.query_response = self.eventloop.create_future()
|
||||
try:
|
||||
await self._wait_for_update(self.query_response)
|
||||
|
@ -1265,7 +1262,6 @@ class MQTTDevice(PowerDevice):
|
|||
raise self.server.error(
|
||||
f"MQTT Power Device {self.name}: "
|
||||
"MQTT Not Connected", 503)
|
||||
async with self.request_mutex:
|
||||
self.query_response = self.eventloop.create_future()
|
||||
new_state = "error"
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue