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 redis import Redis
from platypush.config import Config 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 import Message
from platypush.message.event import Event, StopEvent from platypush.message.event import Event, StopEvent
from platypush.message.event.web.widget import WidgetUpdateEvent from platypush.message.event.web.widget import WidgetUpdateEvent
@ -550,9 +550,4 @@ class HttpUtils(object):
return json.loads(data) return json.loads(data)
@classmethod
def get_plugin(cls, plugin):
return get_plugin(plugin)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et:

View file

@ -225,6 +225,15 @@ $(document).ready(function() {
}; };
var redraw = function() { var redraw = function() {
execute(
{
type: 'request',
action: 'music.snapcast.get_backend_hosts',
},
(response) => {
window.config = window.config || {};
window.config.snapcast_hosts = response.response.output;
var promises = []; var promises = [];
for (var host of Object.keys(window.config.snapcast_hosts)) { for (var host of Object.keys(window.config.snapcast_hosts)) {
@ -251,6 +260,8 @@ $(document).ready(function() {
}).then(function() { }).then(function() {
initBindings(); initBindings();
}); });
}
);
}; };
var initBindings = function() { var initBindings = function() {

View file

@ -1,11 +1,6 @@
<script type="text/javascript" src="{{ url_for('static', filename='js/music.snapcast.js') }}"></script> <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> <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 id="snapcast-host-modal" class="modal snapcast-modal">
<div class="modal-container"> <div class="modal-container">
<div class="modal-header"></div> <div class="modal-header"></div>

View file

@ -40,10 +40,6 @@ class MusicSnapcastPlugin(Plugin):
backend_ports = backend.ports if backend else [self.port] backend_ports = backend.ports if backend else [self.port]
self.backend_hosts = backend_hosts self.backend_hosts = backend_hosts
self.backend_ports = backend_ports 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): def _get_req_id(self):
with self._latest_req_id_lock: with self._latest_req_id_lock:
@ -535,4 +531,16 @@ class MusicSnapcastPlugin(Plugin):
except: pass 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: # vim:sw=4:ts=4:et: