michielpost / Q42.HueApi

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

App key became invalid after PC reboots #219

Closed HsiuWei closed 4 years ago

HsiuWei commented 4 years ago

Hi all, I have a Windows PC application that help user connect to HUE bridge and control the HUE light bulbs.

I will prompt user to push HUE bridge button during first time connection to get the app key and will connect HUE bridge automatically whenever my application restarts since then.

How I get the app key, appLey: var appKey = await client.RegisterAsync("mypersonalappname", "mydevicename");

The issue is whenever the user reboot his PC, the original app key seems become invalid so the SendCommandAsync throws catch error. My application code:

m_hueClient = new HueClient(ip, appKey); 
m_hueClient .Initialize(appKey);

m_hueClient.SendCommandAsync(command);

Let me know, if I am doing anything wrong, thanks!

michielpost commented 4 years ago

Your code looks fine. If you use the HueClient constructor with the appKey, you don't need to call Initialze. It will do that for you.

The application key is provided by the Hue bridge, and app keys won't become invalid due to an application restart. So the problem might be with the storage of the application key in your app? When you restart the app, do you get the same key you have previously from your custom storage?

This library does not store the key for you. You'll need to supply the correct key and then it will work.

HsiuWei commented 4 years ago

Thanks at first! Yes, I supply the correct key after my app restarts. I store the key in Windows registry and read it later on. Not sure what makes the SendCommand fail though.

michielpost commented 4 years ago

Can you inspect the request to the bridge, with https://www.telerik.com/fiddler ? Maybe that way you can figure out what the difference is and if the same key is being sent to the bridge.

HsiuWei commented 4 years ago

I will try the link thanks. I just thought of there are two possible IPs I will get from my HUE bridge. One is 192.168.xx.xx and the other one is 192.168.xx.xx:80 ,which I will treat as two different IPs and store two different App keys to Windows registry. Maybe it is because I use the wrong App key that does not match the current IP(with or without port number). Anyway, should IP with and without port number, 80, makes a difference to SendCommand failure?

michielpost commented 4 years ago

The port number does not matter, as long as you connect to the same Hue bridge. The app key is registered to that bridge. You can even use the same app key on a different PC if you connect to the same bridge.

HsiuWei commented 4 years ago

I see. I found that the key is fine. It is because the internet connection leading SendCommandAsync failure. Once the pc reboot, there's a really short amount of time required for the internet to go online while my service has already up and running trying to connect and send command to the HUE bridge.

Is there a stable way to check if the HUE bridge connects to the PC successfully and have a re-try mechanism if it fails? I am currently using the following method to check the connection:

m_hueClient = new HueClient(ip, appKey); 

var command = new LightCommand();
command.On = true;
if(m_hueClient.SendCommandAsync(command))
   m_status = "connected";
else
{
 m_status = "disconnected";
 delete m_hueClient;
}
michielpost commented 4 years ago

Good to hear you found the problem. The HueClient does not have build in functionality to check if there is an active internet connection. The api calls will fail if the hue bridge can't be reached. There are problably other libraries that provide functionality to check if there is an active network connection, or build in functionality depending on the platform you're targeting.