33import re
44from octoprint_mrbeam .mrb_logger import mrb_logger
55from octoprint_mrbeam .mrbeam_events import MrBeamEvents
6+ from octoprint_mrbeam .iobeam .iobeam_handler import IoBeamValueEvents
7+
8+ LASERHEAD_MAX_TEMP_FALLBACK = 55.0
69
710# singleton
811_instance = None
@@ -30,6 +33,7 @@ def __init__(self, plugin):
3033 self ._settings = plugin ._settings
3134 self ._event_bus = plugin ._event_bus
3235 self ._plugin_version = plugin .get_plugin_version ()
36+ self ._iobeam = None
3337
3438 self ._lh_cache = {}
3539 self ._last_used_lh_serial = None
@@ -74,6 +78,7 @@ def _on_mrbeam_plugin_initialized(self, event, payload):
7478 _ = event
7579 _ = payload
7680 self ._analytics_handler = self ._plugin .analytics_handler
81+ self ._iobeam = self ._plugin .iobeam
7782
7883 def _get_lh_model (self , lh_data ):
7984 try :
@@ -92,19 +97,25 @@ def _get_lh_model(self, lh_data):
9297 )
9398
9499 def set_current_used_lh_data (self , lh_data ):
100+ laser_head_model_changed = False
101+
95102 try :
96103 if self ._valid_lh_data (lh_data ):
97104 self ._current_used_lh_serial = lh_data ["main" ]["serial" ]
98105 self ._current_used_lh_model_id = self ._get_lh_model (lh_data )
99106 self ._current_used_lh_model = self ._LASERHEAD_MODEL_STRING_MAP [str (self ._current_used_lh_model_id )]
100107 # fmt: off
108+ if (self ._current_used_lh_model_id != self ._last_used_lh_model_id ) \
109+ and self ._current_used_lh_model_id is not None :
110+ laser_head_model_changed = True
111+
101112 if (self ._current_used_lh_serial != self ._last_used_lh_serial ) and self ._last_used_lh_model_id is not None :
102113 # fmt: on
103114 if self ._current_used_lh_model_id == 1 :
104115 self ._settings .set_boolean (["laserheadChanged" ], True )
105116 self ._settings .save ()
106117 self ._logger .info (
107- "Laserhead changed: s/n:%s model:%s -> s/n:%s model:%s" ,
118+ "laserhead_handler: Laserhead changed: s/n:%s model:%s -> s/n:%s model:%s" ,
108119 self ._last_used_lh_serial ,
109120 self ._last_used_lh_model_id ,
110121 self ._current_used_lh_serial ,
@@ -124,6 +135,21 @@ def set_current_used_lh_data(self, lh_data):
124135 ),
125136 )
126137
138+ if laser_head_model_changed :
139+ # Now all the information about the new laser head should be present Thus we can fire this event
140+ self ._logger .info (
141+ "laserhead_handler: Laserhead Model changed: s/n:%s model:%s -> s/n:%s model:%s" ,
142+ self ._last_used_lh_serial ,
143+ self ._last_used_lh_model_id ,
144+ self ._current_used_lh_serial ,
145+ self ._current_used_lh_model_id ,
146+ )
147+ # Fire the event
148+ self ._iobeam ._call_callback (
149+ IoBeamValueEvents .LASERHEAD_CHANGED ,
150+ "Laserhead Model changed" ,
151+ )
152+
127153 # BACKWARD_COMPATIBILITY: This is for detecting mrb_hw_info v0.0.20
128154 # This part of the code should never by reached, if reached then this means an update for mrb_hw_info did
129155 # fail TODO Remove this part of the code later
@@ -407,3 +433,79 @@ def _write_laser_heads_file(self, laser_heads_file=None):
407433 self ._logger .info ("Writing to file: {} ..is successful!" .format (laser_heads_file ))
408434 except IOError as e :
409435 self ._logger .error ("Can't open file: {} , Due to error: {}: " .format (laser_heads_file , e ))
436+
437+ @property
438+ def current_laserhead_max_temperature (self ):
439+ """
440+ Return the current laser head max temperature
441+
442+ Returns:
443+ float: Laser head max temp
444+
445+ """
446+ current_laserhead_properties = self ._load_current_laserhead_properties ()
447+
448+ # Handle the exceptions
449+ if ((isinstance (current_laserhead_properties , dict ) is False ) or
450+ ("max_temperature" not in current_laserhead_properties ) or
451+ (isinstance (current_laserhead_properties ["max_temperature" ], float ) is False )):
452+ # Apply fallback
453+ self ._logger .debug ("Current laserhead properties: {}" .format (current_laserhead_properties ))
454+ self ._logger .exception (
455+ "Current Laserhead Max temp couldn't be retrieved, fallback to the temperature value of: {}" .format (
456+ self .default_laserhead_max_temperature ))
457+ return self .default_laserhead_max_temperature
458+ # Reaching here means, everything looks good
459+ self ._logger .debug ("Current Laserhead Max temp:{}" .format (current_laserhead_properties ["max_temperature" ]))
460+ return current_laserhead_properties ["max_temperature" ]
461+
462+ @property
463+ def default_laserhead_max_temperature (self ):
464+ """
465+ Default max temperature for laser head. to be used by other modules at init time
466+
467+ Returns:
468+ float: Laser head default max temp
469+ """
470+
471+ return LASERHEAD_MAX_TEMP_FALLBACK
472+
473+ def _load_current_laserhead_properties (self ):
474+ """
475+ Loads the current detected laser head related properties and return them
476+
477+ Returns:
478+ dict: current laser head properties, None: otherwise
479+
480+ """
481+ # 1. get the ID of the current laser head
482+ laserhead_id = self .get_current_used_lh_model_id ()
483+
484+ # 2. Load the corresponding yaml file and return it's content
485+ lh_properties_file_path = os .path .join (self ._plugin ._basefolder ,
486+ "profiles" , "laserhead" , "laserhead_id_{}.yaml" .format (laserhead_id ))
487+ if not os .path .isfile (lh_properties_file_path ):
488+ self ._logger .exception (
489+ "properties file for current laser head ID: {} doesn't exist or path is invalid. Path: {}" .format (
490+ laserhead_id , lh_properties_file_path ))
491+ return None
492+
493+ self ._logger .debug (
494+ "properties file for current laser head ID: {} exists. Path:{}" .format (
495+ laserhead_id , lh_properties_file_path ) )
496+ try :
497+ with open (lh_properties_file_path ) as lh_properties_yaml_file :
498+ self ._logger .debug (
499+ "properties file for current laser head ID: {} opened successfully" .format (
500+ laserhead_id ))
501+ return yaml .safe_load (lh_properties_yaml_file )
502+ except (IOError , yaml .YAMLError ) as e :
503+ self ._logger .exception (
504+ "Exception: {} while Opening or loading the properties file for current laser head. Path: {}" .format (
505+ e , lh_properties_file_path ))
506+ return None
507+
508+
509+
510+
511+
0 commit comments