From 183a8feed442791e98b4ac29bbcc65abb57ef6a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 25 Nov 2014 09:08:33 +0100 Subject: [PATCH] Plugins can now push messages via the websocket as well --- src/octoprint/plugin/core.py | 15 +++++++++++++++ src/octoprint/server/__init__.py | 2 +- src/octoprint/server/util/sockjs.py | 8 +++++++- src/octoprint/static/js/app/dataupdater.js | 7 +++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/octoprint/plugin/core.py b/src/octoprint/plugin/core.py index 46f5e812..79708d8d 100644 --- a/src/octoprint/plugin/core.py +++ b/src/octoprint/plugin/core.py @@ -111,6 +111,8 @@ class PluginManager(object): self.plugin_hooks = defaultdict(list) self.plugin_implementations = defaultdict(list) + self.registered_clients = [] + self.reload_plugins() def _find_plugins(self): @@ -266,6 +268,19 @@ class PluginManager(object): else: return all_helpers + def register_client(self, client): + if client is None: + return + self.registered_clients.append(client) + + def unregister_client(self, client): + self.registered_clients.remove(client) + + def send_plugin_message(self, plugin, data): + for client in self.registered_clients: + try: client.sendPluginMessage(plugin, data) + except: self.logger.exception("Exception while sending plugin data to client") + class Plugin(object): pass diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 36e1bc18..2807a789 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -392,7 +392,7 @@ class Server(): def _createSocketConnection(self, session): global printer, fileManager, analysisQueue, userManager, eventManager - return util.sockjs.PrinterStateConnection(printer, fileManager, analysisQueue, userManager, eventManager, session) + return util.sockjs.PrinterStateConnection(printer, fileManager, analysisQueue, userManager, eventManager, pluginManager, session) def _checkForRoot(self): if "geteuid" in dir(os) and os.geteuid() == 0: diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index 22374eac..bf9ee3cb 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -15,7 +15,7 @@ from octoprint.events import Events class PrinterStateConnection(sockjs.tornado.SockJSConnection): - def __init__(self, printer, fileManager, analysisQueue, userManager, eventManager, session): + def __init__(self, printer, fileManager, analysisQueue, userManager, eventManager, pluginManager, session): sockjs.tornado.SockJSConnection.__init__(self, session) self._logger = logging.getLogger(__name__) @@ -32,6 +32,7 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection): self._analysisQueue = analysisQueue self._userManager = userManager self._eventManager = eventManager + self._pluginManager = pluginManager def _getRemoteAddress(self, info): forwardedFor = info.headers.get("X-Forwarded-For") @@ -49,6 +50,7 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection): self._printer.registerCallback(self) self._fileManager.register_slicingprogress_callback(self) octoprint.timelapse.registerCallback(self) + self._pluginManager.register_client(self) self._eventManager.fire(Events.CLIENT_OPENED, {"remoteAddress": remoteAddress}) for event in octoprint.events.all_events(): @@ -61,6 +63,7 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection): self._printer.unregisterCallback(self) self._fileManager.unregister_slicingprogress_callback(self) octoprint.timelapse.unregisterCallback(self) + self._pluginManager.unregister_client(self) self._eventManager.fire(Events.CLIENT_CLOSED) for event in octoprint.events.all_events(): @@ -115,6 +118,9 @@ class PrinterStateConnection(sockjs.tornado.SockJSConnection): dict(slicer=slicer, source_location=source_location, source_path=source_path, dest_location=dest_location, dest_path=dest_path, progress=progress) ) + def sendPluginMessage(self, plugin, data): + self._emit("plugin", dict(plugin=plugin, data=data)) + def addLog(self, data): with self._logBacklogMutex: self._logBacklog.append(data) diff --git a/src/octoprint/static/js/app/dataupdater.js b/src/octoprint/static/js/app/dataupdater.js index f6cc01fa..0cb91be7 100644 --- a/src/octoprint/static/js/app/dataupdater.js +++ b/src/octoprint/static/js/app/dataupdater.js @@ -257,6 +257,13 @@ function DataUpdater(allViewModels) { }); break; } + case "plugin": { + _.each(self.allViewModels, function(viewModel) { + if (viewModel.hasOwnProperty("onDataUpdaterPluginMessage")) { + viewModel.onDataUpdaterPluginMessage(data.plugin, data.data); + } + }) + } } } };