Commit 19903ca4 authored by Gottfried Gaisbauer's avatar Gottfried Gaisbauer
Browse files

trackservice

parent a93f6f51
This diff is collapsed.
......@@ -5,12 +5,12 @@ import datetime, os, urllib, sys
import time
import decimal
from sqlalchemy import Boolean, Column, Date, DateTime, Float, Integer, String, Text, Time, ForeignKey
from sqlalchemy import Boolean, Column, Date, DateTime, Float, Integer, String, Text, Time, ForeignKey, ForeignKeyConstraint
from sqlalchemy.orm import relationship
from libraries.database.database import db
class Model:
class AuraDatabaseModel:
def store(self, commit=False):
obj_session = db.session.object_session(self)
if obj_session is not None:
......@@ -42,12 +42,16 @@ class Model:
manualschedule = Schedule()
manualschedule.schedule_id = 0
fallback_trackservice_schedule = TrackServiceSchedule()
fallback_trackservice_schedule.ts_schedule_id = 0
print("Recreating Database...")
db.drop_all()
print("all dropped. creating...")
db.create_all()
print("inserting manual scheduling possibility")
print("inserting manual scheduling possibility and fallback trackservice schedule")
db.session.add(manualschedule)
db.session.add(fallback_trackservice_schedule)
print("all created. commiting...")
db.session.commit()
print("Database recreated!")
......@@ -57,13 +61,15 @@ class Model:
# ------------------------------------------------------------------------------------------ #
class Schedule(db.Model, Model):
class Schedule(db.Model, AuraDatabaseModel):
"""
One specific Schedule for a show on a timeslot
"""
__tablename__ = 'schedule'
# primary and foreign keys
schedule_id = Column(Integer, primary_key=True, autoincrement=False)
show_id = Column(Integer)
show_id = Column(Integer) # well, not needed..
schedule_start = Column(DateTime) # can be null due to manual entries
schedule_end = Column(DateTime) # can be null due to manual entries
......@@ -108,15 +114,17 @@ class Schedule(db.Model, Model):
# ------------------------------------------------------------------------------------------ #
class ScheduleEntry(db.Model, Model):
class ScheduleEntry(db.Model, AuraDatabaseModel):
"""
One schedule can have multiple entries
"""
__tablename__ = 'schedule_entry'
# primary and foreign keys
playlist_id = Column(Integer, primary_key=True, nullable=False, autoincrement=False)
entry_num = Column(Integer, primary_key=True, nullable=False, autoincrement=False)
schedule_id = Column(Integer, ForeignKey("schedule.schedule_id"))
entry_start = Column(DateTime, nullable=False)
entry_end = Column(DateTime)
source = Column(String(256))
......@@ -132,6 +140,7 @@ class ScheduleEntry(db.Model, Model):
entry_end_unix = 0
programme_index = -1
ForeignKeyConstraint(['schedule_id'], ['schedule.schedule_id'])
schedule = relationship("Schedule", foreign_keys=[schedule_id])
# ------------------------------------------------------------------------------------------ #
......@@ -208,67 +217,45 @@ class ScheduleEntry(db.Model, Model):
return "ScheduleEntry starts @ " + ScheduleEntry.entry_start + " and ends @ " + ScheduleEntry.entry_end + " and plays " + ScheduleEntry.source
# def __init__(self, playlist_id, entry_num, schedule_id, entry_start, entry_end, source, artist, track, albumname, genre, tracknum, cdnum, year):
# self.playlist_id = playlist_id
# self.entry_num = entry_num
# self.schedule_id = schedule_id
# self.entry_start = entry_start
# self.entry_end = entry_end
# self.source = source
# self.artist = artist
# self.track = track
# self.albumname = albumname
# self.genre = genre
# self.tracknum = tracknum
# self.cdnum = cdnum
# self.year = year
@staticmethod
def create_from_json(playlist_id, entry_num, schedule_id, entry_start, entry_end, source, artist, track, albumname, genre, tracknum, cdnum, year):
e = ScheduleEntry()
e.playlist_id = playlist_id
e.entry_num = entry_num
e.schedule_id = schedule_id
e.entry_start = entry_start
e.entry_end = entry_end
e.source = source
e.artist = artist
e.track = track
e.albumname = albumname
e.genre = genre
e.tracknum = tracknum
e.cdnum = cdnum
e.year = year
return e
# @property
# def serialize(self):
# """Return object data in easily serializeable format"""
# return {
# 'schedule_id': self.schedule_id,
# 'entry_start': self.dump_datetime(self.entry_start),
# 'entry_end': self.dump_datetime(self.entry_end),
# 'source': self.source
# }
# ------------------------------------------------------------------------------------------ #
class TrackServiceSchedule(db.Model):
class TrackServiceSchedule(db.Model, AuraDatabaseModel):
"""
Trackservice is tracking every schedule.
"""
__tablename__ = 'trackservice_schedule'
# primary and foreign keys
ts_schedule_id = Column(Integer, primary_key=True, autoincrement=True)
schedule_id = Column(Integer, ForeignKey('schedule.schedule_id'))
schedule_id = Column(Integer, ForeignKey("schedule.schedule_id"))
# ------------------------------------------------------------------------------------------ #
@staticmethod
def select_one(schedule_id):
# damn BAND-AID
db.session.commit()
return db.session.query(ScheduleEntry).filter(TrackServiceSchedule.schedule_id == schedule_id).first()
# ------------------------------------------------------------------------------------------ #
class TrackServiceScheduleEntry(db.Model):
class TrackServiceScheduleEntry(db.Model, AuraDatabaseModel):
"""
And a schedule can have multiple entries
"""
__tablename__ = 'trackservice_entry'
# primary and foreign keys. the foreign keys here can be null, because of fallback stuff
ts_entry_id = Column(Integer, primary_key=True, autoincrement=True)
ts_schedule_id = Column(Integer, ForeignKey('trackservice_schedule.ts_schedule_id'))
ts_schedule_id = Column(Integer, ForeignKey("trackservice_schedule.ts_schedule_id"), nullable=True)
playlist_id = Column(Integer, nullable=True)
entry_num = Column(Integer, nullable=True)
fallback = Column(Boolean, default=False)
fallback_start = Column(DateTime, nullable=True, default=None)
source = Column(String(256), nullable=True, default=None)
# foreign key definitions
ForeignKeyConstraint(['playlist_id', 'entry_num'], ['schedule_entry.playlist_id', 'schedule_entry.entry_num'])
#Model.recreate_db(True)
#AuraDatabaseModel.recreate_db(True)
......@@ -12,6 +12,7 @@ from modules.communication.liquidsoap.recorderclient import LiquidSoapRecorderCl
from modules.communication.liquidsoap.initthread import LiquidSoapInitThread
from libraries.enum.consolecolor import TerminalColors
from libraries.exceptions.auraexceptions import LQConnectionError
from libraries.database.broadcasts import TrackServiceSchedule, TrackServiceScheduleEntry
class LiquidSoapCommunicator:
......@@ -109,7 +110,7 @@ class LiquidSoapCommunicator:
# ------------------------------------------------------------------------------------------ #
def activate(self, entry, input_type):
if self.scheduler.get_active_source() == input_type:
self.scheduler.insert_track_service_entry(entry)
self.insert_track_service_entry(entry)
print(TerminalColors.OK_PINK.value + "LiquidSoapCommunicator " + input_type + " already active! Time: " + str(datetime.datetime.now()) + TerminalColors.ENDC.value)
else:
print(TerminalColors.OK_PINK.value + "LiquidSoapCommunicator is activating " + input_type + "! Time: " + str(datetime.datetime.now()) + TerminalColors.ENDC.value)
......@@ -120,7 +121,24 @@ class LiquidSoapCommunicator:
self.channel_volume(o, 0)
self.channel_volume(input_type, entry.volume)
self.disable_transaction(True)
self.scheduler.insert_track_service_entry(entry)
self.insert_track_service_entry(entry)
# ------------------------------------------------------------------------------------------ #
def insert_track_service_entry(self, schedule_entry):
ts_schedule = TrackServiceSchedule.select_one(schedule_entry.schedule.schedule_id)
if not ts_schedule:
if self.debug:
print("no TrackServiceSchedule with id " + str(schedule_entry.schedule.schedule_id) + " in database => creating a new one")
ts_schedule = TrackServiceSchedule()
ts_schedule.schedule_id = schedule_entry.schedule.schedule_id
ts_schedule.store(True)
ts_schedule_entry = TrackServiceScheduleEntry()
ts_schedule_entry.ts_schedule_id = schedule_entry.schedule.schedule_id
ts_schedule_entry.playlist_id = schedule_entry.playlist_id
ts_schedule_entry.entry_num = schedule_entry.entry_num
ts_schedule_entry.store(True)
# ------------------------------------------------------------------------------------------ #
def all_inputs_but(self, input_type):
......
......@@ -340,8 +340,6 @@ class AuraController:
if not self.is_intern:
self.message(simplejson.dumps(self.job_result))
# ------------------------------------------------------------------------------------------ #
def __check_result__(self, result):
"""
......
......@@ -52,7 +52,7 @@ import threading
from libraries.base.schedulerconfig import AuraSchedulerConfig
from modules.communication.redis.messenger import RedisMessenger
from libraries.base.calendar import AuraCalendarService
from libraries.database.broadcasts import Schedule, ScheduleEntry, Model, TrackServiceSchedule, TrackServiceScheduleEntry
from libraries.database.broadcasts import Schedule, ScheduleEntry, AuraDatabaseModel
from libraries.exceptions.auraexceptions import NoProgrammeLoadedException
......@@ -142,7 +142,7 @@ class AuraScheduler():
ScheduleEntry.select_all()
except sqlalchemy.exc.ProgrammingError as e:
if e.__dict__["code"] == "f405":
Model.recreate_db()
AuraDatabaseModel.recreate_db()
else:
raise
......@@ -238,7 +238,7 @@ class AuraScheduler():
# when do we have to start?
diff = entry.entry_start_unix - now_unix
# diff = diff/100 # testing purpose
diff = diff/100 # testing purpose
# create the activation threads and run them after <diff> seconds
if entry.source.startswith("linein"):
......@@ -334,6 +334,8 @@ class AuraScheduler():
return message_queue
# ------------------------------------------------------------------------------------------ #
# ------------------------------------------------------------------------------------------ #
def swap_playlist_entries(self, indexes):
from_entry = None
......
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