Skip to content

Commit 8b6d7cf

Browse files
JFSolutionahmed-mrbeam
authored andcommitted
Feat/sw 467 unify model id in analytics data(#1375)
1 parent a7e6288 commit 8b6d7cf

File tree

3 files changed

+68
-66
lines changed

3 files changed

+68
-66
lines changed

octoprint_mrbeam/analytics/analytics_handler.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,10 @@ def _subscribe(self):
558558
def _event_startup(self, event, payload):
559559
# Here the MrBeamPlugin is not fully initialized yet, so we have to access this data direct from the plugin
560560
payload = {
561-
ak.Device.LaserHead.SERIAL: self._plugin.laserhead_handler.get_current_used_lh_data()[
561+
ak.Device.LaserHead.LAST_USED_SERIAL: self._plugin.laserhead_handler.get_current_used_lh_data()[
562562
"serial"
563563
],
564-
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()[
565-
"model"
566-
],
564+
ak.Device.LaserHead.LAST_USED_HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
567565
ak.Device.Usage.USERS: len(self._plugin._user_manager._users),
568566
}
569567
self._add_device_event(ak.Device.Event.STARTUP, payload=payload)
@@ -875,9 +873,7 @@ def _add_collector_details(self):
875873
ak.Device.LaserHead.SERIAL: self._laserhead_handler.get_current_used_lh_data()[
876874
"serial"
877875
],
878-
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()[
879-
"model"
880-
],
876+
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
881877
}
882878

883879
if self._current_dust_collector:
@@ -913,7 +909,7 @@ def _init_new_job(self):
913909
self._current_job_id = "j_{}_{}".format(self._snr, time.time())
914910
# fmt: off
915911
payload = {
916-
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_data()["model"],
912+
ak.Device.LaserHead.HEAD_MODEL_ID: self._plugin.laserhead_handler.get_current_used_lh_model_id(),
917913
}
918914
# fmt: on
919915
self._add_job_event(ak.Job.Event.LASERJOB_STARTED, payload=payload)

octoprint_mrbeam/analytics/analytics_keys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ class LaserHead:
132132
POWER_75 = "p_75"
133133
POWER_85 = "p_85"
134134
TARGET_POWER = "target_power"
135-
HEAD_MODEL_ID = "head_model_id"
135+
HEAD_MODEL_ID = "laserhead_model_id"
136136
CORRECTION_FACTOR = "correction_factor"
137137
CORRECTION_ENABLED = "correction_enabled"
138138
CORRECTION_OVERRIDE = "correction_override"
139+
LAST_USED_HEAD_MODEL_ID = "last_used_laserhead_model_id"
140+
LAST_USED_SERIAL = "last_used_laserhead_serial"
139141

140142
class Grbl:
141143
FROM_VERSION = "from_version"

octoprint_mrbeam/iobeam/laserhead_handler.py

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class LaserheadHandler(object):
1919
LASER_POWER_GOAL_DEFAULT = 950
2020
LASER_POWER_GOAL_MAX = 1300
2121
LASERHEAD_SERIAL_REGEXP = re.compile("^[0-9a-f-]{36}$")
22+
_LASERHEAD_MODEL_STRING_MAP = {
23+
"0": '0', # dreamcut, mrbeam2 and mrbeam laserheads
24+
"1": 'S', # dreamcut[S] laserhead
25+
}
2226

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

3034
self._lh_cache = {}
3135
self._last_used_lh_serial = None
32-
self._last_used_lh_model = None
36+
self._last_used_lh_model_id = None
3337
self._correction_settings = {}
3438
self._laser_heads_file = os.path.join(
3539
self._settings.getBaseFolder("base"),
@@ -38,53 +42,58 @@ def __init__(self, plugin):
3842
self._load_laser_heads_file() # Loads correction_settings, last_used_lh_serial and lh_cache
3943

4044
self._current_used_lh_serial = self._last_used_lh_serial
41-
self._current_used_lh_model = self._last_used_lh_model
42-
self._current_used_lh_model_id = None
45+
self._current_used_lh_model_id = self._last_used_lh_model_id
4346

4447
self._event_bus.subscribe(
4548
MrBeamEvents.MRB_PLUGIN_INITIALIZED, self._on_mrbeam_plugin_initialized
4649
)
4750

51+
@property
52+
def _current_used_lh_model_string(self):
53+
if str(self._current_used_lh_model_id) in self._LASERHEAD_MODEL_STRING_MAP:
54+
return self._LASERHEAD_MODEL_STRING_MAP.get(str(self._current_used_lh_model_id))
55+
else:
56+
raise ValueError("Unknown laserhead model ID {!r}".format(self._current_used_lh_model_id))
57+
58+
59+
4860
def _on_mrbeam_plugin_initialized(self, event, payload):
4961
self._analytics_handler = self._plugin.analytics_handler
5062

5163
def _get_lh_model(self, lh_data):
5264
try:
5365
read_model = lh_data["head"]["model"]
54-
self._current_used_lh_model_id = read_model
55-
if read_model == 1:
56-
model = "S"
57-
elif read_model == 0:
58-
model = 0
66+
if str(read_model) in self._LASERHEAD_MODEL_STRING_MAP:
67+
model = read_model
5968
else:
6069
model = 0
6170
self._logger.warn(
6271
"No valid Laserhead model found, assume it is model 0"
6372
)
6473
return model
65-
except:
74+
except Exception as e:
6675
self._logger.error(
67-
"Error for Laserhead model, no model found in laserhead data"
76+
"Error for Laserhead model, no model found in laserhead data: {}".format(e)
6877
)
6978

7079
def set_current_used_lh_data(self, lh_data):
7180
try:
7281
if self._valid_lh_data(lh_data):
7382
self._logger.info("Laserhead: %s", lh_data)
7483
self._current_used_lh_serial = lh_data["main"]["serial"]
75-
self._current_used_lh_model = self._get_lh_model(lh_data)
84+
self._current_used_lh_model_id = self._get_lh_model(lh_data)
7685
# fmt: off
77-
if (self._current_used_lh_serial != self._last_used_lh_serial) and self._last_used_lh_model is not None:
86+
if (self._current_used_lh_serial != self._last_used_lh_serial) and self._last_used_lh_model_id is not None:
7887
# fmt: on
79-
if self._current_used_lh_model == "S":
88+
if self._current_used_lh_model_id == 1:
8089
self._settings.set_boolean(["laserheadChanged"], True)
8190
self._settings.save()
8291
self._logger.info(
8392
"Laserhead changed: s/n:%s model:%s -> s/n:%s model:%s",
8493
self._last_used_lh_serial,
85-
self._last_used_lh_model,
94+
self._last_used_lh_model_id,
8695
self._current_used_lh_serial,
87-
self._current_used_lh_model,
96+
self._current_used_lh_model_id,
8897
)
8998
self._write_lh_data_to_cache(lh_data)
9099

@@ -96,7 +105,7 @@ def set_current_used_lh_data(self, lh_data):
96105
MrBeamEvents.LASER_HEAD_READ,
97106
dict(
98107
serial=self._current_used_lh_serial,
99-
model=self._current_used_lh_model,
108+
model=self._current_used_lh_model_id,
100109
),
101110
)
102111

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

110-
elif lh_data is {}:
119+
elif lh_data == {}:
111120
self._logger.warn("Received empty laser head data from iobeam.")
112121

113122
else:
114-
if lh_data.get("main", {}).get("serial", None) is None:
123+
if lh_data.get("main", {}).get("serial") is None:
115124
self._logger.exception(
116125
"Received invalid laser head data from iobeam - no serial number. Laser head dataset: {}".format(
117126
lh_data
118127
),
119128
analytics="received-no-lh-data",
120129
)
121130
elif (
122-
not lh_data.get("power_calibrations", None)
131+
not lh_data.get("power_calibrations")
123132
or not len(lh_data["power_calibrations"]) > 0
124-
or not lh_data["power_calibrations"][-1].get("power_65", None)
125-
or not lh_data["power_calibrations"][-1].get("power_75", None)
126-
or not lh_data["power_calibrations"][-1].get("power_85", None)
133+
or not lh_data["power_calibrations"][-1].get("power_65")
134+
or not lh_data["power_calibrations"][-1].get("power_75")
135+
or not lh_data["power_calibrations"][-1].get("power_85")
127136
):
128137
self._logger.exception(
129138
"Received invalid laser head data from iobeam - invalid power calibrations data: {}".format(
@@ -146,14 +155,14 @@ def set_current_used_lh_data(self, lh_data):
146155
def _valid_lh_data(self, lh_data):
147156
try:
148157
if (
149-
lh_data.get("main", None)
150-
and lh_data["main"].get("serial", None)
151-
and lh_data["head"].get("model", None) is not None
152-
and lh_data.get("power_calibrations", None)
158+
lh_data.get("main")
159+
and lh_data["main"].get("serial")
160+
and lh_data["head"].get("model") is not None
161+
and lh_data.get("power_calibrations")
153162
and len(lh_data["power_calibrations"]) > 0
154-
and lh_data["power_calibrations"][-1].get("power_65", None)
155-
and lh_data["power_calibrations"][-1].get("power_75", None)
156-
and lh_data["power_calibrations"][-1].get("power_85", None)
163+
and lh_data["power_calibrations"][-1].get("power_65")
164+
and lh_data["power_calibrations"][-1].get("power_75")
165+
and lh_data["power_calibrations"][-1].get("power_85")
157166
):
158167
return True
159168
else:
@@ -165,12 +174,12 @@ def _valid_lh_data(self, lh_data):
165174
def _valid_lh_data_backwards_compatibility(self, lh_data):
166175
try:
167176
if (
168-
lh_data.get("serial", None)
169-
and lh_data.get("calibration_data", None)
177+
lh_data.get("serial")
178+
and lh_data.get("calibration_data")
170179
and len(lh_data["calibration_data"]) > 0
171-
and lh_data["calibration_data"][-1].get("power_65", None)
172-
and lh_data["calibration_data"][-1].get("power_75", None)
173-
and lh_data["calibration_data"][-1].get("power_85", None)
180+
and lh_data["calibration_data"][-1].get("power_65")
181+
and lh_data["calibration_data"][-1].get("power_75")
182+
and lh_data["calibration_data"][-1].get("power_85")
174183
):
175184
return True
176185
else:
@@ -186,7 +195,7 @@ def get_current_used_lh_data(self):
186195
if self._current_used_lh_serial:
187196
data = dict(
188197
serial=self._current_used_lh_serial,
189-
model=self._current_used_lh_model,
198+
model=self._current_used_lh_model_string,
190199
info=self._lh_cache[self._current_used_lh_serial],
191200
)
192201
else:
@@ -221,9 +230,9 @@ def get_current_used_lh_model_id(self):
221230
def _validate_lh_serial(self, serial):
222231
try:
223232
return bool(self.LASERHEAD_SERIAL_REGEXP.match(serial))
224-
except:
225-
self.logger.exception(
226-
"_validate_lh_serial() Failed to validate serial due to exception. Serial: %s ",
233+
except Exception as e:
234+
self._logger.exception(
235+
"_validate_lh_serial() Failed to validate serial due to exception. Serial: {serial} e:{e}".format(serial=serial, e=e),
227236
serial,
228237
)
229238
return False
@@ -244,16 +253,16 @@ def _calculate_power_correction_factor(self):
244253
p_85 = None
245254
target_power = self.LASER_POWER_GOAL_DEFAULT
246255
if power_calibration:
247-
p_65 = power_calibration.get("power_65", None)
248-
p_75 = power_calibration.get("power_75", None)
249-
p_85 = power_calibration.get("power_85", None)
256+
p_65 = power_calibration.get("power_65")
257+
p_75 = power_calibration.get("power_75")
258+
p_85 = power_calibration.get("power_85")
250259
target_power = power_calibration.get(
251260
"target_power", self.LASER_POWER_GOAL_DEFAULT
252261
)
253262

254263
# laserhead model S fix for correction factor
255264
# TODO fix this GOAL_MAX problem for all laser heads in a separate issue SW-394
256-
if self._current_used_lh_model == "S":
265+
if self._current_used_lh_model_id == 1:
257266
if target_power < 0 or target_power >= p_85:
258267
self._logger.warn(
259268
"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(
@@ -321,21 +330,16 @@ def _load_laser_heads_file(self):
321330
data = yaml.safe_load(stream)
322331

323332
if data:
324-
if "laser_heads" in data:
325-
self._lh_cache = data["laser_heads"]
326-
327-
if "last_used_lh_serial" in data:
328-
self._last_used_lh_serial = data["last_used_lh_serial"]
329-
330-
if "last_used_lh_model" in data:
331-
self._last_used_lh_model = data["last_used_lh_model"]
332-
333-
if "correction_settings" in data:
334-
self._correction_settings = data["correction_settings"]
335-
except:
336-
self._logger.error(
333+
self._lh_cache = data.get("laser_heads")
334+
self._last_used_lh_serial = data.get("last_used_lh_serial")
335+
self._last_used_lh_model_id = data.get("last_used_lh_model_id")
336+
self._correction_settings = data.get("correction_settings")
337+
except IOError:
338+
self._logger.exception(
337339
"Can't read _laser_heads_file file: %s", self._laser_heads_file
338340
)
341+
else:
342+
self._logger.warn("The laser_heads.yaml file can't be found at this location {}".format(self._laser_heads_file))
339343

340344
def _write_laser_heads_file(self, file=None):
341345
self._logger.info("Writing to laser_heads.yaml...")
@@ -344,11 +348,11 @@ def _write_laser_heads_file(self, file=None):
344348
laser_heads=self._lh_cache,
345349
correction_settings=self._correction_settings,
346350
last_used_lh_serial=self._current_used_lh_serial,
347-
last_used_lh_model=self._current_used_lh_model,
351+
last_used_lh_model_id=self._current_used_lh_model_id,
348352
)
349353
file = self._laser_heads_file if file is None else file
350354
try:
351355
with open(file, "w") as outfile:
352356
yaml.safe_dump(data, outfile, default_flow_style=False)
353-
except:
357+
except IOError:
354358
self._logger.exception("Can't write file %s due to an exception: ", file)

0 commit comments

Comments
 (0)