From 2a1323ba2910c559dcb84fe238b4dcc22c0904bd Mon Sep 17 00:00:00 2001 From: cp2004 <31997505+cp2004@users.noreply.github.com> Date: Tue, 16 Nov 2021 18:36:57 +0000 Subject: [PATCH] :recycle: Rename to generic 'match', add some error handling --- octoprint_ws281x_led_status/triggers.py | 84 +++++++++++++++---------- 1 file changed, 51 insertions(+), 33 deletions(-) diff --git a/octoprint_ws281x_led_status/triggers.py b/octoprint_ws281x_led_status/triggers.py index b2839cc..6d573da 100644 --- a/octoprint_ws281x_led_status/triggers.py +++ b/octoprint_ws281x_led_status/triggers.py @@ -35,7 +35,7 @@ def __init__(self, effect_queue): self.effect_queue = effect_queue - def register_atcommand_handler(self, command, effect, color, delay): + def register_atcommand_handler(self, match, effect, color, delay): """ command: @WS CUSTOM TODO keep this concise, does it need 'CUSTOM' or is there something shorter? effect: Name from constants.EFFECTS @@ -44,42 +44,42 @@ def register_atcommand_handler(self, command, effect, color, delay): """ self.at_command_subscriptions.append( { - "command": command, + "match": match, "effect": effect, "color": color, "delay": delay, } ) - def register_event_handler(self, event, effect, color, delay): + def register_event_handler(self, match, effect, color, delay): """ event: event from `octoprint.events.Events` effect: Name from constants.EFFECTS color: Hex colour delay: ms between frames """ - if event not in list_available_events(): - self._logger.warning("Event ({}) not available, ignoring".format(event)) + if match not in list_available_events(): + self._logger.warning("Event ({}) not available, ignoring".format(match)) return self.at_command_subscriptions.append( { - "event": event, + "match": match, "effect": effect, "color": color, "delay": delay, } ) - def register_gcode_handler(self, match_type, match_pattern, effect, color, delay): + def register_gcode_handler(self, match_type, match, effect, color, delay): """ match_type: 'gcode', 'exact', 'regex' - match_pattern: string to match command against + match: string to match command against effect: Name from constants.EFFECTS color: Hex colour delay: ms between frames """ subscription = { - "pattern": match_pattern, + "match": match, "effect": effect, "color": color, "delay": delay, @@ -102,34 +102,52 @@ def process_settings(self, settings): # Convert the settings into subscriptions self.reset_subscriptions() for entry in settings["atcommand"]: - self.register_atcommand_handler( - entry["command"], - entry["effect"], - entry["color"], - entry["delay"], - ) + try: + self.register_atcommand_handler( + entry["match"], + entry["effect"], + entry["color"], + entry["delay"], + ) + except Exception as e: + self._logger.warning( + "Failed to add atcommand entry to subscriptions, ignoring" + ) + self._logger.exception(e) for entry in settings["event"]: - self.register_event_handler( - entry["event"], - entry["effect"], - entry["color"], - entry["delay"], - ) + try: + self.register_event_handler( + entry["match"], + entry["effect"], + entry["color"], + entry["delay"], + ) + except Exception as e: + self._logger.warning( + "Failed to add event entry to subscriptions, ignoring" + ) + self._logger.exception(e) for entry in settings["gcode"]: - self.register_gcode_handler( - entry["match_type"], - entry["match_pattern"], - entry["effect"], - entry["color"], - entry["delay"], - ) + try: + self.register_gcode_handler( + entry["match_type"], + entry["match"], + entry["effect"], + entry["color"], + entry["delay"], + ) + except Exception as e: + self._logger.warning( + "Failed to add gcode entry to subscriptions, ignoring" + ) + self._logger.exception(e) def on_event(self, event): if len(self.event_subscriptions): for e in self.event_subscriptions: - if e["event"] == event: + if e["match"] == event: self.effect_queue.put( { "type": "custom", @@ -145,7 +163,7 @@ def on_at_command(self, command): """ if len(self.at_command_subscriptions): for at_command in self.at_command_subscriptions: - if at_command["command"].upper() == command: + if at_command["match"].upper() == command: self.effect_queue.put( { "type": "custom", @@ -160,7 +178,7 @@ def on_gcode_command(self, gcode, cmd): if len(self.gcode_command_subscriptions): for gcode_command in self.gcode_command_subscriptions: # Match only G/M code - if gcode_command["match_pattern"] == gcode: + if gcode_command["match"] == gcode: self.effect_queue.put( { "type": "custom", @@ -172,7 +190,7 @@ def on_gcode_command(self, gcode, cmd): if len(self.gcode_exact_subscriptions): for gcode_exact in self.gcode_exact_subscriptions: # Match whole line sent to printer - if gcode_exact["match_pattern"] == cmd: + if gcode_exact["match"] == cmd: self.effect_queue.put( { "type": "custom", @@ -185,7 +203,7 @@ def on_gcode_command(self, gcode, cmd): if len(self.gcode_regex_subscriptions): for gcode_regex in self.gcode_regex_subscriptions: # Match cmd by regex - if re.match(gcode_regex["match_pattern"], cmd): + if re.match(gcode_regex["match"], cmd): self.effect_queue.put( { "type": "custom",