Newer
Older
#
# engine
#
# Playout Daemon for autoradio project
#
#
# Copyright (C) 2017-2018 Gottfried Gaisbauer <gottfried.gaisbauer@servus.at>
#
# This file is part of engine.
#
# engine is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# engine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with engine. If not, see <http://www.gnu.org/licenses/>.
#

Gottfried Gaisbauer
committed
import logging

David Trattnig
committed
from pathlib import Path

Gottfried Gaisbauer
committed
from flask import request, render_template, Flask, Response
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
#from modules.web.routes import Routes

Gottfried Gaisbauer
committed
from modules.monitoring.diskspace_watcher import DiskSpaceWatcher
from libraries.base.logger import AuraLogger

Gottfried Gaisbauer
committed
from libraries.base.config import AuraConfig
def get_config_file():
if len(sys.argv) >= 3 and "--config-file" in sys.argv:
idx = sys.argv.index("--config-file")
return sys.argv[idx + 1]
else:

David Trattnig
committed
return "%s/configuration/engine.ini" % Path(__file__).parent.absolute()

Gottfried Gaisbauer
committed
def get_database_uri():
db_name = config.get("db_name")
db_user = config.get("db_user")
db_pass = config.get("db_pass")
db_host = config.get("db_host")
db_charset = config.get("db_charset", "utf8")
return "mysql://" + db_user + ":" + db_pass + "@" + db_host + "/" + db_name + "?charset=" + db_charset

Gottfried Gaisbauer
committed
def configure_flask():
app.config["SQLALCHEMY_DATABASE_URI"] = get_database_uri()
app.config['BABEL_DEFAULT_LOCALE'] = 'de'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

Gottfried Gaisbauer
committed
config = AuraConfig(get_config_file())
app = Flask(__name__, template_folder=config.get("install_dir") + "/modules/web/templates")
configure_flask()
DB = SQLAlchemy(app)
Base = declarative_base()
class Aura:
logger = None
config = None

Gottfried Gaisbauer
committed
server = None
messenger = None

Gottfried Gaisbauer
committed
# ------------------------------------------------------------------------------------------ #
def __init__(self):

Gottfried Gaisbauer
committed
self.config = config
AuraLogger(self.config)
self.logger = logging.getLogger("AuraEngine")

Gottfried Gaisbauer
committed
from modules.scheduling.scheduler import AuraScheduler
from modules.communication.liquidsoap.communicator import LiquidSoapCommunicator
from modules.communication.redis.adapter import ServerRedisAdapter
if self.config.get("recreate_db") is not None:
AuraScheduler(self.config) # handles recreate and exits program

Gottfried Gaisbauer
committed
# create scheduler and ls_communicator
self.liquidsoapcommunicator = LiquidSoapCommunicator(self.config)
self.scheduler = AuraScheduler(self.config)

Gottfried Gaisbauer
committed
# give both a reference of each other
self.liquidsoapcommunicator.scheduler = self.scheduler
self.scheduler.liquidsoapcommunicator = self.liquidsoapcommunicator

Gottfried Gaisbauer
committed
# create the redis adapter
self.messenger = ServerRedisAdapter(self.config)

Gottfried Gaisbauer
committed
self.messenger.liquidsoapcommunicator = self.liquidsoapcommunicator
#self.diskspace_watcher = DiskSpaceWatcher(self.config, self.logger, self.liquidsoapcommunicator)
#self.diskspace_watcher.start()
# and finally wait for redis message / start listener thread
self.messenger.start()

Gottfried Gaisbauer
committed

Gottfried Gaisbauer
committed
# start the web service

Gottfried Gaisbauer
committed
self.start_web_service()

Gottfried Gaisbauer
committed

Gottfried Gaisbauer
committed
def start_web_service(self):

Gottfried Gaisbauer
committed
try:

Gottfried Gaisbauer
committed
self.logger.info("Listening on Port 5000 for API or Webcalls")

Gottfried Gaisbauer
committed
# Routes(self.scheduler, self.liquidsoapcommunicator, self.messenger, self.config)

Gottfried Gaisbauer
committed
except OSError as e:
self.messenger.halt()
self.logger.critical("AuraEngine already running? Exception: " + e.strerror + ". Exiting...")

Gottfried Gaisbauer
committed
# # ## ## ## ## ## # #
# # ENTRY FUNCTION # #
# # ## ## ## ## ## # #
def main():
aura = Aura()
# FIXME MAKE THE STARTTIME OF A SCHEDULE TO ITS PK

Gottfried Gaisbauer
committed
aura.logger.critical("MAKE THE STARTTIME OF A SCHEDULE TO ITS PK")
if len(sys.argv) >= 2:
if "--use-test-data" in sys.argv:
aura.config.set("use_test_data", True)
if "--recreate-database" in sys.argv:
# # ## ## ## ## ## ## # #
# # End ENTRY FUNCTION # #
# # ## ## ## ## ## ## # #
if __name__ == "__main__":