michielpost / Q42.HueApi

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

How to know/ask for the "link" button to be pressed? #90

Closed jaytonic closed 7 years ago

jaytonic commented 7 years ago

Hi,

I'm doing pretty much what the sample shows:

IEnumerable<string> bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));
IEnumerable<string> iPs = bridgeIPs as string[] ?? bridgeIPs.ToArray();
if (!iPs.Any())
{
    _logger.Warn("There is not any bridge available");
}
string bridgeIP = iPs.First();
_logger.Info($"Connecting to {bridgeIP}");
ILocalHueClient client = new LocalHueClient(bridgeIP); 

string appKey = await client.RegisterAsync("DeskHue", System.Environment.MachineName);

The issue I've is that I don't see when I should ask the user to press the link button? how the other applications does? They asks for the user to press and then try in a loop to do the RegisterAsync? Is there some kind of event that I could register to know the button has been pressed?

Thank you for the support

niels9001 commented 7 years ago

Yeah, that's what I do.. I ask the user to press the button, and just check every 3 seconds if the button has been pressed or not with a total limit of 30 seconds (like the official app).

It works fine, and if everything goes well the user only sees this once :)!

mschuler commented 7 years ago

Hi @jgrossrieder

I'm handling it this way:

private async Task<string> GetApiKeyWithBridgeButtonClick(PhilipsHueBridge bridge)
{
    var endTime = DateTime.UtcNow.AddSeconds(30);
    var client = new LocalHueClient(bridge.IpAddress);

    while (DateTime.UtcNow < endTime)
    {
        try
        {
            var machineName = Environment.MachineName.Replace(' ', '_');

            if (machineName.Length > 19)
            {
                machineName = machineName.Substring(0, 19);
            }

            var appKey = await client.RegisterAsync("Xpressive.Home", machineName);
            return appKey;
        }
        catch { }

        await Task.Delay(TimeSpan.FromSeconds(1));
    }

    return null;
}

As you can see, I ask the user for pressing the button and then I'm polling the RegisterAsync method every second for 30 seconds.

jaytonic commented 7 years ago

@niels9001 @mschuler Thank you for the answer. Thank you, I was just thinking this was not very efficient, is this a limitation from the Q42 API or from the hue API?

BTW, why do you(@mschuler) change the machine name? Is there some limitation on the API?

mschuler commented 7 years ago

Yes, this is a limitation, see Point 7.1.2:

https://developers.meethue.com/documentation/configuration-api#71_create_user

jaytonic commented 7 years ago

Thank you