michielpost / Q42.HueApi

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

How To Properly Start/Stop/Start/Stop Entertainment Stream? #254

Closed d8ahazard closed 2 years ago

d8ahazard commented 2 years ago

I've been having a spot of trouble lately with entertainment groups, and I'm not quite sure why.

Basically, using code lifted directly from the docs and example, I'm encountering two separate issues.

The first is that the stream now just seems to stop after about 10 seconds. Not sure if this is some new update from Philips or if I broke my code and don't recall doing so, but that appears to be for how long streaming will work for before stopping.

Second, and the reason I'm here - is because when I try to stop streaming and then re-start, I now getting an error 80 (internal_error(80)) from Org.BouncyCastle.Crypto.Tls.DtlsTransport.Connect(), which appears to be a handshaking error.

When I "stop streaming", I simply cancel the Token passed to the streaming client on connect, which had been working fine in the past. I've tried adding _client.Dispose() on stop of stream...that doesn't help at all.

So, I'm wondering if you can provide an example of the right way to stop and start an entertainment stream. I'm able to save the streamingHueClient or Group, Stream, whatever into memory and re-use them as needed, but I also need to be able to potentially change the selected entertainment group or lights as needed as well - assuming the user might want to change a mapping or something.

Any help would be greatly appreciated, as this is one of the last things I've got hanging on my list of stuff to fix before releasing my app. ;)

michielpost commented 2 years ago

My advice for reconnecting is to first do a disconnect procedure. This is how I implemented it in my HueLightDJ app: https://github.com/michielpost/HueLightDJ/blob/eb975e424a64de18e4f62f0e15299283b71305af/HueLightDJ.Web/Streaming/StreamingSetup.cs#L261-L283

try
{
  client.LocalHueClient.SetStreamingAsync(_groupId, active: false);
  client.Close();
}
catch { }

Then follow the steps here to connect again: https://github.com/Q42/Q42.HueApi/blob/master/EntertainmentApi.md#connect-to-an-entertainment-group

I always create a new instance of the StreamingHueClient and I don't reuse it. So I'm sure there is no previous state left.

I've also seen the Org.BouncyCastle.Crypto.Tls.DtlsTransport.Connect issue while trying to connect. My solution is to try again, as it almost always works on the second try.

@TripleNico also had some trouble re-activating streaming. Maybe you can find some info in his issue: https://github.com/Q42/Q42.HueApi/issues/253

d8ahazard commented 2 years ago

My advice for reconnecting is to first do a disconnect procedure. This is how I implemented it in my HueLightDJ app: https://github.com/michielpost/HueLightDJ/blob/eb975e424a64de18e4f62f0e15299283b71305af/HueLightDJ.Web/Streaming/StreamingSetup.cs#L261-L283

try
{
  client.LocalHueClient.SetStreamingAsync(_groupId, active: false);
  client.Close();
}
catch { }

Then follow the steps here to connect again: https://github.com/Q42/Q42.HueApi/blob/master/EntertainmentApi.md#connect-to-an-entertainment-group

I always create a new instance of the StreamingHueClient and I don't reuse it. So I'm sure there is no previous state left.

I've also seen the Org.BouncyCastle.Crypto.Tls.DtlsTransport.Connect issue while trying to connect. My solution is to try again, as it almost always works on the second try.

@TripleNico also had some trouble re-activating streaming. Maybe you can find some info in his issue: #253

Thanks, that worked a treat! I wound up wrapping my first connect() attempt in a try/catch, and then if that one fails, try connecting a second time. I'll probably look at implementing @TripleNico's check as well to handle closure of the stream from the App.

michielpost commented 2 years ago

Great to hear! Let me know when your app is finished, so I can include it in the list of apps using Q42.HueApi.