diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index f5a84fd1d..9677002ae 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -113,7 +113,7 @@ Groups will be as follows: """ regex_temp = re.compile( - r"(?PB|C|T(?P\d*)|([\w]+)):\s*(?P%s)(\s*\/?\s*(?P%s))?" + r"(^|\s)(?PB|C|T(?P\d*)|([\w]+)):\s*(?P%s)(\s*\/?\s*(?P%s))?" % (regex_float_pattern, regex_float_pattern) ) """Regex matching temperature entries in line. @@ -341,7 +341,7 @@ class PositionRecord: class TemperatureRecord: - RESERVED_IDENTIFIER_REGEX = re.compile("[0-9]+|[bc]") + RESERVED_IDENTIFIER_REGEX = re.compile(r"B|C|T\d*") def __init__(self): self._tools = {} @@ -366,7 +366,7 @@ class TemperatureRecord: self._chamber = self._to_new_tuple(current, actual, target) def set_custom(self, identifier, actual=None, target=None): - if self.RESERVED_IDENTIFIER_REGEX.match(identifier): + if self.RESERVED_IDENTIFIER_REGEX.fullmatch(identifier): raise ValueError(f"{identifier} is a reserved identifier") current = self._custom.get(identifier, (None, None)) self._custom[identifier] = self._to_new_tuple(current, actual, target) @@ -2103,7 +2103,7 @@ class MachineCom: def _processTemperatures(self, line): current_tool = self._currentTool if self._currentTool is not None else 0 - current_tool_key = "T%d" % current_tool + current_tool_key = f"T{current_tool}" maxToolNum, parsedTemps = parse_temperature_line(line, current_tool) maxToolNum = max( @@ -2159,10 +2159,15 @@ class MachineCom: del parsedTemps["C"] self.last_temperature.set_chamber(actual=actual, target=target) - # all other injected temperatures - for key in parsedTemps.keys(): - actual, target = parsedTemps[key] - self.last_temperature.set_custom(key, actual=actual, target=target) + # all other injected temperatures or temperature-like entries + for key, data in parsedTemps.items(): + try: + actual, target = data + self.last_temperature.set_custom(key, actual=actual, target=target) + except Exception as ex: + self._logger.warning( + f"Could not add custom temperature record {key}: {ex}" + ) ##~~ Serial monitor processing received messages diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 2213f6065..ac600b978 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -284,6 +284,52 @@ class TestCommHelpers(unittest.TestCase): {"T0": (-55.7, 0), "T1": (150.0, 210.0)}, 1, ), + ( + "T:210.04 /210.00 B:52.00 /52.00 @:85 B@:31pS_XYZ:5", + 0, + { + "T0": (210.04, 210.0), + "B": (52.00, 52.0), + }, + 0, + ), + ( + "T:210.04 /210.00 B:52.00 /52.00 @:85 31pS_XYZ:5", + 0, + { + "T0": (210.04, 210.0), + "B": (52.00, 52.0), + "31pS_XYZ": (5, None), + }, + 0, + ), + ( + "T:210.04 /210.00 B:52.00 /52.00 @:85 F0:255.0 /255.0", + 0, + { + "T0": (210.04, 210.0), + "B": (52.00, 52.0), + "F0": (255.0, 255.0), + }, + 0, + ), + ( + "T:210.04 /210.00 @B:52.00 /52.00", + 0, + { + "T0": (210.04, 210.0), + }, + 0, + ), + ( + "T:210.04 /210.00 @B:52.00 /52.00 TXYZ:2", + 0, + { + "T0": (210.04, 210.0), + "TXYZ": (2, None), + }, + 0, + ), ) @unpack def test_process_temperature_line(self, line, current, expected_result, expected_max): @@ -533,3 +579,29 @@ class TestPositionRecord(unittest.TestCase): from octoprint.util.comm import PositionRecord return PositionRecord(**kwargs) + + +@ddt +class TestTemperatureRecord(unittest.TestCase): + @data("TX", "B2", "BX", "SOMETHING_CUSTOM", "1234B456", "blub", "fnord", "C1", "CX") + def test_set_custom(self, identifier): + temperature = self._create_temperature() + + temperature.set_custom(identifier, 1, 2) + + self.assertTrue(identifier in temperature.custom) + + @data("T", "T1", "T42", "B", "C") + def test_set_custom_reserved(self, identifier): + temperature = self._create_temperature() + + try: + temperature.set_custom(identifier, 1, 2) + self.fail(f"Expected ValueError for reserved identifier {identifier}") + except ValueError as ex: + self.assertTrue("is a reserved identifier" in str(ex)) + + def _create_temperature(self, **kwargs): + from octoprint.util.comm import TemperatureRecord + + return TemperatureRecord(**kwargs)