Tutorial

Creating an observable property

Creating an observable property for the door state.

As we create a controller for the door of a thermocycler, the first functionality we want to implement is a method to subscribe to the door's state (open or closed). Subscribing means retrieving the state and continually receiving the updated state as soon as it changes. In the base class, we do not implement the functionality but only the abstract method that defines the general structure of the method:

  • name
  • method type (Observable vs. Unobservable, Property vs. Command)
  • parameters
  • type hints (type of the parameters & return variables)
  • documentation (doc string used to specify functionality in a human-readable way)
door_controller_base.py
@abc.abstractmethod
@sila.ObservableProperty()
async def subscribe_door_state(self) -> sila.Stream[bool]:
    """
    Subscribe to the state of the door.
    """

In this case, the structure is:

  • name: subscribe_door_state
  • method type: observable property
  • parameters: only the required self (properties don't allow input parameters)
  • type hints: the return value is a boolean (sila.Stream as it is observable and continually updating)
  • documentation: everything within the """ ... """. Additional documentation is inferred from the method name and type hints.

Code explanation

The two decorators (@):

  • @abc.abstractmethod only works in classes with metaclass=abc.ABCMeta and forces any sub-classes to implement this method.
  • @sila.ObservableProperty() is defined as: "An observable property is a property that can be read at any time and that offers a subscription mechanism to observe any change of its value."
async def subscribe_door_open(self) -> sila.Stream[bool]:
Here, we define an asynchronous function with async def, meaning the code doesn't wait for this function call to finish to continue with different code. We name the function subscribe_door_open(self) to which we pass self to retrieve the door state, we need to know which door we are talking about. The -> sila.Stream[bool] is the type hint defining the return of an observable boolean (open = True, close = False)


Copyright © 2024