Example #1
0
        private IEnumerable <ISpeaker> GetSpeakersViaZeroconf()
        {
            var results = new Collection <ISpeaker>();

            var wifi          = (WifiManager)ApplicationContext.GetSystemService(Context.WifiService);
            var multicastLock = wifi.CreateMulticastLock("Zeroconf lock");

            try
            {
                multicastLock.Acquire();

                var zeroconfHosts  = ZeroconfResolver.ResolveAsync("_soundtouch._tcp.local.").Result;
                var boseConnection = new BoseConnection(new HttpClient());
                foreach (var zeroconfHost in zeroconfHosts)
                {
                    results.Add(new Speaker(zeroconfHost.DisplayName, zeroconfHost.IPAddress, boseConnection));
                }
            }
            finally
            {
                multicastLock.Release();
            }

            return(results);
        }
Example #2
0
        private async Task <IEnumerable <ISpeaker> > GetSpeakersViaUdp()
        {
            var ipAddresses = new HashSet <string>();

            for (var i = 0; i < 5; i++)
            {
                var          multicastEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
                const string searchString      = "M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nMAN:\"ssdp:discover\"\r\n" +
                                                 "ST:urn:schemas-upnp-org:device:MediaRenderer:1\r\nMX:3\r\n\r\n";

                var udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                udpSocket.SendTo(Encoding.UTF8.GetBytes(searchString), SocketFlags.None, multicastEndPoint);

                var receiveBuffer = new byte[64000];

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                var continueWaiting = true;
                while (continueWaiting)
                {
                    if (udpSocket.Available > 0)
                    {
                        var receivedBytes = udpSocket.Receive(receiveBuffer, SocketFlags.None);

                        if (receivedBytes > 0)
                        {
                            var s     = Encoding.UTF8.GetString(receiveBuffer, 0, receivedBytes);
                            var match = new Regex("Location: http://(.*):8091/").Match(s);
                            if (match.Success && match.Groups.Count == 2)
                            {
                                ipAddresses.Add(match.Groups[1].Value);
                                continueWaiting = false;
                            }
                        }
                    }

                    if (stopwatch.ElapsedMilliseconds > 15000)
                    {
                        stopwatch.Stop();
                        continueWaiting = false;
                    }
                }
            }

            var results        = new Collection <ISpeaker>();
            var httpClient     = new HttpClient();
            var boseConnection = new BoseConnection(httpClient);

            foreach (var ipAddress in ipAddresses)
            {
                results.Add(new Speaker(await GetNameAsync(httpClient, ipAddress), ipAddress, boseConnection));
            }

            return(results);
        }
        public async Task GetPowerStateAsync()
        {
            const string rawXmlContent = "<nowPlaying><ContentItem source=\"STANDBY\" /></nowPlaying>";
            var          httpMock      = new MockHttpMessageHandler();

            httpMock.Expect(HttpMethod.Get, "http://1.2.3.4:8090/now_playing").Respond("application/xml", rawXmlContent);
            var deviceMock = new Mock <ISpeaker>();

            deviceMock.Setup(x => x.IpAddress).Returns("1.2.3.4");
            var httpClient = new HttpClient(httpMock);
            var testee     = new BoseConnection(httpClient);

            var result = await testee.GetPowerStateAsync(deviceMock.Object);

            result.Should().Be(PowerState.TurnedOff);
            httpMock.VerifyNoOutstandingExpectation();
        }
        public async Task PlayAsync()
        {
            const string rawXmlContent = "<itemName>MyItem</itemName>";
            var          httpMock      = new MockHttpMessageHandler();

            httpMock.Expect(HttpMethod.Post, "http://1.2.3.4:8090/select")
            .WithPartialContent(rawXmlContent)
            .Respond(HttpStatusCode.Created);
            var deviceMock = new Mock <ISpeaker>();

            deviceMock.Setup(x => x.IpAddress).Returns("1.2.3.4");
            var httpClient = new HttpClient(httpMock);
            var testee     = new BoseConnection(httpClient);

            await testee.PlayAsync(deviceMock.Object, new ContentItem { ItemName = "MyItem" });

            httpMock.VerifyNoOutstandingExpectation();
        }
        public async Task GetCurrentContentAsync()
        {
            const string rawXmlContent  = "<ContentItem source=\"Amazon\"><itemName>MyItem</itemName></ContentItem>";
            var          rawXmlResponse = $"<nowPlaying>{rawXmlContent}</nowPlaying>";
            var          httpMock       = new MockHttpMessageHandler();

            httpMock.Expect(HttpMethod.Get, "http://1.2.3.4:8090/now_playing").Respond("application/xml", rawXmlResponse);
            var deviceMock = new Mock <ISpeaker>();

            deviceMock.Setup(x => x.IpAddress).Returns("1.2.3.4");
            var httpClient = new HttpClient(httpMock);
            var testee     = new BoseConnection(httpClient);

            var result = await testee.GetCurrentContentAsync(deviceMock.Object);

            result.ItemName.Should().Be("MyItem");
            httpMock.VerifyNoOutstandingExpectation();
        }
        public async Task TurnOff()
        {
            var httpMock = new MockHttpMessageHandler();

            httpMock.Expect(HttpMethod.Get, "http://1.2.3.4:8090/now_playing")
            .Respond("application/xml", "<nowPlaying><ContentItem source=\"TUNEIN\" /></nowPlaying>");
            httpMock.Expect(HttpMethod.Post, "http://1.2.3.4:8090/key")
            .WithContent("<key state=\"press\" sender=\"Gabbo\">POWER</key>")
            .Respond(HttpStatusCode.Created);
            httpMock.Expect(HttpMethod.Post, "http://1.2.3.4:8090/key")
            .WithContent("<key state=\"release\" sender=\"Gabbo\">POWER</key>")
            .Respond(HttpStatusCode.Created);
            var deviceMock = new Mock <ISpeaker>();

            deviceMock.Setup(x => x.IpAddress).Returns("1.2.3.4");
            var httpClient = new HttpClient(httpMock);
            var testee     = new BoseConnection(httpClient);

            await testee.TurnOffAsync(deviceMock.Object);

            httpMock.VerifyNoOutstandingExpectation();
        }