diff --git a/README.md b/README.md index 4942cea..57af9b8 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,13 @@ Called on Prusa Printers when a color change is necessary. An error has occurred. Can refer to many different types of errors. +* Print Progress + +Sends progress reports every 'x' percent where is x is defined by you. +For instance, you can send a progress webhook every 10%, 25%, or 7% if you wanted. +NOTE: 0% and 100% are not triggered by this event, use 'Print Started' and 'Print Done' +for those events. + ## Event Data For details on what 'extra' data is provided with each event, check out the following. https://docs.octoprint.org/en/master/events/index.html#printing diff --git a/octoprint_webhooks/__init__.py b/octoprint_webhooks/__init__.py index 947bd12..69d9fa7 100644 --- a/octoprint_webhooks/__init__.py +++ b/octoprint_webhooks/__init__.py @@ -45,9 +45,12 @@ def check_for_header(headers, name, value): class WebhooksPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin, - octoprint.plugin.EventHandlerPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SimpleApiPlugin): + octoprint.plugin.EventHandlerPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SimpleApiPlugin, + octoprint.plugin.ProgressPlugin): def __init__(self): self.triggered = False + self.last_print_progress = -1 + self.last_print_progress_milestone = 0 def get_update_information(self, *args, **kwargs): return dict( @@ -69,6 +72,7 @@ def get_settings_defaults(self): return dict(url="", apiSecret="", deviceIdentifier="", eventPrintStarted=True, eventPrintDone=True, eventPrintFailed=True, eventPrintPaused=True, eventUserActionNeeded=True, eventError=True, + event_print_progress=False, event_print_progress_interval="50", headers='{\n "Content-Type": "application/json"\n}', data='{\n "deviceIdentifier":"@deviceIdentifier",\n "apiSecret":"@apiSecret",\n "topic":"@topic",\n "message":"@message",\n "extra":"@extra"\n}', http_method="POST", @@ -94,7 +98,30 @@ def get_assets(self): ) def register_custom_events(self, *args, **kwargs): - return ["notify"] + return ["notify", "progress"] + + def on_print_progress(self, storage, path, progress): + # Reset in case of multiple prints + if self.last_print_progress > progress: + self.last_print_progress = -1 + # Get the settings + active = self._settings.get(["event_print_progress"]) + event_print_progress_interval = self._settings.get(["event_print_progress_interval"]) + #self._logger.info("Print Progress" + storage + " - " + path + " - {0}".format(progress)) + if active: + try: + interval = int(event_print_progress_interval) + # Now loop over all the missed progress events and see if they match + for p in range(self.last_print_progress + 1, progress + 1): + if p % interval == 0 and p != 0 and p != 100: + # Send the event for print progress + self.last_print_progress_milestone = p + eventManager().fire(Events.PLUGIN_WEBHOOKS_PROGRESS) + # Update the last print progress + self.last_print_progress = progress + except Exception as e: + self._plugin_manager.send_plugin_message(self._identifier, dict(type="error", hide=True, msg="Invalid Setting for PRINT PROGRESS INTERVAL please use a number without any special characters instead of " + event_print_progress_interval)) + return def get_api_commands(self): return dict( @@ -108,6 +135,8 @@ def on_api_command(self, command, data): event_name = "" if "event" in data: event_name = data["event"] + if event_name == "plugin_webhooks_progress": + self.last_print_progress_milestone = 50 self.on_event(event_name, { "name": "example.gcode", "path": "example.gcode", @@ -138,6 +167,9 @@ def on_event(self, event, payload): elif event == Events.PLUGIN_WEBHOOKS_NOTIFY and self._settings.get(["eventUserActionNeeded"]): topic = "User Action Needed" message = "User action is needed. You might need to change the filament color." + elif event == Events.PLUGIN_WEBHOOKS_PROGRESS and self._settings.get(["event_print_progress"]): + topic = "Print Progress" + message = "Your print is {0}% complete".format(self.last_print_progress_milestone) elif event == Events.ERROR and self._settings.get(["eventError"]): topic = "Error" message = "There was an error." diff --git a/octoprint_webhooks/static/css/webhooks.css b/octoprint_webhooks/static/css/webhooks.css index e1c3b94..15c27c0 100644 --- a/octoprint_webhooks/static/css/webhooks.css +++ b/octoprint_webhooks/static/css/webhooks.css @@ -33,3 +33,12 @@ .control-header-description { margin: 10px 0px 10px 0px; } +.progressBlock { + display: block; +} +.progressHidden { + display: none; +} +.control-group-event { + margin-bottom: 5px; +} diff --git a/octoprint_webhooks/templates/webhooks_settings.jinja2 b/octoprint_webhooks/templates/webhooks_settings.jinja2 index 6af249a..dc27aef 100644 --- a/octoprint_webhooks/templates/webhooks_settings.jinja2 +++ b/octoprint_webhooks/templates/webhooks_settings.jinja2 @@ -14,7 +14,7 @@
Select the events for which you want webhooks sent.
-
+
@@ -24,7 +24,7 @@
-
+
@@ -35,7 +35,7 @@
-
+
@@ -45,7 +45,7 @@
-
+
@@ -55,7 +55,7 @@
-
+
@@ -66,7 +66,7 @@
-
+
@@ -76,6 +76,28 @@
+
+ +
+ + + This is called when print progress milestones are reached. + +
+ +
+ Set the interval you want to receive print progress webhooks. +
+ Setting the progress to 10 will send a webhook at 10%, 20%, 30%, ... +
+ Do NOT put a percent sign in the box. Put 10 instead of 10%. +
+ Note that 0% and 100% events are not triggered - use PRINT STARTED and PRINT FINISHED for those. +
+
+
+
+ @@ -276,6 +298,7 @@ + The event you want to simulate. diff --git a/setup.py b/setup.py index b923657..d4e940d 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoPrint-Webhooks" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "2.2.0" +plugin_version = "2.3.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module