Commit c2d89057 authored by Gottfried Gaisbauer's avatar Gottfried Gaisbauer
Browse files

minor fixes, added requirements

parent 6c679bce
#!/usr/bin/python3
import signal
import sys
import os
from modules.scheduling.scheduler import AuraScheduler
from modules.communication.liquidsoap.communicator import LiquidSoapCommunicator
......@@ -17,7 +17,7 @@ class Aura(AuraCommon):
# ------------------------------------------------------------------------------------------ #
def __init__(self):
AuraCommon.__init__(self, name="AuraEngine")
super(Aura, self).__init__()
server = object
......@@ -52,7 +52,7 @@ class Aura(AuraCommon):
except OSError as e:
self.messenger.halt()
self.logger.critical("AuraEngine already running? Exception: " + e.strerror + ". Exiting...")
sys.exit(0)
os._exit(0)
# # ## ## ## ## ## # #
......
......@@ -67,7 +67,7 @@ from_mail=""
# calendarurl="http://localhost/index.php?option=com_jimtawl&view=calendar&format=json&from=#datefrom#&to=#dateto#"
# calendarurl="http://bermudafunk-kalender.critmass.de/index.php?option=com_jimtawl&view=calendar&format=json&from=#datefrom#&to=#dateto#"
calendarurl="http://localhost:8000/api/v1/playout"
importerurl=""
importerurl="http://localhost:8008/api/v1/groups/_public/playlists/"
[folder]
audiobase="/var/audio/rec"
......
......@@ -18,7 +18,7 @@ class Guru(AuraCommon):
# ------------------------------------------------------------------------------------------ #
def __init__(self):
AuraCommon.__init__(self, name="AuraGuru")
super(Guru, self).__init__()
self.init_argument_parser()
self.handle_arguments()
......@@ -80,9 +80,10 @@ class Guru(AuraCommon):
self.parser.add_argument("-rd", "--recreate-database", action="store_true", dest="recreatedb", default=False, help="Do you want to recreate the database?")
# getter
self.parser.add_argument("-gam", "--get-active-mixer", action="store_true", dest="get_active_mixer", default=False, help="Which mixer is activated?")
self.parser.add_argument("-pms", "--print-mixer-status", action="store_true", dest="get_mixer_status", default=False, help="Prints all mixer sources and their states")
self.parser.add_argument("-pap", "--print-act-programme", action="store_true", dest="get_act_programme", default=False, help="Prints the actual Programme, the controller holds")
self.parser.add_argument("-pcs", "--print-connection-status", action="store_true", dest="get_connection_status", default=False, help="Prints the status of the connection to liquidsoap, pv and tank")
self.parser.add_argument("-gam", "--get-active-mixer", action="store_true", dest="get_active_mixer", default=False, help="Which mixer is activated?")
self.parser.add_argument("-pms", "--print-mixer-status", action="store_true", dest="get_mixer_status", default=False, help="Prints all mixer sources and their states")
self.parser.add_argument("-pap", "--print-act-programme", action="store_true", dest="get_act_programme", default=False, help="Prints the actual Programme, the controller holds")
# liquid manipulation
self.parser.add_argument("-am", "--select-mixer", action="store", dest="select_mixer", default=-1, metavar="MIXERNAME", help="Which mixer should be activated?")
......
......@@ -7,9 +7,12 @@ class AuraCommon:
logger = None
config = None
def __init__(self, name):
def __init__(self, logger=True):
self.read_config()
self.create_logger(name)
# create logger just once
if logger:
self.create_logger("AuraEngine")
def read_config(self):
self.config = ConfigReader()
......
......@@ -7,11 +7,14 @@ class RedisChannel(Enum):
DPE_REPLY = "delete_playlist_entry_reply"
FNP_REPLY = "fetch_new_programme_reply"
GAP_REPLY = "get_act_programme_reply"
SNF_REPLY = "get_next_file_reply"
GCS_REPLY = "get_connection_status_reply"
IPE_REPLY = "insert_playlist_entry_reply"
IP_REPLY = "init_player_reply"
MPE_REPLY = "move_playlist_entry_reply"
PMQ_REPLY = "print_message_queue_reply"
RDB_REPLY = "recreate_database_reply"
SNF_REPLY = "get_next_file_reply"
......@@ -34,6 +34,9 @@ class Padavan:
elif self.args.get_act_programme:
self.get_act_programme()
elif self.args.get_connection_status:
self.get_connection_status()
# elif self.args.add_source:
# print("Guru still has to learn to add a source")
......@@ -118,6 +121,11 @@ class Padavan:
actprogramme = simplejson.loads(json_reply)
self.print_programme(actprogramme)
def get_connection_status(self):
json_reply = self.send_and_wait_redis("aura", "get_connection_status", RedisChannel.GCS_REPLY)
connection_status = simplejson.loads(json_reply)
self.print_connection_status(connection_status)
def print_programme(self, programme):
for entry in programme:
self.stringreply += "idx: " + str(entry["programme_index"]) + \
......@@ -126,6 +134,36 @@ class Padavan:
" - starts @ " + entry["entry_start"] + \
" - plays " + str(entry["source"]) + "\n"
def print_connection_status(self, connection_status):
from libraries.enum.consolecolor import TerminalColors
if connection_status["pv"]:
self.stringreply = "Connection to pv: " + TerminalColors.GREEN.value + " " + str(connection_status["pv"]) + TerminalColors.ENDC.value
else:
self.stringreply = "Connection to pv: " + TerminalColors.RED.value + " " + str(connection_status["pv"]) + TerminalColors.ENDC.value
if connection_status["db"]:
self.stringreply += "\nConnection to db: " + TerminalColors.GREEN.value + " " + str(connection_status["db"]) + TerminalColors.ENDC.value
else:
self.stringreply += "\nConnection to db: " + TerminalColors.RED.value + " " + str(connection_status["db"]) + TerminalColors.ENDC.value
if connection_status["lqs"]:
self.stringreply += "\nConnection to lqs: " + TerminalColors.GREEN.value + " " + str(connection_status["lqs"]) + TerminalColors.ENDC.value
else:
self.stringreply += "\nConnection to lqs: " + TerminalColors.RED.value + " " + str(connection_status["lqs"]) + TerminalColors.ENDC.value
if connection_status["tank"]:
self.stringreply += "\nConnection to tank: " + TerminalColors.GREEN.value + " " + str(connection_status["tank"]) + TerminalColors.ENDC.value
else:
self.stringreply += "\nConnection to tank: " + TerminalColors.RED.value + " " + str(connection_status["tank"]) + TerminalColors.ENDC.value
if connection_status["redis"]:
self.stringreply += "\nConnection to redis: " + TerminalColors.GREEN.value + " " + str(connection_status["redis"]) + TerminalColors.ENDC.value
else:
self.stringreply += "\nConnection to redis: " + TerminalColors.RED.value + " " + str(connection_status["redis"]) + TerminalColors.ENDC.value
def init_player(self):
self.stringreply = self.send_and_wait_redis("aura", "init_player", RedisChannel.IP_REPLY)
......
import urllib
import simplejson
from modules.communication.liquidsoap.communicator import LiquidSoapCommunicator
from libraries.database.broadcasts import ScheduleEntry
from libraries.base.common import AuraCommon
from libraries.exceptions.auraexceptions import LQConnectionError
class ConnectionTester(AuraCommon):
def __init__(self):
super(ConnectionTester, self).__init__(logger=False)
def get_connection_status(self):
dbconn = self.test_db_conn()
pvconn = self.test_pv_conn()
lqsconn = self.test_lqs_conn()
tankconn = self.test_tank_conn()
redisconn = self.test_redis_conn()
status = dict()
status["db"] = dbconn
status["pv"] = pvconn
status["lqs"] = lqsconn
status["tank"] = tankconn
status["redis"] = redisconn
return simplejson.dumps(status)
def test_db_conn(self):
try:
ScheduleEntry.select_all()
except:
return False
return True
def test_lqs_conn(self):
try:
lsc = LiquidSoapCommunicator(self.config)
s = lsc.get_mixer_status()
except LQConnectionError:
return False
if s:
return True
return False
def test_pv_conn(self):
return self.test_url_connection(self.config.get("calendarurl"))
def test_tank_conn(self):
return self.test_url_connection(self.config.get("importerurl")+"1")
def test_redis_conn(self):
from modules.communication.redis.adapter import ClientRedisAdapter
try:
cra = ClientRedisAdapter()
cra.publish("aura", "status")
except:
return False
return True
def test_url_connection(self, url):
try:
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
response.read()
except Exception as e:
print(e)
return False
return True
\ No newline at end of file
......@@ -7,6 +7,7 @@ from datetime import datetime
from threading import Event
from modules.communication.redis.messenger import RedisMessenger
from modules.communication.connection_tester import ConnectionTester
from libraries.database.statestore import RedisStateStore
from libraries.exceptions.auraexceptions import RedisConnectionException
from libraries.enum.consolecolor import TerminalColors
......@@ -23,6 +24,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
redisclient = None
scheduler = None
liquidsoapcommunicator = None
connection_tester = None
def __init__(self):
threading.Thread.__init__(self)
......@@ -41,6 +43,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
self.adminMails = ''
self.redisclient = ClientRedisAdapter()
self.connection_tester = ConnectionTester()
# ------------------------------------------------------------------------------------------ #
def run(self):
......@@ -109,7 +112,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
# ------------------------------------------------------------------------------------------ #
def work(self, item):
if item["data"] == "fetch_new_programme":
self.execute(RedisChannel.FNP_REPLY.value, self.scheduler.fetch_new_programme, )
self.execute(RedisChannel.FNP_REPLY.value, self.scheduler.fetch_new_programme)
elif item["data"] == "init_player":
self.execute(RedisChannel.IP_REPLY.value, self.liquidsoapcommunicator.init_player)
......@@ -117,6 +120,9 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
elif item["data"] == "get_act_programme":
self.execute(RedisChannel.GAP_REPLY.value, self.scheduler.get_act_programme_as_string)
elif item["data"] == "get_connection_status":
self.execute(RedisChannel.GCS_REPLY.value, self.connection_tester.get_connection_status)
elif item["data"] == "print_message_queue":
self.execute(RedisChannel.PMQ_REPLY.value, self.scheduler.print_message_queue)
......@@ -144,6 +150,9 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
elif item["data"] == "recreate_db":
self.execute(RedisChannel.RDB_REPLY.value, self.scheduler.recreate_database)
elif item["data"] == "status":
return True
else:
raise RedisConnectionException("ServerRedisAdapter Cannot understand command: " + item["data"])
......@@ -199,7 +208,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
except:
pass
#self.socket.close()
# self.socket.close()
# ------------------------------------------------------------------------------------------ #
def send(self, message):
......@@ -214,6 +223,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
else:
self.logger.warning("cannot send message via REDIS: "+str(message))
class ClientRedisAdapter(RedisMessenger):
def __init__(self):
......
......@@ -345,7 +345,7 @@ class AuraCalendarService(threading.Thread):
except (urllib.error.URLError, IOError, ValueError) as e:
self.logger.error("Cannot connect to " + self.config.get("calendarurl") + "! reason: " + str(e.reason))
if not self.has_already_fetched: # first fetch
os._exit(1)
sys.exit()
self.has_already_fetched = True
return html_response
......
......@@ -66,16 +66,17 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
Gets data from pv and importer, stores and fires events
"""
redismessenger = RedisMessenger()
liquidsoapcommunicator = None
schedule_entries = None
message_timer = []
job_result = {}
liquidsoapcommunicator = None
schedule_entries = None
active_entry = None
programme = None
client = None
active_entry = None
logger = None
config = None
tried_fetching = 0
fetch_max = 2
......@@ -117,6 +118,7 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
ScheduleEntry.select_all()
except sqlalchemy.exc.ProgrammingError as e:
errcode = e.orig.args[0]
if errcode == 1146: # error for no such table
x = AuraDatabaseModel()
x.recreate_db()
......@@ -134,8 +136,10 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
# write to logger
self.logger.info("Fetch new programmes every started. Going to start next time " + str(next_time))
# fetch new programme and wait
# fetch new programme
self.fetch_new_programme()
# and wait
time.sleep(seconds_to_wait)
# ------------------------------------------------------------------------------------------ #
......@@ -345,7 +349,7 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
# ------------------------------------------------------------------------------------------ #
def fetch_new_programme(self):
if self.tried_fetching == self.fetch_max:
self.logger.error("Cannot connect to PV! No Programme loaded!")
self.logger.error("Cannot connect to PV or Tank! No Programme loaded!")
self.tried_fetching = 0
return ""
......@@ -402,7 +406,6 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
else:
self.error("stop_recording", result, "01")
class CallFunctionTimer(threading.Timer):
logger = None
entry = None
......
Flask==0.12.2
Flask-Babel==0.11.2
Flask-SQLAlchemy==2.2
Flask-WTF==0.14.2
mysqlclient==1.3.7
redis==2.10.5
simplejson==3.11.1
mutagen==1.38
python-dateutil==2.6.0
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment