Removed get_plugin utils method from web server, it messes up with also with the reentrant locks as it runs in another process. Refactored Snapcast frontend to get the backend hosts asynchronously through a plugin method

This commit is contained in:
Fabio Manganiello 2019-01-10 11:52:39 +01:00
parent c1b05226a9
commit a349b45ba4
4 changed files with 46 additions and 37 deletions

View File

@ -15,7 +15,7 @@ from flask import Flask, Response, abort, jsonify, request as http_request, \
from redis import Redis
from platypush.config import Config
from platypush.context import get_backend, get_plugin, get_or_create_event_loop
from platypush.context import get_backend, get_or_create_event_loop
from platypush.message import Message
from platypush.message.event import Event, StopEvent
from platypush.message.event.web.widget import WidgetUpdateEvent
@ -550,9 +550,4 @@ class HttpUtils(object):
return json.loads(data)
@classmethod
def get_plugin(cls, plugin):
return get_plugin(plugin)
# vim:sw=4:ts=4:et:

View File

@ -225,32 +225,43 @@ $(document).ready(function() {
};
var redraw = function() {
var promises = [];
execute(
{
type: 'request',
action: 'music.snapcast.get_backend_hosts',
},
for (var host of Object.keys(window.config.snapcast_hosts)) {
promises.push(
execute({
type: 'request',
action: 'music.snapcast.status',
args: {
host: host,
port: window.config.snapcast_hosts[host],
(response) => {
window.config = window.config || {};
window.config.snapcast_hosts = response.response.output;
var promises = [];
for (var host of Object.keys(window.config.snapcast_hosts)) {
promises.push(
execute({
type: 'request',
action: 'music.snapcast.status',
args: {
host: host,
port: window.config.snapcast_hosts[host],
}
})
);
}
$.when.apply($, promises)
.done(function() {
var statuses = [];
for (var status of arguments) {
statuses.push(status[0].response.output);
}
})
);
}
$.when.apply($, promises)
.done(function() {
var statuses = [];
for (var status of arguments) {
statuses.push(status[0].response.output);
update(statuses);
}).then(function() {
initBindings();
});
}
update(statuses);
}).then(function() {
initBindings();
});
);
};
var initBindings = function() {

View File

@ -1,11 +1,6 @@
<script type="text/javascript" src="{{ url_for('static', filename='js/music.snapcast.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='css/music.snapcast.css') }}"></script>
<script type="text/javascript">
window.config = window.config || {};
window.config.snapcast_hosts = JSON.parse('{{ utils.get_plugin("music.snapcast").backend_hosts_json | safe }}');
</script>
<div id="snapcast-host-modal" class="modal snapcast-modal">
<div class="modal-container">
<div class="modal-header"></div>

View File

@ -40,10 +40,6 @@ class MusicSnapcastPlugin(Plugin):
backend_ports = backend.ports if backend else [self.port]
self.backend_hosts = backend_hosts
self.backend_ports = backend_ports
self.backend_hosts_json = json.dumps({
backend_hosts[i]: backend_ports[i]
for i in range(len(backend_hosts))
})
def _get_req_id(self):
with self._latest_req_id_lock:
@ -535,4 +531,16 @@ class MusicSnapcastPlugin(Plugin):
except: pass
@action
def get_backend_hosts(self):
"""
:return: A dict with the Snapcast hosts configured on the backend
in the format host -> port
"""
hosts = {}
for i in range(len(self.backend_hosts)):
hosts[self.backend_hosts[i]] = self.backend_ports[i]
return hosts
# vim:sw=4:ts=4:et: