15 Commits
0.2.0 ... 1.0.1

Author SHA1 Message Date
ccc58f9301 #16 Codereview Octoprint Plugin Repo 2019-12-01 10:29:03 +01:00
c56b076620 #15 Beschreibung erstellen 2019-11-27 18:39:09 +01:00
78f39e82f3 #15 Beschreibung erstellen 2019-11-27 18:37:04 +01:00
10702134ee #14 Einstellungen reorganisieren
- Beschreibungen angepasst
2019-11-27 18:04:46 +01:00
3e74b23dbc #14 Einstellungen reorganisieren 2019-11-27 17:56:49 +01:00
ebc98694d1 #14 Einstellungen reorganisieren 2019-11-27 17:56:30 +01:00
1ec0b46060 #11 Energieverbrauch näherungsweise berechnen 2019-11-25 21:03:25 +01:00
6b041d7cca #11 Energieverbrauch näherungsweise berechnen 2019-11-25 21:00:24 +01:00
d2e1b00b3f #13 Wenn Request fehlschägt max. 3x versuchen 2019-11-25 20:41:10 +01:00
5d002ffc88 #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde

Api call fix
2019-11-25 20:01:37 +01:00
722c39604e #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde

Api call fix
2019-11-25 19:57:25 +01:00
70355683b2 #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde
2019-11-25 19:53:43 +01:00
37c09bd288 #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde

Logging schreibfehler
Url Fix für Powercycle
2019-11-25 19:50:26 +01:00
c9d706e603 #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde
2019-11-25 19:43:31 +01:00
9eca03c29c #9 Relais ausschalten wenn Octoprint heruntergefahren wird
#7 Relais einschalten wenn Octoprint gestartet wurde
2019-11-25 19:39:10 +01:00
12 changed files with 172 additions and 30 deletions

View File

@ -2,4 +2,31 @@
This OctoPrint plugin enables the system to control the myStrom switch and read the current Powerconsumption of your system
Settings Tab
![settings](settings_page.png)
Mainscreen
You can see the Plugin on the bottom right
![Mainscreen](fullscreen.png)
Switch is off
![Switch off](sidebar_off.png)
Switch is on
![Switch on](sidebar_on.png)
With toggle Button enabled
![toggle on](sidebar_on_button.png)
![toggle off](sidebar_off_button.png)
## Setup
Install via the bundled [Plugin Manager](https://github.com/foosel/OctoPrint/wiki/Plugin:-Plugin-Manager)
or manually using this URL:
https://github.com/da4id/OctoPrint-MyStromSwitch/archive/master.zip

BIN
fullscreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 167 KiB

View File

@ -2,6 +2,7 @@
from __future__ import absolute_import
import ssl
import time
import octoprint.plugin
import requests
@ -19,9 +20,15 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
self.ip = None
self.intervall = 1
self.onOffButtonEnabled = False
self.powerOnOnStart = False
self.powerOffOnShutdown = False
self.powerOffDelay = 0
self._timer = None
self.energy = 0
self.lastTimeStamp = 0
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
@ -34,7 +41,17 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
self._logger.debug("intervall: %s" % self.intervall)
self.onOffButtonEnabled = self._settings.get_boolean(["onOffButtonEnabled"])
self._logger.info("onOffButtonEnabled: %s" % self.onOffButtonEnabled)
self._logger.debug("onOffButtonEnabled: %s" % self.onOffButtonEnabled)
self.powerOnOnStart = self._settings.get_boolean(["powerOnOnStart"])
self._logger.debug("powerOnOnStart: %s" % self.powerOnOnStart)
self.powerOffOnShutdown = self._settings.get_boolean(["powerOffOnShutdown"])
self._logger.debug("powerOffOnShutdown: %s" % self.powerOffOnShutdown)
self.powerOffDelay = self._settings.get_int(["powerOffDelay"])
self._logger.debug("powerOffDelay: %s" % self.powerOffDelay)
self._timer_start()
def get_assets(self):
@ -62,7 +79,15 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
try:
request = requests.get(
'http://{}/report'.format(self.ip), timeout=1)
if request.status_code == 200:
timestamp = time.time()
data = request.json()
if not self.lastTimeStamp == 0:
intervall = timestamp - self.lastTimeStamp
# Energy in Wh
self.energy = self.energy + (intervall * data["power"] / 3600)
self.lastTimeStamp = timestamp
data["energy"] = self.energy
data["onOffButtonEnabled"] = self.onOffButtonEnabled
self._plugin_manager.send_plugin_message(self._identifier, data)
except (requests.exceptions.ConnectionError, ValueError):
@ -71,25 +96,56 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
self._logger.info("Ip is None")
def _setRelaisState(self, newState):
try:
nbRetry = 0
value = '0'
if (newState == True):
value = '1'
while nbRetry < 3:
try:
request = requests.get(
'http://{}/relay'.format(self.ip), params={'state': value}, timeout=1)
if not request.status_code == 200:
self._logger.info("Could not set new Relais State, Http Status Code: {}".format(request.status_code))
if request.status_code == 200:
return
else:
self._logger.info(
"Could not set new Relais State, Http Status Code: {}".format(request.status_code))
except requests.exceptions.ConnectionError:
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.
def _powerCycleRelais(self, newState, time):
nbRetry = 0
value = 'on'
if newState:
value = 'off'
while nbRetry < 3:
try:
request = requests.post(
'http://{}/timer'.format(self.ip), params={'mode': value, 'time': time}, timeout=1)
if request.status_code == 200:
return
else:
self._logger.info("Could not powerCycle Relais, Http Status Code: {}".format(request.status_code))
except requests.exceptions.ConnectionError:
self._logger.info("Error during powerCycle Relais")
nbRetry = nbRetry + 1
def _toggleRelay(self):
nbRetry = 0
while nbRetry < 3:
try:
request = requests.get(
'http://{}/toggle'.format(self.ip), timeout=1)
if not request.status_code == 200:
if request.status_code == 200:
return
else:
self._logger.info("Could not toggle Relay State, Http Status Code: {}".format(request.status_code))
except requests.exceptions.ConnectionError:
self._logger.info("Error during toggle Relais state")
nbRetry = nbRetry + 1
def on_api_command(self, command, data):
if command == "enableRelais":
@ -110,27 +166,47 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
)
def on_after_startup(self):
pass
if self.powerOnOnStart:
self._logger.info("Turn on Relais on Start")
self._setRelaisState(True)
def on_shutdown(self):
pass
if self.powerOffOnShutdown:
if self.powerOffDelay <= 0:
self._logger.info("Turn on Relais off Shutdown")
self._setRelaisState(False)
else:
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:
self.onOffButtonEnabled = False
pass
if current <= 2:
self.powerOnOnStart = False,
self.powerOffOnShutdown = False,
self.powerOffDelay = 0
def get_settings_version(self):
return 2
return 3
def get_settings_defaults(self):
return dict(
ip=None,
intervall=1,
onOffButtonEnabled=False
onOffButtonEnabled=False,
powerOnOnStart=False,
powerOffOnShutdown=False,
powerOffDelay=0
)
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)

