Skip to content

Commit

Permalink
SW-3951 Implement the new laser cutter profile service
Browse files Browse the repository at this point in the history
  • Loading branch information
khaledsherkawi committed Oct 22, 2023
1 parent 0141b67 commit 1dcbf5c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 25 deletions.
61 changes: 44 additions & 17 deletions octoprint_mrbeam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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__
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"])
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion octoprint_mrbeam/filemanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
2 changes: 1 addition & 1 deletion octoprint_mrbeam/iobeam/lid_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions octoprint_mrbeam/iobeam/temperature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
)
Expand Down Expand Up @@ -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"
]
)
Expand Down
5 changes: 3 additions & 2 deletions octoprint_mrbeam/printing/comm_acc2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #########################################################################################################
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions octoprint_mrbeam/printing/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 1dcbf5c

Please sign in to comment.