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