✨ New bundled plugin action_command_notification
Implements support for //action:notification messages from the printer.
This commit is contained in:
1 parent
bec4d04c15
commit
5b92be5889
6 files changed
+250
No files matched your search
@@ -0,0 +1,127 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2020 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import octoprint.plugin
|
||||
|
||||
from octoprint.access import USER_GROUP
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.events import Events
|
||||
|
||||
import flask
|
||||
from flask_babel import gettext
|
||||
|
||||
import time
|
||||
|
||||
class ActionCommandNotificationPlugin(octoprint.plugin.AssetPlugin,
|
||||
octoprint.plugin.SettingsPlugin,
|
||||
octoprint.plugin.SimpleApiPlugin,
|
||||
octoprint.plugin.TemplatePlugin,
|
||||
octoprint.plugin.EventHandlerPlugin):
|
||||
|
||||
def __init__(self):
|
||||
self._notifications = []
|
||||
|
||||
# Additional permissions hook
|
||||
|
||||
def get_additional_permissions(self):
|
||||
return [
|
||||
dict(key="SHOW",
|
||||
name="Show printer notifications",
|
||||
description=gettext("Allows to see printer notifications"),
|
||||
default_groups=[USER_GROUP],
|
||||
roles=["show"]),
|
||||
dict(key="CLEAR",
|
||||
name="Clear printer notifications",
|
||||
description=gettext("Allows to clear printer notifications"),
|
||||
default_groups=[USER_GROUP],
|
||||
roles=["clear"])
|
||||
]
|
||||
|
||||
#~ AssetPlugin
|
||||
|
||||
def get_assets(self):
|
||||
return dict(js=["js/action_command_notification.js"],
|
||||
clientjs=["clientjs/action_command_notification.js"],
|
||||
css=["css/action_command_notification.css"])
|
||||
|
||||
#~ EventHandlerPlugin
|
||||
|
||||
def on_event(self, event, payload):
|
||||
if event == Events.DISCONNECTED:
|
||||
self._clear_notifications()
|
||||
|
||||
#~ SettingsPlugin
|
||||
|
||||
def get_settings_defaults(self):
|
||||
return dict(enable=True,
|
||||
enable_popups=False)
|
||||
|
||||
#~ SimpleApiPlugin
|
||||
|
||||
def on_api_get(self, request):
|
||||
if not Permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_SHOW.can():
|
||||
return flask.abort(403, "Insufficient permissions")
|
||||
return flask.jsonify(notifications=[dict(timestamp=notification[0],
|
||||
message=notification[1]) for notification in self._notifications])
|
||||
|
||||
def get_api_commands(self):
|
||||
return dict(clear=[])
|
||||
|
||||
def on_api_command(self, command, data):
|
||||
if command == "clear":
|
||||
if not Permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_CLEAR.can():
|
||||
return flask.abort(403, "Insufficient permissions")
|
||||
self._clear_notifications()
|
||||
|
||||
#~ TemplatePlugin
|
||||
|
||||
def get_template_configs(self):
|
||||
return [
|
||||
dict(type="settings",
|
||||
name=gettext("Printer Notifications"),
|
||||
custom_bindings=False),
|
||||
dict(type="sidebar",
|
||||
name=gettext("Printer Notifications"),
|
||||
icon="bell-o", styles=["display: none"],
|
||||
data_bind="visible: loginState.hasPermission(access.permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_SHOW) "
|
||||
"&& settings.settings.plugins.action_command_notification.enable")
|
||||
]
|
||||
|
||||
#~ action command handler
|
||||
|
||||
def action_command_handler(self, comm, line, action, *args, **kwargs):
|
||||
if not self._settings.get_boolean(["enable"]):
|
||||
return
|
||||
|
||||
parts = action.split(None, 1)
|
||||
if len(parts) == 1:
|
||||
action = parts[0]
|
||||
parameter = ""
|
||||
else:
|
||||
action, parameter = parts
|
||||
|
||||
if action != "notification":
|
||||
return
|
||||
|
||||
message = parameter.strip()
|
||||
self._notifications.append((time.time(), message))
|
||||
self._plugin_manager.send_plugin_message(self._identifier, dict(message=message))
|
||||
|
||||
def _clear_notifications(self):
|
||||
self._notifications = []
|
||||
self._plugin_manager.send_plugin_message(self._identifier, dict())
|
||||
|
||||
__plugin_name__ = "Action Command Notification Support"
|
||||
__plugin_description__ = "Allows your printer to trigger notifications via action commands on the connection"
|
||||
__plugin_author__ = "Gina Häußge"
|
||||
__plugin_disabling_discouraged__ = gettext("Without this plugin your printer will no longer be able to trigger"
|
||||
" notifications in OctoPrint")
|
||||
__plugin_license__ = "AGPLv3"
|
||||
__plugin_implementation__ = ActionCommandNotificationPlugin()
|
||||
__plugin_hooks__ = {
|
||||
"octoprint.comm.protocol.action": __plugin_implementation__.action_command_handler,
|
||||
"octoprint.access.permissions": __plugin_implementation__.get_additional_permissions
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
(function (global, factory) {
|
||||
if (typeof define === "function" && define.amd) {
|
||||
define(["OctoPrintClient"], factory);
|
||||
} else {
|
||||
factory(global.OctoPrintClient);
|
||||
}
|
||||
})(this, function(OctoPrintClient) {
|
||||
var OctoPrintActionCommandNotificationClient = function(base) {
|
||||
this.base = base;
|
||||
};
|
||||
|
||||
OctoPrintActionCommandNotificationClient.prototype.get = function(refresh, opts) {
|
||||
return this.base.get(this.base.getSimpleApiUrl("action_command_notification"), opts);
|
||||
};
|
||||
|
||||
OctoPrintActionCommandNotificationClient.prototype.clear = function(opts) {
|
||||
return this.base.simpleApiCommand("action_command_notification", "clear", {}, opts);
|
||||
};
|
||||
|
||||
OctoPrintClient.registerPluginComponent("action_command_notification", OctoPrintActionCommandNotificationClient);
|
||||
return OctoPrintActionCommandNotificationClient;
|
||||
});
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
.sidebar_plugin_action_command_notification_entry {
|
||||
padding: 5px;
|
||||
line-height: 20px;
|
||||
border-bottom: 1px solid #ddd;
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
$(function() {
|
||||
function ActionCommandNotificationViewModel(parameters) {
|
||||
var self = this;
|
||||
|
||||
self.loginState = parameters[0];
|
||||
self.access = parameters[1];
|
||||
self.settings = parameters[2];
|
||||
|
||||
self.notifications = ko.observableArray([]);
|
||||
|
||||
self.toDateTimeString = function(timestamp) {
|
||||
return formatDate(timestamp);
|
||||
};
|
||||
|
||||
self.requestData = function() {
|
||||
if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_SHOW)) return;
|
||||
|
||||
OctoPrint.plugins.action_command_notification.get()
|
||||
.done(self.fromResponse)
|
||||
};
|
||||
|
||||
self.fromResponse = function(response) {
|
||||
self.notifications(response.notifications);
|
||||
};
|
||||
|
||||
self.clear = function() {
|
||||
if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_CLEAR)) return;
|
||||
|
||||
OctoPrint.plugins.action_command_notification.clear();
|
||||
};
|
||||
|
||||
self.onStartup = self.onUserLoggedIn = self.onUserLoggedOut = function() {
|
||||
self.requestData();
|
||||
};
|
||||
|
||||
self.onDataUpdaterPluginMessage = function(plugin, data) {
|
||||
if (!self.loginState.hasPermission(self.access.permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_SHOW)) return;
|
||||
if (plugin !== "action_command_notification") {
|
||||
return;
|
||||
}
|
||||
|
||||
self.requestData();
|
||||
|
||||
if (data.message && self.settings.settings.plugins.action_command_notification.enable_popups()) {
|
||||
new PNotify({
|
||||
title: gettext("Printer Notification"),
|
||||
text: data.message,
|
||||
hide: false,
|
||||
icon: "fa fa-bell-o",
|
||||
buttons: {
|
||||
sticker: false,
|
||||
closer: true
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
OCTOPRINT_VIEWMODELS.push({
|
||||
construct: ActionCommandNotificationViewModel,
|
||||
dependencies: ["loginStateViewModel", "accessViewModel", "settingsViewModel"],
|
||||
elements: ["#sidebar_plugin_action_command_notification"]
|
||||
});
|
||||
});
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<h3>{{ _('Action Command Notification Settings') }}</h3>
|
||||
|
||||
<form class="form-horizontal">
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checked: settings.plugins.action_command_notification.enable"> {{ _('Enable') }}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<div class="controls">
|
||||
<label class="checkbox">
|
||||
<input type="checkbox" data-bind="checked: settings.plugins.action_command_notification.enable_popups"> {{ _('Enable popups') }}
|
||||
<span class="help-block">{{ _("Check this if you want notifications to also show up as popups in addition to the notification list in the sidebar") }}</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<div style="display: none" data-bind="visible: notifications().length > 0">
|
||||
<div class="scroll-wrapper" data-bind="foreach: notifications">
|
||||
<div class="sidebar_plugin_action_command_notification_entry">
|
||||
<div class="sidebar_plugin_action_command_notification_entry_timestamp"><small class="muted" data-bind="text: $root.toDateTimeString($data.timestamp)"></small></div>
|
||||
<div class="sidebar_plugin_action_command_notification_entry_message" data-bind="text: message"></div>
|
||||
</div>
|
||||
</div>
|
||||
<button class="btn btn-block" data-bind="click: clear, visible: loginState.hasPermission(access.permissions.PLUGIN_ACTION_COMMAND_NOTIFICATION_CLEAR)">{{ _('Clear notifications') }}</button>
|
||||
</div>
|
||||
<div style="display: none" data-bind="visible: notifications().length == 0">
|
||||
{{ _('There are currently no notifications from your printer.') }}
|
||||
</div>
|
||||
Reference in new issue
Block a user