Added support for setting/changing group streams on Snapcast

This commit is contained in:
Fabio Manganiello 2019-01-08 17:43:17 +01:00
parent 35292968ed
commit 80bd5b684c
4 changed files with 104 additions and 18 deletions

View file

@ -121,19 +121,24 @@
text-align: right;
}
.snapcast-form > .snapcast-group-stream,
.snapcast-form > .snapcast-client-delete {
width: 30%;
margin: 1em auto 0 auto;
text-align: center;
}
.snapcast-form > .snapcast-group-stream > label,
.snapcast-form > .snapcast-client-delete > label {
display: inline;
margin-left: .5em;
color: rgba(200, 44, 23, 1.0);
text-align: right;
}
.snapcast-form > .snapcast-client-delete > label {
color: rgba(200, 44, 23, 1.0);
}
.snapcast-form * > .snapcast-group-clients {
max-width: 15em;
margin: auto;

View file

@ -1,6 +1,8 @@
$(document).ready(function() {
var serverInfo = {},
clientInfo = {},
serverStreams = {},
groupStreams = {},
$container = $('#snapcast-container');
var createPowerToggleElement = function(data) {
@ -71,6 +73,8 @@ $(document).ready(function() {
$container.html('');
serverInfo = {};
clientInfo = {};
serverStreams = {};
groupStreams = {};
var hosts = Object.keys(window.config.snapcast_hosts);
@ -78,6 +82,8 @@ $(document).ready(function() {
var status = statuses[i];
var host = hosts[i];
var name = status.server.host.name || status.server.host.ip;
serverStreams[host] = status.streams;
serverInfo[host] = status.server.host;
serverInfo[host].serverName = status.server.snapserver.serverName;
serverInfo[host].version = status.server.snapserver.version;
@ -114,6 +120,8 @@ $(document).ready(function() {
for (var group of status.groups) {
var groupName = group.name || group.stream_id;
groupStreams[group.id] = group.stream_id;
var $group = $('<div></div>')
.addClass('snapcast-group-container')
.data('name', groupName)
@ -336,6 +344,23 @@ $(document).ready(function() {
$label.appendTo($row);
$row.appendTo($clientsList);
}
var $streams = $form.find('.snapcast-group-stream').find('select[name=stream]');
$streams.html('');
for (var stream of serverStreams[host]) {
var $option = $('<option></option>').val(stream.id)
.text(stream.id);
if (stream.id === groupStreams[groupId]) {
$option.prop('selected', true);
}
$option.appendTo($streams);
}
$form.find('.snapcast-group-stream')
.find('select[name=stream]').html($streams.html());
});
$container.on('click touch', '.snapcast-client-settings', function(evt) {
@ -431,36 +456,48 @@ $(document).ready(function() {
clients.push($(c).attr('name'));
}
var request = {
type: 'request',
action: 'music.snapcast.group_set_clients',
args: {
host: host,
port: window.config.snapcast_hosts[host],
group: groupId,
clients: clients,
},
};
var streamId = $form.find('.snapcast-group-stream')
.find('select[name=stream]').val();
$form.find('input').prop('disabled', true);
var promises = [
execute({
type: 'request',
action: 'music.snapcast.group_set_clients',
args: {
host: host,
port: window.config.snapcast_hosts[host],
group: groupId,
clients: clients,
}
}),
execute({
type: 'request',
action: 'music.snapcast.group_set_stream',
args: {
host: host,
port: window.config.snapcast_hosts[host],
group: groupId,
stream_id: streamId,
}
}),
];
execute(
request,
(response) => {
redraw();
},
$.when.apply($, promises)
.fail(
(xhr, status, error) => {
createNotification({
'icon': 'exclamation',
'text': status + ': ' + error,
});
},
}
).always(
() => {
$form.find('input').prop('disabled', false);
$modal.fadeOut();
}
);
$form.find('input').prop('disabled', true);
return false;
});
};

View file

@ -77,6 +77,10 @@
<div class="row snapcast-group-clients-container">
<div class="row snapcast-group-clients"></div>
</div>
<div class="row snapcast-group-stream">
<select name="stream"></select>
<label for="stream">Stream</label>
</div>
<div class="row snapcast-form-bottom">
<div class="six columns offset-by-six forms-btns">
<input type="button" class="btn-default" data-dismiss-modal="#snapcast-group-modal"

View file

@ -493,4 +493,44 @@ class MusicSnapcastPlugin(Plugin):
except: pass
@action
def group_set_stream(self, group, stream_id, host=None, port=None):
"""
Sets the active stream for a group.
:param group: Group name or ID
:type group: str
:param stream_id: Stream ID
:type stream_id: str
:param host: Snapcast server (default: default configured host)
:type host: str
:param port: Snapcast server port (default: default configured port)
:type port: int
"""
sock = None
try:
sock = self._connect(host or self.host, port or self.port)
group = self._get_group(sock, group)
request = {
'id': self._get_req_id(),
'jsonrpc':'2.0',
'method': 'Group.SetStream',
'params': {
'id': group['id'],
'stream_id': stream_id,
}
}
self._send(sock, request)
return self._recv(sock)
finally:
try: sock.close()
except: pass
# vim:sw=4:ts=4:et: