diff --git a/libraries/database/broadcasts.py b/libraries/database/broadcasts.py index e9c7b0e8cf4e28eede477d9c97b0f8d97a8fc29c..482c4680b90b632912110de42c583ca9af1b0896 100644 --- a/libraries/database/broadcasts.py +++ b/libraries/database/broadcasts.py @@ -8,7 +8,7 @@ import datetime from sqlalchemy import orm, func, Boolean, Column, DateTime, Integer, String, ForeignKey, ForeignKeyConstraint from sqlalchemy.orm import relationship -from sqlalchemy.sql.expression import false +from sqlalchemy.sql.expression import false, true from libraries.database.database import DB from libraries.enum.scheduleentrytype import ScheduleEntryType @@ -26,10 +26,11 @@ class AuraDatabaseModel: DB.session.commit() def delete(self, commit=False): - current_db_sessions = DB.session.object_session(self) - current_db_sessions.delete(self) - return DB.session.delete(self) +# current_db_sessions = DB.session.object_session(self) +# current_db_sessions.delete(self) +# return +# DB.session.delete(self) if commit: DB.session.commit() @@ -120,7 +121,7 @@ class ScheduleEntry(DB.Model, AuraDatabaseModel): entry_start = Column(DateTime) source = Column(String(256)) volume = Column(Integer, default=100) - is_fallback = Column(Boolean, default=False) + fallback_type = Column(Integer, default=0) cleansource = "" entry_start_unix = 0 programme_index = -1 @@ -163,7 +164,7 @@ class ScheduleEntry(DB.Model, AuraDatabaseModel): @staticmethod def select_all(): # fetching all entries - all_entries = DB.session.query(ScheduleEntry).filter(ScheduleEntry.is_fallback == false()).order_by(ScheduleEntry.entry_start).all() + all_entries = DB.session.query(ScheduleEntry).filter(ScheduleEntry.fallback_type == 0).order_by(ScheduleEntry.entry_start).all() cnt = 0 for entry in all_entries: @@ -172,6 +173,16 @@ class ScheduleEntry(DB.Model, AuraDatabaseModel): return all_entries + # ------------------------------------------------------------------------------------------ # + @staticmethod + def truncate(): + all_entries = DB.session.query(ScheduleEntry).filter().order_by(ScheduleEntry.entry_start).all() + + for a in all_entries: + a.delete() + DB.session.commit() + + # ------------------------------------------------------------------------------------------ # @staticmethod def select_next_manual_entry_num(): diff --git a/modules/communication/redis/adapter.py b/modules/communication/redis/adapter.py index dbe97d7ee8966a217b42f0e644e726f8d040525f..2927e5bdf3cec702f5a657d2557d5e5345ef078b 100644 --- a/modules/communication/redis/adapter.py +++ b/modules/communication/redis/adapter.py @@ -120,7 +120,8 @@ 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) + self.execute(RedisChannel.FNP_REPLY.value, self.scheduler.get_act_programme_as_string) elif item["data"] == "shutdown": self.shutdown_event.set() diff --git a/modules/scheduling/calendar.py b/modules/scheduling/calendar.py index 13346c9d5b2b989ea14158476144a14c52c6eb57..eab08851504e2a00c2c8c0fd13d02e77b8f809db 100644 --- a/modules/scheduling/calendar.py +++ b/modules/scheduling/calendar.py @@ -120,9 +120,9 @@ class AuraCalendarService(threading.Thread): # store playlists to play self.store_schedule_playlist(schedule_db, schedule, "playlist") - self.store_schedule_playlist(schedule_db, schedule, "schedule_fallback", True) - self.store_schedule_playlist(schedule_db, schedule, "show_fallback", True) - self.store_schedule_playlist(schedule_db, schedule, "station_fallback", True) + self.store_schedule_playlist(schedule_db, schedule, "schedule_fallback", 1) + self.store_schedule_playlist(schedule_db, schedule, "show_fallback", 2) + self.store_schedule_playlist(schedule_db, schedule, "station_fallback", 3) # release the mutex self.queue.put(schedule) #"fetching_finished") @@ -169,7 +169,7 @@ class AuraCalendarService(threading.Thread): return schedule_db # ------------------------------------------------------------------------------------------ # - def store_schedule_playlist(self, schedule_db, schedule, playlistname, isfallbackplaylist=False): + def store_schedule_playlist(self, schedule_db, schedule, playlistname, fallbackplaylist_type=0): playlist = schedule[playlistname] info = "Schedule playlist (" + playlistname + ") for " + schedule_db.show_name + " stored" @@ -180,7 +180,7 @@ class AuraCalendarService(threading.Thread): lastentry = None for entry in playlist["entries"]: - lastentry = self.store_playlist_entry(schedule_db, playlist, entry, lastentry, entrynum, isfallbackplaylist) + lastentry = self.store_playlist_entry(schedule_db, playlist, entry, lastentry, entrynum, fallbackplaylist_type) entrynum = entrynum + 1 if lastentry is None: @@ -191,7 +191,7 @@ class AuraCalendarService(threading.Thread): self.logger.warning(warning) # ------------------------------------------------------------------------------------------ # - def store_playlist_entry(self, schedule_db, playlist, entry, lastentry, entrynum, isfallbackplaylist=False): + def store_playlist_entry(self, schedule_db, playlist, entry, lastentry, entrynum, fallbackplaylist_type=0): schedule_entry_db = ScheduleEntry.select_one(playlist["playlist_id"], entrynum) havetoadd = False @@ -204,7 +204,7 @@ class AuraCalendarService(threading.Thread): schedule_entry_db.entry_num = entrynum schedule_entry_db.schedule_id = schedule_db.schedule_id schedule_entry_db.source = entry["source"] - schedule_entry_db.is_fallback = isfallbackplaylist + schedule_entry_db.fallback_type = fallbackplaylist_type schedule_entry_db.entry_start = schedule_db.schedule_start + timedelta(seconds=self.get_length(lastentry)) schedule_entry_db.calc_unix_times() @@ -286,7 +286,20 @@ class AuraCalendarService(threading.Thread): # HARDCODED Testdata if schedule[id_name] == 0 or schedule[id_name] is None: # this happens when playlist id is not filled out in pv - json_response = '{"playlist_id": 0}' + # json_response = '{"playlist_id": 0}' + + import random + rand_id = random.randint(1,100) + + if rand_id % 4 == 0: # playlist with two files + json_response = '{"playlist_id":' + str(rand_id) + ',"entries":[{"source":"file:///var/audio/fallback/music.flac"},{"source":"file:///var/audio/fallback/NightmaresOnWax/DJ-Kicks/02 - Only Child - Breakneck.flac"}]}' + elif rand_id % 3 == 0: # playlist with jingle and then http stream + json_response = '{"playlist_id":' + str(rand_id) + ',"entries":[{"source":"file:///var/audio/fallback/music.flac"},{"source":"linein://1"}]}' + elif rand_id % 2 == 0: # playlist with jingle and then linein + json_response = '{"playlist_id":' + str(rand_id) + ',"entries":[{"source":"file:///var/audio/fallback/music.flac"},{"source":"http://chill.out.airtime.pro:8000/chill_a"}]}' + else: # pool playlist + json_response = '{"playlist_id":' + str(rand_id) + ',"entries":[{"source":"pool:///hiphop"}]}' + elif schedule[id_name] % 4 == 0: # playlist with two files json_response = '{"playlist_id":' + str(schedule[id_name]) + ',"entries":[{"source":"file:///var/audio/fallback/music.flac"},{"source":"file:///var/audio/fallback/NightmaresOnWax/DJ-Kicks/01 - Type - Slow Process.flac"}]}' elif schedule[id_name] % 3 == 0: # playlist with jingle and then http stream @@ -296,7 +309,10 @@ class AuraCalendarService(threading.Thread): else: # pool playlist json_response = '{"playlist_id":' + str(schedule[id_name]) + ',"entries":[{"source":"pool:///chillout"}]}' - self.logger.info("Using hardcoded playlists: "+json_response) + if schedule[id_name] == 0 or schedule[id_name] is None: + self.logger.info("Using 'randomized' playlist: " + json_response + " for " + id_name[:-3] + " for show " + schedule["show_name"] + " starting @ " + schedule["start"]) + else: + self.logger.info("Using hardcoded playlist: " + json_response + " for " + id_name[:-3] + " for show " + schedule["show_name"] + " starting @ " + schedule["start"]) try: schedule_entries = simplejson.loads(json_response) @@ -363,12 +379,14 @@ class AuraCalendarService(threading.Thread): act_list = [] now = datetime.now() - for i,curr in enumerate(schedule_from_pv[:-1]): + for index,curr in enumerate(schedule_from_pv[:-1]): date_start = datetime.strptime(curr["start"], "%Y-%m-%dT%H:%M:%S") - date_next_start = datetime.strptime(schedule_from_pv[i+1]["start"], "%Y-%m-%dT%H:%M:%S") + date_next_start = datetime.strptime(schedule_from_pv[index+1]["start"], "%Y-%m-%dT%H:%M:%S") + # append all elements in the future if date_start >= now: act_list.append(curr) + # append the one which is now playing if date_start <= now and date_next_start >= now: act_list.append(curr) diff --git a/modules/scheduling/scheduler.py b/modules/scheduling/scheduler.py index 7afbff2539f75dce149d0dea7c8aded6b3728222..da4b5e80e3087ae22a2587b8480529f6355b42aa 100644 --- a/modules/scheduling/scheduler.py +++ b/modules/scheduling/scheduler.py @@ -143,6 +143,10 @@ class AuraScheduler(ExceptionLogger, threading.Thread): # write to logger self.logger.info("Fetch new programmes every " + str(seconds_to_wait) + "s started. Going to start next time " + str(next_time)) + # empty database + self.logger.info("emptying database") + ScheduleEntry.truncate() + # fetch new programme self.fetch_new_programme() @@ -384,7 +388,7 @@ class AuraScheduler(ExceptionLogger, threading.Thread): if len(self.programme) == 0 and self.tried_fetching == self.fetch_max: self.logger.critical("Programme loaded from database has no entries!") - return self.get_act_programme_as_string() + # return self.get_act_programme_as_string() elif response.startswith("fetching_aborted"): self.logger.warning("Fetching was being aborted from AuraCalendarService! Are you connected? Reason: " + response) else: