From 79cd98d467b6fe8af0398a58ff10e846e160822c Mon Sep 17 00:00:00 2001 From: jneilliii Date: Sat, 21 Dec 2019 22:52:16 -0500 Subject: [PATCH 1/2] Added modifications to allow for the injection of additional temperature data. --- AUTHORS.md | 1 + src/octoprint/printer/standard.py | 11 ++++++--- src/octoprint/util/comm.py | 37 +++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 8398e0cb5..a3c82caf0 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -115,6 +115,7 @@ date of first contribution): * [Ludovico de Nittis](https://github.com/RyuzakiKK) * [Dominik Paľo](https://github.com/DominikPalo) * [Kelly Anderson](https://github.com/cbxbiker61) + * [Jim Neill](https://github.com/jneilliii) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index e870c36e6..c3c895ecf 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -888,7 +888,7 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): printTimeLeft=int(printTimeLeft) if printTimeLeft is not None else None, printTimeLeftOrigin=printTimeLeftOrigin) - def _addTemperatureData(self, tools=None, bed=None, chamber=None): + def _addTemperatureData(self, tools=None, bed=None, chamber=None, custom=None): if tools is None: tools = dict() @@ -899,12 +899,17 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): data["bed"] = self._dict(actual=bed[0], target=bed[1]) if chamber is not None and isinstance(chamber, tuple): data["chamber"] = self._dict(actual=chamber[0], target=chamber[1]) + if custom is None: + custom = dict() + for identifier in custom.keys(): + data[identifier] = self._dict(actual=custom[identifier][0], target=custom[identifier][1]) self._temps.append(data) self._temp = tools self._bedTemp = bed self._chamberTemp = chamber + self._custom = custom self._stateMonitor.add_temperature(self._dict(**data)) @@ -1055,8 +1060,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): """ self._addLog(to_unicode(message, "utf-8", errors="replace")) - def on_comm_temperature_update(self, temp, bedTemp, chamberTemp): - self._addTemperatureData(tools=copy.deepcopy(temp), bed=copy.deepcopy(bedTemp), chamber=copy.deepcopy(chamberTemp)) + def on_comm_temperature_update(self, temp, bedTemp, chamberTemp, customTemp): + self._addTemperatureData(tools=copy.deepcopy(temp), bed=copy.deepcopy(bedTemp), chamber=copy.deepcopy(chamberTemp), custom=copy.deepcopy(customTemp)) def on_comm_position_update(self, position, reason=None): payload = dict(reason=reason) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index d3351de22..e53600660 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -283,6 +283,7 @@ class TemperatureRecord(object): self._tools = dict() self._bed = (None, None) self._chamber = (None, None) + self._custom = dict() def copy_from(self, other): self._tools = other.tools @@ -300,6 +301,10 @@ class TemperatureRecord(object): current = self._chamber self._chamber = self._to_new_tuple(current, actual, target) + def set_custom(self, identifier, actual=None, target=None): + current = self._custom.get(identifier, (None, None)) + self._custom[identifier] = self._to_new_tuple(current, actual, target) + @property def tools(self): return dict(self._tools) @@ -312,6 +317,10 @@ class TemperatureRecord(object): def chamber(self): return self._chamber + @property + def custom(self): + return dict(self._custom) + def as_script_dict(self): result = dict() @@ -328,6 +337,11 @@ class TemperatureRecord(object): result["c"] = dict(actual=chamber[0], target=chamber[1]) + custom = self.custom + for identifier, data in custom.items(): + result[identifier] = dict(actual=data[0], + target=data[1]) + return result @classmethod @@ -1581,22 +1595,31 @@ class MachineCom(object): if not tool in parsedTemps: if shared_nozzle: actual, target = parsedTemps[current_tool_key] + del parsedTemps[current_tool_key] else: continue else: actual, target = parsedTemps[tool] + del parsedTemps[tool] self.last_temperature.set_tool(n, actual=actual, target=target) # bed temperature if "B" in parsedTemps: actual, target = parsedTemps["B"] + del parsedTemps["B"] self.last_temperature.set_bed(actual=actual, target=target) # chamber temperature if "C" in parsedTemps and (self._capability_supported(self.CAPABILITY_CHAMBER_TEMP) or self._printerProfileManager.get_current_or_default()["heatedChamber"]): actual, target = parsedTemps["C"] + 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) + ##~~ Serial monitor processing received messages def _monitor(self): @@ -1867,7 +1890,7 @@ class MachineCom(object): self._heatupWaitStartTime = monotonic_time() self._processTemperatures(line) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) elif supportRepetierTargetTemp and ('TargetExtr' in line or 'TargetBed' in line): matchExtr = regex_repetierTempExtr.match(line) @@ -1878,14 +1901,14 @@ class MachineCom(object): try: target = float(matchExtr.group(2)) self.last_temperature.set_tool(toolNum, target=target) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) except ValueError: pass elif matchBed is not None: try: target = float(matchBed.group(1)) self.last_temperature.set_bed(target=target) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) except ValueError: pass @@ -3661,7 +3684,7 @@ class MachineCom(object): try: target = float(match.group("value")) self.last_temperature.set_tool(toolNum, target=target) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) except ValueError: pass @@ -3674,7 +3697,7 @@ class MachineCom(object): try: target = float(match.group("value")) self.last_temperature.set_bed(target=target) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) except ValueError: pass @@ -3687,7 +3710,7 @@ class MachineCom(object): try: target = float(match.group("value")) self.last_temperature.set_chamber(target=target) - self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber) + self._callback.on_comm_temperature_update(self.last_temperature.tools, self.last_temperature.bed, self.last_temperature.chamber, self.last_temperature.custom) except ValueError: pass @@ -3875,7 +3898,7 @@ class MachineComPrintCallback(object): def on_comm_log(self, message): pass - def on_comm_temperature_update(self, temp, bedTemp, chamberTemp): + def on_comm_temperature_update(self, temp, bedTemp, chamberTemp, customTemp): pass def on_comm_position_update(self, position, reason=None): From 46d4a73955edebb417d2197fe6842b106ddd4b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 6 Apr 2020 16:39:28 +0200 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=20minor=20refactorings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * validate custom identifiers for as_dict method * minor performance improvement --- src/octoprint/printer/standard.py | 17 +++++++++++------ src/octoprint/util/comm.py | 6 +++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index c3c895ecf..10ccd329d 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -891,6 +891,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): def _addTemperatureData(self, tools=None, bed=None, chamber=None, custom=None): if tools is None: tools = dict() + if custom is None: + custom = dict() data = dict(time=int(time.time())) for tool in tools.keys(): @@ -899,10 +901,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): data["bed"] = self._dict(actual=bed[0], target=bed[1]) if chamber is not None and isinstance(chamber, tuple): data["chamber"] = self._dict(actual=chamber[0], target=chamber[1]) - if custom is None: - custom = dict() - for identifier in custom.keys(): - data[identifier] = self._dict(actual=custom[identifier][0], target=custom[identifier][1]) + for identifier, values in custom.items(): + data[identifier] = self._dict(actual=values[0], target=values[1]) self._temps.append(data) @@ -1060,8 +1060,13 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): """ self._addLog(to_unicode(message, "utf-8", errors="replace")) - def on_comm_temperature_update(self, temp, bedTemp, chamberTemp, customTemp): - self._addTemperatureData(tools=copy.deepcopy(temp), bed=copy.deepcopy(bedTemp), chamber=copy.deepcopy(chamberTemp), custom=copy.deepcopy(customTemp)) + def on_comm_temperature_update(self, tools, bed, chamber, custom=None): + if custom is None: + custom = dict() + self._addTemperatureData(tools=copy.deepcopy(tools), + bed=copy.deepcopy(bed), + chamber=copy.deepcopy(chamber), + custom=copy.deepcopy(custom)) def on_comm_position_update(self, position, reason=None): payload = dict(reason=reason) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index e53600660..8e65f6491 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -279,6 +279,8 @@ class PositionRecord(object): return dict((attr, getattr(self, attr)) for attr in attrs) class TemperatureRecord(object): + RESERVED_IDENTIFIER_REGEX = re.compile("[0-9]+|[bc]") + def __init__(self): self._tools = dict() self._bed = (None, None) @@ -302,6 +304,8 @@ class TemperatureRecord(object): 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): + raise ValueError("{} is a reserved identifier".format(identifier)) current = self._custom.get(identifier, (None, None)) self._custom[identifier] = self._to_new_tuple(current, actual, target) @@ -340,7 +344,7 @@ class TemperatureRecord(object): custom = self.custom for identifier, data in custom.items(): result[identifier] = dict(actual=data[0], - target=data[1]) + target=data[1]) return result