Skip to content

Commit

Permalink
<UNDER CONSTRUCTION> Status publishing via multitimer
Browse files Browse the repository at this point in the history
  • Loading branch information
Groovylein committed Apr 23, 2024
1 parent 8169592 commit b4ff177
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/jukebox/components/player/backends/mpd/interfacing_mpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(self, event_loop):
# TODO: If connect fails on first try this is non recoverable
self.connect()
# Start the status listener in an endless loop in the event loop
asyncio.run_coroutine_threadsafe(self._status_listener(), self.loop)
# asyncio.run_coroutine_threadsafe(self._status_listener(), self.loop)

# ------------------------------------------------------------------------------------------------------
# Bring calls to client functions from the synchronous part into the async domain
Expand Down Expand Up @@ -95,21 +95,28 @@ async def _status_listener(self):
# logger.debug("MPD: Idle change in", subsystem)
s = await self.client.status()
# logger.debug(f"MPD: New Status: {s.result()}")
print(f"MPD: New Status: {type(s)} // {s}")
#print(f"MPD: New Status: {type(s)} // {s}")
# Now, do something with it ...
publishing.get_publisher().send('playerstatus', s)


async def _status(self):
return await self.client.status()

@plugin.tag
def status(self):
"""Refresh the current MPD status (by a manual, sync trigger)"""
# Example
# Status: {'volume': '40', 'repeat': '0', 'random': '0', 'single': '0', 'consume': '0', 'partition': 'default',
# 'playlist': '94', 'playlistlength': '22', 'mixrampdb': '0.000000', 'state': 'play', 'song': '0',
# 'songid': '71', 'time': '1:126', 'elapsed': '1.108', 'bitrate': '96', 'duration': '125.988',
# 'audio': '44100:24:2', 'nextsong': '1', 'nextsongid': '72'}
f = asyncio.run_coroutine_threadsafe(self._status(), self.loop).result()
print(f"Status: {f}")
# print(f"Status: {f}")
# Put it into unified structure and notify global player control
# ToDo: propagate to core player
publishing.get_publisher().send('playerstatus', f)
# publishing.get_publisher().send('playerstatus', f)
return f

# -----------------------------------------------------
# Stuff that controls current playback (i.e. moves around in the current playlist, termed "the queue")
Expand Down
24 changes: 24 additions & 0 deletions src/jukebox/components/player/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from typing import Dict, Callable, Optional, Any

import jukebox.plugs as plugin
from components.player.core.player_status import PlayerStatus
from jukebox import multitimer

logger = logging.getLogger('jb.player')

Expand All @@ -29,12 +31,23 @@ class PlayerCtrl:
def __init__(self):
self._backends: Dict[str, Any] = {}
self._active = None
self.player_status = None
self.status_poll_interval = 0.25
self.status_thread = multitimer.GenericEndlessTimerClass('player.timer_status',
self.status_poll_interval, self._status_poll)
self.status_thread.start()

def _status_poll(self):
ret_status = self._active.status()
if ret_status.get('state') == 'play':
self.player_status.update(playing=True, elapsed=ret_status.get('elapsed', '0.0'), duration=ret_status.get('duration', '0.0') )

def register(self, name: str, backend):
self._backends[name] = backend
# For now simply default to first registered backend
if self._active is None:
self._active = self._backends.values().__iter__().__next__()
self.player_status.update(player=name)

@plugin.tag
def get_active(self):
Expand Down Expand Up @@ -64,6 +77,7 @@ def play_uri(self, uri, check_second_swipe=False, **kwargs):
if inst is None:
raise KeyError(f"URI player type unknown: '{player_type}'. Available backends are: {self._backends.keys()}.")
self._active = self._backends.get(player_type)
self.player_status.update(player=player_type)
self._active.play_uri(uri, **kwargs)

def _is_second_swipe(self):
Expand Down Expand Up @@ -106,22 +120,30 @@ def prev(self):
@plugin.tag
def play(self):
self._active.play()
self.player_status.update(playing=True)

@plugin.tag
def play_single(self, uri):
self.play_uri(uri)
self.player_status.update(playing=True)

@plugin.tag
def play_album(self, albumartist, album):
self._active.play_album(albumartist, album)
self.player_status.update(playing=True)

@plugin.tag
def play_folder(self, folder, recursive):
self._active.play_folder(folder, recursive)
self.player_status.update(playing=True)

@plugin.tag
def toggle(self):
self._active.toggle()
if self.player_status.get_value('playing') is False:
self.player_status.update(playing=True)
else:
self.player_status.update(playing=False)

@plugin.tag
def shuffle(self, option='toggle'):
Expand All @@ -133,12 +155,14 @@ def shuffle(self, option='toggle'):
@plugin.tag
def pause(self):
self._active.pause()
self.player_status.update(playing=False)

@plugin.tag
def stop(self):
# Save current state for resume functionality
self._save_state()
self._active.stop()
self.player_status.update(playing=False)

@plugin.tag
def get_queue(self):
Expand Down
5 changes: 4 additions & 1 deletion src/jukebox/components/player/core/player_status.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

import jukebox.plugs as plugin
from jukebox import publishing
from jukebox import publishing, multitimer

logger = logging.getLogger('jb.player')

Expand Down Expand Up @@ -33,6 +33,9 @@ def update(self, **kwargs):

self.publish()

def get_value(self, key):
return self.STATUS.get(key)

def publish(self):
logger.debug(f'Published: {self._player_status}')
return publishing.get_publisher().send(
Expand Down
2 changes: 1 addition & 1 deletion src/jukebox/components/player/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def initialize():
player_arbiter = PlayerCtrl()

player_status = PlayerStatus()
player_status.publish()
player_arbiter.player_status = player_status

# ToDo: remove player_content
# player_content = PlayerData()
Expand Down

0 comments on commit b4ff177

Please sign in to comment.