Skip to content

Commit

Permalink
utils
Browse files Browse the repository at this point in the history
  • Loading branch information
amit lissack committed Oct 23, 2021
1 parent c25c1e7 commit 6ae4a1e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
4 changes: 4 additions & 0 deletions api/src/opentrons/hardware_control/emulation/module_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ async def read(self) -> Message:
m: Message = Message.parse_raw(b)
return m

def close(self) -> None:
"""Close the client."""
self._writer.close()


async def wait_emulators(
client: ModuleServerClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def run_emulator_server(host: str, port: int, emulator: AbstractEmulator)
Args:
host: Host to listen on.
port: Port to listen on.
emulator: Emaulator instance.
emulator: Emulator instance.
Returns:
None
Expand Down
56 changes: 22 additions & 34 deletions g-code-testing/g_code_parsing/g_code_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
from opentrons.hardware_control.emulation.tempdeck import TempDeckEmulator
from opentrons.hardware_control.emulation.thermocycler import \
ThermocyclerEmulator
from opentrons.hardware_control.emulation.types import ModuleType
from opentrons.protocols.parse import parse
from opentrons.protocols.execution import execute
from contextlib import contextmanager
from opentrons.protocol_api import ProtocolContext
from opentrons.config.robot_configs import build_config
from opentrons.hardware_control.emulation.app import Application
from opentrons.hardware_control.emulation import module_server
from opentrons.hardware_control.emulation.scripts import run_app
from opentrons.hardware_control import API, ThreadManager
from g_code_parsing.g_code_program.g_code_program import (
GCodeProgram,
Expand Down Expand Up @@ -76,39 +78,27 @@ def _set_env_vars(settings: Settings) -> None:
)

@staticmethod
def _start_emulation_app(application: Application, emulator_settings: Settings) -> Process:
def _start_emulation_app(emulator_settings: Settings) -> Process:
"""Start emulated OT-2"""
async def _run_emulation_environment() -> None:
await asyncio.gather(
# Start application
application.run(),
# Add magdeck emulator
run_emulator_client(
host="localhost",
port=emulator_settings.magdeck_proxy.emulator_port,
emulator=MagDeckEmulator(Parser()),
),
# Add temperature emulator
run_emulator_client(
host="localhost",
port=emulator_settings.temperature_proxy.emulator_port,
emulator=TempDeckEmulator(Parser()),
),
# Add thermocycler emulator
run_emulator_client(
host="localhost",
port=emulator_settings.thermocycler_proxy.emulator_port,
emulator=ThermocyclerEmulator(Parser()),
),
)
modules = [ModuleType.Magnetic, ModuleType.Temperature, ModuleType.Thermocycler]

def runit():
asyncio.run(_run_emulation_environment())
asyncio.run(run_app.run(emulator_settings, modules=[m.value for m in modules]))

t = Process(target=runit)
t.daemon = True
t.start()
return t
proc = Process(target=runit)
proc.daemon = True
proc.start()

async def _wait_ready() -> None:
c = await module_server.ModuleServerClient.connect(host="localhost", port=emulator_settings.module_server.port)
await module_server.wait_emulators(client=c, modules=modules, timeout=5)
c.close()

proc2 = Process(target=lambda: asyncio.run(_wait_ready()))
proc2.start()
proc2.join()

return proc

@staticmethod
def _emulate_hardware(settings: Settings) -> ThreadManager:
Expand Down Expand Up @@ -138,8 +128,7 @@ def run_protocol(self, path: str) -> Generator:
:return: GCodeProgram with all the parsed data
"""
file_path = os.path.join(get_configuration_dir(), path)
emulator_app = Application(self._config)
app_process = self._start_emulation_app(application=emulator_app, emulator_settings=self._config)
app_process = self._start_emulation_app(emulator_settings=self._config)
protocol = self._get_protocol(file_path)
context = ProtocolContext(
implementation=ProtocolContextImplementation(
Expand All @@ -161,8 +150,7 @@ def run_http(self, executable: Callable):
:param executable: Function connected to HTTP Request to execute
:return:
"""
emulator_app = Application(self._config)
app_process = self._start_emulation_app(application=emulator_app, emulator_settings=self._config)
app_process = self._start_emulation_app(emulator_settings=self._config)
with GCodeWatcher(emulator_settings=self._config) as watcher:
asyncio.run(executable(hardware=self._emulate_hardware(settings=self._config)))
yield GCodeProgram.from_g_code_watcher(watcher)
Expand Down

0 comments on commit 6ae4a1e

Please sign in to comment.