---
# System prepended metadata

title: Interface for communication between modules

---

# Interface for communication between modules

- Having a parent Publisher and subscriber class will not provide enough rigidity to the current code structure.
- **Use Enums to define events**
- Each module should have an Enum: <ModuleName>EventEnum
    - For example for `BayManager` - `BayManagerEventsEnum` or `bay_manager_events`
- This enum will contain all the events that can occur in this module.

```python
from enum import Enum
    
class BayManagerEventsEnum(Enum):
    READY_DISK=1
    DISK_STATUS=2
    PHYSICALLY_DISCONNECTED=3
    REMOVED_DRIVES=4

class Publisher:
    ...
    def __init__(EventsEnum):
    subscribers = {event: dict() for event in EventsEnum}
    
    def get_subscribers(self, event):
        return self.events[event]
    
    def register(self, event, subscriber, callback):
        if event in BayManagerEventsEnum:
            self.get_subscribers(event)[subsriber] = callback
    
    def unregister(self, event, subscriber):
        del self.get_subscribers(event)[subscriber]
    
    def notify(self, event, message):
        for subscriber, callback in self.get_subscribers(event).items():
            callback(message)

class BayManager(Publihser):
    def __init__():
        super.__init__(BayManagerEventsEnum)

```
- **Having events defined by Enums, use a dictionary to register and notify**
- The subscribers can subscribe to any event in a module
```python
# Example of how API provider can subscribe to an event 

import BayManagerEventsEnum
class API:
    
    def __init__(_bayManager):
        self._bayManager = _bayManager
        self._bayManager.register(BayManagerEventsEnum.READY_DISK, self, self.ready_events_callback)
    
    def ready_events_callback(message=None):
        self._ready_disks()
```
A better way to register subscribers would be during object creation rather than during starting the modules. This can prevent many bugs, for example:-
- Suppose we create object of BayManager and start it before passing it to API class constructor.
- If a drivebay is already inserted when the readout_station.service is started, it is a possibility that BayManager can detect and store the inserted drivebay before API can register itself.
- It can cause API to miss the data of inserted drivebay, and it will show empty on the UI even though the drivebay is inserted.
    
