Skip to content

Commit

Permalink
Add Octoprint 1.7 warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Vojacek committed Sep 29, 2022
1 parent 1560ce1 commit 3d802b7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 15 deletions.
7 changes: 6 additions & 1 deletion octoprint_karmen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from octoprint.settings import settings
import octoprint.plugin
from octoprint.util.version import is_octoprint_compatible
from .websocket_proxy import Connector
from .utils import SentryWrapper

Expand Down Expand Up @@ -38,6 +39,7 @@ def get_template_vars(self):
else:
key_redacted = (key[:2] + "*" * (len(key) - 4) + key[-2:]) if key else None
return {
"is_octoprint_compatible": self.is_octoprint_compatible,
"ws_server": self._settings.get(["ws_server"]),
"path_whitelist": list(filter(None, self._settings.get(["path_whitelist"]).split(";"))),
"api_port": self.port,
Expand Down Expand Up @@ -107,8 +109,10 @@ def ws_proxy_connect(self):
api_url = f"{self.host}:{self.port}"
url = f"{ws_server_url}/{key}"
if not key:
self._logger.info("No Karmen device key provided; Not connecting.")
self._logger.warning("No Karmen device key provided.")
return
if not self.is_octoprint_compatible:
self._logger.warning("Incompatible octoprint.")
self.con = Connector(url, api_url, self._logger, self._settings.get(["path_whitelist"]), self.sentry)
self.con.connect()

Expand All @@ -119,6 +123,7 @@ def ws_proxy_reconnect(self):
self.ws_proxy_connect()

def on_startup(self, host, port):
self.is_octoprint_compatible = is_octoprint_compatible(">1.8")
self.con = None
self.host = host
self.port = port
Expand Down
80 changes: 67 additions & 13 deletions octoprint_karmen/static/js/karmen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,80 @@
* Author: Pavel Vojacek
* License: MIT
*/
$(function() {
$(function () {
function KarmenViewModel(parameters) {
var self = this;
let performCheck = function () {
OctoPrint.system.getInfo().then((info) => {
function versionCompare(v1, v2, options) {
var lexicographical = options && options.lexicographical,
zeroExtend = options && options.zeroExtend,
v1parts = v1.split('.'),
v2parts = v2.split('.');

// assign the injected parameters, e.g.:
// self.loginStateViewModel = parameters[0];
// self.settingsViewModel = parameters[1];
function isValidPart(x) {
return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x);
}

// TODO: Implement your plugin's view model here.
if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
return NaN;
}

if (zeroExtend) {
while (v1parts.length < v2parts.length) v1parts.push("0");
while (v2parts.length < v1parts.length) v2parts.push("0");
}

if (!lexicographical) {
v1parts = v1parts.map(Number);
v2parts = v2parts.map(Number);
}

for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) {
return 1;
}

if (v1parts[i] == v2parts[i]) {
continue;
}
else if (v1parts[i] > v2parts[i]) {
return 1;
}
else {
return -1;
}
}

if (v1parts.length != v2parts.length) {
return -1;
}

return 0;
}

let version = info.systeminfo["octoprint.version"]
if (versionCompare(version, "1.8") < 0) {
self.notify = new PNotify({
text:`This version of Karmen Connector is not supported in Octoprint version ${version}.
Please upgrade to Octoprint 1.8+ or use legacy plugin.
Check plugin settings for details.`,
title: "Karmen Connector",
type: "error",
delay: 10000,
});
}
});
}

self.onUserLoggedIn = function () {
performCheck();
}
}

/* view model class, parameters for constructor, container to bind to
* Please see http://docs.octoprint.org/en/master/plugins/viewmodels.html#registering-custom-viewmodels for more details
* and a full list of the available options.
*/
OCTOPRINT_VIEWMODELS.push({
construct: KarmenViewModel,
// ViewModels your plugin depends on, e.g. loginStateViewModel, settingsViewModel, ...
dependencies: [ /* "loginStateViewModel", "settingsViewModel" */ ],
// Elements to bind to, e.g. #settings_plugin_karmen, #tab_plugin_karmen, ...
elements: [ /* ... */ ]
dependencies: ["settingsViewModel", "loginStateViewModel"],
elements: [ /* ... */]
});
});
15 changes: 15 additions & 0 deletions octoprint_karmen/templates/karmen_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
</a>
</div>
{% if is_octoprint_compatible %}
<div class="karmen-block">
<form class="form-horizontal">
<p>
Expand Down Expand Up @@ -94,3 +95,17 @@
<p>Never share your API or device keys with ANYONE.</p>
<p>Please follow our guidelines and do not use the primary Octoprint key when setting up the Karmen plugin. Rather, create a special key for that purpose as described in our <a href="https://docs.karmen.tech/"/>documentation</a></p>
</div>
{% else %}
<div class=karmen-block">
<h4>This version is incompatible with Octoprint 1.7 and lower</h4>
<p>You can:</p>
<ul>
<li>Upgrade Octoprint to version 1.8 or higher (highly recommended)</li>
or
<li>Install version 0.4.0 of Karmen Connector</li>
</ul>
<p>To install version 0.4.0 go to <b>Octoprint Settings</b> -> <b>Plugin Manager</b> -> <b>+ Get more</b> and to <b>... from URL</b> field enter
<code>https://github.com/fragaria/karmen-octoprint-plugin/archive/refs/tags/0.4.0-legacy.zip</code> and click <b>Install</b>
</p>
</div>
{% endif %}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
plugin_license = "MIT"

# Any additional requirements besides OctoPrint should be listed here
plugin_requires = ['Octoprint>=1.8',]
plugin_requires = []

### --------------------------------------------------------------------------------------------------------------------
### More advanced options that you usually shouldn't have to touch follow after this point
Expand Down

0 comments on commit 3d802b7

Please sign in to comment.