Add PowerOFF when print is finished

neue Einstellmöglichkeiten anzeigen
This commit is contained in:
David Zingg
2020-02-29 16:08:03 +01:00
parent c31d7b7bca
commit 3d7cb06446
6 changed files with 373 additions and 15 deletions

@ -1,11 +1,10 @@
# coding=utf-8
from __future__ import absolute_import
import ssl
import time
import octoprint.plugin
import requests
import ssl
import time
from octoprint.util import RepeatedTimer
@ -13,6 +12,7 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.StartupPlugin,
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.SimpleApiPlugin,
octoprint.plugin.ShutdownPlugin):
@ -23,6 +23,11 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
self.powerOnOnStart = False
self.powerOffOnShutdown = False
self.powerOffDelay = 0
self.showShutdownOctopiOption = False
self.showPowerOffPrintFinishOption = False
self.shutdownDelay = 60
self.shutdownAfterPrintFinished = False
self.powerOffAfterPrintFinished = False
self._timer = None
@ -52,7 +57,16 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
self.powerOffDelay = self._settings.get_int(["powerOffDelay"])
self._logger.debug("powerOffDelay: %s" % self.powerOffDelay)
self._timer_start()
self.showShutdownOctopiOption = self._settings.get_boolean(["showShutdownOctopiOption"])
self._logger.debug("showShutdownOctopiOption: %s" % self.showShutdownOctopiOption)
self.showPowerOffPrintFinishOption = self._settings.get_boolean(["showPowerOffPrintFinishOption"])
self._logger.debug("showPowerOffPrintFinishOption: %s" % self.showPowerOffPrintFinishOption)
self.shutdownDelay = self._settings.get_int(["shutdownDelay"])
self._logger.debug("shutdownDelay: %s" % self.shutdownDelay)
self._status_timer_start()
def get_assets(self):
return dict(js=["js/mystromswitch.js"], css=["css/mystromswitch.css"])
@ -64,17 +78,17 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
icon="power-off"),
dict(type="settings", custom_bindings=False)]
def _timer_start(self):
def _status_timer_start(self):
if self._timer is not None:
self._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._timer_task)
self._timer = RepeatedTimer(self.intervall, self._status_timer_task)
self._timer.start()
def _timer_task(self):
def _status_timer_task(self):
if self.ip is not None:
try:
try:
@ -170,6 +184,18 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
elif command == "toggleRelais":
self._logger.info("toggleRelais")
self._toggleRelay()
elif command == "enableShutdownAfterFinish":
self._logger.info("enableShutdownAfterFinish")
self.shutdownAfterPrintFinished = True
elif command == "disableShutdownAfterFinish":
self._logger.info("disableShutdownAfterFinish")
self.disableShutdownAfterFinish = False
elif command == "enablePowerOffAfterFinish":
self._logger.info("enablePowerOffAfterFinish")
self.powerOffAfterPrintFinished = True
elif command == "disablePowerOffAfterFinish":
self._logger.info("disablePowerOffAfterFinish")
self.powerOffAfterPrintFinished = False
def get_api_commands(self):
return dict(
@ -197,14 +223,17 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
if target > current:
if current <= 1:
self.onOffButtonEnabled = False
pass
if current <= 2:
self.powerOnOnStart = False,
self.powerOffOnShutdown = False,
self.powerOffDelay = 0
if current <= 3:
self.showShutdownOctopiOption = False
self.showPowerOffPrintFinishOption = False
self.shutdownDelay = 60
def get_settings_version(self):
return 3
return 4
def get_settings_defaults(self):
return dict(
@ -213,7 +242,10 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
onOffButtonEnabled=False,
powerOnOnStart=False,
powerOffOnShutdown=False,
powerOffDelay=0
powerOffDelay=0,
showShutdownOctopiOption=False,
showPowerOffPrintFinishOption=False,
shutdownDelay=60
)
def get_settings_restricted_paths(self):
@ -226,6 +258,31 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
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
if not self._settings.global_get(["server", "commands", "systemShutdownCommand"]):
self._logger.warning("systemShutdownCommand is not defined. Aborting shutdown...")
return
if event not in [Events.PRINT_DONE, Events.PRINT_FAILED]:
return
if event == Events.PRINT_FAILED and not self._printer.is_closed_or_error():
# Cancelled job
return
if event in [Events.PRINT_DONE, Events.PRINT_FAILED]:
webcam_config = self._settings.global_get(["webcam", "timelapse"], merged=True)
timelapse_type = webcam_config["type"]
if (timelapse_type is not None and timelapse_type != "off"):
self._wait_for_timelapse_start()
else:
self._timer_start()
return
def get_update_information(self):
return dict(
mystromswitch=dict(

@ -7,6 +7,8 @@ $(function() {
self.printer = parameters[2];
self.onOffButtonEnabled = ko.observable();
self.showShutdownOctopiOption = ko.observable();
self.showPowerOffPrintFinishOption = ko.observable();
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
self.mystromswitchEnergyValue = document.getElementById("mystromswitchEnergyValue")
@ -22,11 +24,46 @@ $(function() {
})
}
self.onmystromswitchEvent = function() {
//self.onmystromswitchEvent = function() {
//}
//self.onOffButtonEnabled.subscribe(self.onmystromswitchEvent, self);
self.onAutomaticShutdownEnabledChanged = function(){
var cmd = "disableShutdownAfterFinish";
if (self.automaticShutdownEnabled()) {
var cmd = "enableShutdownAfterFinish";
}
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: cmd
}),
contentType: "application/json; charset=UTF-8"
})
}
self.onOffButtonEnabled.subscribe(self.onmystromswitchEvent, self);
self.onAutomaticPowerOffEnabledChanged = function(){
var cmd = "disablePowerOffAfterFinish";
if (self.automaticShutdownEnabled()) {
var cmd = "enablePowerOffAfterFinish";
}
$.ajax({
url: API_BASEURL + "plugin/mystromswitch",
type: "POST",
dataType: "json",
data: JSON.stringify({
command: cmd
}),
contentType: "application/json; charset=UTF-8"
})
}
self.automaticShutdownEnabled.subscribe(self.onAutomaticShutdownEnabledChanged,self);
self.automaticPowerOffEnabled.subscribe(self.onAutomaticPowerOffEnabledChanged,self);
self.onDataUpdaterPluginMessage = function(plugin, data) {
if (plugin != "mystromswitch" && plugin != "octoprint_mystromswitch") {

@ -61,4 +61,35 @@
<span class="help-block"><small>{{ _('Delay in seconds after octoprint is shutted down to switch off relays. This settings is recommended to make sure that Raspberry Pi is completely shutted down when you switch power off. Switching off when your Raspberry Pi is running could lead to unrepairable damage to your SD Card!') }}</small></span>
</div>
</div>
<h4>Automatic Power Off Features</h4>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.showShutdownOctopiOption">Show option to automatic shutdown Octoprint after Print is finished
</label>
<span class="help-block"><small>{{ _('This setting shows the option to shut Octoprint down and switch your mySwitch off after your Print is finished.') }}</small></span>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.showPowerOffPrintFinishOption">Show option to turn off Relais after Print is finished
</label>
<span class="help-block"><small>{{ _('This setting shows the option to switch your mySwitch off after your Print is finished') }}</small></span>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Delay after print is finished') }}</label>
<div class="controls">
<div class="input-append">
<input type="number" class="input-block-level" data-bind="value: settings.plugins.mystromswitch.shutdownDelay">
<span class="add-on">seconds</span>
</div>
<span class="help-block"><small>{{ _('Delay in seconds after after print is finished') }}</small></span>
</div>
</div>
</form>

@ -2,4 +2,13 @@
<label class="control-label" id="mystromswitchPowerValue">Powerconsumption 0.0W</label>
<label class="control-label" id="mystromswitchEnergyValue">Energy 0.0Wh</label>
<button class="btn btn-primary" data-bind="click: onToggleRelayEvent, visible : onOffButtonEnabled">{{ _('Toggle Relais') }}</button>
<label class="checkbox">
<input type="checkbox" data-bind="enable: loginState.isUser(), checked: automaticShutdownEnabled, visible:showShutdownOctopiOption">
{{ _('Shutdown Octoprint after print finishes') }}
</label>
<label class="checkbox">
<input type="checkbox" data-bind="enable: loginState.isUser(), checked: automaticPowerOffEnabled, visible:showPowerOffPrintFinishOption">
{{ _('Power Off Relais after print finishes') }}
</label>
</div>