From 4d42ee70a936dc3eeca3280386aa0d8c10927150 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20S=C3=A1nchez-Gallego?= Date: Fri, 26 Jul 2024 15:14:05 +0000 Subject: [PATCH] Prevent race conditins dome open vs drive disabled --- python/lvmecp/dome.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/python/lvmecp/dome.py b/python/lvmecp/dome.py index ae720a9..9cc3cf5 100644 --- a/python/lvmecp/dome.py +++ b/python/lvmecp/dome.py @@ -10,6 +10,7 @@ import asyncio import warnings +from time import time from types import SimpleNamespace from lvmecp import log @@ -118,6 +119,8 @@ async def _move(self, open: bool, force: bool = False): await self.modbus["drive_enabled"].set(True) await asyncio.sleep(0.5) + + last_enabled: float = 0.0 while True: # Still moving. await asyncio.sleep(2) @@ -125,14 +128,17 @@ async def _move(self, open: bool, force: bool = False): drive_enabled = await self.modbus["drive_enabled"].get() move_done = await self.modbus["dome_open" if open else "dome_closed"].get() + if drive_enabled: + last_enabled = time() + if not drive_enabled and move_done: break - if not drive_enabled: - # This usually means the movement has been stopped. - raise DomeError( - "Dome drive has been disabled. Was the dome was stopped." - ) + # Check if the drive is not enabled for more than 5 seconds without the + # movement being done. This usually means the dome has been manually + # stopped. + if not drive_enabled and (time() - last_enabled) > 5: + raise DomeError("Dome drive has been disabled.") await self.update(use_cache=False)