michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
411 stars 114 forks source link

StreamingHueClient.Connect() Timeout #163

Closed richardoliverpearce closed 5 years ago

richardoliverpearce commented 5 years ago

Hi, I believe this is a bug - please correct me if I'm wrong (using Q42.HueApi.Entertainment 3.8.1). My Philips Ethernet Bridge is updated to the latest version (1810251352). I've just tried the sample in the source code and it exhibits the same problem.

I'm getting a TimeoutException thrown when trying to call StreamingHueClient.Connect(). Please find the stack trace below:

internal_error(80) ---> System.TimeoutException: The operation has timed out.

   at Q42.HueApi.Streaming.Connection.UdpTransport.Receive(Byte[] buf, Int32 off, Int32 len, Int32 waitMillis)
   at Org.BouncyCastle.Crypto.Tls.DtlsRecordLayer.ReceiveRecord(Byte[] buf, Int32 off, Int32 len, Int32 waitMillis)
   at Org.BouncyCastle.Crypto.Tls.DtlsRecordLayer.Receive(Byte[] buf, Int32 off, Int32 len, Int32 waitMillis)
   at Org.BouncyCastle.Crypto.Tls.DtlsReliableHandshake.ReceiveMessage()

The example code causing this exception is (very heavily inspired from your streaming documentation):

    class Program
    {
        static void Main(string[] args)
        {
            var applicationName = "applicationName";
            var deviceName = "deviceName";

            Task.Run(async () =>
            {
                var bridge = (await new HttpBridgeLocator()
                        .LocateBridgesAsync(TimeSpan.FromSeconds(5)))
                    .First();

                var localClient = new LocalHueClient(bridge.IpAddress);

                var appKey = await localClient.RegisterAsync(applicationName, deviceName);
                var streamingKey = (await LocalHueClient.RegisterAsync(bridge.IpAddress, applicationName, deviceName, true)).StreamingClientKey;

                var client = new StreamingHueClient(bridge.IpAddress, appKey, streamingKey);

                var all = await client.LocalHueClient.GetEntertainmentGroups();
                var group = all.First();

                var entGroup = new StreamingGroup(group.Locations);

                await client.Connect(group.Id);
            }).Wait();
        }
    }
michielpost commented 5 years ago

You're registering your application twice with RegisterAsync and using the appkey and streamingkey from different app registrations.

This should work:

var registration = (await LocalHueClient.RegisterAsync(bridge.IpAddress, applicationName, deviceName, true));
var appKey = registration.Username;
var streamingKey = registration.StreamingClientKey;
richardoliverpearce commented 5 years ago

Hi Michiel, you are absolutely right, I'll close the issue now thanks.