Merge pull request #3903 from shadycuz/feat/dynamic_gcode_events

Send events for gcode scripts
This commit is contained in:
Gina Häußge authored and GitHub committed 2021-01-18 16:53:26 +01:00
commit 8af4144a66
5 files changed
+126

No files matched your search

+1
View File
@@ -133,6 +133,7 @@ date of first contribution):
* ["coliss86"](https://github.com/coliss86)
* ["MichaIng"](https://github.com/MichaIng)
* ["jasonbcox"](https://github.com/jasonbcox)
* ["shadycuz"](https://github.com/shadycuz)
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
+28
View File
@@ -539,6 +539,34 @@ PrintResumed
.. versionchanged:: 1.4.0
GcodeScript${ScriptName}Running
A custom :ref:`GCODE script <sec-features-gcode_scripts>` has started running.
Payload:
* ``name``: the file's name
* ``path``: the file's path within its storage location
* ``origin``: the origin storage location of the file, either ``local`` or ``sdcard``
* ``size``: the file's size in bytes (if available)
* ``owner``: the user who started the print job (if available)
* ``time``: the time needed for the print, in seconds (float)
.. versionadded:: 1.6.0
GcodeScript${ScriptName}Finished
A custom :ref:`GCODE script <sec-features-gcode_scripts>` has finished running.
Payload:
* ``name``: the file's name
* ``path``: the file's path within its storage location
* ``origin``: the origin storage location of the file, either ``local`` or ``sdcard``
* ``size``: the file's size in bytes (if available)
* ``owner``: the user who started the print job (if available)
* ``time``: the time needed for the print, in seconds (float)
.. versionadded:: 1.6.0
.. _sec-events-available_events-gcode_processing:
GCODE processing
+9
View File
@@ -43,6 +43,15 @@ The following GCODE scripts are sent by OctoPrint automatically:
Plugins may extend these scripts through :ref:`a hook <sec-plugins-hook-comm-protocol-scripts>`.
.. _sec-features-gcode_scripts-events:
Events
------
Every GCODE script that is executed will emit two events. The event name will start with 'GcodeScript' followed by the capitalized name
of the script. When ``afterPrintDone`` has started the event will be ``GcodeScriptAfterPrintDoneRunning`` and once it has completed the last event
will be ``GcodeScriptAfterPrintDoneFinished``. You can find more details in the :ref:`Events <sec-events-available_events-printing>` documentation.
.. _sec-features-gcode_scripts-snippets:
Snippets
+12
View File
@@ -444,6 +444,15 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback):
if name is None or not name:
raise ValueError("name must be set")
# .capitalize() will lowercase all letters but the first
# this code preserves existing CamelCase
event_name = name[0].upper() + name[1:]
event_start = "GcodeScript{}Running".format(event_name)
payload = context.get("event", None) if isinstance(context, dict) else None
eventManager().fire(event_start, payload)
result = self._comm.sendGcodeScript(
name,
part_of_job=part_of_job,
@@ -453,6 +462,9 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback):
if not result and must_be_set:
raise UnknownScript(name)
event_end = "GcodeScript{}Finished".format(event_name)
eventManager().fire(event_end, payload)
def jog(self, axes, relative=True, speed=None, *args, **kwargs):
if isinstance(axes, basestring):
# legacy parameter format, there should be an amount as first anonymous positional arguments too
+76
View File
@@ -0,0 +1,76 @@
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License"
import unittest
from unittest.mock import ANY, MagicMock, call, patch
from octoprint.printer.standard import Printer
class ScriptsTestCase(unittest.TestCase):
def setUp(self):
# mock comm
self.comm = MagicMock()
# mock event manager
self.event_manager = MagicMock()
self.event_manager_patcher = patch("octoprint.printer.standard.eventManager")
self.event_manager_getter = self.event_manager_patcher.start()
self.event_manager_getter.return_value = self.event_manager
# mock plugin manager
self.plugin_manager = MagicMock()
self.plugin_manager.get_hooks.return_value = {}
self.plugin_manager_patcher = patch("octoprint.printer.standard.plugin_manager")
self.plugin_manager_getter = self.plugin_manager_patcher.start()
self.plugin_manager_getter.return_value = self.plugin_manager
# mock settings
self.settings = MagicMock()
self.settings.getInt.return_value = 1
self.settings.getBoolean.return_value = False
self.settings_patcher = patch("octoprint.printer.standard.settings")
self.settings_getter = self.settings_patcher.start()
self.settings_getter.return_value = self.settings
self.printer = Printer(MagicMock(), MagicMock(), MagicMock())
self.printer._comm = self.comm
def tearDown(self):
self.settings_patcher.stop()
self.plugin_manager_patcher.stop()
self.event_manager_patcher.stop()
def test_event_name(self):
self.printer.script("testEvent")
self.event_manager.fire.assert_any_call("GcodeScriptTestEventRunning", ANY)
def test_event_order(self):
self.printer.script("EventOrder")
expected_order = [
call("GcodeScriptEventOrderRunning", ANY),
call("GcodeScriptEventOrderFinished", ANY),
]
self.event_manager.fire.assert_has_calls(expected_order)
def test_payload_handling(self):
expected_paylod = {"payloadKey": "payloadValue"}
context = {"event": expected_paylod}
self.printer.script("GetPayload", context)
self.event_manager.fire.assert_called_with(ANY, expected_paylod)
self.printer.script("WrongPayload", [])
self.event_manager.fire.assert_called_with(ANY, None)
self.printer.script("WrongContext", {"noEvent": {}})
self.event_manager.fire.assert_called_with(ANY, None)