From a30c4fa41746416e06ec1edbf7805a04ad21aa09 Mon Sep 17 00:00:00 2001 From: Kenneth Yang <82800265+kjy5@users.noreply.github.com> Date: Tue, 9 Jul 2024 09:43:38 -0700 Subject: [PATCH] 365 stop individual manipulators (#366) * Add stop and stop all commands * Define server event --- src/ephys_link/back_end/platform_handler.py | 21 +++++++++++++++++++-- src/ephys_link/back_end/server.py | 7 ++++++- src/ephys_link/bindings/fake_bindings.py | 2 +- src/ephys_link/bindings/ump_4_bindings.py | 5 ++--- src/ephys_link/util/base_bindings.py | 4 ++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/ephys_link/back_end/platform_handler.py b/src/ephys_link/back_end/platform_handler.py index a702d0f..52114a2 100644 --- a/src/ephys_link/back_end/platform_handler.py +++ b/src/ephys_link/back_end/platform_handler.py @@ -266,14 +266,31 @@ async def set_inside_brain(self, request: SetInsideBrainRequest) -> BooleanState else: return BooleanStateResponse(state=request.inside) - async def stop(self) -> str: + async def stop(self, manipulator_id: str) -> str: + """Stop a manipulator. + + :param manipulator_id: Manipulator ID. + :type manipulator_id: str + :returns: Error message if any. + :rtype: str + """ + try: + await self._bindings.stop(manipulator_id) + except Exception as e: + self._console.exception_error_print("Stop", e) + return self._console.pretty_exception(e) + else: + return "" + + async def stop_all(self) -> str: """Stop all manipulators. :returns: Error message if any. :rtype: str """ try: - await self._bindings.stop() + for manipulator_id in await self._bindings.get_manipulators(): + await self._bindings.stop(manipulator_id) except Exception as e: self._console.exception_error_print("Stop", e) return self._console.pretty_exception(e) diff --git a/src/ephys_link/back_end/server.py b/src/ephys_link/back_end/server.py index 4698482..340ea41 100644 --- a/src/ephys_link/back_end/server.py +++ b/src/ephys_link/back_end/server.py @@ -189,7 +189,12 @@ async def platform_event_handler(self, event: str, *args: tuple[Any]) -> str: self._platform_handler.set_inside_brain, SetInsideBrainRequest, event, args ) case "stop": - return await self._platform_handler.stop() + request_data = args[1] + if request_data: + return await self._platform_handler.stop(str(request_data)) + return self._malformed_request_response(event, request_data) + case "stop_all": + return await self._platform_handler.stop_all() case _: self._console.error_print(f"Unknown event: {event}.") return dumps({"error": "Unknown event."}) diff --git a/src/ephys_link/bindings/fake_bindings.py b/src/ephys_link/bindings/fake_bindings.py index 38f41fe..0187fd0 100644 --- a/src/ephys_link/bindings/fake_bindings.py +++ b/src/ephys_link/bindings/fake_bindings.py @@ -44,7 +44,7 @@ async def set_position(self, manipulator_id: str, position: Vector4, _: float) - self._positions[int(manipulator_id)] = position return position - async def stop(self) -> None: + async def stop(self, _: str) -> None: pass def platform_space_to_unified_space(self, platform_space: Vector4) -> Vector4: diff --git a/src/ephys_link/bindings/ump_4_bindings.py b/src/ephys_link/bindings/ump_4_bindings.py index 5c537fd..8cbd800 100644 --- a/src/ephys_link/bindings/ump_4_bindings.py +++ b/src/ephys_link/bindings/ump_4_bindings.py @@ -91,9 +91,8 @@ async def set_position(self, manipulator_id: str, position: Vector4, speed: floa return um_to_mm(array_to_vector4(movement.last_pos)) - async def stop(self) -> None: - for device_ids in await self.get_manipulators(): - self._get_device(device_ids).stop() + async def stop(self, manipulator_id: str) -> None: + self._get_device(manipulator_id).stop() def platform_space_to_unified_space(self, platform_space: Vector4) -> Vector4: # unified <- platform diff --git a/src/ephys_link/util/base_bindings.py b/src/ephys_link/util/base_bindings.py index 1a95c0d..bfa3354 100644 --- a/src/ephys_link/util/base_bindings.py +++ b/src/ephys_link/util/base_bindings.py @@ -102,8 +102,8 @@ async def set_position(self, manipulator_id: str, position: Vector4, speed: floa """ @abstractmethod - async def stop(self) -> None: - """Stop all manipulators.""" + async def stop(self, manipulator_id: str) -> None: + """Stop a manipulator.""" @abstractmethod def platform_space_to_unified_space(self, platform_space: Vector4) -> Vector4: