Skip to content

Commit

Permalink
Add Device/DeviceSet.init() async method
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed Jul 8, 2023
1 parent 78622b6 commit 56ee366
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/gort/gort.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
__all__ = ["GortClient", "Gort", "GortDeviceSet", "GortDevice"]


DevType = TypeVar("DevType", bound="GortDeviceSet | GortDevice")


class GortClient(AMQPClient):
"""The main ``gort`` client, used to communicate with the actor system.
Expand Down Expand Up @@ -72,13 +75,16 @@ def __init__(
self.set_verbosity()

self.actors: dict[str, RemoteActor] = {}
self.config = config.copy()

self.ags = AGSet(self, config["ags"]["devices"])
self.guiders = GuiderSet(self, config["guiders"]["devices"])
self.telescopes = TelescopeSet(self, config["telescopes"]["devices"])
self.nps = NPSSet(self, config["nps"]["devices"])
self.specs = SpectrographSet(self, config["specs"]["devices"])
self.enclosure = Enclosure(self, name="enclosure", actor="lvmecp")
self.__device_sets = []

self.ags = self.add_device(AGSet, config["ags"]["devices"])
self.guiders = self.add_device(GuiderSet, config["guiders"]["devices"])
self.telescopes = self.add_device(TelescopeSet, config["telescopes"]["devices"])
self.nps = self.add_device(NPSSet, config["nps"]["devices"])
self.specs = self.add_device(SpectrographSet, config["specs"]["devices"])
self.enclosure = self.add_device(Enclosure, name="enclosure", actor="lvmecp")

async def init(self) -> Self:
"""Initialises the client.
Expand All @@ -95,8 +101,19 @@ async def init(self) -> Self:

await asyncio.gather(*[ractor.init() for ractor in self.actors.values()])

# Initialise device sets.
await asyncio.gather(*[dev.init() for dev in self.__device_sets])

return self

def add_device(self, class_: Type[DevType], *args, **kwargs) -> DevType:
"""Adds a new device or device set to Gort."""

ds = class_(self, *args, **kwargs)
self.__device_sets.append(ds)

return ds

@property
def connected(self):
"""Returns `True` if the client is connected."""
Expand Down Expand Up @@ -183,6 +200,14 @@ def __init__(self, gort: GortClient, data: dict[str, dict], **kwargs):

dict.__init__(self, _dict_data)

async def init(self):
"""Runs asynchronous tasks that must be executed on init."""

# Run devices init methods.
await asyncio.gather(*[dev.init() for dev in self.values()])

return

def __getattribute__(self, __name: str) -> Any:
if __name in self:
return self.__getitem__(__name)
Expand Down Expand Up @@ -286,6 +311,16 @@ def __init__(self, gort: GortClient, name: str, actor: str):
self.name = name
self.actor = gort.add_actor(actor)

async def init(self):
"""Runs asynchronous tasks that must be executed on init.
If the device is part of a `.DeviceSet`, this method is called
by `.DeviceSet.init`.
"""

return

def write_to_log(
self,
message: str,
Expand Down

0 comments on commit 56ee366

Please sign in to comment.