Skip to content

Commit

Permalink
Added Print Progress Event
Browse files Browse the repository at this point in the history
Ability to get progress updates on custom intervals.
  • Loading branch information
2blane committed May 3, 2020
1 parent 8f84821 commit 6be678a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 9 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions octoprint_webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
Expand All @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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."
Expand Down
9 changes: 9 additions & 0 deletions octoprint_webhooks/static/css/webhooks.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@
.control-header-description {
margin: 10px 0px 10px 0px;
}
.progressBlock {
display: block;
}
.progressHidden {
display: none;
}
.control-group-event {
margin-bottom: 5px;
}
35 changes: 29 additions & 6 deletions octoprint_webhooks/templates/webhooks_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="control-header-description">
Select the events for which you want webhooks sent.
</div>
<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('PRINT STARTED') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventPrintStarted">
Expand All @@ -24,7 +24,7 @@
</div>
</div>

<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('PRINT DONE') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventPrintDone">
Expand All @@ -35,7 +35,7 @@
</div>


<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('PRINT FAILED') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventPrintFailed">
Expand All @@ -45,7 +45,7 @@
</div>
</div>

<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('PRINT PAUSED') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventPrintPaused">
Expand All @@ -55,7 +55,7 @@
</div>
</div>

<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('USER ACTION NEEDED') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventUserActionNeeded">
Expand All @@ -66,7 +66,7 @@
</div>
</div>

<div class="control-group">
<div class="control-group-event">
<label class="control-label">{{ _('ERROR') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventError">
Expand All @@ -76,6 +76,28 @@
</div>
</div>

<div class="control-group-event">
<label class="control-label">{{ _('PRINT PROGRESS') }}</label>
<div class="controls">
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.event_print_progress">
<span class="control-description2">
This is called when print progress milestones are reached.
</span>
<div id="progress-container" data-bind="css: { 'progressBlock': settings.settings.plugins.webhooks.event_print_progress(), 'progressHidden': !settings.settings.plugins.webhooks.event_print_progress()}">
<input type="number" class="input-block-level" data-bind="value: settings.settings.plugins.webhooks.event_print_progress_interval">
<div class="control-description2">
Set the interval you want to receive print progress webhooks.
<br/>
Setting the progress to 10 will send a webhook at 10%, 20%, 30%, ...
<br/>
Do NOT put a percent sign in the box. Put 10 instead of 10%.
<br/>
Note that 0% and 100% events are not triggered - use PRINT STARTED and PRINT FINISHED for those.
</div>
</div>
</div>
</div>

<!-- ------------------ -->
<!-- ------------------ -->
<!-- WEBHOOK PARAMETERS -->
Expand Down Expand Up @@ -276,6 +298,7 @@
<option value="PrintPaused">Print Paused</option>
<option value="plugin_webhooks_notify">User Action Needed</option>
<option value="Error">Error</option>
<option value="plugin_webhooks_progress">Print Progress</option>
</select>
<span class="control-description2">
The event you want to simulate.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6be678a

Please sign in to comment.