Skip to content

Commit

Permalink
Feat: specify the printer relay (#203)
Browse files Browse the repository at this point in the history
* Introducing printer relay setting.

* Introducing a migration.

* A bit logging.

* Updating the settings version test.

* Fixing the condition.

* Another way: common setting - printer, incl. template implementation.

* Updating the bindings test.

* Fix iteration in tests.

* Updating snapshot.

* Fixing the default scope approach in handle_plugin_event method.

* Testing the migraion script.

* Ref: extracting some vars in template.

* Extracting one more var for shortening the expression.

* Updating snapshot.
  • Loading branch information
RobinTail authored Sep 15, 2023
1 parent 779f3f3 commit 4cdfe47
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 20 deletions.
4 changes: 3 additions & 1 deletion octoprint_octorelay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ def on_event(self, event, payload):
#elif event == Events.PRINT_CANCELLED:
# self.print_stopped()

def handle_plugin_event(self, event, scope = RELAY_INDEXES):
def handle_plugin_event(self, event, scope = None):
if scope is None:
scope = RELAY_INDEXES
self._logger.debug(f"Handling the plugin event {event} having scope: {scope}")
settings = self._settings.get([], merged=True) # expensive
needs_ui_update = False
Expand Down
9 changes: 6 additions & 3 deletions octoprint_octorelay/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
PREEMPTIVE_CANCELLATION_CUTOFF = 2

# Versioning of the plugin's default settings described below
SETTINGS_VERSION = 3
SETTINGS_VERSION = 4

# Plugin's default settings, immutable getter
# Warning: every amendment or deletion of these settings requires:
# - to increase the SETTINGS_VERSION above
# - and migration to preserve user's previous configuration intact
def get_default_settings():
return {
"common": {
"printer": "r2"
},
"r1": {
"active": False,
"relay_pin": 4,
Expand Down Expand Up @@ -274,8 +277,8 @@ def get_default_settings():
},
}

# Keys of the default settings, used for iterations: [r1...r8]
RELAY_INDEXES = get_default_settings().keys()
# Keys for iterating the relay settings [r1..r8]
RELAY_INDEXES = list(map(lambda x: f"r{x}", range(1,9)))

# Plugin templates, immutable getter
def get_templates():
Expand Down
11 changes: 10 additions & 1 deletion octoprint_octorelay/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ def v2(settings, logger):
logger.debug(f"Replacing it with: {after}")
settings.set([index], after)

def v3(settings, logger):
"""Migration from v3 to v4"""
# There was no common/printer setting
before = settings.get(["r2"]) # the r2 supposed to be the printer relay by default
logger.debug(f"Relay r2 stored settings: {before}")
printer_relay = "r2" if before.get("label_text") is None else None
logger.debug(f"Setting the printer relay index {printer_relay}")
settings.set(["common"], { "printer": printer_relay })

# List of migration functions starting from v0->v1
migrators = [ v0, v1, v2 ]
migrators = [ v0, v1, v2, v3 ]

def migrate(current: int, settings, logger):
# Current version number corresponds to the list index to begin migrations from
Expand Down
31 changes: 29 additions & 2 deletions octoprint_octorelay/templates/octorelay_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
{% endfor %}
</ul>

<div class="tab-content">
<div class="tab-content" data-bind="using: settings.plugins.octorelay">
{% for n in range(1,9) %}
<div
id="relay_settings_{{n}}"
class="tab-pane fade"
data-bind="css: { 'active in': 1 === {{n}} }, using: settings.plugins.octorelay.r{{n}}"
data-bind="css: { 'active in': 1 === {{n}} }, using: r{{n}}"
>
<div class="control-group">
<label class="control-label">{{ _('Active') }}</label>
Expand Down Expand Up @@ -61,6 +61,33 @@
</div>
</div>

<div class="control-group">
<label class="control-label">{{ _('This is printer relay') }}</label>
<div class="controls">
<div class="btn-group">
{% for value, option in plugin_octorelay_boolean.items() %}
<!--ko let: {
isThisRelay: $parent.common.printer() === 'r{{n}}',
isAnotherRelay: $parent.common.printer() !== 'r{{n}}',
radioValue: {{value}} ? 'r{{n}}' : null
} -->
<!--ko let: { classBinding: {
'active btn-{{option.color}}': {{value}} ? isThisRelay : isAnotherRelay
} } -->
<label class="btn" data-bind="css: classBinding">
<input
type="radio"
data-bind="checkedValue: radioValue, checked: $parent.common.printer"
/>
{{ _(option.caption) }}
</label>
<!--/ko-->
<!--/ko-->
{% endfor %}
</div>
</div>
</div>

{% for state in ["on", "off"] %}
<div class="control-group">
<label class="control-label">
Expand Down
Loading

0 comments on commit 4cdfe47

Please sign in to comment.