From 21b18b377ed7b04e4d5b42d4fbb644eb4b399ee9 Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Thu, 12 Dec 2024 14:45:07 -0700 Subject: [PATCH] In maintel/mtcs.py, add a context manager to ensure m1m3 is in engineering mode before/after some operation. --- .../lsst/ts/observatory/control/maintel/mtcs.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python/lsst/ts/observatory/control/maintel/mtcs.py b/python/lsst/ts/observatory/control/maintel/mtcs.py index 5c80b84d..c8e35bc6 100644 --- a/python/lsst/ts/observatory/control/maintel/mtcs.py +++ b/python/lsst/ts/observatory/control/maintel/mtcs.py @@ -1707,6 +1707,22 @@ async def exit_m1m3_engineering_mode(self) -> None: else: self.log.warning("M1M3 not in engineering mode.") + @contextlib.asynccontextmanager + async def m1m3_in_engineering_mode(self) -> typing.AsyncIterator[None]: + """Context manager to ensure m1m3 enters and exists engineering + mode before/after an operation. + + If M1M3 is in engineering mode before, this context manager is a + nop, e.g. it will leave M1M3 in engineering mode afterwards. + """ + try: + m1m3_in_engineering_mode_before = await self.is_m1m3_in_engineering_mode() + await self.enter_m1m3_engineering_mode() + yield + finally: + if not m1m3_in_engineering_mode_before: + await self.exit_m1m3_engineering_mode() + async def run_m1m3_hard_point_test(self, hp: int) -> None: """Test an M1M3 hard point.