From 36c20217749001891206ae56e7974866cd0341c3 Mon Sep 17 00:00:00 2001 From: jneilliii Date: Sat, 11 Aug 2018 14:15:15 -0400 Subject: [PATCH] 0.5.0 --- octoprint_youtubelive/__init__.py | 80 ++++++++++++------- .../static/js/youtubelive.js | 9 +++ .../templates/youtubelive_settings.jinja2 | 6 ++ .../templates/youtubelive_tab.jinja2 | 2 +- setup.py | 2 +- 5 files changed, 69 insertions(+), 30 deletions(-) diff --git a/octoprint_youtubelive/__init__.py b/octoprint_youtubelive/__init__.py index 14939f2..8248124 100644 --- a/octoprint_youtubelive/__init__.py +++ b/octoprint_youtubelive/__init__.py @@ -9,13 +9,15 @@ class youtubelive(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.AssetPlugin, octoprint.plugin.SettingsPlugin, - octoprint.plugin.SimpleApiPlugin): + octoprint.plugin.SimpleApiPlugin, + octoprint.plugin.EventHandlerPlugin): def __init__(self): self.client = docker.from_env() self.container = None ##~~ StartupPlugin + def on_after_startup(self): self._logger.info("OctoPrint-YouTubeLive loaded! Checking stream status.") try: @@ -27,10 +29,12 @@ def on_after_startup(self): self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) ##~~ TemplatePlugin + def get_template_configs(self): return [dict(type="settings",custom_bindings=False)] ##~~ AssetPlugin + def get_assets(self): return dict( js=["js/youtubelive.js"], @@ -38,10 +42,11 @@ def get_assets(self): ) ##~~ SettingsPlugin + def get_settings_defaults(self): - return dict(channel_id="",stream_id="",streaming=False) + return dict(channel_id="",stream_id="",streaming=False,auto_start=False) - ##~~ SimpleApiPlugin mixin + ##~~ SimpleApiPlugin def get_api_commands(self): return dict(startStream=[],stopStream=[],checkStream=[]) @@ -53,39 +58,58 @@ def on_api_command(self, command, data): if command == 'startStream': self._logger.info("Start stream command received.") - if not self.container: - filters = [] - if self._settings.global_get(["webcam","flipH"]): - filters.append("hflip") - if self._settings.global_get(["webcam","flipV"]): - filters.append("vflip") - if self._settings.global_get(["webcam","rotate90"]): - filters.append("transpose=cclock") - if len(filters) == 0: - filters.append("null") - try: - self.container = self.client.containers.run("octoprint/youtubelive:latest",command=[self._settings.global_get(["webcam","stream"]),self._settings.get(["stream_id"]),",".join(filters)],detach=True,privileged=True,name="YouTubeLive",auto_remove=True) - self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True)) - except Exception, e: - self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False)) - return + self.startStream() + if command == 'stopStream': self._logger.info("Stop stream command received.") - if self.container: - try: - self.container.stop() - self.container = None - self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) - except Exception, e: - self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False)) - else: - self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) + self.stopStream() + if command == 'checkStream': self._logger.info("Checking stream status.") if self.container: self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True)) else: self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) + + ##-- EventHandlerPlugin + + def on_event(self, event, payload): + if event == "PrintStarted" and self._settings.get(["auto_start"]): + self.startStream() + + if event in ["PrintDone","PrintCancelled"] and self._settings.get(["auto_start"]): + self.stopStream() + + ##-- Utility Functions + + def startStream(self): + if not self.container: + filters = [] + if self._settings.global_get(["webcam","flipH"]): + filters.append("hflip") + if self._settings.global_get(["webcam","flipV"]): + filters.append("vflip") + if self._settings.global_get(["webcam","rotate90"]): + filters.append("transpose=cclock") + if len(filters) == 0: + filters.append("null") + try: + self.container = self.client.containers.run("octoprint/youtubelive:latest",command=[self._settings.global_get(["webcam","stream"]),self._settings.get(["stream_id"]),",".join(filters)],detach=True,privileged=True,name="YouTubeLive",auto_remove=True) + self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=True)) + except Exception, e: + self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False)) + return + + def stopStream(self): + if self.container: + try: + self.container.stop() + self.container = None + self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) + except Exception, e: + self._plugin_manager.send_plugin_message(self._identifier, dict(error=str(e),status=True,streaming=False)) + else: + self._plugin_manager.send_plugin_message(self._identifier, dict(status=True,streaming=False)) ##~~ Softwareupdate hook def get_update_information(self): diff --git a/octoprint_youtubelive/static/js/youtubelive.js b/octoprint_youtubelive/static/js/youtubelive.js index 7d7eb64..bc09029 100644 --- a/octoprint_youtubelive/static/js/youtubelive.js +++ b/octoprint_youtubelive/static/js/youtubelive.js @@ -7,6 +7,7 @@ $(function () { self.stream_id = ko.observable(); self.streaming = ko.observable(); self.processing = ko.observable(false); + self.view_url = ko.observable(); self.icon = ko.pureComputed(function() { var icons = []; if (self.streaming() && !self.processing()) { @@ -53,6 +54,14 @@ $(function () { }) } + self.onTabChange = function(next, current) { + if(next == '#tab_plugin_youtubelive'){ + self.view_url('https://www.youtube.com/embed/live_stream?channel=' + self.settingsViewModel.settings.plugins.youtubelive.channel_id()); + } else { + self.view_url(''); + } + } + self.onDataUpdaterPluginMessage = function(plugin, data) { if (plugin != "youtubelive") { return; diff --git a/octoprint_youtubelive/templates/youtubelive_settings.jinja2 b/octoprint_youtubelive/templates/youtubelive_settings.jinja2 index f003f0c..79a2774 100644 --- a/octoprint_youtubelive/templates/youtubelive_settings.jinja2 +++ b/octoprint_youtubelive/templates/youtubelive_settings.jinja2 @@ -12,4 +12,10 @@ +
+ +
+ +
+
\ No newline at end of file diff --git a/octoprint_youtubelive/templates/youtubelive_tab.jinja2 b/octoprint_youtubelive/templates/youtubelive_tab.jinja2 index 76405d1..ff2ad21 100644 --- a/octoprint_youtubelive/templates/youtubelive_tab.jinja2 +++ b/octoprint_youtubelive/templates/youtubelive_tab.jinja2 @@ -1,5 +1,5 @@
- +
diff --git a/setup.py b/setup.py index 90c6b68..1c8b23d 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "YouTube Live" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.4.0" +plugin_version = "0.5.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module