From bc095ab3788e15041204043497fe2e5f3a4ba628 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Wed, 19 Feb 2020 14:50:35 +0100
Subject: [PATCH] Added clear queue and queue playlist.

---
 .../communication/liquidsoap/communicator.py  | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/modules/communication/liquidsoap/communicator.py b/modules/communication/liquidsoap/communicator.py
index d8c4e67e..ddd1ee53 100644
--- a/modules/communication/liquidsoap/communicator.py
+++ b/modules/communication/liquidsoap/communicator.py
@@ -323,6 +323,62 @@ class LiquidSoapCommunicator(ExceptionLogger):
             pass
 
 
+
+    def activate_playlist(self, playlist, cue_in=0.0):
+        """
+        Activates a new Playlist.
+
+        Args:
+            new_entry (Playlist): The playlist to be played
+        
+        Raises:
+            (LQConnectionError): In case connecting to LiquidSoap isn't possible
+        """
+        
+        # Grab the actual active entry
+        active_entry = self.scheduler.get_active_entry()
+        # Set default channel, if no previous track is available
+        current_channel = ScheduleEntryType.FILESYSTEM 
+        if active_entry:
+            current_channel = active_entry.type
+
+        try:
+            # FIXME clearing creates some serious timing issues
+            # To activate this feature we'd need some more sophisticated
+            # Liquidsoap logic, such as >= 2 filesystem channels and
+            # possiblities to pause pre-queued channels or cleaning them
+            # after each completed schedule.
+
+            # self.enable_transaction()
+            # #if active_entry:
+            #     #self.fade_out(active_entry)
+            # res = self.clear_queue()
+            # self.logger.info("Clear Queue Response: "+res)
+            # self.disable_transaction()
+
+
+            self.enable_transaction()
+            self.reload_channels()
+            # self.fade_in(playlist.entries[0])
+            for new_entry in playlist.entries:
+                if current_channel == new_entry.type:
+                    self.activate_same_channel(new_entry, cue_in)
+                else:
+                    self.activate_different_channel(new_entry, cue_in, current_channel)
+                    current_channel = new_entry.type
+
+            self.disable_transaction()
+
+            # FIXME Implement TrackService bi-directionally, log fallbacks too.
+            self.logger.critical("FIXME: Implement TrackService")
+#           self.insert_track_service_entry(new_entry)
+        except LQConnectionError:
+            # we already caught and handled this error in __send_lqc_command__, 
+            # but we do not want to execute this function further and pass the exception
+            pass
+
+
+
     def activate_same_channel(self, entry, cue_in=0.0, activate_different_channel=False):
         """
         Activates a playlist entry for the current channel.
@@ -380,6 +436,17 @@ class LiquidSoapCommunicator(ExceptionLogger):
         # Set active channel to wanted volume
         self.channel_volume(entry.type.value, entry.volume)
 
+
+
+    def clear_queue(self):
+        """
+        Removes all tracks currently queued.
+        """
+        self.logger.info(TerminalColors.PINK.value + "LiquidSoapCommunicator is clearing the filesystem queue!" + TerminalColors.ENDC.value)
+        return self.__send_lqc_command__(self.client, "fs", "clear", )
+
+
+
     # ------------------------------------------------------------------------------------------ #
     def insert_track_service_entry(self, schedule_entry):
         # create trackservice entry
-- 
GitLab