From 501db5756d6cd54af6af0afc8212a28bb93cb376 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Fri, 17 Jul 2020 15:56:17 +0200
Subject: [PATCH] Call trackservice plugin for all entry types.

---
 modules/core/engine.py                      |  6 ++-
 modules/core/events.py                      | 41 ++++++++++-----------
 modules/plugins/{api.py => trackservice.py} |  3 +-
 3 files changed, 26 insertions(+), 24 deletions(-)
 rename modules/plugins/{api.py => trackservice.py} (92%)

diff --git a/modules/core/engine.py b/modules/core/engine.py
index dee253d3..31d7b2f0 100644
--- a/modules/core/engine.py
+++ b/modules/core/engine.py
@@ -314,6 +314,10 @@ class SoundSystem():
                     self.logger.info("Clear Queue Response: " + res)
                     self.disable_transaction()
                 Thread(target=clean_up).start()
+            
+            # Filesystem meta-changes trigger the event via Liquidsoap
+            if not entry.channel in ChannelType.FILESYSTEM.channels:
+                self.on_play(entry)
 
 
 
@@ -323,7 +327,7 @@ class SoundSystem():
         when some entry is actually playing.
 
         Args:
-            source (String):    The URI of the media source currently being played
+            source (String):    The `Entry` or URI or of the media source currently being played
         """
         self.event_dispatcher.on_play(source)
 
diff --git a/modules/core/events.py b/modules/core/events.py
index 8c05f027..b202d073 100644
--- a/modules/core/events.py
+++ b/modules/core/events.py
@@ -26,14 +26,14 @@ from modules.plugins.monitor    import AuraMonitor
 from modules.core.state         import PlayerStateService
 
 
-from modules.plugins.api import TrackserviceHandler
+from modules.plugins.trackservice import TrackserviceHandler
 
 
 class EventBinding():
     """
     A binding between the event dispatcher and some event handler.
 
-    This allows you to subscribe to events in this way:
+    It allows you to subscribe to events in a chained way:
 
         ```
         binding = dispatcher.attach(AuraMonitor)
@@ -66,7 +66,7 @@ class EventBinding():
 
 class EngineEventDispatcher():
     """
-    Performs execution of handlers for engine events.
+    Executes handlers for engine events.
     """
     logger = None
     config = None
@@ -90,18 +90,13 @@ class EngineEventDispatcher():
         self.soundsystem = soundsystem
         self.scheduler = scheduler
         self.player_state = PlayerStateService(self.config)
-
-        # self.api_handler = ApiHandler(self.config)
-        # self.monitoring = Monitoring(self.config, self.soundsystem)
         
         binding = self.attach(AuraMonitor)
         binding.subscribe("on_boot")
-
         binding = self.attach(TrackserviceHandler)
         binding.subscribe("on_play")
 
 
-
     def attach(self, clazz):
         """
         Creates an intance of the given Class.
@@ -158,35 +153,37 @@ class EngineEventDispatcher():
         self.logger.debug("on_boot(..)")
         self.call_event("on_boot", None)
 
-        # self.monitoring.on_boot()
-
 
     def on_ready(self):
         """
         Called when the engine is booted and ready to play.
         """
-        self.logger.debug("on_initialized(..)")
+        self.logger.debug("on_ready(..)")
         self.scheduler.on_ready()
 
 
     def on_play(self, source):
         """
         Event Handler which is called by the soundsystem implementation (i.e. Liquidsoap)
-        when some entry is actually playing.
+        when some entry is actually playing. Note that this event resolves the source URI
+        and passes an `PlaylistEntry` to event handlers.
 
         Args:
-            source (String):    The URI of the media source currently being played
+            source (String):    The `Entry` or URI or of the media source currently being played
         """
         self.logger.debug("on_play(..)")
-
-        self.logger.info(SU.pink("Source '%s' started playing" % source))
-        try:
-            self.player_state.store_trackservice_entry(source)
-            # self.player_state.get_current_entry(source)
-        except NoActiveEntryException:
-            self.on_idle()
-
-        self.call_event("on_initialized", None)
+        entry = None
+
+        if isinstance(source, str):
+            try:
+                self.logger.info(SU.pink("Source '%s' started playing. Resolving ..." % source))
+                entry = self.player_state.resolve_entry(source)                
+            except NoActiveEntryException:
+                self.logger.error("Cannot resolve '%s'" % source)
+        else:
+            entry = source
+
+        self.call_event("on_play", entry)
 
 
     def on_stop(self, entry):
diff --git a/modules/plugins/api.py b/modules/plugins/trackservice.py
similarity index 92%
rename from modules/plugins/api.py
rename to modules/plugins/trackservice.py
index b5e28352..715ce886 100644
--- a/modules/plugins/api.py
+++ b/modules/plugins/trackservice.py
@@ -22,7 +22,7 @@ import logging
 
 class TrackserviceHandler():
     """
-    ApiHandler is in charge of external API calls
+    Sends the trackservice entry to the `engine-api` REST endpoint.
     """
     logger = None
     config = None
@@ -43,6 +43,7 @@ class TrackserviceHandler():
         Some track started playing.
         """
         self.logger.info("::: CALLED TRACKSERVICE HANDLER :::")
+        self.logger.info("PLAYING: %s <<< " % (str(entry)))
         self.store_trackservice(entry)
         self.store_schedule(entry)
 
-- 
GitLab