Skip to content

Commit

Permalink
hardware controller tweaks from PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
b-cooper committed Nov 18, 2019
1 parent 82dac3f commit 1718908
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
2 changes: 0 additions & 2 deletions api/src/opentrons/hardware_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
from .controller import Controller
from . import modules
from .types import Axis, HardwareAPILike, CriticalPoint
if TYPE_CHECKING:
from .types import RegisterModules # noqa (F501)


mod_log = logging.getLogger(__name__)
Expand Down
32 changes: 19 additions & 13 deletions api/src/opentrons/hardware_control/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
try:
import aionotify # type: ignore
import aionotify
except OSError:
aionotify = None # type: ignore

Expand All @@ -15,7 +15,7 @@
from . import modules

if TYPE_CHECKING:
from .types import RegisterModules # noqa (F501)
from .dev_types import RegisterModules # noqa (F501)

MODULE_LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,7 +51,7 @@ def __init__(self, config):
path='/dev',
flags=(aionotify.Flags.CREATE | aionotify.Flags.DELETE))
except AttributeError:
MODULE_LOG.info(
MODULE_LOG.warning(
'Failed to initiate aionotify, cannot watch modules,'
'likely because not running on linux')

Expand Down Expand Up @@ -137,17 +137,23 @@ async def watch_modules(self, loop: asyncio.AbstractEventLoop,
while can_watch and (not self._module_watcher.closed):
event = await self._module_watcher.get_event()
flags = aionotify.Flags.parse(event.flags)
if 'ot_module' in event.name:
if event is not None and 'ot_module' in event.name:
maybe_module_at_port = modules.get_module_at_port(event.name)
if maybe_module_at_port is not None and \
aionotify.Flags.DELETE in flags:
new_modules = None
removed_modules = None
if maybe_module_at_port is not None:
if aionotify.Flags.DELETE in flags:
removed_modules = [maybe_module_at_port]
MODULE_LOG.info(
f'Module Removed: {maybe_module_at_port}')
elif aionotify.Flags.CREATE in flags:
new_modules = [maybe_module_at_port]
MODULE_LOG.info(
f'Module Added: {maybe_module_at_port}')
await register_modules(
removed_modules=[maybe_module_at_port])
MODULE_LOG.info(f'Module Removed: {maybe_module_at_port}')
if maybe_module_at_port is not None and \
aionotify.Flags.CREATE in flags:
await register_modules(new_modules=[maybe_module_at_port])
MODULE_LOG.info(f'Module Added: {maybe_module_at_port}')
removed_modules=removed_modules,
new_modules=new_modules,
)

async def build_module(self,
port: str,
Expand Down Expand Up @@ -229,5 +235,5 @@ async def delay(self, duration_s: int):
await asyncio.sleep(duration_s)
self.resume()

def __exit__(self, exc_type, exc_value, traceback):
def __del__(self, exc_type, exc_value, traceback):
self._module_watcher.close()
12 changes: 12 additions & 0 deletions api/src/opentrons/hardware_control/dev_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
try:
from typing_extensions import Protocol
except ModuleNotFoundError:
Protocol = None # type: ignore

if Protocol is not None:
class RegisterModules(Protocol):
async def __call__(
self,
new_modules: List[ModuleAtPort] = None,
removed_modules: List[ModuleAtPort] = None
) -> None: ...
2 changes: 1 addition & 1 deletion api/src/opentrons/hardware_control/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from opentrons.drivers.smoothie_drivers import SimulatingDriver
from . import modules
if TYPE_CHECKING:
from .types import RegisterModules # noqa (F501)
from .dev_types import RegisterModules # noqa (F501)


MODULE_LOG = logging.getLogger(__name__)
Expand Down
13 changes: 0 additions & 13 deletions api/src/opentrons/hardware_control/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import enum
from typing import Tuple, List
try:
from typing_extensions import Protocol
except ModuleNotFoundError:
Protocol = None # type: ignore

import opentrons.types
from .modules import ModuleAtPort
Expand Down Expand Up @@ -96,12 +92,3 @@ class CriticalPoint(enum.Enum):
The end of the front-most nozzle of a multipipette with a tip attached.
Only relevant when a multichannel pipette is present.
"""


if Protocol is not None:
class RegisterModules(Protocol):
async def __call__(
self,
new_modules: List[ModuleAtPort] = None,
removed_modules: List[ModuleAtPort] = None
) -> None: ...

0 comments on commit 1718908

Please sign in to comment.