Skip to content

Commit 6be678a

Browse files
committed
Added Print Progress Event
Ability to get progress updates on custom intervals.
1 parent 8f84821 commit 6be678a

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ Called on Prusa Printers when a color change is necessary.
119119

120120
An error has occurred. Can refer to many different types of errors.
121121

122+
* Print Progress
123+
124+
Sends progress reports every 'x' percent where is x is defined by you.
125+
For instance, you can send a progress webhook every 10%, 25%, or 7% if you wanted.
126+
NOTE: 0% and 100% are not triggered by this event, use 'Print Started' and 'Print Done'
127+
for those events.
128+
122129
## Event Data
123130
For details on what 'extra' data is provided with each event, check out the following.
124131
https://docs.octoprint.org/en/master/events/index.html#printing

octoprint_webhooks/__init__.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ def check_for_header(headers, name, value):
4545

4646

4747
class WebhooksPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SettingsPlugin,
48-
octoprint.plugin.EventHandlerPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SimpleApiPlugin):
48+
octoprint.plugin.EventHandlerPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SimpleApiPlugin,
49+
octoprint.plugin.ProgressPlugin):
4950
def __init__(self):
5051
self.triggered = False
52+
self.last_print_progress = -1
53+
self.last_print_progress_milestone = 0
5154

5255
def get_update_information(self, *args, **kwargs):
5356
return dict(
@@ -69,6 +72,7 @@ def get_settings_defaults(self):
6972
return dict(url="", apiSecret="", deviceIdentifier="",
7073
eventPrintStarted=True, eventPrintDone=True, eventPrintFailed=True, eventPrintPaused=True,
7174
eventUserActionNeeded=True, eventError=True,
75+
event_print_progress=False, event_print_progress_interval="50",
7276
headers='{\n "Content-Type": "application/json"\n}',
7377
data='{\n "deviceIdentifier":"@deviceIdentifier",\n "apiSecret":"@apiSecret",\n "topic":"@topic",\n "message":"@message",\n "extra":"@extra"\n}',
7478
http_method="POST",
@@ -94,7 +98,30 @@ def get_assets(self):
9498
)
9599

96100
def register_custom_events(self, *args, **kwargs):
97-
return ["notify"]
101+
return ["notify", "progress"]
102+
103+
def on_print_progress(self, storage, path, progress):
104+
# Reset in case of multiple prints
105+
if self.last_print_progress > progress:
106+
self.last_print_progress = -1
107+
# Get the settings
108+
active = self._settings.get(["event_print_progress"])
109+
event_print_progress_interval = self._settings.get(["event_print_progress_interval"])
110+
#self._logger.info("Print Progress" + storage + " - " + path + " - {0}".format(progress))
111+
if active:
112+
try:
113+
interval = int(event_print_progress_interval)
114+
# Now loop over all the missed progress events and see if they match
115+
for p in range(self.last_print_progress + 1, progress + 1):
116+
if p % interval == 0 and p != 0 and p != 100:
117+
# Send the event for print progress
118+
self.last_print_progress_milestone = p
119+
eventManager().fire(Events.PLUGIN_WEBHOOKS_PROGRESS)
120+
# Update the last print progress
121+
self.last_print_progress = progress
122+
except Exception as e:
123+
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))
124+
return
98125

99126
def get_api_commands(self):
100127
return dict(
@@ -108,6 +135,8 @@ def on_api_command(self, command, data):
108135
event_name = ""
109136
if "event" in data:
110137
event_name = data["event"]
138+
if event_name == "plugin_webhooks_progress":
139+
self.last_print_progress_milestone = 50
111140
self.on_event(event_name, {
112141
"name": "example.gcode",
113142
"path": "example.gcode",
@@ -138,6 +167,9 @@ def on_event(self, event, payload):
138167
elif event == Events.PLUGIN_WEBHOOKS_NOTIFY and self._settings.get(["eventUserActionNeeded"]):
139168
topic = "User Action Needed"
140169
message = "User action is needed. You might need to change the filament color."
170+
elif event == Events.PLUGIN_WEBHOOKS_PROGRESS and self._settings.get(["event_print_progress"]):
171+
topic = "Print Progress"
172+
message = "Your print is {0}% complete".format(self.last_print_progress_milestone)
141173
elif event == Events.ERROR and self._settings.get(["eventError"]):
142174
topic = "Error"
143175
message = "There was an error."

octoprint_webhooks/static/css/webhooks.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@
3333
.control-header-description {
3434
margin: 10px 0px 10px 0px;
3535
}
36+
.progressBlock {
37+
display: block;
38+
}
39+
.progressHidden {
40+
display: none;
41+
}
42+
.control-group-event {
43+
margin-bottom: 5px;
44+
}

octoprint_webhooks/templates/webhooks_settings.jinja2

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="control-header-description">
1515
Select the events for which you want webhooks sent.
1616
</div>
17-
<div class="control-group">
17+
<div class="control-group-event">
1818
<label class="control-label">{{ _('PRINT STARTED') }}</label>
1919
<div class="controls">
2020
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.eventPrintStarted">
@@ -24,7 +24,7 @@
2424
</div>
2525
</div>
2626

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

3737

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

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

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

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

79+
<div class="control-group-event">
80+
<label class="control-label">{{ _('PRINT PROGRESS') }}</label>
81+
<div class="controls">
82+
<input type="checkbox" data-bind="checked: settings.settings.plugins.webhooks.event_print_progress">
83+
<span class="control-description2">
84+
This is called when print progress milestones are reached.
85+
</span>
86+
<div id="progress-container" data-bind="css: { 'progressBlock': settings.settings.plugins.webhooks.event_print_progress(), 'progressHidden': !settings.settings.plugins.webhooks.event_print_progress()}">
87+
<input type="number" class="input-block-level" data-bind="value: settings.settings.plugins.webhooks.event_print_progress_interval">
88+
<div class="control-description2">
89+
Set the interval you want to receive print progress webhooks.
90+
<br/>
91+
Setting the progress to 10 will send a webhook at 10%, 20%, 30%, ...
92+
<br/>
93+
Do NOT put a percent sign in the box. Put 10 instead of 10%.
94+
<br/>
95+
Note that 0% and 100% events are not triggered - use PRINT STARTED and PRINT FINISHED for those.
96+
</div>
97+
</div>
98+
</div>
99+
</div>
100+
79101
<!-- ------------------ -->
80102
<!-- ------------------ -->
81103
<!-- WEBHOOK PARAMETERS -->
@@ -276,6 +298,7 @@
276298
<option value="PrintPaused">Print Paused</option>
277299
<option value="plugin_webhooks_notify">User Action Needed</option>
278300
<option value="Error">Error</option>
301+
<option value="plugin_webhooks_progress">Print Progress</option>
279302
</select>
280303
<span class="control-description2">
281304
The event you want to simulate.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
plugin_name = "OctoPrint-Webhooks"
1515

1616
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
17-
plugin_version = "2.2.0"
17+
plugin_version = "2.3.0"
1818

1919
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
2020
# module

0 commit comments

Comments
 (0)