diff --git a/docs/features/gcode_scripts.rst b/docs/features/gcode_scripts.rst index aa8fb1329..bccce9e66 100644 --- a/docs/features/gcode_scripts.rst +++ b/docs/features/gcode_scripts.rst @@ -84,6 +84,8 @@ All GCODE scripts have access to the following template variables through the te to the corresponding temperature in degrees Celsius. Note that not all tools your printer has must necessarily be present here, neither must the heated bed - it depends on whether OctoPrint has values for a tool or the bed. Also note that ``actual`` and ``target`` might be ``None``. + * ``last_fanspeed``: Last fan speed set. It contains the value taken from command (M106 and M107) sent through OctoPrint. + The value might be ``None`` if no fan speed has been set. * ``script``: An object wrapping the script's type (``gcode``) and name (e.g. ``afterPrintCancelled``) as ``script.type`` and ``script.name`` respectively. * ``plugins``: An object containing variables provided by plugins (e.g ``plugins.myplugin.myvariable``) @@ -99,6 +101,7 @@ There are a few additional template variables available for the following specif "Log position on pause" under Settings > Serial > Advanced options! * ``pause_temperature``: Last known temperature values when the print was paused. See ``last_temperature`` above for the structure to expect here. + * ``pause_fanspeed``: Last known fan speed value when the print was paused. See ``last_fanspeed`` above for the structure to expect here. * ``afterPrintCancelled`` @@ -109,6 +112,7 @@ There are a few additional template variables available for the following specif "Log position on cancel" under Settings > Serial > Advanced options! * ``cancel_temperature``: Last known temperature values when the print was cancelled. See ``last_temperature`` above for the structure to expect here. + * ``cancel_fanspeed``: Last known fan speed value when the print was cancelled. See ``last_fanspeed`` above for the structure to expect here. * ``beforeToolChange`` and ``afterToolChange`` diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index ceebc5364..f2e4408aa 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -757,6 +757,10 @@ class MachineCom: self.pause_position = PositionRecord() self.cancel_position = PositionRecord() + self.last_fanspeed = None + self.pause_fanspeed = None + self.cancel_fanspeed = None + self._record_pause_data = False self._record_cancel_data = False @@ -1296,6 +1300,7 @@ class MachineCom: "printer_profile": self._printerProfileManager.get_current_or_default(), "last_position": self.last_position, "last_temperature": self.last_temperature.as_script_dict(), + "last_fanspeed": self.last_fanspeed, } ) @@ -1304,6 +1309,7 @@ class MachineCom: { "pause_position": self.pause_position, "pause_temperature": self.pause_temperature.as_script_dict(), + "pause_fanspeed": self.pause_fanspeed, } ) elif scriptName == "afterPrintCancelled": @@ -1311,6 +1317,7 @@ class MachineCom: { "cancel_position": self.cancel_position, "cancel_temperature": self.cancel_temperature.as_script_dict(), + "cancel_fanspeed": self.cancel_fanspeed, } ) @@ -2613,6 +2620,7 @@ class MachineCom: self._record_pause_data = False self.pause_position.copy_from(self.last_position) self.pause_temperature.copy_from(self.last_temperature) + self.pause_fanspeed = self.last_fanspeed self._pause_preparation_done() if self._record_cancel_data: @@ -2620,6 +2628,7 @@ class MachineCom: self._record_cancel_data = False self.cancel_position.copy_from(self.last_position) self.cancel_temperature.copy_from(self.last_temperature) + self.cancel_fanspeed = self.last_fanspeed self._cancel_preparation_done() self._callback.on_comm_position_update( @@ -5368,6 +5377,22 @@ class MachineCom: self._timeout = self._get_new_communication_timeout() + _timeout self._dwelling_until = time.monotonic() + _timeout + def _gcode_M106_sent( + self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs + ): + s_match = regexes_parameters["intS"].search(cmd) + + fanspeed = 0 + if s_match: + fanspeed = int(s_match.group("value")) + + self.last_fanspeed = fanspeed + + def _gcode_M107_sent( + self, cmd, cmd_type=None, gcode=None, subcode=None, *args, **kwargs + ): + self.last_fanspeed = 0 + def _emergency_force_send(self, cmd, message, gcode=None, *args, **kwargs): # only jump the queue with our command if the EMERGENCY_PARSER capability is available if not self._capability_supported(self.CAPABILITY_EMERGENCY_PARSER):