Skip to content

Commit f39010a

Browse files
Merge pull request mrbeam#1510 from mrbeam/v0.10.5-rc
Minor bug fixes and enhancements
2 parents e355d69 + ee3becc commit f39010a

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

octoprint_mrbeam/__version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.10.4-hotfix.1"
1+
__version__ = "0.10.5"

octoprint_mrbeam/analytics/usage_handler.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import yaml
44

5+
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamValueEvents
56
from octoprint_mrbeam.mrb_logger import mrb_logger
67
from octoprint.events import Events as OctoPrintEvents
78
from octoprint_mrbeam.mrbeam_events import MrBeamEvents
@@ -18,7 +19,6 @@ def usageHandler(plugin):
1819

1920

2021
class UsageHandler(object):
21-
MAX_DUST_FACTOR = 2.0
2222
MIN_DUST_FACTOR = 0.5
2323
MAX_DUST_VALUE = 0.5
2424
MIN_DUST_VALUE = 0.2
@@ -40,12 +40,6 @@ def __init__(self, plugin):
4040
self.start_ntp_synced = None
4141

4242
self._last_dust_value = None
43-
self._dust_mapping_m = (self.MAX_DUST_FACTOR - self.MIN_DUST_FACTOR) / (
44-
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
45-
)
46-
self._dust_mapping_b = (
47-
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
48-
)
4943

5044
analyticsfolder = os.path.join(
5145
self._settings.getBaseFolder("base"),
@@ -78,12 +72,26 @@ def _on_mrbeam_plugin_initialized(self, event, payload):
7872
self._laser_head_serial = self._lh["serial"]
7973
else:
8074
self._laser_head_serial = "no_serial"
81-
75+
self._calculate_dust_mapping()
8276
self._init_missing_usage_data()
8377
self.log_usage()
8478

8579
self._subscribe()
8680

81+
def _calculate_dust_mapping(self):
82+
max_dust_factor = self._laserhead_handler.current_laserhead_max_dust_factor
83+
self._dust_mapping_m = (max_dust_factor - self.MIN_DUST_FACTOR) / (
84+
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
85+
)
86+
self._dust_mapping_b = (
87+
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
88+
)
89+
self._logger.debug(
90+
"new dust mapping -> {} - {} - {}".format(
91+
max_dust_factor, self._dust_mapping_m, self._dust_mapping_b
92+
)
93+
)
94+
8795
def log_usage(self):
8896
self._logger.info(
8997
"Usage: total_usage: {}, pre-filter: {}, main filter: {}, current laser head: {}, mechanics: {}, compressor: {} - {}".format(
@@ -117,6 +125,9 @@ def _subscribe(self):
117125
self._event_bus.subscribe(
118126
MrBeamEvents.LASER_HEAD_READ, self.event_laser_head_read
119127
)
128+
self._plugin.iobeam.subscribe(
129+
IoBeamValueEvents.LASERHEAD_CHANGED, self._event_laserhead_changed
130+
)
120131

121132
def event_laser_head_read(self, event, payload):
122133
# Update laser head info if necessary --> Only update if there is a serial number different than the previous
@@ -164,6 +175,17 @@ def event_stop(self, event, payload):
164175

165176
self.write_usage_analytics(action="job_finished")
166177

178+
def _event_laserhead_changed(self, event):
179+
"""
180+
will be triggered if the laser head changed,
181+
refreshes the laserhead max dust factor that will be used for the new laser head
182+
183+
Returns:
184+
185+
"""
186+
self._logger.debug("Laserhead changed recalculate dust mapping")
187+
self._calculate_dust_mapping()
188+
167189
def _set_time(self, job_duration):
168190
if job_duration is not None and job_duration > 0.0:
169191

octoprint_mrbeam/iobeam/laserhead_handler.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamValueEvents
77

88
LASERHEAD_MAX_TEMP_FALLBACK = 55.0
9+
LASERHEAD_MAX_DUST_FACTOR_FALLBACK = 3.0 # selected the highest factor
910

1011
# singleton
1112
_instance = None
@@ -34,6 +35,7 @@ def __init__(self, plugin):
3435
self._event_bus = plugin._event_bus
3536
self._plugin_version = plugin.get_plugin_version()
3637
self._iobeam = None
38+
self._laserhead_properties = {}
3739

3840
self._lh_cache = {}
3941
self._last_used_lh_serial = None
@@ -443,7 +445,7 @@ def current_laserhead_max_temperature(self):
443445
float: Laser head max temp
444446
445447
"""
446-
current_laserhead_properties = self._load_current_laserhead_properties()
448+
current_laserhead_properties = self._get_laserhead_properties()
447449

448450
# Handle the exceptions
449451
if((isinstance(current_laserhead_properties, dict) is False) or
@@ -472,7 +474,7 @@ def default_laserhead_max_temperature(self):
472474

473475
def _load_current_laserhead_properties(self):
474476
"""
475-
Loads the current detected laser head related properties and return them
477+
Loads the current detected laser head related properties from the laser head profile files and return them
476478
477479
Returns:
478480
dict: current laser head properties, None: otherwise
@@ -505,7 +507,65 @@ def _load_current_laserhead_properties(self):
505507
e, lh_properties_file_path))
506508
return None
507509

510+
def _get_laserhead_properties(self):
511+
"""
512+
returns the current saved laser head properties or load new if the laser head id changed
513+
514+
Returns:
515+
dict: current laser head properties, None: otherwise
516+
517+
"""
518+
# 1. get the ID of the current laser head
519+
laserhead_id = self.get_current_used_lh_model_id()
520+
self._logger.debug("laserhead id compare {} - {}".format(laserhead_id, self._laserhead_properties.get("laserhead_id", None)))
521+
if laserhead_id != self._laserhead_properties.get("laserhead_id", None):
522+
self._logger.debug("new laserhead_id -> load current laserhead porperties")
523+
# 2. Load the corresponding yaml file and return it's content
524+
self._laserhead_properties = self._load_current_laserhead_properties()
525+
if self._laserhead_properties is not None:
526+
self._laserhead_properties.update({'laserhead_id': laserhead_id})
527+
else:
528+
self._logger.debug("no new laserhead_id -> return current laserhead_properties")
529+
530+
self._logger.debug(
531+
"_laserhead_properties - {}".format(self._laserhead_properties))
532+
return self._laserhead_properties
533+
534+
@property
535+
def current_laserhead_max_dust_factor(self):
536+
"""
537+
Return the current laser head max dust factor
538+
539+
Returns:
540+
float: Laser head max dust factor
541+
542+
"""
543+
current_laserhead_properties = self._get_laserhead_properties()
544+
545+
# Handle the exceptions
546+
if ((isinstance(current_laserhead_properties, dict) is False) or
547+
("max_dust_factor" not in current_laserhead_properties) or
548+
(isinstance(current_laserhead_properties["max_dust_factor"], float) is False)):
549+
# Apply fallback
550+
self._logger.debug("Current laserhead properties: {}".format(current_laserhead_properties))
551+
self._logger.exception(
552+
"Current Laserhead max dust factor couldn't be retrieved, fallback to the factor value of: {}".format(
553+
self.default_laserhead_max_dust_factor))
554+
return self.default_laserhead_max_dust_factor
555+
# Reaching here means, everything looks good
556+
self._logger.debug("Current Laserhead max dust factor:{}".format(current_laserhead_properties["max_dust_factor"]))
557+
return current_laserhead_properties["max_dust_factor"]
558+
559+
@property
560+
def default_laserhead_max_dust_factor(self):
561+
"""
562+
Default max dust factor for laser head. to be used by other modules at init time
563+
564+
Returns:
565+
float: Laser head default max dust factor
566+
"""
508567

568+
return LASERHEAD_MAX_DUST_FACTOR_FALLBACK
509569

510570

511571

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
max_temperature: 55.0
2+
max_dust_factor: 2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
max_temperature: 59.0
2+
max_dust_factor: 3.0

0 commit comments

Comments
 (0)