Пример #1
0
    def onMessage(self, payload, isBinary):
        if isBinary:
            logger.warning(
                "Binary message received ({} bytes): ignoring".format(
                    len(payload)))
            return

        try:
            unicode_message = payload.decode('utf8')
        except UnicodeDecodeError:
            logger.exception("Cannot decode {}: ignoring".format(payload))
            return

        try:
            message = json.loads(unicode_message)
        except json.JSONDecodeError:
            logger.exception(
                "Cannot load {}: ignoring".format(unicode_message))
            return

        ok, warning = self.validate_message(message)
        if not ok:
            logger.info("Received {}".format(message))
            logger.warning(warning)
            logger.info("Ignoring")
        else:
            logger.debug("Received {}".format(message))
            self.log_message(message, to_server=False)
            self.factory.process_event(message[P.EVENT])
Пример #2
0
 def process_rule(self, rule, context):
     try:
         self.if_conditions_then_actions(
             rule['content']['conditions'], rule['content']['actions'], context)
     except Exception:
         message = "Error while processing rule {}".format(rule['name'])
         self.factory.send_notification('error', message)
         logger.exception()
Пример #3
0
    def __init__(self, *args, **kwargs):
        super(SoundPlayer, self).__init__(*args, **kwargs)

        initial_master_volume = self.node_params.get('master_volume', None)
        master_volume_mixer = self.node_params.get('mixer', '')
        default_initial_volume = self.node_params.get('default_volume', 100)

        player = self.node_params.get('player', 'pyglet')
        if player == 'vlc':
            self.sound_player = self.vlc_play_sound
        elif player == 'pyglet':
            self.sound_player = self.pyglet_play_sound
            self.pyglet_players = []
            loop = LoopingCall(self.pyglet_loop)
            loop.start(1 / 30)
        else:
            raise ValueError(
                "Bad player value ({}). Possible values are vlc or "
                "pyglet.".format(player))

        try:
            self.master_volume = MasterVolume(
                initial_volume=initial_master_volume,
                mixer=master_volume_mixer,
            )
        except Exception:
            logger.exception()
            logger.warning(
                "Unable to initialize the master volume controller. Further "
                "actions on the master volume will be ignored.")
            self.master_volume = None

        self.sounds = {}
        for sound in self.node_params.get('sounds', []):
            id_ = sound['id']
            path = sound['path']
            volume = sound.get('volume', default_initial_volume)

            if player == 'vlc':
                media = vlc.Media(path)
                media.parse()
                # / 1000 because we will be interested by the value in seconds
                duration = media.get_duration() / 1000
                self.sounds[id_] = {
                    'media': media,
                    'duration': duration,
                    'volume': volume,
                }
            else:
                media = pyglet.media.load(path, streaming=False)
                self.sounds[id_] = {
                    'media': media,
                    'volume': volume,
                }
Пример #4
0
    def __init__(self, *args, **kwargs):
        super(MusicPlayer, self).__init__(*args, **kwargs)

        initial_master_volume = self.node_params.get('master_volume', None)
        master_volume_mixer = self.node_params.get('mixer', '')
        default_initial_volume = self.node_params.get('default_volume', 100)

        player = self.node_params.get('player', 'vlc')
        if player == 'vlc':
            track_handler = VLCSelfReleasingTrackPlayer
            looping_track_handler = VLCLoopingTrackPlayer
        elif player == 'pyglet':
            track_handler = PygletTrackPlayer
            looping_track_handler = PygletLoopingTrackPlayer
            loop = LoopingCall(self.pyglet_loop)
            loop.start(1 / 30)
        else:
            raise ValueError(
                "Bad player value ({}). Possible values are vlc or "
                "pyglet.".format(player))

        try:
            self.master_volume = MasterVolume(
                initial_volume=initial_master_volume,
                mixer=master_volume_mixer,
            )
        except Exception:
            logger.exception()
            logger.warning(
                "Unable to initialize the master volume controller. Further "
                "actions on the master volume will be ignored.")
            self.master_volume = None

        self.tracks = {}
        for track in self.node_params.get('tracks', []):
            id_ = track['id']
            path = track['path']
            volume = track.get('volume', default_initial_volume)
            mode = track.get('mode', 'one_shot')
            if mode == 'loop':
                loop_a = track['loop_a']
                loop_b = track['loop_b']
                handler = looping_track_handler(
                    media_path=path,
                    loop_a=loop_a,
                    loop_b=loop_b,
                    initial_volume=volume,
                )
            else:
                handler = track_handler(
                    media_path=path,
                    initial_volume=volume,
                )
            self.tracks[id_] = handler
Пример #5
0
 def compute_and_schedule(self):
     now = time.monotonic()
     try:
         computed_time = self.processor.compute(self.time, {})
     except Exception:
         message = "Error while computing {}".format(self.time)
         self.processor.factory.send_notification('error', message)
         logger.exception()
     else:
         self.last_computed_delay = computed_time
         self.last_schedule_timestamp = now
         self.task = reactor.callLater(computed_time, self.perform_task)
Пример #6
0
    def onMessage(self, payload, isBinary):
        if isBinary:
            logger.warning(
                "Binary message received ({} bytes): ignoring".format(
                    len(payload)))
            return

        try:
            unicode_message = payload.decode('utf8')
        except UnicodeDecodeError:
            logger.exception("Cannot load {}: ignoring".format(payload))
            return

        try:
            message = json.loads(unicode_message)
        except json.JSONDecodeError:
            logger.exception(
                "Cannot load {}: ignoring".format(unicode_message))
            return

        ok, warning = self.validate_message(message)
        if not ok:
            logger.info("Received {}".format(message))
            logger.warning(warning)
            logger.info("Ignoring")
        else:
            logger.debug("Received {}".format(message))

            self.log_message(message, to_server=True)

            if self.client_type == P.CLIENT_NODE:
                if message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_EVENT:
                    event = message[P.EVENT]
                    self.factory.process_event(self.name, self.channel, event)
                elif message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_LOG:
                    level = message[P.LOG_LEVEL]
                    content = message[P.LOG_CONTENT]
                    if level == P.LOG_LEVEL_INFO:
                        self.factory.send_log_info(content)
                    elif level == P.LOG_LEVEL_ERROR:
                        self.factory.send_log_error(content)

            elif self.client_type == P.CLIENT_ADMIN:
                room_id = message[P.ROOM_ID]

                if message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_RUN:
                    self.factory.process_run_room(room_id)
                elif message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_HALT:
                    self.factory.process_halt_room(room_id)
                elif message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_RESET:
                    self.factory.process_reset_room(room_id)
                elif message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_SEND_EVENT_TO:
                    node = message[P.SEND_EVENT_NODE]
                    event_to_send = message[P.EVENT]
                    self.factory.process_send_event_to(room_id, node,
                                                       event_to_send)
                elif message[P.MESSAGE_TYPE] == P.MESSAGE_TYPE_SEND_EVENT_AS:
                    node = message[P.SEND_EVENT_NODE]
                    event_to_send = message[P.EVENT]
                    self.factory.process_send_event_as(room_id, node,
                                                       event_to_send)
                elif message[
                        P.MESSAGE_TYPE] == P.MESSAGE_TYPE_PRESSED_ADMIN_BUTTON:
                    button_id = message[P.PRESSED_BUTTON_ID]
                    self.factory.process_on_admin_button_pressed(
                        room_id, button_id)

            else:
                self.process_i_am_message(message)