Skip to content

Commit

Permalink
Add configurable re-fire threshold for received gcode hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
RoboMagus committed Nov 28, 2022
1 parent bfd7977 commit 4ded625
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
21 changes: 17 additions & 4 deletions octoprint_custom_gcode_events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
## ToDo:
# - Match types: exact, startswith, contains, regex

def ToIntOrDefault(value, default):
try:
v = int(value)
except ValueError:
v = default
return v

class Custom_gcode_eventsPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
Expand All @@ -20,14 +27,16 @@ class Custom_gcode_eventsPlugin(octoprint.plugin.SettingsPlugin,
'topic': '',
'event': '',
'exactMatch': False,
'reFireThreshold': '',
'enabled': True }

##~~ SettingsPlugin mixin

def get_settings_defaults(self):
return dict(
received_gcode_hooks=[{'gcode': '', 'topic': '', 'event': '', 'exactMatch': True, 'enabled': True}],
sent_gcode_hooks=[{'gcode': '', 'topic': '', 'event': '', 'exactMatch': True, 'enabled': True}]
received_gcode_hooks=[{'gcode': '', 'topic': '', 'event': '', 'exactMatch': True, 'reFireThreshold': '', 'enabled': True}],
sent_gcode_hooks=[{'gcode': '', 'topic': '', 'event': '', 'exactMatch': True, 'enabled': True}],
default_refire_threshold='5'
)

def checkEventEntry(self, entry):
Expand All @@ -43,6 +52,7 @@ def updateEventEntry(self, entry):
def on_settings_initialized(self):
self.received_gcode_hooks = self._settings.get(["received_gcode_hooks"])
self.sent_gcode_hooks = self._settings.get(["sent_gcode_hooks"])
self.default_refire_threshold = ToIntOrDefault(self._settings.get(["default_refire_threshold"]), 5)

# On initialization check for incomplete settings!
modified=False
Expand Down Expand Up @@ -74,6 +84,7 @@ def on_settings_save(self, data):
for idx, hook in enumerate(received_gcode_hooks):
received_gcode_hooks[idx]["topic"] = hook["topic"].strip().lower().replace(" ","_").replace("-","_").replace("/","_").replace("$","").replace("#","")
received_gcode_hooks[idx]["event"] = hook["event"].strip()
received_gcode_hooks[idx]["reFireThreshold"] = ToIntOrDefault(hook["reFireThreshold"], '')

sent_gcode_hooks = self._settings.get(["sent_gcode_hooks"])
for idx, hook in enumerate(sent_gcode_hooks):
Expand All @@ -85,6 +96,7 @@ def on_settings_save(self, data):

self.received_gcode_hooks = self._settings.get(["received_gcode_hooks"])
self.sent_gcode_hooks = self._settings.get(["sent_gcode_hooks"])
self.default_refire_threshold = ToIntOrDefault(self._settings.get(["default_refire_threshold"]), 5)

self._logger.debug("received_gcode_hooks settings saved: '{}'".format(self.received_gcode_hooks))
self._logger.debug(" sent_gcode_hooks settings saved: '{}'".format(self.sent_gcode_hooks ))
Expand All @@ -101,7 +113,6 @@ def recv_callback(self, comm_instance, line, *args, **kwargs):
return line
# Do processing...
try:
refire_threshold = int(time.time()) - 5
for entry in self.received_gcode_hooks:
if entry["enabled"]:
_match = False
Expand All @@ -111,11 +122,13 @@ def recv_callback(self, comm_instance, line, *args, **kwargs):
_match = True

if _match:
_refire_threshold = ToIntOrDefault(entry.get('reFireThreshold', self.default_refire_threshold), self.default_refire_threshold)
refire_threshold = int(time.time()) - _refire_threshold
if entry.get('timestamp', 0) <= refire_threshold:
self._logger.info("Received match for '{}'. Firing event 'gcode_event_{}'".format(entry["gcode"], entry["topic"]))
self.fire_event(entry, {"gcode": line})
else:
self._logger.debug("Prevent firinng for event '{}'. Occured within repetition interval!!".format(entry["gcode"]))
self._logger.debug("Prevent firing for event '{}'. Occured within repetition interval ({} s)!!".format(entry["gcode"], _refire_threshold))
entry["timestamp"] = int(time.time())

except:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
<h2>{{ _('Custom GCode Events') }}</h2>
<span>Enter a match for the sent or received GCode in the 'GCode' field, and an event topic string to be emitted when a match for the GCode occurs in the 'Topic' field. Note that this event string must be lower-case and without spaces (Or it will be made into one upon saving)!! This string will also be prepended with 'gcode_event_' as an event identifier.</span></br>
<span>Optionally you can add additional context in the 'Event' field. This is usefull in case multiple GCode matches are sent to the same Event-Topic in order to distinguish what event has expired by the MQTT recepient.</span></br>
<span>GCode matches can be selected to be either an exact match, or match when the sent / received GCode contains the text specified below. Note that checking for exact matches is significantly faster and should be considered to be used whenever possible to avoid slowdowns!!</span>
<span>GCode matches can be selected to be either an exact match, or match when the sent / received GCode contains the text specified below. Note that checking for exact matches is significantly faster and should be considered to be used whenever possible to avoid slowdowns!!</span></br>
<span>The ReFire Threshold field allows you to set a threshold in seconds for which repeated events are suppressed. When left empty the default refire threshold defined at the bottom is used.</span>
<h3>{{ _('GCode Received Hooks') }}</h3>
<div class="row-fluid">
<div class="span1 text-center"><h4>{{ _('Enabled') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('G-Code') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('Topic') }}</h4></div>
<div class="span2 text-center"><h4>{{ _('G-Code') }}</h4></div>
<div class="span2 text-center"><h4>{{ _('Topic') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('Event') }}</h4></div>
<div class="span1 text-center"><h4>{{ _('Exact Match') }}</h4></div>
<div class="span1 text-center"><h4>{{ _('ReFire Threshold') }}</h4></div>
</div>
<div data-bind="foreach: settings.settings.plugins.custom_gcode_events.received_gcode_hooks">
<div class="row-fluid" style="margin-bottom: 5px">
<div class="span1">
<input style="padding: 4px; margin: 2px;" type="checkbox" class="span12 text-right" data-bind="checked: enabled">
</div>
<div class="span3">
<div class="span2">
<input type="text" class="span12 text-right" data-bind="value: gcode, enable: enabled">
</div>
<div class="span3">
<div class="span2">
<input type="text" class="span12 text-right" data-bind="value: topic, enable: enabled">
</div>
<div class="span3">
Expand All @@ -29,21 +31,24 @@
<div class="span1">
<input style="padding: 4px; margin: 2px;" type="checkbox" class="span12 text-right" data-bind="checked: exactMatch, enable: enabled">
</div>
<div class="span1">
<input type="number" class="input-block-level" size="3" min="0" max="360" step="1" data-bind="value: reFireThreshold, enable: enabled">
</div>
<div class="span1">
<button title="Remove Hook" class="btn btn-danger" data-bind="click: $parent.removeRcvdEvent"><i class="fa fa-trash-o"></i></button>
</div>
</div>
</div>
<div class="row-fluid">
<div class="offset11 span1">
<div class="offset10 span1">
<button title="Add Rcvd Hook" class="btn btn-primary" data-bind="click: addRcvdEvent"><i class="fa fa-plus"></i></button>
</div>
</div>
<h3>{{ _('GCode Sent Hooks') }}</h3>
<div class="row-fluid">
<div class="span1 text-center"><h4>{{ _('Enabled') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('G-Code') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('Topic') }}</h4></div>
<div class="span2 text-center"><h4>{{ _('G-Code') }}</h4></div>
<div class="span2 text-center"><h4>{{ _('Topic') }}</h4></div>
<div class="span3 text-center"><h4>{{ _('Event') }}</h4></div>
<div class="span1 text-center"><h4>{{ _('Exact Match') }}</h4></div>
</div>
Expand All @@ -52,10 +57,10 @@
<div class="span1">
<input style="padding: 4px; margin: 2px;" type="checkbox" class="span12 text-right" data-bind="checked: enabled">
</div>
<div class="span3">
<div class="span2">
<input type="text" class="span12 text-right" data-bind="value: gcode, enable: enabled">
</div>
<div class="span3">
<div class="span2">
<input type="text" class="span12 text-right" data-bind="value: topic, enable: enabled">
</div>
<div class="span3">
Expand All @@ -70,9 +75,20 @@
</div>
</div>
<div class="row-fluid">
<div class="offset11 span1">
<div class="offset9 span1">
<button title="Add Sent Hook" class="btn btn-primary" data-bind="click: addSntEvent"><i class="fa fa-plus"></i></button>
</div>
</div>
<h3>{{ _('Defaults') }}</h3>
<div class="control-group">
<label class="control-label">Default re-fire threshold.</label>
<div class="controls">
<i class="icon icon-info-sign" title="Time in seconds between events for which re-firing the event is suppressed." data-toggle="tooltip"></i>
<div class="input-append">
<input type="number" class="input-block-level" size="3" min="0" max="360" step="1" data-bind="value: settings.settings.plugins.custom_gcode_events.default_refire_threshold">
<span class="add-on">s</span>
</div>
</div>
</div>
</form>
<!-- /ko -->

0 comments on commit 4ded625

Please sign in to comment.