From 5d6c1349b794fab608d2895f8b2ff2c9ce9ddb6d Mon Sep 17 00:00:00 2001 From: amit lissack Date: Sat, 23 Oct 2021 10:15:55 -0400 Subject: [PATCH] module server integrated into module control. --- .../emulation/module_server/helpers.py | 12 ++++++++++++ .../hardware_control/emulation/proxy.py | 2 +- .../hardware_control/emulation/types.py | 4 ++-- .../hardware_control/module_control.py | 19 ++++--------------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/api/src/opentrons/hardware_control/emulation/module_server/helpers.py b/api/src/opentrons/hardware_control/emulation/module_server/helpers.py index 153a67b4dc45..c2ec0da562ba 100644 --- a/api/src/opentrons/hardware_control/emulation/module_server/helpers.py +++ b/api/src/opentrons/hardware_control/emulation/module_server/helpers.py @@ -5,6 +5,7 @@ from opentrons.hardware_control.emulation.module_server.client import ModuleServerClient from opentrons.hardware_control.emulation.module_server.models import Message from opentrons.hardware_control.emulation.module_server.server import log +from opentrons.hardware_control.emulation.settings import Settings from opentrons.hardware_control.emulation.types import ModuleType from opentrons.hardware_control.modules import ModuleAtPort @@ -12,6 +13,17 @@ """Signature of method to be notified of new and removed modules.""" +async def listen_module_connection(callback: NotifyMethod) -> None: + """Listen for module emulator connections.""" + settings = Settings() + try: + client = await ModuleServerClient.connect(host=settings.module_server.host, port=settings.module_server.port, interval_seconds=1.0) + listener = ModuleListener(client=client, notify_method=callback) + await listener.run() + except IOError: + log.exception("Failed to connect to module server.") + + class ModuleListener: """Provide a callback for listening for new and removed module connections.""" diff --git a/api/src/opentrons/hardware_control/emulation/proxy.py b/api/src/opentrons/hardware_control/emulation/proxy.py index 6877c89ebf5a..3ebc94a961a7 100644 --- a/api/src/opentrons/hardware_control/emulation/proxy.py +++ b/api/src/opentrons/hardware_control/emulation/proxy.py @@ -115,7 +115,7 @@ async def _handle_server_connection( self._cons.append(connection) self._event_listener.on_server_connected( server_type=self._name, - client_uri=f"{socket.gethostname()}:{self._settings.driver_port}", + client_uri=f"socket://{socket.gethostname()}:{self._settings.driver_port}", identifier=connection.identifier, ) diff --git a/api/src/opentrons/hardware_control/emulation/types.py b/api/src/opentrons/hardware_control/emulation/types.py index cbdd3eecb11c..1a5a7028ef34 100644 --- a/api/src/opentrons/hardware_control/emulation/types.py +++ b/api/src/opentrons/hardware_control/emulation/types.py @@ -4,7 +4,7 @@ class ModuleType(str, Enum): """Module type enumeration.""" - Magnetic = "magnetic" - Temperature = "temperature" + Magnetic = "magdeck" + Temperature = "tempdeck" Thermocycler = "thermocycler" Heatershaker = "heatershaker" diff --git a/api/src/opentrons/hardware_control/module_control.py b/api/src/opentrons/hardware_control/module_control.py index a9d9b7d69171..db79d51c2b76 100644 --- a/api/src/opentrons/hardware_control/module_control.py +++ b/api/src/opentrons/hardware_control/module_control.py @@ -8,6 +8,8 @@ from opentrons.config import IS_ROBOT, IS_LINUX from opentrons.drivers.rpi_drivers import types, usb, usb_simulator +from opentrons.hardware_control.emulation.module_server.helpers import \ + listen_module_connection from opentrons.hardware_control.modules import ModuleAtPort from .types import AionotifyEvent, BoardRevision @@ -41,6 +43,8 @@ async def build( mc_instance = cls(api_instance, board_revision) if not api_instance.is_simulator: await mc_instance.register_modules(mc_instance.scan()) + api_instance.loop.create_task(listen_module_connection(mc_instance.register_modules)) + return mc_instance @property @@ -184,21 +188,6 @@ def scan(self) -> List[modules.ModuleAtPort]: if module_at_port: discovered_modules.append(module_at_port) - # Check for emulator environment variables - emulator_uri = os.environ.get("OT_THERMOCYCLER_EMULATOR_URI") - if emulator_uri: - discovered_modules.append( - ModuleAtPort(port=emulator_uri, name="thermocycler") - ) - - emulator_uri = os.environ.get("OT_TEMPERATURE_EMULATOR_URI") - if emulator_uri: - discovered_modules.append(ModuleAtPort(port=emulator_uri, name="tempdeck")) - - emulator_uri = os.environ.get("OT_MAGNETIC_EMULATOR_URI") - if emulator_uri: - discovered_modules.append(ModuleAtPort(port=emulator_uri, name="magdeck")) - log.debug("Discovered modules: {}".format(discovered_modules)) return discovered_modules