refactor methods into appropriate locations

This commit is contained in:
Martin Konecny 2013-05-02 18:46:16 -04:00
parent 84efb4a9b7
commit 6c42064c14
3 changed files with 65 additions and 63 deletions

View File

@ -107,10 +107,13 @@ class PypoFetch(Thread):
self.update_liquidsoap_transition_fade(m['transition_fade'])
elif command == 'switch_source':
self.logger.info("switch_on_source show command received...")
self.switch_source(self.logger, self.telnet_lock, m['sourcename'], m['status'])
self.pypo_liquidsoap.\
get_telnet_dispatcher().\
switch_source(m['sourcename'], m['status'])
elif command == 'disconnect_source':
self.logger.info("disconnect_on_source show command received...")
self.disconnect_source(self.logger, self.telnet_lock, m['sourcename'])
self.pypo_liquidsoap.get_telnet_dispatcher().\
disconnect_source(m['sourcename'])
else:
self.logger.info("Unknown command: %s" % command)
@ -128,65 +131,7 @@ class PypoFetch(Thread):
self.logger.error("traceback: %s", top)
self.logger.error("Exception in handling Message Handler message: %s", e)
@staticmethod
def disconnect_source(logger, lock, sourcename):
logger.debug('Disconnecting source: %s', sourcename)
command = ""
if(sourcename == "master_dj"):
command += "master_harbor.kick\n"
elif(sourcename == "live_dj"):
command += "live_dj_harbor.kick\n"
try:
lock.acquire()
tn = telnetlib.Telnet(config_static['ls_host'], config_static['ls_port'])
logger.info(command)
tn.write(command)
tn.write('exit\n')
tn.read_all()
except Exception, e:
logger.error(traceback.format_exc())
finally:
lock.release()
@staticmethod
def telnet_send(logger, lock, commands):
try:
lock.acquire()
tn = telnetlib.Telnet(config_static['ls_host'], config_static['ls_port'])
for i in commands:
logger.info(i)
tn.write(i)
tn.write('exit\n')
tn.read_all()
except Exception, e:
logger.error(str(e))
finally:
lock.release()
@staticmethod
def switch_source(logger, lock, sourcename, status):
logger.debug('Switching source: %s to "%s" status', sourcename, status)
command = "streams."
if sourcename == "master_dj":
command += "master_dj_"
elif sourcename == "live_dj":
command += "live_dj_"
elif sourcename == "scheduled_play":
command += "scheduled_play_"
if status == "on":
command += "start\n"
else:
command += "stop\n"
PypoFetch.telnet_send(logger, lock, [command])
#TODO: Merge this with switch_source
def switch_source_temp(self, sourcename, status):
self.logger.debug('Switching source: %s to "%s" status', sourcename, status)
command = "streams."
@ -227,7 +172,7 @@ class PypoFetch(Thread):
commands.append(('vars.stream_metadata_type %s\n' % stream_format).encode('utf-8'))
commands.append(('vars.station_name %s\n' % station_name).encode('utf-8'))
commands.append(('vars.default_dj_fade %s\n' % fade).encode('utf-8'))
PypoFetch.telnet_send(self.logger, self.telnet_lock, commands)
self.pypo_liquidsoap.get_telnet_dispatcher().telnet_send(commands)
def restart_liquidsoap(self):
try:

View File

@ -23,6 +23,9 @@ class PypoLiquidsoap():
port,\
self.liq_queue_tracker.keys())
def get_telnet_dispatcher(self):
return self.telnet_liquidsoap
def play(self, media_item):
if media_item["type"] == eventtypes.FILE:
@ -67,9 +70,9 @@ class PypoLiquidsoap():
def handle_event_type(self, media_item):
if media_item['event_type'] == "kick_out":
PypoFetch.disconnect_source(self.logger, self.telnet_lock, "live_dj")
self.telnet_liquidsoap.disconnect_source("live_dj")
elif media_item['event_type'] == "switch_off":
PypoFetch.switch_source(self.logger, self.telnet_lock, "live_dj", "off")
self.telnet_liquidsoap.switch_source("live_dj", "off")
def is_media_item_finished(self, media_item):

View File

@ -193,6 +193,60 @@ class TelnetLiquidsoap:
finally:
self.telnet_lock.release()
def disconnect_source(self, sourcename):
self.logger.debug('Disconnecting source: %s', sourcename)
command = ""
if(sourcename == "master_dj"):
command += "master_harbor.kick\n"
elif(sourcename == "live_dj"):
command += "live_dj_harbor.kick\n"
try:
self.telnet_lock.acquire()
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
self.logger.info(command)
tn.write(command)
tn.write('exit\n')
tn.read_all()
except Exception, e:
self.logger.error(traceback.format_exc())
finally:
self.telnet_lock.release()
def telnet_send(self, commands):
try:
self.telnet_lock.acquire()
tn = telnetlib.Telnet(self.ls_host, self.ls_port)
for i in commands:
self.logger.info(i)
tn.write(i)
tn.write('exit\n')
tn.read_all()
except Exception, e:
self.logger.error(str(e))
finally:
self.telnet_lock.release()
def switch_source(self, sourcename, status):
self.logger.debug('Switching source: %s to "%s" status', sourcename, status)
command = "streams."
if sourcename == "master_dj":
command += "master_dj_"
elif sourcename == "live_dj":
command += "live_dj_"
elif sourcename == "scheduled_play":
command += "scheduled_play_"
if status == "on":
command += "start\n"
else:
command += "stop\n"
self.telnet_send([command])
class DummyTelnetLiquidsoap:
def __init__(self, telnet_lock, logger):