From c619d89fa447f84e97e1d28a2b0ec4f40c6a5119 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 10 Jan 2019 17:22:52 +0100 Subject: [PATCH] Added method to programmatically get the list of active Snapcast connections --- platypush/plugins/music/snapcast.py | 65 +++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/platypush/plugins/music/snapcast.py b/platypush/plugins/music/snapcast.py index 8a54d410d4..98a337a1f5 100644 --- a/platypush/plugins/music/snapcast.py +++ b/platypush/plugins/music/snapcast.py @@ -2,6 +2,7 @@ import json import socket import threading +from platypush.config import Config from platypush.context import get_backend from platypush.plugins import Plugin, action @@ -543,4 +544,68 @@ class MusicSnapcastPlugin(Plugin): hosts[self.backend_hosts[i]] = self.backend_ports[i] return hosts + @action + def get_playing_streams(self, exclude_local=True): + """ + Returns the remote streams configured in the `music.snapcast` backend + that are currently active and unmuted. + + :param exclude_local: Exclude localhost connections (default: true) + :type exclude_local: bool + + :returns: dict with the host->port mapping. Example:: + + { + "hosts": { + "server_1": 1705, + "server_2": 1705, + "server_3": 1705 + } + } + """ + + backend_hosts = self.get_backend_hosts().output + + def _worker(host, port): + if exclude_local and (host == 'localhost' + or host == Config.get('device_id')): + del backend_hosts[host] + return + + server_status = self.status(host=host, port=port).output + client_status = self.status(host=host, port=port, + client=Config.get('device_id')).output + + if client_status.get('config', {}).get('volume', {}).get('muted'): + del backend_hosts[host] + return + + group = [g for g in server_status.get('groups', {}) + if g.get('id') == client_status.get('group_id')].pop(0) + + if group.get('muted'): + del backend_hosts[host] + return + + stream = [s for s in server_status.get('streams') + if s.get('id') == group.get('stream_id')].pop(0) + + if stream.get('status') != 'playing': + del backend_hosts[host] + return + + workers = [] + + for host, port in backend_hosts.items(): + w = threading.Thread(target=_worker, args=(host,port)) + w.start() + workers.append(w) + + while workers: + w = workers.pop() + w.join() + + return {'hosts': backend_hosts} + + # vim:sw=4:ts=4:et: