🐛 Fix custom temperature handling

* Fix too broad regex
  * Fix lack of error handling in case of reserved identifier

All of these got already introduced in 1.4.1, but now with #4388
they are WAY more likely to pop up (e.g. in garbage lines too) and
completely nuke the connection.

Closes #4456
This commit is contained in:
Gina Häußge committed 2022-03-15 12:38:11 +01:00
1 parent 6cd87d1c84
commit df3b3420ab
2 files changed
+85 -8

No files matched your search

+13 -8
View File
@@ -113,7 +113,7 @@ Groups will be as follows:
"""
regex_temp = re.compile(
r"(?P<sensor>B|C|T(?P<toolnum>\d*)|([\w]+)):\s*(?P<actual>%s)(\s*\/?\s*(?P<target>%s))?"
r"(^|\s)(?P<sensor>B|C|T(?P<toolnum>\d*)|([\w]+)):\s*(?P<actual>%s)(\s*\/?\s*(?P<target>%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
+72
View File
@@ -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)