Implemented dispatcher

This commit is contained in:
jupfi 2023-07-06 16:40:35 +02:00
parent 86a1a8ed5a
commit b0eee8aeb3
4 changed files with 59 additions and 12 deletions

View file

@ -13,13 +13,11 @@ class BaseSpectrometer(Module):
@property
def pulse_program(self):
"""Pulse program of the spectrometer."""
raise NotImplementedError
def start_measurement(self):
"""Starts the measurement."""
"""Pulse program of the spectrometer.
"""
raise NotImplementedError
def set_active(self):
"""Sets the spectrometer as the active spectrometer."""
"""Sets the spectrometer as the active spectrometer.
"""
self.change_spectrometer.emit(self._model.name)

View file

@ -0,0 +1,11 @@
from nqrduck.module.module_controller import ModuleController
class BaseSpectrometerController(ModuleController):
def __init__(self, module):
super().__init__(module)
def start_measurement(self):
"""Starts the measurement.
"""
raise NotImplementedError

View file

@ -1,15 +1,23 @@
import logging
from PyQt5.QtCore import pyqtSlot
from nqrduck.module.module_controller import ModuleController
from nqrduck.core.main_controller import MainController
from nqrduck_spectrometer.base_spectrometer import BaseSpectrometer
logger = logging.getLogger(__name__)
class SpectrometerController(ModuleController):
"""This class is the controller for the spectrometer module."""
def __init__(self, module):
"""This method initializes the controller.
:param module: The module that this controller belongs to.
"""
super().__init__(module)
def _load_spectrometer_modules(self):
"""This method loads the spectrometer modules and adds them to the spectrometer model."""
# Get the modules with entry points in the nqrduck group
modules = MainController._get_modules()
logger.debug("Found modules: %s", modules)
@ -17,18 +25,43 @@ class SpectrometerController(ModuleController):
for module_name, module in modules.items():
# Check if the module instance is a spectrometer by checking if it inherits from BaseSpectrometer
if not issubclass(type(module), BaseSpectrometer):
logger.debug("Module is not a spectrometer: %s ... skipping", module_name)
logger.debug(
"Module is not a spectrometer: %s ... skipping", module_name
)
continue
# Import the module
logger.debug("Loading spectromter module: %s", module_name)
module.model.widget_changed.connect(self._module.view.on_spectrometer_widget_changed)
module.model.widget_changed.connect(
self._module.view.on_spectrometer_widget_changed
)
logger.debug("Adding spectrometer to spectrometer model: %s", module_name)
self._module.model.add_spectrometers(module_name, module)
self._module.view.create_menu_entry()
def process_signals(self, key: str, value: str):
if key == "start_measurement":
self.on_measurement_start()
def on_loading(self):
self._module.model.spectrometer_added.connect(self._module.view.on_spectrometer_added)
self._module.model.active_spectrometer_changed.connect(self._module.view.on_active_spectrometer_changed)
self._load_spectrometer_modules()
"""This method is called when the module is loaded.
It connects the signals from the spectrometer model to the view.
"""
self._module.model.spectrometer_added.connect(
self._module.view.on_spectrometer_added
)
self._module.model.active_spectrometer_changed.connect(
self._module.view.on_active_spectrometer_changed
)
self._load_spectrometer_modules()
def on_measurement_start(self):
"""This method is called when a measurement is started.
It calls the on_measurement_start method of the active spectrometer.
"""
logger.debug(
"Measurement started with spectrometer: %s",
self._module.model.active_spectrometer,
)
self._module.model.active_spectrometer.controller.start_measurement()

View file

@ -16,6 +16,8 @@ class SpectrometerModel(ModuleModel):
@property
def active_spectrometer(self):
"""The currently active spectrometer. This is the one that is currently being used.
"""
return self._active_spectrometer
@active_spectrometer.setter
@ -25,11 +27,14 @@ class SpectrometerModel(ModuleModel):
@property
def available_spectrometers(self):
"""A dictionary of all available spectrometers. The key is the name of the spectrometer and the value is the module.
"""
return self._available_spectrometers
def add_spectrometers(self, spectrometer_module_name, module):
self._available_spectrometers [spectrometer_module_name] = module
logger.debug("Added module: %s", spectrometer_module_name)
self.spectrometer_added.emit(module)
self.active_spectrometer = module