From 85e18076a0d8a2e7fa37feab80ffb2fa331bf25e Mon Sep 17 00:00:00 2001 From: David Zingg Date: Wed, 1 Jul 2020 20:10:17 +0200 Subject: [PATCH] #21 Add PowerOFF when print is finished --- .idea/workspace.xml | 18 ++++----- octoprint_mystromswitch/__init__.py | 60 +++++++++++++++++++---------- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3cec675..9f9cb7c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,7 @@ - + diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index cd45b0e..7f8e003 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -27,8 +27,13 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.showShutdownOctopiOption = False self.showPowerOffPrintFinishOption = False self.shutdownDelay = 60 - self.shutdownAfterPrintFinished = False - self.powerOffAfterPrintFinished = False + + self.rememberShutdown = False + self.lastPowerOff = False + self.lastShutdown = False + + self.shutdownAfterPrintFinished = self.lastShutdown if self.rememberShutdown else False + self.powerOffAfterPrintFinished = self.lastPowerOff if self.rememberShutdown else False self._status_timer = None self._abort_timer = None @@ -69,6 +74,19 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.shutdownDelay = self._settings.get_int(["shutdownDelay"]) self._logger.debug("shutdownDelay: %s" % self.shutdownDelay) + self.rememberShutdown = self._settings.get_boolean(["rememberShutdown"]) + self._logger.debug("rememberShutdown: %s" % self.rememberShutdown) + + self.lastPowerOff = self._settings.get_int(["lastPowerOff"]) + self._logger.debug("lastPowerOff: %s" % self.lastPowerOff) + + self.lastShutdown = self._settings.get_int(["lastShutdown"]) + self._logger.debug("lastShutdown: %s" % self.lastShutdown) + + if self.rememberShutdown: + self.powerOffAfterPrintFinished = self.lastPowerOff + self.shutdownAfterPrintFinished = self.lastShutdown + self._status_timer_start() def get_assets(self): @@ -123,9 +141,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, if self._abort_timer is not None: self._abort_timer.cancel() self._abort_timer = None - if self.shutdownAfterPrintFinished: + if self.shutdownAfterPrintFinished and self.showShutdownOctopiOption: self._shutdown_system() - elif self.powerOffAfterPrintFinished: + elif self.powerOffAfterPrintFinished and self.showPowerOffPrintFinishOption: self._logger.info("only Shutdown Relais") self._setRelaisState(False) @@ -151,7 +169,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.exception("Error when shutting down: {error}".format(error=e)) return - def _status_timer_task(self): if self.ip is not None: try: @@ -188,7 +205,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, "v": self.powerOffAfterPrintFinished} self._plugin_manager.send_plugin_message(self._identifier, data) - def _setRelaisState(self, newState): nbRetry = 0 value = '0' @@ -207,7 +223,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.info("Error during set Relais state") nbRetry = nbRetry + 1 - # Sets the switch to a specific inverse newState, # waits for a specified amount of time (max 3h), # then sets the the switch to the newState. @@ -235,7 +250,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.exception(exp) nbRetry = nbRetry + 1 - def _toggleRelay(self): nbRetry = 0 while nbRetry < 3: @@ -250,7 +264,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.info("Error during toggle Relais state") nbRetry = nbRetry + 1 - def on_api_command(self, command, data): if command == "enableRelais": self._logger.info("enableRelais") @@ -264,16 +277,23 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, elif command == "enableShutdownAfterFinish": self._logger.info("enableShutdownAfterFinish") self.shutdownAfterPrintFinished = True + if self.rememberShutdown: + self.lastShutdown = self.shutdownAfterPrintFinished elif command == "disableShutdownAfterFinish": self._logger.info("disableShutdownAfterFinish") self.shutdownAfterPrintFinished = False + if self.rememberShutdown: + self.lastShutdown = self.shutdownAfterPrintFinished elif command == "enablePowerOffAfterFinish": self._logger.info("enablePowerOffAfterFinish") self.powerOffAfterPrintFinished = True + if self.rememberShutdown: + self.lastPowerOff = self.powerOffAfterPrintFinished elif command == "disablePowerOffAfterFinish": self._logger.info("disablePowerOffAfterFinish") self.powerOffAfterPrintFinished = False - + if self.rememberShutdown: + self.lastPowerOff = self.powerOffAfterPrintFinished def get_api_commands(self): return dict( @@ -286,13 +306,11 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, enablePowerOffAfterFinish=[] ) - def on_after_startup(self): if self.powerOnOnStart: self._logger.info("Turn on Relais on Start") self._setRelaisState(True) - def on_shutdown(self): self._logger.info("on_shutdown_event") if self.powerOffOnShutdown: @@ -303,7 +321,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._logger.info("Turn off Relais on Shutdown Delayed") self._powerCycleRelais(False, self.powerOffDelay) - def on_settings_migrate(self, target, current): if target > current: if current <= 1: @@ -316,11 +333,13 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.showShutdownOctopiOption = False self.showPowerOffPrintFinishOption = False self.shutdownDelay = 60 - + if current <= 4: + self.lastShutdown = False + self.lastPowerOff = False + self.rememberShutdown = False def get_settings_version(self): - return 4 - + return 5 def get_settings_defaults(self): return dict( @@ -332,22 +351,22 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, powerOffDelay=0, showShutdownOctopiOption=False, showPowerOffPrintFinishOption=False, - shutdownDelay=60 + shutdownDelay=60, + lastShutdown=False, + lastPowerOff=False, + rememberShutdown=False ) - def get_settings_restricted_paths(self): return dict(admin=[ ['ip'] ]) - def on_settings_save(self, data): self._logger.info("on_settings_save") octoprint.plugin.SettingsPlugin.on_settings_save(self, data) self.initialize() - def on_event(self, event, payload): if not self.shutdownAfterPrintFinished and not self.powerOffAfterPrintFinished: return @@ -372,7 +391,6 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self._shutdown_timer_start() return - def get_update_information(self): return dict( mystromswitch=dict(