View File

@ -8,6 +8,7 @@ $(function() {
self.onOffButtonEnabled = ko.observable();
self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue")
self.mystromswitchEnergyValue = document.getElementById("mystromswitchEnergyValue")
self.onToggleRelayEvent = function(){
$.ajax({
@ -32,6 +33,7 @@ $(function() {
return;
}
self.onOffButtonEnabled(data.onOffButtonEnabled);
self.mystromswitchEnergyValue.innerHTML = "Energy: "+data.energy.toFixed(1)+"Wh"
if(data.relay == false){
self.mystromswitchPowerValue.innerHTML = "Relay is off";
} else if (data.power != null) {

View File

@ -1,8 +1,7 @@
<form class="form-horizontal">
<h4>General</h4>
<hr>
<div class="control-group">
<label class="control-label">{{ _('IP-Address') }}</label>
<label class="control-label">{{ _('URL or IP-Address of your switch') }}</label>
<div class="controls">
<input type="text" class="input-block-level" data-bind="value: settings.plugins.mystromswitch.ip">
</div>
@ -13,16 +12,53 @@
<div class="controls">
<div class="input-append">
<input type="number" class="input-mini text-right" data-bind="value: settings.plugins.mystromswitch.intervall">
<span class="add-on">sec</span>
<span class="add-on">seconds</span>
</div>
<span class="help-block"><small>{{ _('Intervall in seconds where relays state, power and energy consumption are refreshed') }}</small></span>
</div>
</div>
<hr>
<h4>Octoprint Hardware (Raspberry Pi) is not switched on/off by the relais</h4>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.onOffButtonEnabled">Toggle Button Enabled
</label>
<span class="help-block"><small>{{ _('Shows an on/off Button on the left side to switch the relays on or off. This setting is only recommended if only your printer is switched on/off') }}</small></span>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" id="mystromswitchShowToggleButton" data-bind="checked: settings.plugins.mystromswitch.onOffButtonEnabled">Toggle Button Enabled
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.powerOnOnStart">Turn relais ON on Octoprint start
</label>
<span class="help-block"><small>{{ _('This setting switches your mySwitch on if Octoprint is starting up. Turn relays OFF on Octoprint shutdown could also be interesting for you') }}</small></span>
</div>
</div>
<hr>
<h4>Octoprint Hardware (Raspberry Pi) is switched on/off by the relais</h4>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.mystromswitch.powerOffOnShutdown">Turn relais OFF on Octoprint shutdown
</label>
<span class="help-block"><small>{{ _('This setting switches your mySwitch off if Octoprint is shutting down. If your Raspberry Pi is also switched by the relays its recommended to use an turn relay off delay of 60 seconds or more. Keep in mind this switches also off if you restart Octoprint service!') }}</small></span>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Turn Relais Off Delay') }}</label>
<div class="controls">
<div class="input-append">
<input type="number" class="input-block-level" data-bind="value: settings.plugins.mystromswitch.powerOffDelay">
<span class="add-on">seconds</span>
</div>
<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>
</form>

View File

@ -1,4 +1,5 @@
<div class="sidebar_plugin_mystromswitch">
<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>
</div>

BIN
settings_page.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View File

@ -14,7 +14,7 @@ plugin_package = "octoprint_mystromswitch"
plugin_name = "OctoPrint-MyStromSwitch"
# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.2.0"
plugin_version = "1.0.1"
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module

BIN
sidebar_off.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
sidebar_off_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
sidebar_on.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
sidebar_on_button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB