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 Feb 28, 2021
1 parent 3e3bb3e commit 9d8c7b5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
28 changes: 28 additions & 0 deletions octoprint_psucontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import octoprint.plugin
from octoprint.server import user_permission
from octoprint.events import Events
from octoprint.util import RepeatedTimer
import time
import subprocess
import threading
Expand Down Expand Up @@ -103,6 +104,7 @@ def __init__(self):
self.autoOnTriggerGCodeCommands = ''
self._autoOnTriggerGCodeCommandsArray = []
self.enablePowerOffWarningDialog = True
self.enableIdleCountdownTimer = True
self.powerOffWhenIdle = False
self.idleTimeout = 0
self.idleIgnoreCommands = ''
Expand All @@ -120,6 +122,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 @@ -200,6 +205,9 @@ def on_settings_initialized(self):
self.enablePowerOffWarningDialog = self._settings.get_boolean(["enablePowerOffWarningDialog"])
self._logger.debug("enablePowerOffWarningDialog: %s" % self.enablePowerOffWarningDialog)

self.enableIdleCountdownTimer = self._settings.get_boolean(["enableIdleCountdownTimer"])
self._logger.debug("enableIdleCountdownTimer: %s" % self.enableIdleCountdownTimer)

self.powerOffWhenIdle = self._settings.get_boolean(["powerOffWhenIdle"])
self._logger.debug("powerOffWhenIdle: %s" % self.powerOffWhenIdle)

Expand Down Expand Up @@ -393,22 +401,40 @@ def _check_psu_state(self):
self._check_psu_state_event.wait(self.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.powerOffWhenIdle or not self.enableIdleCountdownTimer or self._printer.is_printing() or self._printer.is_paused():
self.idleTimeLeft = None
else:
self.idleTimeLeft = time.strftime("%-M:%S", time.gmtime((self.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.powerOffWhenIdle and self.isPSUOn:
self._idleTimer = ResettableTimer(self.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 Expand Up @@ -663,6 +689,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 @@ -707,6 +734,7 @@ def on_settings_save(self, data):
self.idleTimeout = self._settings.get_int(["idleTimeout"])
self.idleIgnoreCommands = self._settings.get(["idleIgnoreCommands"])
self.enablePowerOffWarningDialog = self._settings.get_boolean(["enablePowerOffWarningDialog"])
self.enableIdleCountdownTimer = self._settings.get_boolean(["enableIdleCountdownTimer"])
self._idleIgnoreCommandsArray = self.idleIgnoreCommands.split(',')
self.idleTimeoutWaitTemp = self._settings.get_int(["idleTimeoutWaitTemp"])

Expand Down
7 changes: 6 additions & 1 deletion octoprint_psucontrol/static/js/psucontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ $(function() {

self.settingsViewModel = parameters[0]
self.loginState = parameters[1];

self.settings = undefined;
self.scripts_gcode_psucontrol_post_on = ko.observable(undefined);
self.scripts_gcode_psucontrol_pre_off = ko.observable(undefined);

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

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

Expand Down Expand Up @@ -78,6 +79,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: hasGPIO() && (settings.plugins.psucontrol.switchingMethod() === "GPIO" || settings.plugins.psucontrol.sensingMethod() === "GPIO") -->
Expand Down

0 comments on commit 9d8c7b5

Please sign in to comment.