✨ Basic printer control & backup/restore on recovery page

This commit is contained in:
Gina Häußge committed 2020-07-21 15:59:21 +02:00
1 parent b973b5fab2
commit 8b939d89a5
4 files changed
+339 -12

No files matched your search

+1
View File
@@ -0,0 +1 @@
#workdialog .pre-output span{display:block}#workdialog .pre-output .message{font-weight:700}#workdialog .pre-output .error{font-weight:700;color:#900}#workdialog .pre-output .stdout{color:#333}#workdialog .pre-output .stderr{color:#900}#workdialog .pre-output .call{color:#009}
+201 -1
View File
@@ -1,8 +1,11 @@
$(function() {
//~~ OctoPrint client setup
var OctoPrint = window.OctoPrint;
OctoPrint.options.baseurl = BASE_URL;
OctoPrint.socket.connect();
var l10n = getQueryParameterByName("l10n");
if (l10n) {
OctoPrint.options.locale = l10n;
@@ -16,16 +19,79 @@ $(function() {
}
babel.Translations.load(catalog).install();
//~~ Lodash setup
_.mixin({"sprintf": sprintf, "vsprintf": vsprintf});
//~~ View Model
function RecoveryViewModel() {
var self = this;
self.connected = ko.observable(true);
self.known = ko.observable(false);
self.permitted = ko.observable(false);
self.username = ko.observable(undefined);
// system commands
self.systemCommands = ko.observableArray([]);
// printer
self.printerConnected = ko.observable(false);
self.jobInProgress = ko.observable(false);
// backup
self.backupSupported = ko.observable(false);
self.restoreSupported = ko.observable(false);
self.backups = ko.observableArray([]);
self.excludeFromBackup = ko.observableArray([]);
// working dialog
self.workDialog = $("#workdialog");
self.workInProgress = ko.observable(false);
self.workLoglines = ko.observableArray([]);
self.workTitle = ko.observable("");
self.request = function() {
OctoPrint.browser.passiveLogin()
.done(function(resp) {
self.username(resp.name);
self.permitted(_.includes(resp.needs.role, "admin"));
self.known(true);
OctoPrint.socket.sendAuth(resp.name, resp.session);
});
OctoPrint.system.getCommandsForSource("core")
.done(function(resp) {
self.systemCommands(resp);
});
OctoPrint.printer.getFullState()
.done(function(resp) {
self.printerConnected(true);
self.jobInProgress(resp.state.flags.printing);
})
.fail(function(xhr) {
self.printerConnected(false);
self.jobInProgress(false);
});
if (OctoPrint.plugins.backup) {
OctoPrint.plugins.backup.get()
.done(function(resp) {
self.backupSupported(true);
self.restoreSupported(resp.restore_supported);
var backups = resp.backups;
backups.sort(function(a, b) { return b.date - a.date });
self.backups(backups);
});
} else {
self.backupSupported(false);
self.restoreSupported(false);
self.backups([]);
}
}
self.executeSystemCommand = function(command) {
@@ -45,9 +111,143 @@ $(function() {
}
}
self.request();
self.cancelPrint = function() {
OctoPrint.job.cancel()
.done(function() {
self.request();
});
}
self.disconnectPrinter = function() {
OctoPrint.connection.disconnect()
.done(function() {
self.request();
});
}
self.createBackup = function() {
self.workInProgress(true);
self.workTitle(gettext("Creating backup..."));
self.workLoglines.removeAll();
self.workDialog.modal({keyboard: false, backdrop: "static", show: true});
if (!self.backupSupported()) return;
var excluded = self.excludeFromBackup();
OctoPrint.plugins.backup.createBackup(excluded)
.done(function() {
self.excludeFromBackup([]);
})
}
self.restoreBackup = function(backup) {
if (!self.restoreSupported()) return;
var perform = function() {
self.workInProgress(true);
self.workTitle(gettext("Restoring backup..."));
self.workLoglines.removeAll();
self.workLoglines.push({line: "Preparing to restore...", stream: "message"});
self.workLoglines.push({line: " ", stream: "message"});
self.workDialog.modal({keyboard: false, backdrop: "static", show: true});
OctoPrint.plugins.backup.restoreBackup(backup);
};
showConfirmationDialog(_.sprintf(gettext("You are about to restore the backup file \"%(name)s\". This cannot be undone."), {name: _.escape(backup.name)}),
perform);
}
self.logout = function() {
OctoPrint.browser.logout()
.done(function() {
window.location.href = LOGIN_URL;
})
}
self.reconnect = function() {
OctoPrint.socket.reconnect();
}
self.onSocketConnected = function() {
self.connected(true);
self.request();
}
self.onSocketDisconnected = function() {
self.connected(false);
}
self.onSocketMessage = function(event, data) {
console.log("onSocketMessage", event, data);
if (event === "plugin" && data.plugin === "backup") {
switch (data.data.type) {
case "logline": {
self.workLoglines.push(self._preprocessLine({line: data.data.line, stream: data.data.stream}));
break;
}
case "backup_started": {
self.workLoglines.push({line: gettext("Creating backup..."), stream: "message"});
self.workLoglines.push({line: " ", stream: "message"});
break;
}
case "backup_failed": {
self.workLoglines.push({line: " ", stream: "message"});
self.workLoglines.push({line: gettext("Backup creation failed! Check octoprint.log for reasons as to why."), stream: "error"});
self.workInProgress(false);
self.request();
break;
}
case "backup_done": {
self.workLoglines.push({line: " ", stream: "message"});
self.workLoglines.push({line: gettext("Backup created successfully!"), stream: "message"});
self.workInProgress(false);
self.request();
break;
}
case "restore_started": {
self.workLoglines.push({line: gettext("Restoring from backup..."), stream: "message"});
self.workLoglines.push({line: " ", stream: "message"});
break;
}
case "restore_failed": {
self.workLoglines.push({line: " ", stream: "message"});
self.workLoglines.push({line: gettext("Restore failed! Check the above output and octoprint.log for reasons as to why."), stream: "error"});
self.workInProgress(false);
break;
}
case "restore_done": {
self.workLoglines.push({line: " ", stream: "message"});
self.workLoglines.push({line: gettext("Restore successful! The server will now be restarted!"), stream: "message"});
self.workInProgress(false);
break;
}
case "installing_plugin": {
self.workLoglines.push({line: " ", stream: "message"});
self.workLoglines.push({
line: _.sprintf(gettext("Installing plugin \"%(plugin)s\"..."), {plugin: _.escape(data.data.plugin)}),
stream: "message"
});
break;
}
}
}
}
}
var viewModel = new RecoveryViewModel();
OctoPrint.socket.onConnected = function() {
viewModel.onSocketConnected();
};
OctoPrint.socket.onDisconnected = function() {
viewModel.onSocketDisconnected();
};
OctoPrint.socket.onMessage("*", function(data) {
viewModel.onSocketMessage(data.event, data.data);
});
ko.applyBindings(viewModel, document.getElementById("navbar"));
ko.applyBindings(viewModel, document.getElementById("recovery"));
ko.applyBindings(viewModel, document.getElementById("workdialog"));
});
+28
View File
@@ -0,0 +1,28 @@
#workdialog {
.pre-output {
span {
display: block; // so we don't need to add individual <br> tags to each line
}
.message {
font-weight: bold;
}
.error {
font-weight: bold;
color: #990000;
}
.stdout {
color: #333333;
}
.stderr {
color: #990000;
}
.call {
color: #000099;
}
}
}
+109 -11
View File
@@ -1,7 +1,7 @@
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>OctoPrint Login</title>
<title>OctoPrint Recovery</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='img/tentacle-32x32.png') }}">
<link rel="mask-icon" href="{{ url_for('static', filename='img/mask.svg') }}" color="#56BE37">
@@ -14,6 +14,7 @@
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="css/bootstrap-responsive.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="vendor/font-awesome-4.7.0/css/font-awesome.min.css") }}">
<link rel="stylesheet" href="{{ url_for("static", filename="css/recovery.css") }}">
{% for url in theming %}
<link rel="stylesheet" href="{{ url }}">
@@ -23,13 +24,17 @@
<script>
var BASE_URL = "{{ url_for('index') }}";
var LOGIN_URL = "{{ url_for('login', redirect=url_for('recovery'), permissions="ADMIN") }}";
var LOCALE = "";
</script>
<script src="{{ url_for("static", filename="js/lib/jquery/jquery.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/knockout.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/sockjs.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/bootstrap/bootstrap.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/lodash.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/sprintf.min.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/babel.js") }}"></script>
<script src="{{ url_for("static", filename="js/lib/moment-with-locales.min.js") }}"></script>
{% assets "js_client" %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
@@ -41,16 +46,109 @@
<script src="{{ url_for("static", filename="js/recovery/recovery.js") }}"></script>
</head>
<body>
<div id="recovery" class="container">
<form class="form-recovery">
<h2 class="form-recovery-heading">{{ _('Recovery options') }}</h2>
<fieldset>
<legend>{{ _('System commands') }}</legend>
<!-- ko foreach: systemCommands -->
<button class="btn btn-block" data-bind="text: name, click: function() { $root.executeSystemCommand($data) }"></button>
<!-- /ko -->
</fieldset>
</form>
<div id="navbar" class="navbar navbar-inverse navbar-static-top">
<div class="navbar-inner">
<span class="brand">OctoPrint Recovery</span>
<ul class="nav pull-right">
<li><p class="navbar-text">Logged in as <strong data-bind="text: username"></strong> (<a href="javascript:void(0)" data-bind="click: logout">Logout</a>)</p></li>
</ul>
</div>
</div>
<div id="recovery" class="container">
<div class="row-fluid">
<div class="span3">
<ul class="nav nav-list">
<li class="nav-header">{{ _('Navigation') }}</li>
<li><a href="#system-commands">{{ _('System commands') }}</a></li>
<li data-bind="visible: printerConnected"><a href="#printer-control">{{ _('Printer control') }}</a></li>
<li data-bind="visible: backupSupported"><a href="#backups">{{ _('Backups') }}</a></li>
</ul>
</div>
<div class="span9">
<div data-bind="visible: !connected()" class="alert alert-error">{{ _('Server is currently offline.') }} <a href="javascript:void(0)" data-bind="click: reconnect">{{ _('Reconnect...') }}</a></div>
<div data-bind="visible: known() && !permitted()" class="alert alert-error">{{ _('You lack admin permissions.') }}</div>
<div style="margin-bottom: 2em">
<h1 id="system-commands">{{ _('System commands') }}</h1>
<!-- ko foreach: systemCommands -->
<button class="btn btn-block" data-bind="text: name, click: function() { $root.executeSystemCommand($data) }"></button>
<!-- /ko -->
</div>
<div data-bind="visible: printerConnected" style="margin-bottom: 2em">
<h1 id="printer-control">{{ _('Printer control') }}</h1>
<button class="btn btn-block" data-bind="visible: jobInProgress, click: cancelPrint">{{ _('Cancel print') }}</button>
<button class="btn btn-block" data-bind="click: disconnectPrinter">{{ _('Disconnect printer') }}</button>
</div>
<div data-bind="visible: backupSupported" style="margin-bottom: 2em">
<h1 id="backups">{{ _('Backup & Restore') }}</h1>
<small data-bind="visible: !restoreSupported()">{{ _('Online restore is not supported on your system.') }}</small>
<h2>{{ _('Create backup') }}</h2>
<form class="form-horizontal">
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="value: 'uploads', checked: excludeFromBackup"> {{ _('Exclude uploaded files & metadata from backup') }}
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="value: 'timelapse', checked: excludeFromBackup"> {{ _('Exclude timelapses from backup') }}
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary" data-bind="enable: !workInProgress(), click: createBackup"><i class="fa fa-spinner fa-spin" data-bind="visible: workInProgress"></i> {{ _('Create backup now') }}</button>
</div>
</div>
</form>
<h2>{{ _('Existing backups') }}</h2>
<table data-bind="visible: backups().length > 0" class="table table-hover">
<thead>
<th>{{ _('Name') }}</th>
<th style="text-align: right">{{ _('Size') }}</th>
<th style="text-align: center">{{ _('Date') }}</th>
<th style="text-align: center">{{ _('Actions') }}</th>
</thead>
<tbody data-bind="foreach: backups">
<tr>
<td><a href="javascript:void(0)" data-bind="text: name, attr: {href: url}"></a></td>
<td style="text-align: right" data-bind="text: formatSize(size)"></td>
<td style="text-align: center" data-bind="text: formatDate(date)"></td>
<td style="text-align: center">
<button class="btn btn-small" data-bind="visible: $root.restoreSupported, click: $root.restoreBackup">{{ _('Restore') }}</button>
<a class="btn btn-small" data-bind="attr: {href: url}">{{ _('Download') }}</a>
</td>
</tr>
</tbody>
</table>
<p data-bind="visible: backups().length <= 0">
{{ _('There are no backups yet.') }}
</p>
</div>
</div>
</div>
</div>
<div id="workdialog" class="modal hide fade">
<div class="modal-header">
<h3 data-bind="text: workTitle"></h3>
</div>
<div class="modal-body">
<pre class="pre-scrollable pre-output" style="height: 170px" data-bind="foreach: workLoglines"><span data-bind="text: line, css: {stdout: stream == 'stdout', stderr: stream == 'stderr', call: stream == 'call', message: stream == 'message', error: stream == 'error'}"></span></pre>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" data-bind="enable: !workInProgress()" aria-hidden="true">{{ _('Close') }}</button>
</div>
</div>
</body>
</html>