Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
1ec0b46060 | |||
6b041d7cca | |||
d2e1b00b3f |
@ -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":
|
||||
|
@ -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) {
|
||||
|
@ -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>
|
||||
|
2
setup.py
2
setup.py
@ -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 = "0.4.0"
|
||||
|
||||
# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
|
||||
# module
|
||||
|
Reference in New Issue
Block a user