def create_speaker(self, request):
        """Create new speaker.

        Args:
            request (SpeakerForm)

        Returns:
            SpeakerForm updated with the new object's key

        Raises:
            endpoints.BadRequestException if the speaker info is invalid
            models.ConflictException if a speaker with same email already exists
        """
        # Validate fields
        if not request.name:
            raise endpoints.BadRequestException("Speaker 'name' field required")
        if not request.email:
            raise endpoints.BadRequestException("Speaker 'email' field required")

        # Check that speaker with same email does not exist
        speaker = Speaker.query(Speaker.email == request.email).get()
        if speaker:
            raise ConflictException("Speaker already exists with email: %s" % request.email)

        # Allocate speaker ID and generate speaker key
        s_id = Speaker.allocate_ids(size = 1)[0]
        s_key = ndb.Key(Speaker, s_id)

        # Create new speaker
        speaker = Speaker.to_object(request)
        speaker.key = s_key # set the key since this is a new object
        speaker.put()

        # Returm form back
        return speaker.to_form()
Ejemplo n.º 2
0
    def discover(self) -> Set[Speaker]:
        """Discovers local Sonos speakers

        :returns: Set of discovered speakers
        :rtype: set[models.speaker.Speaker]
        """
        speakers = set()
        speaker: soco.SoCo
        discovery = soco.discover(include_invisible=True)
        if discovery is not None:
            for speaker in discovery:
                name = speaker.player_name
                speaker_instance = Speaker(speaker_id=speaker.uid,
                                           name=name,
                                           ip_address=speaker.ip_address)
                if not speaker.is_visible and len({
                        x
                        for x in speaker.group
                        if x.is_visible and x.player_name == name
                }) == 1:
                    speaker_instance.name = name + ' (R)'
                elif len(self.get_stereo_pair_slaves(speaker)) == 1:
                    speaker_instance.name = name + ' (L)'
                speakers.add(speaker_instance)
        return speakers
Ejemplo n.º 3
0
    def test_it_can_find_existing_speakers(self):
        SpeakerService.find_or_create('*****@*****.**')
        SpeakerService.find_or_create('*****@*****.**')
        speakers = Speaker.query().fetch(10)
        self.assertEqual(2, len(speakers))
        speaker1 = speakers[0]

        SpeakerService.find_or_create('*****@*****.**')
        SpeakerService.find_or_create('*****@*****.**')
        speakers = Speaker.query().fetch(10)
        self.assertEqual(2, len(speakers))

        speakers = Speaker.query(Speaker.email == speaker1.email).fetch(10)
        self.assertEqual(1, len(speakers))
Ejemplo n.º 4
0
    def find_or_create(email):
        """Finds an existing speaker by email, or creates a new one if one does
        not exist.

        Args:
            email (string)

        Returns:
             Speaker
        """
        speaker = Speaker.query(Speaker.email == email).get()

        if speaker is not None:
            return speaker.key.urlsafe()
        else:
            return Speaker(email=email).put().urlsafe()
    def get_speakers(self):
        """Get list of all speakers.

        Returns:
            SpeakerForms
        """
        speakers = Speaker.query().fetch()
        return SpeakerForms(
            items = [s.to_form() for s in speakers]
        )
Ejemplo n.º 6
0
    def get_speakers(self):
        """Gets a list of all existing speakers.

        Returns:
             SpeakerForms
        """
        speakers = Speaker.query().fetch()

        return SpeakerForms(
            items=[self.copy_entity_to_form(
                SpeakerForm(), speaker) for speaker in speakers])
Ejemplo n.º 7
0
    def load(self) -> None:
        """Loads the configuration file and parses it into class attributes."""
        self.type = NodeType[self.data.get('type').upper()]

        # load rooms
        for room_data in self.data.get('rooms'):
            self.rooms.append(Room.from_json(room_data))

        # load nodes
        for node_data in self.data.get('nodes'):
            self.nodes.append(Node.from_json(node_data, self))

        # load speakers
        for speaker_data in self.data.get('speakers'):
            self.speakers.append(Speaker.from_json(speaker_data, self))

        # load rooms calibration data after loading speakers
        for room_data in self.data.get('rooms'):
            room = self.room_repository.get_room(room_data.get('id'), fail=True)
            room.calibration_points_from_json(room_data.get('calibration_points'), self)
Ejemplo n.º 8
0
    pops.populate_clusters()

    if args.policy_type == 'reactive':
        assert args.mode == 'test', 'Reactive policy does not need training, only use with "mode=test"'
        model = ReactiveSpeaker(args.n_attrs)
    elif args.policy_type == 'reactive_max':
        assert args.mode == 'test', 'Reactive policy does not need training, only use with "mode=test"'
        model = ReactiveMaxSpeaker(args.n_attrs)
    elif args.policy_type == 'random':
        assert args.mode == 'test', 'Random policy does not need training, only use with "mode=test"'
        model = RandomSpeaker(args.n_attrs)
    else:

        # Model to test it with.
        model = Speaker(args.n_attrs, args.h_dim, embed_agents=args.embed_agents,
                        policy_type=args.policy_type, epsilon_greedy=args.epsilon_greedy,
                        eval_epsilon_greedy=args.eval_epsilon_greedy)

        if args.mode == 'test':
            model_path = os.path.join(str(args.results_dir), 'model.pt')

            if os.path.isfile(model_path):
                model.load_state_dict(torch.load(
                    model_path, map_location=args.device))

        model.to(args.device)

    dataset = ImgRefDataset(pops, args.n_attr_samples, args.n_attrs,
                            args.n_targets, speaker_dataset, listener_dataset,
                            args.batch_sz, model, args.eval_step, args.eval_idx_list)
Ejemplo n.º 9
0
    def test_it_can_create_new_speakers(self):
        SpeakerService.find_or_create('*****@*****.**')

        speakers = Speaker.query().fetch(2)
        self.assertEqual(1, len(speakers))
        self.assertEquals('*****@*****.**', speakers[0].email)