Skip to content

Commit

Permalink
Feat/sw 467 unify model id in analytics data(#1375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-MrBeam authored Oct 1, 2021
1 parent 50f755f commit c49d8eb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 66 deletions.
12 changes: 4 additions & 8 deletions octoprint_mrbeam/analytics/analytics_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,10 @@ def _subscribe(self):
def _event_startup(self, event, payload):
# Here the MrBeamPlugin is not fully initialized yet, so we have to access this data direct from the plugin
payload = {
ak.Device.LaserHead.SERIAL: self._plugin.laserhead_handler.get_current_used_lh_data()[
ak.Device.LaserHead.LAST_USED_SERIAL: self._plugin.laserhead_handler.get_current_used_lh_data()[
"serial"
],
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()[
"model"
],
ak.Device.LaserHead.LAST_USED_HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
ak.Device.Usage.USERS: len(self._plugin._user_manager._users),
}
self._add_device_event(ak.Device.Event.STARTUP, payload=payload)
Expand Down Expand Up @@ -875,9 +873,7 @@ def _add_collector_details(self):
ak.Device.LaserHead.SERIAL: self._laserhead_handler.get_current_used_lh_data()[
"serial"
],
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()[
"model"
],
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
}

if self._current_dust_collector:
Expand Down Expand Up @@ -913,7 +909,7 @@ def _init_new_job(self):
self._current_job_id = "j_{}_{}".format(self._snr, time.time())
# fmt: off
payload = {
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()["model"],
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
}
# fmt: on
self._add_job_event(ak.Job.Event.LASERJOB_STARTED, payload=payload)
Expand Down
4 changes: 3 additions & 1 deletion octoprint_mrbeam/analytics/analytics_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ class LaserHead:
POWER_75 = "p_75"
POWER_85 = "p_85"
TARGET_POWER = "target_power"
HEAD_MODEL_ID = "head_model_id"
HEAD_MODEL_ID = "laserhead_model_id"
CORRECTION_FACTOR = "correction_factor"
CORRECTION_ENABLED = "correction_enabled"
CORRECTION_OVERRIDE = "correction_override"
LAST_USED_HEAD_MODEL_ID = "last_used_laserhead_model_id"
LAST_USED_SERIAL = "last_used_laserhead_serial"

class Grbl:
FROM_VERSION = "from_version"
Expand Down
118 changes: 61 additions & 57 deletions octoprint_mrbeam/iobeam/laserhead_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class LaserheadHandler(object):
LASER_POWER_GOAL_DEFAULT = 950
LASER_POWER_GOAL_MAX = 1300
LASERHEAD_SERIAL_REGEXP = re.compile("^[0-9a-f-]{36}$")
_LASERHEAD_MODEL_STRING_MAP = {
"0": '0', # dreamcut, mrbeam2 and mrbeam laserheads
"1": 'S', # dreamcut[S] laserhead
}

def __init__(self, plugin):
self._logger = mrb_logger("octoprint.plugins.mrbeam.iobeam.laserhead")
Expand All @@ -29,7 +33,7 @@ def __init__(self, plugin):

self._lh_cache = {}
self._last_used_lh_serial = None
self._last_used_lh_model = None
self._last_used_lh_model_id = None
self._correction_settings = {}
self._laser_heads_file = os.path.join(
self._settings.getBaseFolder("base"),
Expand All @@ -38,53 +42,58 @@ def __init__(self, plugin):
self._load_laser_heads_file() # Loads correction_settings, last_used_lh_serial and lh_cache

self._current_used_lh_serial = self._last_used_lh_serial
self._current_used_lh_model = self._last_used_lh_model
self._current_used_lh_model_id = None
self._current_used_lh_model_id = self._last_used_lh_model_id

self._event_bus.subscribe(
MrBeamEvents.MRB_PLUGIN_INITIALIZED, self._on_mrbeam_plugin_initialized
)

@property
def _current_used_lh_model_string(self):
if str(self._current_used_lh_model_id) in self._LASERHEAD_MODEL_STRING_MAP:
return self._LASERHEAD_MODEL_STRING_MAP.get(str(self._current_used_lh_model_id))
else:
raise ValueError("Unknown laserhead model ID {!r}".format(self._current_used_lh_model_id))



def _on_mrbeam_plugin_initialized(self, event, payload):
self._analytics_handler = self._plugin.analytics_handler

def _get_lh_model(self, lh_data):
try:
read_model = lh_data["head"]["model"]
self._current_used_lh_model_id = read_model
if read_model == 1:
model = "S"
elif read_model == 0:
model = 0
if str(read_model) in self._LASERHEAD_MODEL_STRING_MAP:
model = read_model
else:
model = 0
self._logger.warn(
"No valid Laserhead model found, assume it is model 0"
)
return model
except:
except Exception as e:
self._logger.error(
"Error for Laserhead model, no model found in laserhead data"
"Error for Laserhead model, no model found in laserhead data: {}".format(e)
)

def set_current_used_lh_data(self, lh_data):
try:
if self._valid_lh_data(lh_data):
self._logger.info("Laserhead: %s", lh_data)
self._current_used_lh_serial = lh_data["main"]["serial"]
self._current_used_lh_model = self._get_lh_model(lh_data)
self._current_used_lh_model_id = self._get_lh_model(lh_data)
# fmt: off
if (self._current_used_lh_serial != self._last_used_lh_serial) and self._last_used_lh_model is not None:
if (self._current_used_lh_serial != self._last_used_lh_serial) and self._last_used_lh_model_id is not None:
# fmt: on
if self._current_used_lh_model == "S":
if self._current_used_lh_model_id == 1:
self._settings.set_boolean(["laserheadChanged"], True)
self._settings.save()
self._logger.info(
"Laserhead changed: s/n:%s model:%s -> s/n:%s model:%s",
self._last_used_lh_serial,
self._last_used_lh_model,
self._last_used_lh_model_id,
self._current_used_lh_serial,
self._current_used_lh_model,
self._current_used_lh_model_id,
)
self._write_lh_data_to_cache(lh_data)

Expand All @@ -96,7 +105,7 @@ def set_current_used_lh_data(self, lh_data):
MrBeamEvents.LASER_HEAD_READ,
dict(
serial=self._current_used_lh_serial,
model=self._current_used_lh_model,
model=self._current_used_lh_model_id,
),
)

Expand All @@ -107,23 +116,23 @@ def set_current_used_lh_data(self, lh_data):
"Received old laser head data from iobeam.", analytics=True
)

