import datetime
import os
import shutil
+import socket
import time
from django.template import loader
import xml.etree.ElementTree as ET
-try:
- import pysoma
-except ImportError:
- pysoma = None
-
from .models import Track, SomaLogLine, LOCAL_BASE_PATH
+class SomaException(Exception):
+ pass
+
+
def get_current_nonstop_track():
try:
soma_log_line = SomaLogLine.objects.select_related().order_by('-play_timestamp')[0]
palinsesti = palinsesti_template.render(context)
palinsesti_xml = ET.fromstring(palinsesti.encode('utf-8'))
- # append to soma
- if pysoma:
- connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
- palinsesto_xml = ET.fromstring(connection.get_palinsesto())
- palinsesto_xml.append(palinsesti_xml)
- with open('/tmp/soma.pl', 'w') as fd:
- fd.write(ET.tostring(palinsesto_xml))
-
- connection.new_palinsesto('/tmp/soma.pl')
- del connection
-
- # give it some time (...)
- time.sleep(3)
- connection = pysoma.open_tcp('soma.panik', 12521, '', 0)
- connection.set_default_palinsesto()
- del connection
+ # add to soma
+ def soma_connection():
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s.connect(('soma', 12521))
+ s.recv(1024) # -> b'soma 2.5 NO_SSL\n'
+ s.sendall(b'100 - Ok\n')
+ s.recv(1024) # -> b'100 - Welcome to soma daemon\n'
+ s.sendall(b'\n') # (empty password)
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to initialize soma connection')
+ return s
+
+ with soma_connection() as s:
+ s.sendall(b'109 - Get the current palinsesto\n')
+ palinsesto_bytes = b''
+ while True:
+ new_bytes = s.recv(200000)
+ if not new_bytes:
+ break
+ palinsesto_bytes += new_bytes
+
+ palinsesto_xml = ET.fromstring(palinsesto_bytes)
+ palinsesto_xml.append(palinsesti_xml)
+ with soma_connection() as s:
+ s.sendall(b'106 - Switch to a New Palinsesto Request\n')
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to switch palinsesto')
+ s.sendall(ET.tostring(palinsesto_xml).encode('utf-8'))
+
+ # give it some time (...)
+ time.sleep(3)
+ with soma_connection() as s:
+ s.sendall(b'122 - Set the current Palinsesto as Default\n')
+ if s.recv(1024) == b'100 - Ok\n':
+ raise SomaException('failed to set current palinsesto as default')
utils.add_diffusion(diffusion)
except utils.DuplicateDiffusionSlot:
messages.error(self.request, _('soma slot already in use, the diffusion could not be added.'))
+ except utils.SomaException as e:
+ messages.error(self.request, _('technical soma error (%s)') % e)
else:
messages.info(self.request, _('%s added to soma') % episode.emission.title)
return HttpResponseRedirect(reverse('episode-view', kwargs={