diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index 6dd0afd..a384367 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -12,11 +12,14 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.StartupPlugin, + octoprint.plugin.SimpleApiPlugin, octoprint.plugin.ShutdownPlugin): def __init__(self): self.ip = None self.intervall = 1 + self.onOffButtonEnabled = False + self._timer = None self.ctx = ssl.create_default_context() @@ -29,6 +32,9 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.intervall = self._settings.get_int(["intervall"]) self._logger.debug("intervall: %s" % self.intervall) + + self.onOffButtonEnabled = self._settings.get_boolean(["onOffButtonEnabled"]) + self._logger.debug("onOffButtonEnabled: %s" % self.onOffButtonEnabled) self._timer_start() def get_assets(self): @@ -63,6 +69,38 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, else: self._logger.info("Ip is None") + def _setRelaisState(self, newState): + try: + value = '0' + if (newState == True): + value = '1' + 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)) + except requests.exceptions.ConnectionError: + self._logger.info("Error during set Relais state") + + def _toggleRelay(self): + try: + request = requests.get( + 'http://{}/toggle'.format(self.ip), timeout=1) + if not request.status_code == 200: + 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") + + def on_api_command(self, command, data): + if command == "enableRelais": + self._logger.info("enableRelais") + self._setRelaisState(True) + elif command == "disableRelais": + self._logger.info("disableRelais") + self._setRelaisState(False) + elif command == "toggleRelais": + self._logger.info("toggleRelais") + self._toggleRelay() + def on_after_startup(self): pass @@ -70,15 +108,19 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, pass def on_settings_migrate(self, target, current): - pass + if target > current: + if current <= 1: + self.onOffButtonEnabled = False + pass def get_settings_version(self): - return 1 + return 2 def get_settings_defaults(self): return dict( ip=None, - intervall=1 + intervall=1, + onOffButtonEnabled=False ) def on_settings_save(self, data): diff --git a/octoprint_mystromswitch/static/css/mystromswitch.css b/octoprint_mystromswitch/static/css/mystromswitch.css index 13eb0ab..fabdd89 100644 --- a/octoprint_mystromswitch/static/css/mystromswitch.css +++ b/octoprint_mystromswitch/static/css/mystromswitch.css @@ -32,6 +32,20 @@ display:none; } +.mystromToggleButton { + text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.25); + display: block; + height: 100%; + padding-left: 10px; + padding-right: 10px; + margin-left: 20px; + margin-right: 20px; + font-size: 14px; + font-weight: bold; + color: #fff; + background-color: #eb0000; +} + #touch body #tabs #sidebar_plugin_mystromswitch_wrapper_link a:before, #touch body #navbar #all_touchui_settings #sidebar_plugin_mystromswitch_wrapper_link a:before,#touch body #tabs #sidebar_plugin_mystromswitch_wrapper_link2 a:before, #touch body #navbar #all_touchui_settings #sidebar_plugin_mystromswitch_wrapper_link2 a:before { content: "\f011"; } diff --git a/octoprint_mystromswitch/static/js/mystromswitch.js b/octoprint_mystromswitch/static/js/mystromswitch.js index f0d9a6e..d72565d 100644 --- a/octoprint_mystromswitch/static/js/mystromswitch.js +++ b/octoprint_mystromswitch/static/js/mystromswitch.js @@ -9,6 +9,21 @@ $(function() { self.mystromswitchEnabled = ko.observable(); self.mystromswitchPowerValue = document.getElementById("mystromswitchPowerValue") + self.mystromToggleButton = document.getElementById("myStromRelaisToggleButton") + + self.onToggleRelayEvent = function(){ + $.ajax({ + url: API_BASEURL + "plugin/mystromswitch", + type: "POST", + dataType: "json", + data: JSON.stringify({ + command: "toggleRelais", + }), + contentType: "application/json; charset=UTF-8" + }) + } + self.mystromToggleButton.addEventListener("click",self.onToggleRelayEvent) + self.onmystromswitchEvent = function() { if (self.mystromswitchEnabled()) { $.ajax({ @@ -16,8 +31,7 @@ $(function() { type: "POST", dataType: "json", data: JSON.stringify({ - command: "enable", - eventView : false + command: "enableRelais", }), contentType: "application/json; charset=UTF-8" }) @@ -27,8 +41,7 @@ $(function() { type: "POST", dataType: "json", data: JSON.stringify({ - command: "disable", - eventView : false + command: "disableRelais", }), contentType: "application/json; charset=UTF-8" }) @@ -42,9 +55,11 @@ $(function() { return; } self.mystromswitchEnabled(data.mystromswitchEnabled); - if (data.power != null) { - self.mystromswitchPowerValue.innerHTML = "Power Consumption "+data.power.toFixed(2)+"W" - } + if(data.relay == false){ + self.mystromswitchPowerValue.innerHTML = "Relay is off"; + } else if (data.power != null) { + self.mystromswitchPowerValue.innerHTML = "Power Consumption "+data.power.toFixed(1)+"W"; + } } } diff --git a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 index d1ab90b..955e6ca 100644 --- a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 +++ b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 @@ -1,3 +1,4 @@ diff --git a/setup.py b/setup.py index 59980c9..494ee87 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.1.0" +plugin_version = "0.2.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module