elif lh_data is {}:
elif lh_data == {}:
self._logger.warn("Received empty laser head data from iobeam.")

else:
if lh_data.get("main", {}).get("serial", None) is None:
if lh_data.get("main", {}).get("serial") is None:
self._logger.exception(
"Received invalid laser head data from iobeam - no serial number. Laser head dataset: {}".format(
lh_data
),
analytics="received-no-lh-data",
)
elif (
not lh_data.get("power_calibrations", None)
not lh_data.get("power_calibrations")
or not len(lh_data["power_calibrations"]) > 0
or not lh_data["power_calibrations"][-1].get("power_65", None)
or not lh_data["power_calibrations"][-1].get("power_75", None)
or not lh_data["power_calibrations"][-1].get("power_85", None)
or not lh_data["power_calibrations"][-1].get("power_65")
or not lh_data["power_calibrations"][-1].get("power_75")
or not lh_data["power_calibrations"][-1].get("power_85")
):
self._logger.exception(
"Received invalid laser head data from iobeam - invalid power calibrations data: {}".format(
Expand All @@ -146,14 +155,14 @@ def set_current_used_lh_data(self, lh_data):
def _valid_lh_data(self, lh_data):
try:
if (
lh_data.get("main", None)
and lh_data["main"].get("serial", None)
and lh_data["head"].get("model", None) is not None
and lh_data.get("power_calibrations", None)
lh_data.get("main")
and lh_data["main"].get("serial")
and lh_data["head"].get("model") is not None
and lh_data.get("power_calibrations")
and len(lh_data["power_calibrations"]) > 0
and lh_data["power_calibrations"][-1].get("power_65", None)
and lh_data["power_calibrations"][-1].get("power_75", None)
and lh_data["power_calibrations"][-1].get("power_85", None)
and lh_data["power_calibrations"][-1].get("power_65")
and lh_data["power_calibrations"][-1].get("power_75")
and lh_data["power_calibrations"][-1].get("power_85")
):
return True
else:
Expand All @@ -165,12 +174,12 @@ def _valid_lh_data(self, lh_data):
def _valid_lh_data_backwards_compatibility(self, lh_data):
try:
if (
lh_data.get("serial", None)
and lh_data.get("calibration_data", None)
lh_data.get("serial")
and lh_data.get("calibration_data")
and len(lh_data["calibration_data"]) > 0
and lh_data["calibration_data"][-1].get("power_65", None)
and lh_data["calibration_data"][-1].get("power_75", None)
and lh_data["calibration_data"][-1].get("power_85", None)
and lh_data["calibration_data"][-1].get("power_65")
and lh_data["calibration_data"][-1].get("power_75")
and lh_data["calibration_data"][-1].get("power_85")
):
return True
else:
Expand All @@ -186,7 +195,7 @@ def get_current_used_lh_data(self):
if self._current_used_lh_serial:
data = dict(
serial=self._current_used_lh_serial,
model=self._current_used_lh_model,
model=self._current_used_lh_model_string,
info=self._lh_cache[self._current_used_lh_serial],
)
else:
Expand Down Expand Up @@ -221,9 +230,9 @@ def get_current_used_lh_model_id(self):
def _validate_lh_serial(self, serial):
try:
return bool(self.LASERHEAD_SERIAL_REGEXP.match(serial))
except:
self.logger.exception(
"_validate_lh_serial() Failed to validate serial due to exception. Serial: %s ",
except Exception as e:
self._logger.exception(
"_validate_lh_serial() Failed to validate serial due to exception. Serial: {serial} e:{e}".format(serial=serial, e=e),
serial,
)
return False
Expand All @@ -244,16 +253,16 @@ def _calculate_power_correction_factor(self):
p_85 = None
target_power = self.LASER_POWER_GOAL_DEFAULT
if power_calibration:
p_65 = power_calibration.get("power_65", None)
p_75 = power_calibration.get("power_75", None)
p_85 = power_calibration.get("power_85", None)
p_65 = power_calibration.get("power_65")
p_75 = power_calibration.get("power_75")
p_85 = power_calibration.get("power_85")
target_power = power_calibration.get(
"target_power", self.LASER_POWER_GOAL_DEFAULT
)

# laserhead model S fix for correction factor
# TODO fix this GOAL_MAX problem for all laser heads in a separate issue SW-394
if self._current_used_lh_model == "S":
if self._current_used_lh_model_id == 1:
if target_power < 0 or target_power >= p_85:
self._logger.warn(
"Laserhead target_power ({target}) over p_85 ({p_85}) => target_power will be set to GOAL_DEFAULT ({default}) for the calculation of the correction factor".format(
Expand Down Expand Up @@ -321,21 +330,16 @@ def _load_laser_heads_file(self):
data = yaml.safe_load(stream)

if data:
if "laser_heads" in data:
self._lh_cache = data["laser_heads"]

if "last_used_lh_serial" in data:
self._last_used_lh_serial = data["last_used_lh_serial"]

if "last_used_lh_model" in data:
self._last_used_lh_model = data["last_used_lh_model"]

if "correction_settings" in data:
self._correction_settings = data["correction_settings"]
except:
self._logger.error(
self._lh_cache = data.get("laser_heads")
self._last_used_lh_serial = data.get("last_used_lh_serial")
self._last_used_lh_model_id = data.get("last_used_lh_model_id")
self._correction_settings = data.get("correction_settings")
except IOError:
self._logger.exception(
"Can't read _laser_heads_file file: %s", self._laser_heads_file
)
else:
self._logger.warn("The laser_heads.yaml file can't be found at this location {}".format(self._laser_heads_file))

def _write_laser_heads_file(self, file=None):
self._logger.info("Writing to laser_heads.yaml...")
Expand All @@ -344,11 +348,11 @@ def _write_laser_heads_file(self, file=None):
laser_heads=self._lh_cache,
correction_settings=self._correction_settings,
last_used_lh_serial=self._current_used_lh_serial,
last_used_lh_model=self._current_used_lh_model,
last_used_lh_model_id=self._current_used_lh_model_id,
)
file = self._laser_heads_file if file is None else file
try:
with open(file, "w") as outfile:
yaml.safe_dump(data, outfile, default_flow_style=False)
except:
except IOError:
self._logger.exception("Can't write file %s due to an exception: ", file)

0 comments on commit c49d8eb

Please sign in to comment.