From 1dcbf5c3f51206f0c744a3abd276ca5d1d950400 Mon Sep 17 00:00:00 2001 From: khaledsherkawi Date: Sun, 22 Oct 2023 20:44:15 +0200 Subject: [PATCH] SW-3951 Implement the new laser cutter profile service --- octoprint_mrbeam/__init__.py | 61 +++++++++++++------ octoprint_mrbeam/filemanager/__init__.py | 2 +- octoprint_mrbeam/iobeam/lid_handler.py | 2 +- .../iobeam/temperature_manager.py | 4 +- octoprint_mrbeam/printing/comm_acc2.py | 5 +- octoprint_mrbeam/printing/printer.py | 6 +- 6 files changed, 55 insertions(+), 25 deletions(-) diff --git a/octoprint_mrbeam/__init__.py b/octoprint_mrbeam/__init__.py index d47dbdec3..7557b00bb 100644 --- a/octoprint_mrbeam/__init__.py +++ b/octoprint_mrbeam/__init__.py @@ -112,6 +112,8 @@ from octoprint_mrbeam.util import get_thread from octoprint_mrbeam import camera from octoprint_mrbeam.util.version_comparator import compare_pep440_versions +from octoprint_mrbeam.constant.profile import laser_cutter +from octoprint_mrbeam.enums.laser_cutter_mode import LaserCutterModeEnum # this is a easy&simple way to access the plugin and all injections everywhere within the plugin __builtin__._mrbeam_plugin_implementation = None @@ -170,6 +172,8 @@ class MrBeamPlugin( RESTART_OCTOPRINT_CMD = "sudo systemctl restart octoprint.service" def __init__(self): + self._laser_cutter_mode_service = None + self.laser_cutter_profile_service = None self.mrbeam_plugin_initialized = False self._shutting_down = False self._slicing_commands = dict() @@ -199,9 +203,7 @@ def __init__(self): # Create the ``laserCutterProfileManager`` early to inject into the ``Laser`` # See ``laser_factory`` - self.laserCutterProfileManager = laserCutterProfileManager( - profile_id=self._device_info.get_type() - ) + self.laser_cutter_profile_service = laser_cutter_profile_service() self._boot_grace_period_counter = 0 @@ -218,9 +220,6 @@ def __init__(self): # Jinja custom filters need to be loaded already on instance creation FilterLoader.load_custom_jinja_filters() - # Initialize the laser cutter mode service attribute - self._laser_cutter_mode_service = None - # inside initialize() OctoPrint is already loaded, not assured during __init__()! def initialize(self): self._plugin_version = __version__ @@ -299,11 +298,39 @@ def initialize(self): self._octoprint_connectivity_checker = self._connectivity_checker self._connectivity_checker = ConnectivityChecker(self) + # Initialize the laser cutter mode service + self._laser_cutter_mode_service = laser_cutter_mode_service(self) + + # Update the laser cutter profile service based on the detected mode + self.update_laser_cutter_profile_service() + # Try to connect again as the laser cutter profile might have changed + # This will disconnect then connect in case of a current connection + self._try_to_connect_laser() + self._do_initial_log() self._printer.register_user_notification_system(self.user_notification_system) - # Initialize the laser cutter mode service - self._laser_cutter_mode_service = laser_cutter_mode_service(self) + def update_laser_cutter_profile_service(self): + corresponding_laser_cutter_profile = self.get_corresponding_laser_cutter_profile() + corresponding_laser_cutter_profile_id = corresponding_laser_cutter_profile['id'] + if self.laser_cutter_profile_service.exists(corresponding_laser_cutter_profile_id): + self._logger.info("Laser cutter profile already exists.") + else: + self._logger.info("Laser cutter profile does not exist. Creating it.") + self.laser_cutter_profile_service.save(corresponding_laser_cutter_profile) + self.laser_cutter_profile_service.select(corresponding_laser_cutter_profile_id) + + def get_corresponding_laser_cutter_profile(self): + if self.get_laser_cutter_mode() == LaserCutterModeEnum.DEFAULT.value and self._device_info.get_series() != "2C": + return laser_cutter.profile_1.profile + elif self.get_laser_cutter_mode() == LaserCutterModeEnum.DEFAULT.value and self._device_info.get_series() == "2C": + return dict_merge(laser_cutter.profile_1.profile, laser_cutter.profile_2.profile) + elif self.get_laser_cutter_mode() == LaserCutterModeEnum.ROTARY.value and self._device_info.get_series() != "2C": + return dict_merge(laser_cutter.profile_1.profile, laser_cutter.profile_3.profile) + elif self.get_laser_cutter_mode() == LaserCutterModeEnum.ROTARY.value and self._device_info.get_series() == "2C": + return dict_merge(laser_cutter.profile_1.profile, dict_merge(laser_cutter.profile_2.profile, laser_cutter.profile_3.profile)) + else: + return laser_cutter.profile_1.profile def get_settings(self): return self._settings @@ -343,7 +370,7 @@ def _try_to_connect_laser(self): and self._laserhead_ready and self._printer.is_closed_or_error() ): - self._printer.connect() + self._printer.connect(profile=self.laser_cutter_profile_service.get_current_or_default()) def _init_frontend_logger(self): handler = logging.handlers.RotatingFileHandler( @@ -383,7 +410,7 @@ def _do_initial_log(self): msg = ( "MrBeam Lasercutter Profile: %s" - % self.laserCutterProfileManager.get_current_or_default() + % self.laser_cutter_profile_service.get_current_or_default() ) self._logger.info(msg, terminal=True) self._frontend_logger.info(msg) @@ -597,7 +624,7 @@ def on_settings_load(self): fps=self._settings.get(["leds", "fps"]), ), isFirstRun=self.isFirstRun(), - laser_cutter_mode=self._settings.get(["laser_cutter_mode"]), + laser_cutter_mode=self.get_laser_cutter_mode(), ) def on_settings_save(self, data): @@ -863,7 +890,7 @@ def on_ui_render(self, now, request, render_kwargs): enable_accesscontrol and self._user_manager.hasBeenCustomized() ) - selectedProfile = self.laserCutterProfileManager.get_current_or_default() + selectedProfile = self.laser_cutter_profile_service.get_current_or_default() enable_focus = selectedProfile["focus"] safety_glasses = selectedProfile["glasses"] # render_kwargs["templates"]["settings"]["entries"]["serial"][1]["template"] = "settings/serialconnection.jinja2" @@ -1621,7 +1648,7 @@ def printLabel(self): ) @restricted_access_or_calibration_tool_mode def engraveCalibrationMarkers(self, intensity, feedrate): - profile = self.laserCutterProfileManager.get_current_or_default() + profile = self.laser_cutter_profile_service.get_current_or_default() try: i = int(int(intensity) / 100.0 * JobParams.Max.INTENSITY) f = int(feedrate) @@ -1710,7 +1737,7 @@ def generateCalibrationMarkersSvg(self): """Used from the calibration screen to engrave the calibration markers.""" # TODO mv this func to other file - profile = self.laserCutterProfileManager.get_current_or_default() + profile = self.laser_cutter_profile_service.get_current_or_default() cm = CalibrationMarker( str(profile["volume"]["width"]), str(profile["volume"]["depth"]) ) @@ -2103,7 +2130,7 @@ def on_api_command(self, command, data): parse_csv( device_model=self.get_model_id(), laserhead_model=self.get_current_laser_head_model(), - laser_cutter_mode=self.get_laser_cutter_mode(), + laser_cutter_mode = self.get_laser_cutter_mode() if self.get_laser_cutter_mode() is not None else LaserCutterModeEnum.DEFAULT.value, ) ), 200, @@ -2590,7 +2617,7 @@ def is_job_cancelled(): is_job_cancelled() # check before conversion started - profile = self.laserCutterProfileManager.get_current_or_default() + profile = self.laser_cutter_profile_service.get_current_or_default() maxWidth = profile["volume"]["width"] maxHeight = profile["volume"]["depth"] @@ -2819,7 +2846,7 @@ def laser_factory(self, components, *args, **kwargs): return Laser( components["file_manager"], components["analysis_queue"], - self.laserCutterProfileManager, + self.laser_cutter_profile_service, ) def laser_filemanager(self, *args, **kwargs): diff --git a/octoprint_mrbeam/filemanager/__init__.py b/octoprint_mrbeam/filemanager/__init__.py index de259c9d9..c4e5957db 100644 --- a/octoprint_mrbeam/filemanager/__init__.py +++ b/octoprint_mrbeam/filemanager/__init__.py @@ -50,7 +50,7 @@ def __init__(self, plugin): self, self._plugin._analysis_queue, self._plugin._slicing_manager, - self._plugin.laserCutterProfileManager, + self._plugin.laser_cutter_profile_service, initial_storage_managers=storage_managers, ) diff --git a/octoprint_mrbeam/iobeam/lid_handler.py b/octoprint_mrbeam/iobeam/lid_handler.py index f553e78a8..3f16256a4 100644 --- a/octoprint_mrbeam/iobeam/lid_handler.py +++ b/octoprint_mrbeam/iobeam/lid_handler.py @@ -93,7 +93,7 @@ def __init__(self, plugin): self._printer = plugin._printer self._plugin_manager = plugin._plugin_manager self._laserCutterProfile = ( - plugin.laserCutterProfileManager.get_current_or_default() + plugin.laser_cutter_profile_service.get_current_or_default() ) self._logger = mrb_logger( "octoprint.plugins.mrbeam.iobeam.lidhandler", logging.INFO diff --git a/octoprint_mrbeam/iobeam/temperature_manager.py b/octoprint_mrbeam/iobeam/temperature_manager.py index 286002275..bc49ebe4c 100644 --- a/octoprint_mrbeam/iobeam/temperature_manager.py +++ b/octoprint_mrbeam/iobeam/temperature_manager.py @@ -55,7 +55,7 @@ def __init__(self, plugin, laser): self._plugin.laserhead_handler.current_laserhead_high_temperature_warn_offset ) self.cooling_duration = ( - plugin.laserCutterProfileManager.get_current_or_default()["laser"][ + plugin.laser_cutter_profile_service.get_current_or_default()["laser"][ "cooling_duration" ] ) @@ -147,7 +147,7 @@ def reset(self, kwargs): self._plugin.laserhead_handler.current_laserhead_high_temperature_warn_offset ) self.cooling_duration = ( - self._plugin.laserCutterProfileManager.get_current_or_default()["laser"][ + self._plugin.laser_cutter_profile_service.get_current_or_default()["laser"][ "cooling_duration" ] ) diff --git a/octoprint_mrbeam/printing/comm_acc2.py b/octoprint_mrbeam/printing/comm_acc2.py index 80e6df29c..d84bd4179 100644 --- a/octoprint_mrbeam/printing/comm_acc2.py +++ b/octoprint_mrbeam/printing/comm_acc2.py @@ -40,6 +40,7 @@ from octoprint_mrbeam.util import dict_get from octoprint_mrbeam.util.cmd_exec import exec_cmd_output from octoprint_mrbeam.mrbeam_events import MrBeamEvents +from octoprint_mrbeam.service.profile.laser_cutter_profile import laser_cutter_profile_service ### MachineCom ######################################################################################################### @@ -164,7 +165,7 @@ def __init__( self._port = port self._baudrate = baudrate self._callback = callbackObject - self._laserCutterProfile = laserCutterProfileManager().get_current_or_default() + self._laserCutterProfile = printerProfileManager.get_current_or_default() self._state = self.STATE_NONE self._grbl_state = None @@ -1948,7 +1949,7 @@ def reset_grbl_auto_update_config(self): try: self._laserCutterProfile["grbl"]["auto_update_file"] = None self._laserCutterProfile["grbl"]["auto_update_version"] = None - laserCutterProfileManager().save( + laser_cutter_profile_service().save( self._laserCutterProfile, allow_overwrite=True ) except Exception: diff --git a/octoprint_mrbeam/printing/printer.py b/octoprint_mrbeam/printing/printer.py index b0546c1e5..fadbaf610 100644 --- a/octoprint_mrbeam/printing/printer.py +++ b/octoprint_mrbeam/printing/printer.py @@ -8,6 +8,7 @@ from octoprint_mrbeam.filemanager.analysis import beam_analysis_queue_factory from octoprint_mrbeam.util import dict_merge from octoprint_mrbeam.util.errors import ErrorCodes +from octoprint_mrbeam.service.profile.laser_cutter_profile import laser_cutter_profile_service class Laser(Printer): @@ -48,6 +49,7 @@ def __init__(self, fileManager, analysisQueue, printerProfileManager): current_z=None, ) self._user_notification_system = None + self._printerProfileManager = laser_cutter_profile_service() self._event_bus = eventManager() self._event_bus.subscribe( @@ -72,8 +74,8 @@ def connect(self, port=None, baudrate=None, profile=None): if self._comm is not None: self._comm.close() - eventManager().fire(Events.CONNECTING, payload=dict(profile=profile)) - self._printerProfileManager.select(profile) + eventManager().fire(Events.CONNECTING, payload=dict(profile=self._printerProfileManager.get_current_or_default()['id'])) + # self._printerProfileManager.select(profile) self._comm = comm.MachineCom( port, baudrate,