Skip to content

Commit

Permalink
module server integrated into module control.
Browse files Browse the repository at this point in the history
  • Loading branch information
amit lissack committed Oct 23, 2021
1 parent 1db0a02 commit dfe0d68
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
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

NotifyMethod = Callable[[List[ModuleAtPort], List[ModuleAtPort]], Awaitable[None]]
"""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."""

Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/hardware_control/emulation/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
4 changes: 2 additions & 2 deletions api/src/opentrons/hardware_control/emulation/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class ModuleType(str, Enum):
"""Module type enumeration."""

Magnetic = "magnetic"
Temperature = "temperature"
Magnetic = "magdeck"
Temperature = "tempdeck"
Thermocycler = "thermocycler"
Heatershaker = "heatershaker"
19 changes: 4 additions & 15 deletions api/src/opentrons/hardware_control/module_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit dfe0d68

Please sign in to comment.