Environment detection & logging on startup

Incl. OctoPi version & RPi model through bundled plugin that only
gets loaded if OctoPi is detected.
This commit is contained in:
Gina Häußge committed 2017-11-10 17:32:59 +01:00
1 parent 0bb343e1d3
commit b2d70de144
19 files changed
+494 -53

No files matched your search

@@ -0,0 +1,154 @@
# coding=utf-8
from __future__ import absolute_import, division, print_function
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License"
import flask
import os
import octoprint.plugin
_OCTOPI_VERSION_PATH = "/etc/octopi_version"
_CPUINFO_PATH = "/proc/cpuinfo"
# based on https://elinux.org/RPi_HardwareHistory#Which_Pi_have_I_got.3F
_RPI_REVISION_MAP = {
"Beta": "1B (Beta)",
"0002": "1B",
"0003": "1B",
"0004": "1B",
"0005": "1B",
"0006": "1B",
"0007": "1A",
"0008": "1A",
"0009": "1A",
"000d": "1B",
"000e": "1B",
"000f": "1B",
"0010": "B+",
"0011": "CM1",
"0012": "A+",
"0013": "B+",
"0014": "CM1",
"0015": "A+",
"a01040": "2B",
"a01041": "2B",
"a21041": "2B",
"a22042": "2B",
"900021": "A+",
"900032": "B+",
"900092": "Zero",
"900093": "Zero",
"920093": "Zero",
"9000c1": "Zero W",
"a02082": "3B",
"a020a0": "CM3",
"a22082": "3B",
"a32082": "3B",
}
def get_octopi_version():
with open(_OCTOPI_VERSION_PATH, "r") as f:
version_line = f.readline()
return version_line.strip()
def get_pi_revision():
with open(_CPUINFO_PATH) as f:
for line in f:
if line and line.startswith("Revision:"):
return line[line.index(":") + 1:].strip()
return "unknown"
def get_pi_model(revision):
if revision.startswith("1000"):
# strip flag for over-volted (https://elinux.org/RPi_HardwareHistory#Which_Pi_have_I_got.3F)
revision = revision[4:]
return _RPI_REVISION_MAP.get(revision.lower(), "unknown")
class OctoPiSupportPlugin(octoprint.plugin.EnvironmentDetectionPlugin,
octoprint.plugin.SimpleApiPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin):
def __init__(self):
self._version = None
self._revision = None
self._model = None
#~~ EnvironmentDetectionPlugin
def get_additional_environment(self):
return dict(version=self._get_version(),
revision=self._get_revision(),
model=self._get_model())
#~~ SimpleApiPlugin
def on_api_get(self, request):
return flask.jsonify(version=self._get_version(),
revision=self._get_revision(),
model=self._get_model())
#~~ AssetPlugin
def get_assets(self):
return dict(
js=["js/octopi_support.js"],
css=["css/octopi_support.css"]
)
#~~ TemplatePlugin
def get_template_configs(self):
return [
dict(type="about", name="About OctoPi", template="octopi_support_about.jinja2")
]
def get_template_vars(self):
version = self._get_version()
revision = self._get_revision()
model = self._get_model()
return dict(version=version,
rpi=(revision is not None and revision != "unknown" and model is not None and model != "unknown"),
rpi_revision=revision,
rpi_model=model)
#~~ Helpers
def _get_version(self):
if self._version is None:
try:
self._version = get_octopi_version()
except:
self._logger.exception("Error while reading OctoPi version from file {}".format(_OCTOPI_VERSION_PATH))
return self._version
def _get_model(self):
if self._model is None:
try:
self._model = get_pi_model(self._get_revision())
except:
self._logger.exception("Error while detecting RPi model")
return self._model
def _get_revision(self):
if self._revision is None:
try:
self._revision = get_pi_revision()
except:
self._logger.exception("Error while detecting RPi revision")
return self._revision
def __plugin_check__():
from octoprint.util.platform import get_os
return get_os() == "linux" and os.path.exists(_OCTOPI_VERSION_PATH)
def __plugin_load__():
global __plugin_implementation__
__plugin_implementation__ = OctoPiSupportPlugin()
@@ -0,0 +1,3 @@
#octopi_support_footer {
white-space: nowrap;
}
@@ -0,0 +1,53 @@
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["OctoPrintClient"], factory);
} else {
factory(global.OctoPrintClient);
}
})(this, function(OctoPrintClient) {
var OctoPrintOctoPiSupportClient = function(base) {
this.base = base;
};
OctoPrintOctoPiSupportClient.prototype.get = function(opts) {
return this.base.get(this.base.getSimpleApiUrl("octopi_support"));
};
OctoPrintClient.registerPluginComponent("octopi_support", OctoPrintOctoPiSupportClient);
return OctoPrintOctoPiSupportClient;
});
$(function() {
function OctoPiSupportViewModel(parameters) {
var self = this;
self.requestData = function() {
OctoPrint.plugins.octopi_support.get()
.done(function(response) {
$("#octopi_support_footer").remove();
if (!response.version) return;
var octoPrintVersion = $(".footer span.version");
var octoPiVersion = $("<span id='octopi_support_footer'> " + gettext("running on") + " " + gettext("OctoPi")
+ " <span class='octopi_version'>" + response.version + "</span></span>")
$(octoPiVersion).insertAfter(octoPrintVersion);
})
};
self.onStartup = function() {
self.requestData();
};
self.onServerReconnect = function() {
self.requestData();
};
}
// view model class, parameters for constructor, container to bind to
ADDITIONAL_VIEWMODELS.push([
OctoPiSupportViewModel,
[],
[]
]);
});
@@ -0,0 +1,41 @@
<h3>{{ _('About OctoPi') }}</h3>
<h4>{{ _('The ready-to-go Raspberry Pi image with OctoPrint') }}</h4>
<p>Version <span class="plugin_octopi_support_version">{{ plugin_octopi_support_version }}</span></p>
<ul>
<li>Website: <a href="http://octoprint.org/download/" target="_blank" rel="noreferrer noopener">octoprint.org/download</a> &amp; <a href="https://octopi.octoprint.org" target="_blank" rel="noreferrer noopener">octopi.octoprint.org</a></li>
<li>Source Code: <a href="https://github.com/guysoft/OctoPi" target="_blank" rel="noreferrer noopener">github.com/guysoft/OctoPi</a></li>
</ul>
<p>
&copy; 2013-{{ now.strftime("%Y") }} <a href="https://github.com/guysoft/OctoPi/graphs/contributors" target="_blank" rel="noreferrer noopener">The OctoPi Authors</a>
</p>
<p>
OctoPi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
</p>
<p>
OctoPi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
</p>
<p>
For a copy of the GNU General Public License, see
<a href="https://www.gnu.org/licenses/gpl-3.0.en.html" target="_blank" rel="noreferrer noopener">www.gnu.org/licenses/gpl-3.0.en.html</a>.
</p>
{% if plugin_octopi_support_rpi %}
<h3>{{ _('About this Raspberry Pi') }}</h3>
<ul>
<li>Model: {{ plugin_octopi_support_rpi_model }}</li>
<li>Revision: {{ plugin_octopi_support_rpi_revision }}</li>
</ul>
{% endif %}
@@ -15,6 +15,7 @@ from octoprint.server.util.flask import restricted_access, with_revalidation_che
from octoprint.server import admin_permission, VERSION
from octoprint.util.pip import LocalPipCaller, UnknownPip
from octoprint.util.version import get_octoprint_version_string, get_octoprint_version, is_octoprint_compatible
from octoprint.util.platform import get_os
from flask import jsonify, make_response
from flask.ext.babel import gettext
@@ -241,7 +242,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
available=self._repository_available,
plugins=self._repository_plugins
),
os=self._get_os(),
os=get_os(),
octoprint=get_octoprint_version_string(),
pip=dict(
available=self._pip_caller.available,
@@ -759,7 +760,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
if repo_data is None:
return False
current_os = self._get_os()
current_os = get_os()
octoprint_version = get_octoprint_version(base=True)
def map_repository_entry(entry):
@@ -878,14 +879,6 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin,
return positive_match and not negative_match
@classmethod
def _get_os(cls):
for identifier, platforms in cls.OPERATING_SYSTEMS.items():
if (callable(platforms) and platforms(sys.platform)) or (isinstance(platforms, list) and sys.platform in platforms):
return identifier
else:
return "unmapped"
@property
def _reconnect_hooks(self):
reconnect_hooks = self.__class__.RECONNECT_HOOKS