@@ -45,9 +45,12 @@ def check_for_header(headers, name, value):
45
45
46
46
47
47
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 ):
49
50
def __init__ (self ):
50
51
self .triggered = False
52
+ self .last_print_progress = - 1
53
+ self .last_print_progress_milestone = 0
51
54
52
55
def get_update_information (self , * args , ** kwargs ):
53
56
return dict (
@@ -69,6 +72,7 @@ def get_settings_defaults(self):
69
72
return dict (url = "" , apiSecret = "" , deviceIdentifier = "" ,
70
73
eventPrintStarted = True , eventPrintDone = True , eventPrintFailed = True , eventPrintPaused = True ,
71
74
eventUserActionNeeded = True , eventError = True ,
75
+ event_print_progress = False , event_print_progress_interval = "50" ,
72
76
headers = '{\n "Content-Type": "application/json"\n }' ,
73
77
data = '{\n "deviceIdentifier":"@deviceIdentifier",\n "apiSecret":"@apiSecret",\n "topic":"@topic",\n "message":"@message",\n "extra":"@extra"\n }' ,
74
78
http_method = "POST" ,
@@ -94,7 +98,30 @@ def get_assets(self):
94
98
)
95
99
96
100
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
98
125
99
126
def get_api_commands (self ):
100
127
return dict (
@@ -108,6 +135,8 @@ def on_api_command(self, command, data):
108
135
event_name = ""
109
136
if "event" in data :
110
137
event_name = data ["event" ]
138
+ if event_name == "plugin_webhooks_progress" :
139
+ self .last_print_progress_milestone = 50
111
140
self .on_event (event_name , {
112
141
"name" : "example.gcode" ,
113
142
"path" : "example.gcode" ,
@@ -138,6 +167,9 @@ def on_event(self, event, payload):
138
167
elif event == Events .PLUGIN_WEBHOOKS_NOTIFY and self ._settings .get (["eventUserActionNeeded" ]):
139
168
topic = "User Action Needed"
140
169
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 )
141
173
elif event == Events .ERROR and self ._settings .get (["eventError" ]):
142
174
topic = "Error"
143
175
message = "There was an error."
0 commit comments