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); }
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; } } }
public static void Main() { UPnPDiscovery discovery = new UPnPDiscovery { //SearchTarget = DiscoverySearchTargetFactory.ServiceTypeSearch("AVTransport", "1") SearchTarget = DiscoverySearchTargetFactory.AllSearch(), WaitTimeInSeconds = 2 }; IList <UPnPDevice> devices = discovery.Search().Result; Console.WriteLine("Devices Found: " + devices.Count); devices.Foreach((i) => PrintDevice(i)); var NetworkLight = devices.SelectMany(s => s.Services).FirstOrDefault(s => s.Type == "urn:schemas-upnp-org:service:SwitchPower:1"); if (NetworkLight != null) { // do something var control = new SwitchPowerServiceControl(NetworkLight); Console.WriteLine("Type 'A' to set on the lights, 'B' to sot the lights off. 'C' toggles the light, 'Q' to exit."); while (true) { ConsoleKeyInfo info = Console.ReadKey(); switch (info.Key) { case ConsoleKey.Q: return; case ConsoleKey.A: control.SendAction("SetTarget", new Dictionary <string, object>() { { "newTargetValue", "True" } }).Wait(); break; case ConsoleKey.B: control.SendAction("SetTarget", new Dictionary <string, object>() { { "newTargetValue", "False" } }).Wait(); break; case ConsoleKey.C: var state = control.GetLightState().Result; control.SetLightState(!state).Wait(); break; } } } //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; // } //} Console.ReadLine(); }