Plugins can now push messages via the websocket as well
This commit is contained in:
parent
e3253fce16
commit
183a8feed4
4 changed files with 30 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue