9 Commits
0.3.0 ... 1.0.1

12 changed files with 112 additions and 36 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
@ -25,6 +26,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
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
@ -75,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):
@ -84,40 +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):
try:
nbRetry = 0
value = 'on'
if (newState == True):
if newState:
value = 'off'
while nbRetry < 3:
try:
request = requests.post(
'http://{}/timer'.format(self.ip), params={'mode': value, 'time': time}, timeout=1)
if not request.status_code == 200:
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":
@ -169,11 +197,16 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin,
ip=None,
intervall=1,
onOffButtonEnabled=False,
owerOnOnStart=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,21 @@
<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>
</div>
<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>
@ -31,21 +35,30 @@
<label class="checkbox">
<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.3.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