Skip to content

Commit

Permalink
🚀 3.9.1, #209
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTail authored Sep 20, 2023
2 parents 1581874 + dddac77 commit df6d58a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Each relay has the following settings *(in order of appearance)*:
| Icon `ON` / `OFF` | An image or emoji to indicate the relay state (supports HTML) |
| GPIO Number | The [GPIO pin on the Raspberry Pi](https://pinout.xyz/) |
| Inverted output | For normally closed relay: the relay is `ON` without power |
| Warn if turning `OFF` | Enables a confirmation dialog when turning the relay `OFF` |
| Confirm turning `OFF` | Enables a confirmation dialog when turning the relay `OFF` |
| Alert on switches ahead | Notifies on upcoming switch with an ability to cancel it |
| **Events:** | Behavior customization (automation) |
| on Startup | The state to switch the relay to when OctoPrint started |
Expand Down
27 changes: 19 additions & 8 deletions octoprint_octorelay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .const import (
get_default_settings, get_templates, get_ui_vars, RELAY_INDEXES, ASSETS, SWITCH_PERMISSION, UPDATES_CONFIG,
POLLING_INTERVAL, UPDATE_COMMAND, GET_STATUS_COMMAND, LIST_ALL_COMMAND, AT_COMMAND, SETTINGS_VERSION,
STARTUP, PRINTING_STOPPED, PRINTING_STARTED, CANCELLATION_EXCEPTIONS, PREEMPTIVE_CANCELLATION_CUTOFF,
STARTUP, PRINTING_STOPPED, PRINTING_STARTED, PRIORITIES, FALLBACK_PRIORITY, PREEMPTIVE_CANCELLATION_CUTOFF,
CANCEL_TASK_COMMAND, USER_ACTION, TURNED_ON
)
from .driver import Relay
Expand Down Expand Up @@ -187,16 +187,17 @@ def handle_plugin_event(self, event, scope = None):
needs_ui_update = False
for index in scope:
if bool(settings[index]["active"]):
did_cancel = self.cancel_tasks(subject = index, initiator = event) # issue 205
needs_ui_update = needs_ui_update or did_cancel
target = settings[index]["rules"][event]["state"]
if target is not None:
target = bool(target)
if target and event == TURNED_ON:
self._logger.debug(f"Skipping {index} to avoid infinite loop")
continue # avoid infinite loop
self.cancel_tasks(subject = index, initiator = event)
delay = int(settings[index]["rules"][event]["delay"] or 0)
if delay == 0:
self.toggle_relay(index, target)
self.toggle_relay(index, target) # UI update conducted by the polling thread
else:
self._logger.debug(f"Postponing the switching of the relay {index} by {delay}s")
task = Task(
Expand Down Expand Up @@ -237,15 +238,19 @@ def toggle_relay(self, index, target: Optional[bool] = None):
if state:
self.handle_plugin_event(TURNED_ON, scope = [index])

def cancel_tasks(self, subject: str, initiator: str, target: Optional[bool] = None, owner: Optional[str] = None):
def cancel_tasks(
self, subject: str, initiator: str,
target: Optional[bool] = None, owner: Optional[str] = None
) -> bool: # returns True when cancelled some tasks
self._logger.debug(f"Cancelling tasks by request from {initiator} for relay {subject}")
exceptions = CANCELLATION_EXCEPTIONS.get(initiator) or []
priority = PRIORITIES.get(initiator) or FALLBACK_PRIORITY
count_before = len(self.tasks)
def handler(task: Task):
not_exception = task.owner not in exceptions
lower_priority = (PRIORITIES.get(task.owner) or FALLBACK_PRIORITY) >= priority
same_subject = subject == task.subject
same_target = True if target is None else task.target == target
same_owner = True if owner is None else task.owner == owner
if same_subject and not_exception and same_target and same_owner:
if same_subject and lower_priority and same_target and same_owner:
try:
task.cancel_timer()
self._logger.info(f"Cancelled the task: {task}")
Expand All @@ -254,7 +259,13 @@ def handler(task: Task):
return False # exclude
return True # include
self.tasks = list(filter(handler, self.tasks))
self._logger.debug("The cancelled tasks removed from the registry")
count_cancelled = count_before - len(self.tasks)
did_cancel = count_cancelled > 0
self._logger.debug(
f"Cancelled ({count_cancelled}) tasks and removed from the registry"
if did_cancel else "No tasks cancelled"
)
return did_cancel

def run_system_command(self, cmd):
if cmd:
Expand Down
16 changes: 13 additions & 3 deletions octoprint_octorelay/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@
TURNED_ON = "TURNED_ON"
USER_ACTION = "USER_ACTION"

# Task cancellation exceptions
# { eventHappened: [ events which postponed timers should NOT be cancelled ]
CANCELLATION_EXCEPTIONS = {}
# Event having higher or same priority (lower or equal number here) cancells the tasks placed by previous events
# Highest priority is 1.
PRIORITIES = {
USER_ACTION: 1,
STARTUP: 2,
PRINTING_STARTED: 2,
PRINTING_STOPPED: 2,
TURNED_ON: 3
}

# Missing events above will be treated as ones having this priority
FALLBACK_PRIORITY = 5

# min seconds before the task can be cancelled
PREEMPTIVE_CANCELLATION_CUTOFF = 2

Expand Down
3 changes: 2 additions & 1 deletion octoprint_octorelay/static/css/octorelay.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
color: inherit;
}

#settings_plugin_octorelay .control-label span.label {
#settings_plugin_octorelay .control-label span.label,
#settings_plugin_octorelay .help-inline span.label {
zoom: 0.85; /* not scale */
}

Expand Down
9 changes: 8 additions & 1 deletion octoprint_octorelay/templates/octorelay_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
<!--/ko-->
{% endfor %}
</div>
<span class="help-inline">
{{ _('Disconnects when turning') }}
<span class="label">{{ _('OFF') }}</span>
</span>
</div>
</div>

Expand Down Expand Up @@ -163,7 +167,7 @@

<div class="control-group">
<label class="control-label">
{{ _('Warn if turning') }}
{{ _('Confirm turning') }}
<span class="label">{{ _('OFF') }}</span>
</label>
<div class="controls">
Expand All @@ -183,6 +187,9 @@
<!--/ko-->
{% endfor %}
</div>
<span class="help-inline">
{{ _('Enables an extra dialog') }}
</span>
</div>
</div>

Expand Down
72 changes: 64 additions & 8 deletions tests/snapshots/snap_test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -343,7 +347,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -376,6 +380,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -821,6 +828,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -929,7 +940,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -962,6 +973,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -1407,6 +1421,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -1515,7 +1533,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -1548,6 +1566,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -1993,6 +2014,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -2101,7 +2126,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -2134,6 +2159,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -2579,6 +2607,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -2687,7 +2719,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -2720,6 +2752,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -3165,6 +3200,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -3273,7 +3312,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -3306,6 +3345,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -3751,6 +3793,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -3859,7 +3905,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -3892,6 +3938,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down Expand Up @@ -4337,6 +4386,10 @@
<!--/ko-->
</div>
<span class="help-inline">
Disconnects when turning
<span class="label">OFF</span>
</span>
</div>
</div>
Expand Down Expand Up @@ -4445,7 +4498,7 @@
<div class="control-group">
<label class="control-label">
Warn if turning
Confirm turning
<span class="label">OFF</span>
</label>
<div class="controls">
Expand Down Expand Up @@ -4478,6 +4531,9 @@
<!--/ko-->
</div>
<span class="help-inline">
Enables an extra dialog
</span>
</div>
</div>
Expand Down
Loading

0 comments on commit df6d58a

Please sign in to comment.