diff --git a/octoprint_psucontrol/__init__.py b/octoprint_psucontrol/__init__.py index e98c427..8006627 100644 --- a/octoprint_psucontrol/__init__.py +++ b/octoprint_psucontrol/__init__.py @@ -7,6 +7,7 @@ import octoprint.plugin from octoprint.events import Events +from octoprint.util import RepeatedTimer import time import subprocess import threading @@ -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 = {} @@ -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', @@ -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: diff --git a/octoprint_psucontrol/static/js/psucontrol.js b/octoprint_psucontrol/static/js/psucontrol.js index 6c9b7e3..3fbafa5 100644 --- a/octoprint_psucontrol/static/js/psucontrol.js +++ b/octoprint_psucontrol/static/js/psucontrol.js @@ -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"); @@ -102,6 +103,10 @@ $(function() { if (data.isPSUOn !== undefined) { self.isPSUOn(data.isPSUOn); } + + if (data.idleTimeLeft !== undefined) { + self.idleTimeLeft(data.idleTimeLeft); + } }; self.togglePSU = function() { diff --git a/octoprint_psucontrol/templates/psucontrol_navbar.jinja2 b/octoprint_psucontrol/templates/psucontrol_navbar.jinja2 index f4b574a..374ef47 100644 --- a/octoprint_psucontrol/templates/psucontrol_navbar.jinja2 +++ b/octoprint_psucontrol/templates/psucontrol_navbar.jinja2 @@ -1,3 +1,4 @@ diff --git a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 index df6b08a..b055aed 100644 --- a/octoprint_psucontrol/templates/psucontrol_settings.jinja2 +++ b/octoprint_psucontrol/templates/psucontrol_settings.jinja2 @@ -5,6 +5,9 @@ +