Skip to content

Commit

Permalink
Add navbar idle countdown timer w/ settings toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
SSMCD committed May 22, 2022
1 parent 577c7f3 commit 30e547e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
23 changes: 22 additions & 1 deletion octoprint_psucontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import octoprint.plugin
from octoprint.events import Events
from octoprint.util import RepeatedTimer
import time
import subprocess
import threading
Expand Down Expand Up @@ -62,6 +63,9 @@ def __init__(self):
self._check_psu_state_thread = None
self._check_psu_state_event = threading.Event()
self._idleTimer = None
self._idleCountdown = None
self._idleStartTime = 0
self._idleTimeLeft = None
self._waitForHeaters = False
self._skipIdleTimer = False
self._configuredGPIOPins = {}
Expand Down Expand Up @@ -96,6 +100,7 @@ def get_settings_defaults(self):
autoOn = False,
autoOnTriggerGCodeCommands = "G0,G1,G2,G3,G10,G11,G28,G29,G32,M104,M106,M109,M140,M190",
enablePowerOffWarningDialog = True,
enableIdleCountdownTimer = True,
powerOffWhenIdle = False,
idleTimeout = 30,
idleIgnoreCommands = 'M105',
Expand Down Expand Up @@ -320,25 +325,41 @@ def _check_psu_state(self):
self._check_psu_state_event.wait(self.config['sensePollingInterval'])
self._check_psu_state_event.clear()

def _set_start_time(self):
self._idleStartTime = time.time()

def _refresh_countdown(self):
if self._idleStartTime == 0 or not self.config['powerOffWhenIdle'] or not self.config['enableIdleCountdownTimer'] or self._printer.is_printing() or self._printer.is_paused():
self.idleTimeLeft = None
else:
self.idleTimeLeft = time.strftime("%-M:%S", time.gmtime((self.config['idleTimeout'] * 60) - (time.time() - self._idleStartTime)))
self._plugin_manager.send_plugin_message(self._identifier, dict(idleTimeLeft=self.idleTimeLeft))

def _start_idle_timer(self):
self._stop_idle_timer()

if self.config['powerOffWhenIdle'] and self.isPSUOn:
self._idleTimer = ResettableTimer(self.config['idleTimeout'] * 60, self._idle_poweroff)
self._idleCountdown = RepeatedTimer(1.0, self._refresh_countdown)
self._idleTimer.start()
self._set_start_time()
self._idleCountdown.start()


def _stop_idle_timer(self):
if self._idleTimer:
self._idleTimer.cancel()
self._idleTimer = None

self._idleStartTime = 0
self._idleCountdown.cancel()
self._idleCountdown = None
self._refresh_countdown()

def _reset_idle_timer(self):
try:
if self._idleTimer.is_alive():
self._idleTimer.reset()
self._set_start_time()
else:
raise Exception()
except:
Expand Down
5 changes: 5 additions & 0 deletions octoprint_psucontrol/static/js/psucontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $(function() {
self.scripts_gcode_psucontrol_pre_off = ko.observable(undefined);

self.isPSUOn = ko.observable(undefined);
self.idleTimeLeft = ko.observable(undefined);

self.psu_indicator = $("#psucontrol_indicator");

Expand Down Expand Up @@ -102,6 +103,10 @@ $(function() {
if (data.isPSUOn !== undefined) {
self.isPSUOn(data.isPSUOn);
}

if (data.idleTimeLeft !== undefined) {
self.idleTimeLeft(data.idleTimeLeft);
}
};

self.togglePSU = function() {
Expand Down
1 change: 1 addition & 0 deletions octoprint_psucontrol/templates/psucontrol_navbar.jinja2
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<a id="psucontrol_indicator" class="pull-right" title="Toggle PSU" href="#" data-bind="click: function() { loginState.isUser() && $root.togglePSU(); }, visible: (isPSUOn() !== undefined)" style="display: none">
<i class="icon-bolt"></i>
<span data-bind="attr: { title: 'Idle Countdown' }, html: idleTimeLeft()"></span>
</a>
3 changes: 3 additions & 0 deletions octoprint_psucontrol/templates/psucontrol_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.psucontrol.enablePowerOffWarningDialog"> Show warning dialog when powering off via toggle button.
</label>
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.psucontrol.enableIdleCountdownTimer"> Show idle countdown timer in the navbar.
</label>
</div>
</div>
<!-- ko if: settings.plugins.psucontrol.switchingMethod() === "GPIO" || settings.plugins.psucontrol.sensingMethod() === "GPIO" -->
Expand Down

0 comments on commit 30e547e

Please sign in to comment.