Exemple #1
0
def play_media_as_bed(studio_id, media_id):
    studio = Studio.query.get(studio_id)
    media = Media.query.get(media_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    if not isinstance(media, Media):
        return error_message("bad_argument")

    liq.set_var("{}.bed.switch".format(studio.slug), True)

    logger.debug("Setting media {} as bed on studio {}".format(media.id, studio.id))
    load_media("bed_{}".format(studio.id), media, studio.bed_liqname)

    timer = max(media.length - 2, 1)
    logger.warning("Scheduling bed looping in {} seconds...".format(timer))
    loop_bed.s(studio.id, media.id).apply_async(countdown=timer)

    studio.bed_on_air_since = datetime.datetime.now()
    studio.bed = media
    studio.mark_as_changed()
    db.session.commit()

    return ok(and_squack=True)
Exemple #2
0
def select_studio(element_id=None, studio_id=None):
    if element_id:
        current_element = PlaylistElement.query.get(element_id)

        if not current_element:
            return error_message("bad_argument")

        studio = current_element.playlist.studio
    elif studio_id:
        studio = Studio.query.get(studio_id)

        if not studio:
            return error_message("bad_argument")
    else:
        return error_message("bad_argument")

    liq.set_var("selected", studio.slug)

    # TODO: ensure all other studios are not selected anymore
    studio.selected = True
    studio.mark_as_changed()

    if element_id:
        # noinspection PyUnboundLocalVariable
        current_element.mark_as_done()
        studio.jukebox.curpos += 1

    studio.mark_as_changed()
    db.session.commit()

    play_current_element.delay(studio.id)
Exemple #3
0
def play_media_as_bed(studio_id, media_id):
    studio = Studio.query.get(studio_id)
    media = Media.query.get(media_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    if not isinstance(media, Media):
        return error_message("bad_argument")

    liq.set_var("{}.bed.switch".format(studio.slug), True)

    logger.debug("Setting media {} as bed on studio {}".format(
        media.id, studio.id))
    load_media("bed_{}".format(studio.id), media, studio.bed_liqname)

    timer = max(media.length - 2, 1)
    logger.warning("Scheduling bed looping in {} seconds...".format(timer))
    loop_bed.s(studio.id, media.id).apply_async(countdown=timer)

    studio.bed_on_air_since = datetime.datetime.now()
    studio.bed = media
    studio.mark_as_changed()
    db.session.commit()

    return ok(and_squack=True)
Exemple #4
0
def select_studio(element_id=None, studio_id=None):
    if element_id:
        current_element = PlaylistElement.query.get(element_id)

        if not current_element:
            return error_message("bad_argument")

        studio = current_element.playlist.studio
    elif studio_id:
        studio = Studio.query.get(studio_id)

        if not studio:
            return error_message("bad_argument")
    else:
        return error_message("bad_argument")

    liq.set_var("selected", studio.slug)

    # TODO: ensure all other studios are not selected anymore
    studio.selected = True
    studio.mark_as_changed()

    if element_id:
        # noinspection PyUnboundLocalVariable
        current_element.mark_as_done()
        studio.jukebox.curpos += 1

    studio.mark_as_changed()
    db.session.commit()

    play_current_element.delay(studio.id)
Exemple #5
0
def play_current_element(studio_id):
    studio = Studio.query.get(studio_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    current_element = studio.jukebox.current_element

    if current_element is None:
        logger.warning("Nothing to play or to do. Sleeping...")
        return ok()

    with redis_instance.lock("element_{}_play".format(current_element.id)):
        logger.debug("Refreshing current element {} (status={})".format(
            current_element.id, current_element.status))

        # Current element may have changed since we waited for the lock
        db.session.expire_all()
        current_element = db.session.query(PlaylistElement).get(
            studio.jukebox.current_element.id)

        logger.debug("Playing current element {} (status={})".format(
            current_element.id, current_element.status))

        if current_element.status == 'playing':
            logger.warning("Already playing (current_element={})...".format(
                current_element.id))
            return error_message("already_playing")

        elif current_element.status in ('ready', 'loaded'):
            if current_element.media:
                if liq.get_var("{}.jukebox.switch".format(
                        studio.slug)) != "true":
                    logger.warn("Jukebox was not active, activating...")
                    liq.set_var("{}.jukebox.switch".format(studio.slug), True)

                if current_element.status == 'ready':
                    load_media(current_element.id, current_element.media,
                               studio.jukebox_liqname)

            current_element.status = 'playing'
            current_element.on_air_since = datetime.datetime.now()
            studio.jukebox.mark_as_changed()
            db.session.commit()

            if current_element.media:
                timer = max(current_element.media.length - 2, 1)
                logger.warning(
                    "Scheduling task for {} seconds...".format(timer))
                check_next_element.s(
                    studio.id, current_element.id).apply_async(countdown=timer)
            elif current_element.action:
                task_name_to_schedule = current_element.action.task
                celery.send_task(task_name_to_schedule,
                                 kwargs={"element_id": current_element.id})

    return ok()
Exemple #6
0
def start_live(element_id=-1):
    playlist_element = PlaylistElement.query.get(element_id)

    if not playlist_element:
        return error_message("bad_argument")

    studio = playlist_element.playlist.studio

    liq.set_var("{}.jukebox.switch".format(studio.slug), False)
    liq.set_var("{}.plateau.volume".format(studio.slug), 1.0)

    return ok()
Exemple #7
0
def start_live(element_id=-1):
    playlist_element = PlaylistElement.query.get(element_id)

    if not playlist_element:
        return error_message("bad_argument")

    studio = playlist_element.playlist.studio

    liq.set_var("{}.jukebox.switch".format(studio.slug), False)
    liq.set_var("{}.plateau.volume".format(studio.slug), 1.0)

    return ok()
Exemple #8
0
def play_current_element(studio_id):
    studio = Studio.query.get(studio_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    current_element = studio.jukebox.current_element

    if current_element is None:
        logger.warning("Nothing to play or to do. Sleeping...")
        return ok()

    with redis_instance.lock("element_{}_play".format(current_element.id)):
        logger.debug("Refreshing current element {} (status={})".format(current_element.id, current_element.status))

        # Current element may have changed since we waited for the lock
        db.session.expire_all()
        current_element = db.session.query(PlaylistElement).get(studio.jukebox.current_element.id)

        logger.debug("Playing current element {} (status={})".format(current_element.id, current_element.status))

        if current_element.status == 'playing':
            logger.warning("Already playing (current_element={})...".format(current_element.id))
            return error_message("already_playing")

        elif current_element.status in ('ready', 'loaded'):
            if current_element.media:
                if liq.get_var("{}.jukebox.switch".format(studio.slug)) != "true":
                    logger.warn("Jukebox was not active, activating...")
                    liq.set_var("{}.jukebox.switch".format(studio.slug), True)

                if current_element.status == 'ready':
                    load_media(current_element.id, current_element.media, studio.jukebox_liqname)

            current_element.status = 'playing'
            current_element.on_air_since = datetime.datetime.now()
            studio.jukebox.mark_as_changed()
            db.session.commit()

            if current_element.media:
                timer = max(current_element.media.length - 2, 1)
                logger.warning("Scheduling task for {} seconds...".format(timer))
                check_next_element.s(studio.id, current_element.id).apply_async(countdown=timer)
            elif current_element.action:
                task_name_to_schedule = current_element.action.task
                celery.send_task(task_name_to_schedule, kwargs={"element_id": current_element.id})

    return ok()
Exemple #9
0
def end_studio(element_id=-1):
    current_element = PlaylistElement.query.get(element_id)

    if not current_element:
        return error_message("bad_argument")

    studio = current_element.playlist.studio

    liq.set_var("selected", "permanent")

    studio.selected = False
    studio.mark_as_changed()

    current_element.mark_as_done()
    studio.jukebox.curpos += 1
    studio.mark_as_changed()
    db.session.commit()

    # DO NOT PLAY NEXT ELEMENT (stop here)
    liq.set_var("{}.jukebox.switch".format(studio.slug), False)

    return ok()
Exemple #10
0
def end_studio(element_id=-1):
    current_element = PlaylistElement.query.get(element_id)

    if not current_element:
        return error_message("bad_argument")

    studio = current_element.playlist.studio

    liq.set_var("selected", "permanent")

    studio.selected = False
    studio.mark_as_changed()

    current_element.mark_as_done()
    studio.jukebox.curpos += 1
    studio.mark_as_changed()
    db.session.commit()

    # DO NOT PLAY NEXT ELEMENT (stop here)
    liq.set_var("{}.jukebox.switch".format(studio.slug), False)

    return ok()
Exemple #11
0
def end_live(studio_id):
    studio = Studio.query.get(studio_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    flush_bed(studio)

    liq.set_var("{}.jukebox.switch".format(studio.slug), True)
    liq.set_var("{}.jukebox.volume".format(studio.slug), 1.0)
    liq.set_var("{}.plateau.volume".format(studio.slug), 0.0)

    current_element = studio.jukebox.current_element
    current_element.mark_as_done()

    studio.jukebox.curpos += 1
    studio.mark_as_changed()

    db.session.commit()

    play_current_element.delay(studio.id)

    return ok(and_squack=True)
Exemple #12
0
def end_live(studio_id):
    studio = Studio.query.get(studio_id)

    if not isinstance(studio, Studio):
        return error_message("bad_argument")

    flush_bed(studio)

    liq.set_var("{}.jukebox.switch".format(studio.slug), True)
    liq.set_var("{}.jukebox.volume".format(studio.slug), 1.0)
    liq.set_var("{}.plateau.volume".format(studio.slug), 0.0)

    current_element = studio.jukebox.current_element
    current_element.mark_as_done()

    studio.jukebox.curpos += 1
    studio.mark_as_changed()

    db.session.commit()

    play_current_element.delay(studio.id)

    return ok(and_squack=True)