From 5b92be58890b6a5d2771286fa87eeda1f54e07a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 21 Apr 2020 15:31:08 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20bundled=20plugin=20action=5Fc?= =?UTF-8?q?ommand=5Fnotification?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements support for //action:notification messages from the printer. --- .../action_command_notification/__init__.py | 127 ++++++++++++++++++ .../clientjs/action_command_notification.js | 22 +++ .../css/action_command_notification.css | 5 + .../static/js/action_command_notification.js | 65 +++++++++ ...ction_command_notification_settings.jinja2 | 19 +++ ...action_command_notification_sidebar.jinja2 | 12 ++ 6 files changed, 250 insertions(+) create mode 100644 src/octoprint/plugins/action_command_notification/__init__.py create mode 100644 src/octoprint/plugins/action_command_notification/static/clientjs/action_command_notification.js create mode 100644 src/octoprint/plugins/action_command_notification/static/css/action_command_notification.css create mode 100644 src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js create mode 100644 src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 create mode 100644 src/octoprint/plugins/action_command_notification/templates/action_command_notification_sidebar.jinja2 diff --git a/src/octoprint/plugins/action_command_notification/__init__.py b/src/octoprint/plugins/action_command_notification/__init__.py new file mode 100644 index 000000000..e45a2af6b --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/__init__.py @@ -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 +} diff --git a/src/octoprint/plugins/action_command_notification/static/clientjs/action_command_notification.js b/src/octoprint/plugins/action_command_notification/static/clientjs/action_command_notification.js new file mode 100644 index 000000000..249b69704 --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/static/clientjs/action_command_notification.js @@ -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; +}); diff --git a/src/octoprint/plugins/action_command_notification/static/css/action_command_notification.css b/src/octoprint/plugins/action_command_notification/static/css/action_command_notification.css new file mode 100644 index 000000000..ed15d2c39 --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/static/css/action_command_notification.css @@ -0,0 +1,5 @@ +.sidebar_plugin_action_command_notification_entry { + padding: 5px; + line-height: 20px; + border-bottom: 1px solid #ddd; +} diff --git a/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js b/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js new file mode 100644 index 000000000..c319d378c --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/static/js/action_command_notification.js @@ -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"] + }); +}); diff --git a/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 new file mode 100644 index 000000000..64f676176 --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_settings.jinja2 @@ -0,0 +1,19 @@ +

{{ _('Action Command Notification Settings') }}

+ +
+
+
+ +
+
+
+
+ +
+
+
diff --git a/src/octoprint/plugins/action_command_notification/templates/action_command_notification_sidebar.jinja2 b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_sidebar.jinja2 new file mode 100644 index 000000000..67e8557c7 --- /dev/null +++ b/src/octoprint/plugins/action_command_notification/templates/action_command_notification_sidebar.jinja2 @@ -0,0 +1,12 @@ +
+
+
+ +
+
+
+ +
+
+ {{ _('There are currently no notifications from your printer.') }} +