diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index 37dcb43..7faa453 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -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,9 +79,17 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, try: request = requests.get( 'http://{}/report'.format(self.ip), timeout=1) - data = request.json() - data["onOffButtonEnabled"] = self.onOffButtonEnabled - self._plugin_manager.send_plugin_message(self._identifier, data) + 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.powerConsumed + data["onOffButtonEnabled"] = self.onOffButtonEnabled + self._plugin_manager.send_plugin_message(self._identifier, data) except (requests.exceptions.ConnectionError, ValueError): self._logger.info('Connection Error Host: {}'.format(self.ip)) else: diff --git a/octoprint_mystromswitch/static/js/mystromswitch.js b/octoprint_mystromswitch/static/js/mystromswitch.js index e6bbbc3..63ec276 100644 --- a/octoprint_mystromswitch/static/js/mystromswitch.js +++ b/octoprint_mystromswitch/static/js/mystromswitch.js @@ -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) { diff --git a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 index 0dc213f..8fa3af7 100644 --- a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 +++ b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 @@ -1,4 +1,5 @@
diff --git a/setup.py b/setup.py index 59df301..bc82a91 100644 --- a/setup.py +++ b/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