Skip to content

Commit

Permalink
Merge pull request #1510 from mrbeam/v0.10.5-rc
Browse files Browse the repository at this point in the history
Minor bug fixes and enhancements
  • Loading branch information
khaledsherkawi authored Jun 21, 2022
2 parents e355d69 + ee3becc commit f39010a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
2 changes: 1 addition & 1 deletion octoprint_mrbeam/__version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.10.4-hotfix.1"
__version__ = "0.10.5"
38 changes: 30 additions & 8 deletions octoprint_mrbeam/analytics/usage_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import yaml

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


class UsageHandler(object):
MAX_DUST_FACTOR = 2.0
MIN_DUST_FACTOR = 0.5
MAX_DUST_VALUE = 0.5
MIN_DUST_VALUE = 0.2
Expand All @@ -40,12 +40,6 @@ def __init__(self, plugin):
self.start_ntp_synced = None

self._last_dust_value = None
self._dust_mapping_m = (self.MAX_DUST_FACTOR - self.MIN_DUST_FACTOR) / (
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
)
self._dust_mapping_b = (
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
)

analyticsfolder = os.path.join(
self._settings.getBaseFolder("base"),
Expand Down Expand Up @@ -78,12 +72,26 @@ def _on_mrbeam_plugin_initialized(self, event, payload):
self._laser_head_serial = self._lh["serial"]
else:
self._laser_head_serial = "no_serial"

self._calculate_dust_mapping()
self._init_missing_usage_data()
self.log_usage()

self._subscribe()

def _calculate_dust_mapping(self):
max_dust_factor = self._laserhead_handler.current_laserhead_max_dust_factor
self._dust_mapping_m = (max_dust_factor - self.MIN_DUST_FACTOR) / (
self.MAX_DUST_VALUE - self.MIN_DUST_VALUE
)
self._dust_mapping_b = (
self.MIN_DUST_FACTOR - self._dust_mapping_m * self.MIN_DUST_VALUE
)
self._logger.debug(
"new dust mapping -> {} - {} - {}".format(
max_dust_factor, self._dust_mapping_m, self._dust_mapping_b
)
)

def log_usage(self):
self._logger.info(
"Usage: total_usage: {}, pre-filter: {}, main filter: {}, current laser head: {}, mechanics: {}, compressor: {} - {}".format(
Expand Down Expand Up @@ -117,6 +125,9 @@ def _subscribe(self):
self._event_bus.subscribe(
MrBeamEvents.LASER_HEAD_READ, self.event_laser_head_read
)
self._plugin.iobeam.subscribe(
IoBeamValueEvents.LASERHEAD_CHANGED, self._event_laserhead_changed
)

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

self.write_usage_analytics(action="job_finished")

def _event_laserhead_changed(self, event):
"""
will be triggered if the laser head changed,
refreshes the laserhead max dust factor that will be used for the new laser head
Returns:
"""
self._logger.debug("Laserhead changed recalculate dust mapping")
self._calculate_dust_mapping()

def _set_time(self, job_duration):
if job_duration is not None and job_duration > 0.0:

Expand Down
64 changes: 62 additions & 2 deletions octoprint_mrbeam/iobeam/laserhead_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from octoprint_mrbeam.iobeam.iobeam_handler import IoBeamValueEvents

LASERHEAD_MAX_TEMP_FALLBACK = 55.0
LASERHEAD_MAX_DUST_FACTOR_FALLBACK = 3.0 # selected the highest factor

# singleton
_instance = None
Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(self, plugin):
self._event_bus = plugin._event_bus
self._plugin_version = plugin.get_plugin_version()
self._iobeam = None
self._laserhead_properties = {}

self._lh_cache = {}
self._last_used_lh_serial = None
Expand Down Expand Up @@ -443,7 +445,7 @@ def current_laserhead_max_temperature(self):
float: Laser head max temp
"""
current_laserhead_properties = self._load_current_laserhead_properties()
current_laserhead_properties = self._get_laserhead_properties()

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

def _load_current_laserhead_properties(self):
"""
Loads the current detected laser head related properties and return them
Loads the current detected laser head related properties from the laser head profile files and return them
Returns:
dict: current laser head properties, None: otherwise
Expand Down Expand Up @@ -505,7 +507,65 @@ def _load_current_laserhead_properties(self):
e, lh_properties_file_path))
return None

def _get_laserhead_properties(self):
"""
returns the current saved laser head properties or load new if the laser head id changed
Returns:
dict: current laser head properties, None: otherwise
"""
# 1. get the ID of the current laser head
laserhead_id = self.get_current_used_lh_model_id()
self._logger.debug("laserhead id compare {} - {}".format(laserhead_id, self._laserhead_properties.get("laserhead_id", None)))
if laserhead_id != self._laserhead_properties.get("laserhead_id", None):
self._logger.debug("new laserhead_id -> load current laserhead porperties")
# 2. Load the corresponding yaml file and return it's content
self._laserhead_properties = self._load_current_laserhead_properties()
if self._laserhead_properties is not None:
self._laserhead_properties.update({'laserhead_id': laserhead_id})
else:
self._logger.debug("no new laserhead_id -> return current laserhead_properties")

self._logger.debug(
"_laserhead_properties - {}".format(self._laserhead_properties))
return self._laserhead_properties

@property
def current_laserhead_max_dust_factor(self):
"""
Return the current laser head max dust factor
Returns:
float: Laser head max dust factor
"""
current_laserhead_properties = self._get_laserhead_properties()

# Handle the exceptions
if ((isinstance(current_laserhead_properties, dict) is False) or
("max_dust_factor" not in current_laserhead_properties) or
(isinstance(current_laserhead_properties["max_dust_factor"], float) is False)):
# Apply fallback
self._logger.debug("Current laserhead properties: {}".format(current_laserhead_properties))
self._logger.exception(
"Current Laserhead max dust factor couldn't be retrieved, fallback to the factor value of: {}".format(
self.default_laserhead_max_dust_factor))
return self.default_laserhead_max_dust_factor
# Reaching here means, everything looks good
self._logger.debug("Current Laserhead max dust factor:{}".format(current_laserhead_properties["max_dust_factor"]))
return current_laserhead_properties["max_dust_factor"]

@property
def default_laserhead_max_dust_factor(self):
"""
Default max dust factor for laser head. to be used by other modules at init time
Returns:
float: Laser head default max dust factor
"""

return LASERHEAD_MAX_DUST_FACTOR_FALLBACK



1 change: 1 addition & 0 deletions octoprint_mrbeam/profiles/laserhead/laserhead_id_0.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
max_temperature: 55.0
max_dust_factor: 2.0
1 change: 1 addition & 0 deletions octoprint_mrbeam/profiles/laserhead/laserhead_id_1.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
max_temperature: 59.0
max_dust_factor: 3.0

0 comments on commit f39010a

Please sign in to comment.