Exemple #1
0
        public async Task <IList <SonosSpeaker> > FindSpeakers()
        {
            UPnPDiscovery discovery = new UPnPDiscovery {
                SearchTarget = DiscoverySearchTargetFactory.ServiceTypeSearch("AVTransport", "1")
            };
            IList <UPnPDevice> devices = await discovery.Search();

            IEnumerable <UPnPDevice> sonosDevices = devices.Where(x => x.Properties["friendlyName"].ToLower().Contains("sonos"));
            IList <SonosSpeaker>     speakers     = new List <SonosSpeaker>();



            foreach (UPnPDevice sonosDevice in sonosDevices)
            {
                UPnPDevice device = sonosDevice
                                    .SubDevices
                                    .FirstOrDefault(x => x.Services.Any(y => y.Type == UPnPSonosServiceTypes.AvService));


                speakers.Add(new SonosSpeaker
                {
                    Name    = GetName(device.Properties["friendlyName"]),
                    Uuid    = sonosDevice.Properties["UDN"].Replace("uuid:", ""),
                    Control = new SonosSpeakerControlService(sonosDevice)
                });
            }

            return(speakers);
        }
Exemple #2
0
        public static void Main()
        {
            UPnPDiscovery discovery = new UPnPDiscovery
            {
                SearchTarget = DiscoverySearchTargetFactory.ServiceTypeSearch("AVTransport", "1")
            };
            IList <UPnPDevice> devices = discovery.Search().Result;

            UPnPServer server = new UPnPServer();

            IList <UPnPDevice> sonosDevices =
                devices.Where(x => x.Properties["friendlyName"].ToLower().Contains("sonos")).ToList();

            IList <UPnPService> avServices = sonosDevices
                                             .SelectMany(x => x.SubDevices)
                                             .SelectMany(x => x.Services)
                                             .Where(x => x.Type == "urn:schemas-upnp-org:service:AVTransport:1").ToList();

            IList <AvTransportServiceControl> speakers = avServices.Select(x => new AvTransportServiceControl(x)).ToList();


            server.Start(24458);

            speakers.Foreach(x =>
            {
                server.SubscribeToControl(x);
                x.OnLastChangeEvent += (sender, args) => { Console.WriteLine("SOMETHING: " + args.TransportState); };
            });


            while (true)
            {
                ConsoleKeyInfo info = Console.ReadKey();

                switch (info.Key)
                {
                case ConsoleKey.Q:
                    return;

                case ConsoleKey.A:
                    speakers.Foreach(
                        x => x.SendAction("Play", new Dictionary <string, string>()
                    {
                        { "InstanceID", "0" }, { "Speed", "1" }
                    }).Wait());
                    break;

                case ConsoleKey.S:
                    speakers.Foreach(x => x.SendAction("Pause", new Dictionary <string, string>()
                    {
                        { "InstanceID", "0" }
                    }).Wait());
                    break;
                }
            }
        }