From d5899903da6b7bd3607bc7c34921a7f4ee3cb576 Mon Sep 17 00:00:00 2001 From: Glenn Hall Date: Wed, 22 May 2024 09:19:42 -0400 Subject: [PATCH] added urls in settings --- octoprint_zupfe/__init__.py | 4 +-- octoprint_zupfe/expo_backoff.py | 5 +++- octoprint_zupfe/loops/mjpeg_manager.py | 2 +- octoprint_zupfe/startup.py | 3 +++ octoprint_zupfe/static/js/zupfe.js | 7 ++++- octoprint_zupfe/static/less/zupfe.less | 24 +++++++++++++++++ .../templates/zupfe_settings.jinja2 | 26 +++++++++++++++++-- octoprint_zupfe/transport/websocket.py | 14 +++++++++- octoprint_zupfe/zupfe_api.py | 10 +++++++ octoprint_zupfe/zupfe_template.py | 3 +++ setup.py | 2 +- 11 files changed, 91 insertions(+), 9 deletions(-) diff --git a/octoprint_zupfe/__init__.py b/octoprint_zupfe/__init__.py index 9609fc3..d1e62cb 100644 --- a/octoprint_zupfe/__init__.py +++ b/octoprint_zupfe/__init__.py @@ -62,7 +62,7 @@ def __init__(self): @property def version(self): # TODO get the version from setup.py - return "o.0.1.2" + return "o.0.1.3" @property def host(self): @@ -220,7 +220,7 @@ def on_after_startup(self): def get_settings_defaults(self): return { - 'backend_url': 'https://zupfe.velor.ca', + 'backend_url': 'https://backend.zupfe.velor.ca', 'frontend_url': 'https://zupfe.velor.ca', 'linked': False, 'api_key': None, diff --git a/octoprint_zupfe/expo_backoff.py b/octoprint_zupfe/expo_backoff.py index 62e25a2..8e995ae 100644 --- a/octoprint_zupfe/expo_backoff.py +++ b/octoprint_zupfe/expo_backoff.py @@ -12,11 +12,14 @@ def __init__(self, base_interval=1, max_interval=60, jitter_factor=0.5): self.current_interval = base_interval self.last_call_time = time.time() + def reset(self): + self.current_interval = self.base_interval + def consume(self): current_time = time.time() # Check if wait was called after a 10-minute interval if current_time - self.last_call_time >= 600: - self.current_interval = self.base_interval + self.reset() else: # Increase the backoff interval, up to the max_interval self.current_interval = min(self.current_interval * 2, self.max_interval) diff --git a/octoprint_zupfe/loops/mjpeg_manager.py b/octoprint_zupfe/loops/mjpeg_manager.py index 3aef491..e7c89e5 100644 --- a/octoprint_zupfe/loops/mjpeg_manager.py +++ b/octoprint_zupfe/loops/mjpeg_manager.py @@ -72,7 +72,7 @@ def remove_recipient(self, transport, camera_id): manager.remove_recipient(transport) - if manager.is_done: + if not manager.running: self._plugin.logger.debug(f"Camera {camera_id} has no more recipients, stopping thread") self._managers.pop(camera_id) diff --git a/octoprint_zupfe/startup.py b/octoprint_zupfe/startup.py index 863247a..6459b41 100644 --- a/octoprint_zupfe/startup.py +++ b/octoprint_zupfe/startup.py @@ -34,6 +34,9 @@ async def on_connected(plugin): except Exception as e: plugin.logger.error(str(e)) +async def reconnect_backend(plugin): + plugin.backend.ws.close() + await initialize_backend(plugin) async def initialize_backend(plugin): plugin.logger.debug('Initializing backend') diff --git a/octoprint_zupfe/static/js/zupfe.js b/octoprint_zupfe/static/js/zupfe.js index aea512b..0dd2b2b 100644 --- a/octoprint_zupfe/static/js/zupfe.js +++ b/octoprint_zupfe/static/js/zupfe.js @@ -70,7 +70,6 @@ $(function () { ;(async () => { let urls = await fetch_until_ok('/plugin/zupfe/urls'); self.urls(urls); - console.log(urls) self.backend_initialized(true); let connection = await fetch_until_ok('/plugin/zupfe/connection/status'); @@ -161,6 +160,12 @@ $(function () { // }) } + self.reconnect = async() => { + await fetch('/plugin/zupfe/reconnect', { + method: 'POST' + }) + } + self.onAllBound = () => { self.all_bound(true); let navbarRoot = $("#navbar_plugin_zupfe"); diff --git a/octoprint_zupfe/static/less/zupfe.less b/octoprint_zupfe/static/less/zupfe.less index 586385a..5e4157e 100644 --- a/octoprint_zupfe/static/less/zupfe.less +++ b/octoprint_zupfe/static/less/zupfe.less @@ -35,6 +35,30 @@ #wizard_plugin_zupfe, #settings_plugin_zupfe { + .note { + color: grey; + } + + .printer-info { + align-items: center; + color: grey; + padding: 5px 8px; + border: 1px solid lightgray; + border-radius: 5px; + display: grid; + column-gap: 12px; + row-gap: 3px; + grid-template-columns: max-content 1fr; + + input { + margin: 0; + } + + .info-label { + font-weight: bold; + } + } + .message-offline { display: none; } diff --git a/octoprint_zupfe/templates/zupfe_settings.jinja2 b/octoprint_zupfe/templates/zupfe_settings.jinja2 index ef7ea2b..131f504 100644 --- a/octoprint_zupfe/templates/zupfe_settings.jinja2 +++ b/octoprint_zupfe/templates/zupfe_settings.jinja2 @@ -1,6 +1,7 @@ -
+
@@ -25,11 +26,16 @@ data-bind="click: unlinkPrinter, attr: { disabled: !backend_connected() || !backend_initialized() || !api_key() }"> Unlink +
-
+
+
+ Plugin version + {{ plugin_zupfe_octoprint_zupfe_version }} + + Printer id + {{ plugin_zupfe_octo_id }} + + Frontend * + + + Backend * + +
+ +
* You need to restart OctoPrint to make these changes effective.
+
This printer is no longer found in our system. We apologize for the inconvenience, please re-link it. diff --git a/octoprint_zupfe/transport/websocket.py b/octoprint_zupfe/transport/websocket.py index b14a4e7..eea254a 100644 --- a/octoprint_zupfe/transport/websocket.py +++ b/octoprint_zupfe/transport/websocket.py @@ -141,9 +141,13 @@ def _run_ws(self): 'x-api-key': self._api_key } + def on_open(wss_app): + backoff.reset() + self._on_open(wss_app) + self._ws = websocket.WebSocketApp(self._backend_ws_url, header=headers, - on_open=self._on_open, + on_open=on_open, on_message=self._on_message, on_error=self._on_error, on_close=self._on_close) @@ -168,13 +172,21 @@ def _run_ws(self): backoff.sleep() def close(self): + logger.debug(f"Stopping websocket thread") self._running = False self._closed = True + logger.debug(f"Forcefully closing websocket") + self._ws.close() def set_credentials(self, octo_id, api_key): self._api_key = api_key self._octo_id = octo_id + def reconnect(self): + logger.debug(f"Forcefully closing websocket") + self._ws.close() + self._closed = True + def connect(self, octo_id, api_key): self.set_credentials(octo_id, api_key) self._closed = False diff --git a/octoprint_zupfe/zupfe_api.py b/octoprint_zupfe/zupfe_api.py index a5a55fe..d021164 100644 --- a/octoprint_zupfe/zupfe_api.py +++ b/octoprint_zupfe/zupfe_api.py @@ -2,6 +2,8 @@ import octoprint from flask import jsonify +from octoprint_zupfe.startup import reconnect_backend + class ZupfeApiPlugin(octoprint.plugin.BlueprintPlugin): @@ -27,3 +29,11 @@ def get_urls(self): if not self.backend.is_initialized: return flask.abort(503) return jsonify(self.backend.urls) + + @octoprint.plugin.BlueprintPlugin.route("/reconnect", methods=["POST"]) + def reconnect(self): + if not self.backend.is_initialized: + return flask.abort(503) + + self.worker.submit_coroutines(reconnect_backend(self)) + return {} diff --git a/octoprint_zupfe/zupfe_template.py b/octoprint_zupfe/zupfe_template.py index 4ae251f..c7202c5 100644 --- a/octoprint_zupfe/zupfe_template.py +++ b/octoprint_zupfe/zupfe_template.py @@ -5,6 +5,9 @@ class ZupfeTemplate(octoprint.plugin.TemplatePlugin): def get_template_vars(self): return { 'frontend_url': self.settings.get('frontend_url', 'https://zupfe.velor.ca'), + 'backend_url': self.settings.get('backend_url', 'https://backend.zupfe.velor.ca'), + 'octo_id': self.settings.get('octoprint_id', ''), + 'octoprint_zupfe_version': self.version, } def get_assets(self): diff --git a/setup.py b/setup.py index 4f9041e..178873a 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoPrint-Zupfe" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.1.2" +plugin_version = "0.1.3" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module