diff --git a/modules/plugins/trackservice.py b/modules/plugins/trackservice.py index 0e4e832973f2a3b797351fb684c3cdeaf8a8ff83..e2e4c8d5b108dc5cc8d11c93a7a1319d2031bebc 100644 --- a/modules/plugins/trackservice.py +++ b/modules/plugins/trackservice.py @@ -27,12 +27,12 @@ from modules.base.utils import SimpleUtil as SU class TrackserviceHandler(): """ - Sends the trackservice entry to the `engine-api` REST endpoint. + Sends the trackservice entry and studio clock information to the `engine-api` REST endpoint. """ logger = None config = None soundsystem = None - last_schedule = None + last_playlist = None def __init__(self, config, soundsystem): @@ -49,7 +49,7 @@ class TrackserviceHandler(): Some track started playing. """ self.store_trackservice(entry) - self.store_show_info(entry) + self.store_clock_info(entry) def store_trackservice(self, entry): @@ -57,33 +57,88 @@ class TrackserviceHandler(): Posts the given `PlaylistEntry` to the Engine API Playlog. """ data = dict() - data["track_start"] = entry.entry_start + diff = (entry.entry_start_actual - entry.entry_start).total_seconds() + self.logger.info("There's a difference of %s seconds between planned and actual start of the entry" % diff) + data["track_start"] = entry.entry_start_actual data["track_artist"] = entry.meta_data.artist data["track_album"] = entry.meta_data.album data["track_title"] = entry.meta_data.title data["track_duration"] = entry.duration data["track_type"] = entry.get_type().numeric - data["timeslot_id"] = entry.playlist.schedule.schedule_id + data["schedule_id"] = entry.playlist.schedule.schedule_id data["show_name"] = entry.playlist.schedule.show_name data["log_source"] = self.config.get("api_engine_number") data = SU.clean_dictionary(data) self.logger.info("Posting schedule update to Engine API...") - url = self.config.get("api_engine_playlog_url") + url = self.config.get("api_engine_store_playlog") headers = {'content-type': 'application/json'} body = json.dumps(data, indent=4, sort_keys=True, default=str) response = requests.post(url, data=body, headers=headers) self.logger.info("Engine API response: %s" % response.status_code) - - def store_show_info(self, entry): + def store_clock_info(self, entry): """ Posts the current and next show information to the Engine API. """ - current_schedule = entry.playlist.schedule - if current_schedule == self.last_schedule: - self.logger.info("Schedule didn't change since last update.") + current_playlist = entry.playlist + if current_playlist == self.last_playlist: + self.logger.info("Playlist didn't change since last update.") else: - self.logger.info("Posting schedule update to Engine API...") - # TODO Implement \ No newline at end of file + self.last_playlist = current_playlist + current_schedule = current_playlist.schedule + next_schedule = self.soundsystem.scheduler.get_next_schedules(1) + if next_schedule: next_schedule = next_schedule[0] + + data = dict() + data["engine_source"] = self.config.get("api_engine_number") + + if current_playlist: + data["current_playlist"] = dict() + data["current_playlist"]["playlist_id"] = current_playlist.playlist_id + data["current_playlist"]["entries"] = [] + for e in current_playlist.entries: + entry = dict() + entry["track_start"] = e.entry_start + entry["track_artist"] = e.meta_data.artist + entry["track_album"] = e.meta_data.album + entry["track_title"] = e.meta_data.title + entry["track_duration"] = e.duration + entry["track_type"] = e.get_type().numeric + entry = SU.clean_dictionary(entry) + data["current_playlist"]["entries"].append(entry) + + if current_schedule: + cs = dict() + cs["schedule_id"] = current_schedule.schedule_id + cs["schedule_start"] = current_schedule.schedule_start + cs["schedule_end"] = current_schedule.schedule_end + cs["show_id"] = current_schedule.show_id + cs["show_name"] = current_schedule.show_name + cs["playlist_id"] = current_schedule.playlist_id + cs["fallback_type"] = current_schedule.fallback_state.id + cs = SU.clean_dictionary(cs) + data["current_schedule"] = cs + + if next_schedule: + ns = dict() + ns["schedule_id"] = next_schedule.schedule_id + ns["schedule_start"] = next_schedule.schedule_start + ns["schedule_end"] = next_schedule.schedule_end + ns["show_id"] = next_schedule.show_id + ns["show_name"] = next_schedule.show_name + ns["playlist_id"] = next_schedule.playlist_id + ns["fallback_type"] = next_schedule.fallback_state.id + ns = SU.clean_dictionary(ns) + data["next_schedule"] = ns + + + data = SU.clean_dictionary(data) + + self.logger.info("Posting clock info update to Engine API...") + url = self.config.get("api_engine_store_clock") + headers = {'content-type': 'application/json'} + body = json.dumps(data, indent=4, sort_keys=True, default=str) + response = requests.put(url, data=body, headers=headers) + self.logger.info("Engine API response: %s" % response.status_code) \ No newline at end of file