start controller view

This commit is contained in:
bkfox
2015-11-09 18:19:47 +01:00
parent ecde02725e
commit 4fbd30a460
8 changed files with 232 additions and 10 deletions

View File

@ -105,11 +105,19 @@ class Command (BaseCommand):
)
}
def liquid_station (self, station):
station.streams = [
self.liquid_stream(stream)
for stream in models.Stream.objects.filter(
program__active = True, program__station = station
)
]
return station
def get_config (self, output = None):
context = {
'streams': [
self.liquid_stream(stream)
for stream in models.Stream.objects.filter(program__active = True)
'stations': [ self.liquid_station(station)
for station in models.Station.objects.filter(active = True)
],
'settings': settings,
}

View File

@ -0,0 +1,11 @@
{% for station in stations %}
{% for name, source in station.sources.items %}
<div class="source">
{{ name }}:
{{ source.initial_uri }}
<time style="font-size: 0.9em; color: grey">{{ source.on_air }}</time>
</div>
{% endfor %}
{% endfor %}

View File

@ -0,0 +1,58 @@
{% comment %}
A station has multiple sources:
- dealer: a controlled playlist playing once each track, that reloads on file
change. This is used for scheduled sounds.
- streams: a rotate source with all playlists
- single: security song
{% endcomment %}
{% with name=station.name streams=station.streams %}
def make_station_{{ name }} () = \
{# dealer #}
dealer = interactive_source('{{ name }}_dealer', playlist.once( \
reload_mode='watch', \
'/tmp/dealer.m3u', \
)) \
\
dealer_on = interactive.bool("{{ name }}_dealer_on", false) \
\
{# streams #}
streams = interactive_source("streams", rotate([ \
{% for stream in streams %}
{% if stream.delay %}
delay({{ stream.delay }}., stream("{{ name }}_{{ stream.name }}", "{{ stream.file }}")), \
{% endif %}
{% endfor %}
switch([ \
{% for stream in streams %}
{% if stream.begin and stream.end %}
({ stream.begin, stream.end }, stream("{{ name }}_{{ stream.name }}", "{{ stream.file }}")), \
{% endif %}
{% endfor %}
]), \
{% for stream in streams %}
{% if not stream.delay %}
{% if not stream.begin or not stream.end %}
stream("{{ name }}_{{ stream.name }}", "{{ stream.file }}"), \
{% endif %}
{% endif %}
{% endfor %}
])) \
\
{# station #}
interactive_source ( \
"{{ name }}", \
fallback(track_sensitive = false, [ \
at(dealer_on, dealer), \
{% if station.fallback %}
streams, \
single("{{ station.fallback }}") \
{% else %}
mksafe(streams) \
{% endif %}
]) \
) \
end \
{% endwith %}

View File

@ -0,0 +1,9 @@
from django.conf.urls import url
import aircox_liquidsoap.views as views
urlpatterns = [
url('^controller/', views.LiquidControl.as_view(), name = 'liquid-controller'),
]

View File

@ -1,4 +1,6 @@
import socket
import re
import aircox_liquidsoap.settings as settings
class Controller:
@ -23,12 +25,26 @@ class Controller:
def send (self, data):
if self.open():
return -1
return ''
data = bytes(data + '\n', encoding='utf-8')
self.socket.sendall(data)
return self.socket.recv(10240).decode('utf-8')
def get (self, stream = None):
print(self.send('station.get'))
def parse (self, string):
string = string.split('\n')
data = {}
for line in string:
line = re.search(r'(?P<key>[^=]+)="?(?P<value>([^"]|\\")+)"?', line)
if not line:
continue
line = line.groupdict()
data[line['key']] = line['value']
return data
def get (self, station, source = None):
if source:
r = self.send('{}_{}.get'.format(station.get_slug_name(), source))
else:
r = self.send('{}.get'.format(station.get_slug_name()))
return self.parse(r) if r else None

View File

@ -0,0 +1,47 @@
import re
from django.views.generic.base import View, TemplateResponseMixin
from django.template.loader import render_to_string
from django.shortcuts import render
from django.http import HttpResponse
import aircox_liquidsoap.settings as settings
import aircox_liquidsoap.utils as utils
import aircox_programs.models as models
class LiquidControl (View):
template_name = 'aircox_liquidsoap/controller.html'
def get_context_data (self, **kwargs):
stations = models.Station.objects.all()
controller = utils.Controller()
for station in stations:
name = station.get_slug_name()
streams = models.Stream.objects.filter(
program__active = True,
program__station = station
)
# list sources
sources = [ 'dealer' ] + \
[ stream.program.get_slug_name() for stream in streams]
# sources status
station.sources = { name: controller.get(station) }
station.sources.update({
source: controller.get(station, source)
for source in sources
})
return {
'request': self.request,
'stations': stations,
}
def get (self, request = None, **kwargs):
self.request = request
context = self.get_context_data(**kwargs)
return render(request, self.template_name, context)