From 2bcc5631b6d1de6e7939826ff6bd292bbea432c6 Mon Sep 17 00:00:00 2001 From: David Zingg Date: Wed, 20 Nov 2019 20:46:04 +0100 Subject: [PATCH] #6 UI Updates senden und anzeigen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - unnötigen Code entfernen, erster Versuch --- octoprint_mystromswitch/__init__.py | 6 +- .../static/js/mystromswitch.js | 130 +----------------- .../templates/mystromswitch_sidebar.jinja2 | 5 +- 3 files changed, 10 insertions(+), 131 deletions(-) diff --git a/octoprint_mystromswitch/__init__.py b/octoprint_mystromswitch/__init__.py index 7421e0a..9d0adbe 100644 --- a/octoprint_mystromswitch/__init__.py +++ b/octoprint_mystromswitch/__init__.py @@ -56,8 +56,8 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, try: request = requests.get( 'http://{}/report'.format(self.ip), timeout=1) - self.data = request.json() - self._logger.info(self.data) + self._plugin_manager.send_plugin_message(self._identifier, + request.json()) except (requests.exceptions.ConnectionError, ValueError): self._logger.info('Connection Error Host: {}'.format(self.ip)) # Do all Staff here @@ -74,6 +74,8 @@ class MyStromSwitchPlugin(octoprint.plugin.SettingsPlugin, self.initialize() def on_settings_migrate(self, target, current): + if current >= target: + return pass def get_settings_version(self): diff --git a/octoprint_mystromswitch/static/js/mystromswitch.js b/octoprint_mystromswitch/static/js/mystromswitch.js index 0996002..fb48df3 100644 --- a/octoprint_mystromswitch/static/js/mystromswitch.js +++ b/octoprint_mystromswitch/static/js/mystromswitch.js @@ -6,113 +6,7 @@ $(function() { self.settings = parameters[1]; self.printer = parameters[2]; - - self.mystromswitchEnabled = ko.observable(); - - self.testButtonChangeStatus = function (stat) { - $("#tester_mystromswitch_gcode").prop("disabled", stat); - $("#tester_mystromswitch_api").prop("disabled", stat); - $("#tester_mystromswitch_api_custom").prop("disabled", stat); - } - - self.eventChangeCheckToRadio = function (id, listOff) { - $(id).on("change", function () { - if ($(this).prop("checked") == true) - { - listOff.forEach(function(element) { - if (id != element.id) - { - if ($(element.id).prop("checked") == true) - { - $(element.id).unbind("change"); - $(element.id).trigger("click"); - self.eventChangeCheckToRadio(element.id, listOff); - } - } - }); - } - }) - } - - $("#tester_mystromswitch_gcode").on("click", function () { - self.settings.saveData(); - $(this).children("i").show(); - setTimeout(function (current) { - $.ajax({ - url: API_BASEURL + "plugin/mystromswitch", - type: "POST", - dataType: "json", - data: JSON.stringify({ - command: "shutdown", - mode: 1, - eventView : true - }), - contentType: "application/json; charset=UTF-8" - }).done(function() { - current.children("i").hide(); - }); - - }, 1000, $(this)); - - }); - $("#tester_mystromswitch_api").on("click", function () { - self.settings.saveData(); - $(this).children("i").show(); - setTimeout(function (current) { - $.ajax({ - url: API_BASEURL + "plugin/mystromswitch", - type: "POST", - dataType: "json", - data: JSON.stringify({ - command: "shutdown", - mode: 2, - eventView : true - }), - contentType: "application/json; charset=UTF-8" - }).done(function() { - current.children("i").hide(); - }); - }, 1000, $(this)); - - }); - - $("#tester_mystromswitch_api_custom").on("click", function () { - self.settings.saveData(); - $(this).children("i").show(); - setTimeout(function (current) { - $.ajax({ - url: API_BASEURL + "plugin/mystromswitch", - type: "POST", - dataType: "json", - data: JSON.stringify({ - command: "shutdown", - mode: 3, - eventView : true - }), - contentType: "application/json; charset=UTF-8" - }).done(function() { - current.children("i").hide(); - }); - }, 1000, $(this)); - - }); - self.listOffMode = [ - {"id" : "#mystromswitch_mode_shutdown_gcode"}, - {"id" : "#mystromswitch_mode_shutdown_api"}, - {"id" : "#mystromswitch_mode_shutdown_api_custom"}, - ] - self.listOffHTTPMethode = [ - {"id" : "#mystromswitch_api_custom_GET"}, - {"id" : "#mystromswitch_api_custom_POST"}, - {"id" : "#mystromswitch_api_custom_PUT"} - ] - self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_gcode", self.listOffMode); - self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_api", self.listOffMode); - self.eventChangeCheckToRadio("#mystromswitch_mode_shutdown_api_custom", self.listOffMode); - - self.eventChangeCheckToRadio("#mystromswitch_api_custom_GET", self.listOffHTTPMethode); - self.eventChangeCheckToRadio("#mystromswitch_api_custom_POST", self.listOffHTTPMethode); - self.eventChangeCheckToRadio("#mystromswitch_api_custom_PUT", self.listOffHTTPMethode); + // Hack to remove automatically added Cancel button // See https://github.com/sciactive/pnotify/issues/141 @@ -207,14 +101,7 @@ $(function() { self.onUserLoggedOut = function() { } - - self.onEventPrinterStateChanged = function(payload) { - if (payload.state_id == "PRINTING" || payload.state_id == "PAUSED"){ - self.testButtonChangeStatus(true); - } else { - self.testButtonChangeStatus(false); - } - } + self.onmystromswitchEvent = function() { if (self.mystromswitchEnabled()) { @@ -250,22 +137,15 @@ $(function() { } self.mystromswitchEnabled(data.mystromswitchEnabled); - if (data.type == "timeout") { - if ((data.timeout_value != null) && (data.timeout_value > 0)) { - self.timeoutPopupOptions.text = self.timeoutPopupText + data.timeout_value; + if (data.power != null) { + self.timeoutPopupOptions.text = self.timeoutPopupText + data.power; if (typeof self.timeoutPopup != "undefined") { self.timeoutPopup.update(self.timeoutPopupOptions); } else { self.timeoutPopup = new PNotify(self.timeoutPopupOptions); - self.timeoutPopup.get().on('pnotify.cancel', function() {self.abortShutdown(true);}); - } - } else { - if (typeof self.timeoutPopup != "undefined") { - self.timeoutPopup.remove(); - self.timeoutPopup = undefined; + self.timeoutPopup.get().on('pnotify.cancel', function() {}); } } - } } self.abortShutdown = function(abortShutdownValue) { diff --git a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 index 75e5106..82da445 100644 --- a/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 +++ b/octoprint_mystromswitch/templates/mystromswitch_sidebar.jinja2 @@ -1,6 +1,3 @@