From a3e770158c5404ee04d921ea7f2dcf3e7fa54dc5 Mon Sep 17 00:00:00 2001 From: David Zingg Date: Sun, 28 Jun 2020 15:56:09 +0200 Subject: [PATCH] #21 Add PowerOFF when print is finished --- .idea/workspace.xml | 19 +++--- octoprint_mystromswitch/__init__.py | 66 ++++++++++++++----- .../templates/mystromswitch_settings.jinja2 | 6 +- 3 files changed, 64 insertions(+), 27 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 13eb9d6..17a195f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,8 @@ - + + diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index 632924d..0d27a54 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -30,7 +30,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.shutdownAfterPrintFinished = False self.powerOffAfterPrintFinished = False - self._timer = None + self._status_timer = None + self._abort_timer = None + self._wait_for_timelapse_timer = None self.energy = 0 self.lastTimeStamp = 0 @@ -79,15 +81,57 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, icon="power-off"), dict(type="settings", custom_bindings=False)] + def _shutdown_timer_start(self): + if self._abort_timer is not None: + return + + if self._wait_for_timelapse_timer is not None: + self._wait_for_timelapse_timer.cancel() + + self._logger.info("Starting abort shutdown timer.") + + self._timeout_value = self.shutdownDelay + self._abort_timer = RepeatedTimer(1, self._shutdown_timer_task) + self._abort_timer.start() + + def _shutdown_timer_task(self): + if self._timeout_value is None: + return + + self._timeout_value -= 1 + if self._timeout_value <= 0: + if self._wait_for_timelapse_timer is not None: + self._wait_for_timelapse_timer.cancel() + self._wait_for_timelapse_timer = None + if self._abort_timer is not None: + self._abort_timer.cancel() + self._abort_timer = None + if self.shutdownAfterPrintFinished: + self._shutdown_system() + elif self.powerOffAfterPrintFinished: + self._setRelaisState(False) + def _status_timer_start(self): - if self._timer is not None: - self._timer.cancel() + if self._status_timer is not None: + self._status_timer.cancel() self._logger.info("Canceling Timer") if self.intervall >= 1 and self.ip is not None: self._logger.info("Starting timer") - self._timer = RepeatedTimer(self.intervall, self._status_timer_task) - self._timer.start() + self._status_timer = RepeatedTimer(self.intervall, self._status_timer_task) + self._status_timer.start() + + def _shutdown_system(self): + self._powerCycleRelais(False, self.powerOffDelay) + if self.shutdownAfterPrintFinished: + shutdown_command = self._settings.global_get(["server", "commands", "systemShutdownCommand"]) + self._logger.info("Shutting down system with command: {command}".format(command=shutdown_command)) + try: + import sarge + p = sarge.run(shutdown_command, async=True) + except Exception as e: + self._logger.exception("Error when shutting down: {error}".format(error=e)) + return def _status_timer_task(self): if self.ip is not None: @@ -199,7 +243,7 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.shutdownAfterPrintFinished = True elif command == "disableShutdownAfterFinish": self._logger.info("disableShutdownAfterFinish") - self.disableShutdownAfterFinish = False + self.shutdownAfterPrintFinished = False elif command == "enablePowerOffAfterFinish": self._logger.info("enablePowerOffAfterFinish") self.powerOffAfterPrintFinished = True @@ -207,7 +251,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.info("disablePowerOffAfterFinish") self.powerOffAfterPrintFinished = False - def get_api_commands(self): return dict( enableRelais=[], @@ -275,13 +318,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, def on_event(self, event, payload): - if event == Events.CLIENT_OPENED: - #self._plugin_manager.send_plugin_message(self._identifier, - # dict(automaticShutdownEnabled=self.shutdownAfterPrintFinished)) - #self._plugin_manager.send_plugin_message(self._identifier, - # dict(automaticPowerOffEnabled=self.powerOffAfterPrintFinished)) - return - if not self.shutdownAfterPrintFinished and not self.powerOffAfterPrintFinished: return @@ -302,7 +338,7 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, if (timelapse_type is not None and timelapse_type != "off"): self._wait_for_timelapse_start() else: - self._timer_start() + self._shutdown_timer_start() return def get_update_information(self): diff --git a/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 b/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 index 933b399..7a48b03 100644 --- a/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 +++ b/octoprint_mystromswitch/templates/mystromswitch_settings.jinja2 @@ -68,7 +68,7 @@ - {{ _('This setting shows the option to shut Octoprint down and switch your mySwitch off after your Print is finished.') }} + {{ _('This setting shows the option to shut Octoprint down and switch your mySwitch off after your Print is finished. Use the "Delay after print is finished" option to determine after how many seconds Octoprint will be shutted down. After this use "Turn Relais Off Delay" to determine after how many seconds the Relas will be switched off') }} @@ -77,11 +77,11 @@ - {{ _('This setting shows the option to switch your mySwitch off after your Print is finished') }} + {{ _('This setting shows the option to only switch your mySwitch off after your Print is finished. Use the "Delay after print is finished" Option to determine after how many seconds the Relais will switch off') }} -
+