Skip to content

Commit

Permalink
added urls in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
glennerichall committed May 22, 2024
1 parent 9ec8988 commit d589990
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 9 deletions.
4 changes: 2 additions & 2 deletions octoprint_zupfe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion octoprint_zupfe/expo_backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion octoprint_zupfe/loops/mjpeg_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions octoprint_zupfe/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 6 additions & 1 deletion octoprint_zupfe/static/js/zupfe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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");
Expand Down
24 changes: 24 additions & 0 deletions octoprint_zupfe/static/less/zupfe.less
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 24 additions & 2 deletions octoprint_zupfe/templates/zupfe_settings.jinja2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script type="text/javascript" src="/plugin/zupfe/static/js/constants.js"></script>

<div id="zupfe-settings-content" data-bind="css: { 'offline' : !backend_connected() || !backend_initialized() || !api_key(), 'lost-octoid' : api_key_invalid() }">
<div id="zupfe-settings-content"
data-bind="css: { 'offline' : !backend_connected() || !backend_initialized() || !api_key(), 'lost-octoid' : api_key_invalid() }">

<div class="wizard-header">
<img class="zupfe-logo" src="/plugin/zupfe/static/img/logo.svg"/>
Expand All @@ -25,11 +26,16 @@
data-bind="click: unlinkPrinter, attr: { disabled: !backend_connected() || !backend_initialized() || !api_key() }">
Unlink
</button>
<button class="btn btn-secondary btn-complete"
id="settings-zupfe-btn-reconnect"
data-bind="click: reconnect, attr: { disabled: !backend_connected() || !backend_initialized() || !api_key() }">
Force reconnection
</button>
</div>
</div>

<div class="unlinked">
<form data-bind="attr: {action: urls()[URL_PRINTER_LINK] }" method="POST" target="_blank">
<form data-bind="attr: {action: urls()[URL_PRINTER_LINK] }" method="POST" target="_blank">
<input type="hidden" name="apiKey" data-bind="attr: { value: api_key() }"/>
<input class="btn btn-primary btn-complete"
id="settings-zupfe-btn-complete"
Expand All @@ -38,6 +44,22 @@
</form>
</div>

<div class="printer-info">
<span class="info-label">Plugin version</span>
<span>{{ plugin_zupfe_octoprint_zupfe_version }}</span>

<span class="info-label">Printer id</span>
<span>{{ plugin_zupfe_octo_id }}</span>

<span class="info-label">Frontend <sup>*</sup></span>
<input type="text" class="input-block-level" data-bind="value: settings.settings.plugins.zupfe.frontend_url">

<span class="info-label">Backend <sup>*</sup></span>
<input type="text" class="input-block-level" data-bind="value: settings.settings.plugins.zupfe.backend_url">
</div>

<div class="note">* You need to restart OctoPrint to make these changes effective.</div>

<div class="lost-octoid-message">
This printer is no longer found in our system.
We apologize for the inconvenience, please re-link it.
Expand Down
14 changes: 13 additions & 1 deletion octoprint_zupfe/transport/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions octoprint_zupfe/zupfe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import octoprint
from flask import jsonify

from octoprint_zupfe.startup import reconnect_backend


class ZupfeApiPlugin(octoprint.plugin.BlueprintPlugin):

Expand All @@ -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 {}
3 changes: 3 additions & 0 deletions octoprint_zupfe/zupfe_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d589990

Please sign in to comment.