Beispiel #1
0
        private static void _detectionBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            log.Info("Starting bridge detection...");


            Dictionary <string, BasicConfig> newdetectedBridge = new Dictionary <string, BasicConfig>();

            // Detect using UPNP

            try
            {
                ServiceController sc = new ServiceController("SSDPSRV");

                if (sc.Status == ServiceControllerStatus.Running)
                {
                    log.Info("Starting UPNP detection of bridges...");
                    try
                    {
                        List <ManagedUPnP.Device> upnpDevices =
                            Discovery.FindDevices(null, 3000, int.MaxValue, out bool finished).ToList();

                        foreach (ManagedUPnP.Device dev in upnpDevices)
                        {
                            if (!dev.ModelName.Contains("Philips hue bridge"))
                            {
                                continue;
                            }
                            Bridge bridge = new Bridge()
                            {
                                IpAddress = IPAddress.Parse(dev.RootHostName)
                            };
                            BasicConfig bresult = bridge.GetBridgeBasicConfig();
                            if (bresult != null)
                            {
                                log.Info($"Bridge found : {bridge.Name} at {bridge.IpAddress} ");
                                newdetectedBridge.Add(dev.RootHostName, bresult);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignored
                    }

                    log.Info("Ending UPNP detection of bridges...");
                }
            }
            catch (Exception ex)
            {
                log.Error($"UPNP detection error : {ex.Message}");
            }

            // If not bridge are found via upnp try the portal.
            log.Info("Starting bridge portal detection...");
            if (newdetectedBridge.Count == 0)
            {
                // Detect using Portal
                CommResult comres = Comm.SendRequest(new Uri("http://www.meethue.com/api/nupnp"), WebRequestType.Get);

                switch (comres.Status)
                {
                case WebExceptionStatus.Success:
                    List <HueDevice> portalDevices = Serializer.DeserializeToObject <List <HueDevice> >(comres.Data);
                    foreach (HueDevice dev in portalDevices)
                    {
                        if (newdetectedBridge.ContainsKey(dev.internalipaddress))
                        {
                            continue;
                        }
                        Bridge bridge = new Bridge()
                        {
                            IpAddress = IPAddress.Parse(dev.internalipaddress)
                        };
                        BasicConfig bresult = bridge.GetBridgeBasicConfig();
                        if (bresult != null)
                        {
                            log.Info($"Bridge found : {bridge.Name} at {bridge.IpAddress} ");
                            newdetectedBridge.Add(dev.internalipaddress, bresult);
                        }
                    }
                    break;

                case WebExceptionStatus.Timeout:
                    log.Info($"Timeout while detecting bridge via portal");
                    OnPortalDetectionTimedOut?.Invoke(null, new DetectionErrorEventArgs(comres.Data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.Data));
                    break;

                default:
                    log.Info($"Unknown error while detecting bridge via portal");
                    OnPortalDetectionError?.Invoke(null, new DetectionErrorEventArgs(comres.Data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.Data));
                    break;
                }
            }
            log.Info("Ending bridge portal detection...");

            Dictionary <string, Bridge> bridges = newdetectedBridge.Select(kvp => new Bridge
            {
                IpAddress  = IPAddress.Parse(kvp.Key),
                Mac        = kvp.Value.mac,
                ApiVersion = kvp.Value.apiversion,
                SwVersion  = kvp.Value.swversion,
                Name       = kvp.Value.name ?? ""
            }).ToDictionary(p => p.Mac, p => p);

            // Process all bridges to get needed settings.
            e.Result = bridges;
            log.Info("Ending bridge detection.");
        }
Beispiel #2
0
        private static void _detectionBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            Dictionary <string, BasicConfig> newdetectedBridge = new Dictionary <string, BasicConfig>();

            // Detect using UPNP
            bool finished;

            List <ManagedUPnP.Device> upnpDevices =
                Discovery.FindDevices(null, 3000, int.MaxValue, out finished).ToList();

            foreach (ManagedUPnP.Device dev in upnpDevices)
            {
                if (!dev.ModelName.Contains("Philips hue bridge"))
                {
                    continue;
                }

                CommandResult bresult = GetBridgeBasicConfig(IPAddress.Parse(dev.RootHostName));
                if (bresult.Success)
                {
                    newdetectedBridge.Add(dev.RootHostName, (BasicConfig)bresult.resultobject);
                }
            }


            // If not bridge are found via upnp try the portal.
            if (newdetectedBridge.Count == 0)
            {
                // Detect using Portal
                CommResult comres = Communication.SendRequest(new Uri("http://www.meethue.com/api/nupnp"), WebRequestType.GET);

                switch (comres.status)
                {
                case WebExceptionStatus.Success:
                    List <Device> portalDevices = Serializer.DeserializeToObject <List <Device> >(comres.data);
                    foreach (Device dev in portalDevices)
                    {
                        if (newdetectedBridge.ContainsKey(dev.internalipaddress))
                        {
                            continue;
                        }
                        CommandResult bresult = GetBridgeBasicConfig(IPAddress.Parse(dev.internalipaddress));
                        if (bresult.Success)
                        {
                            newdetectedBridge.Add(dev.internalipaddress, (BasicConfig)bresult.resultobject);
                        }
                    }
                    break;

                case WebExceptionStatus.Timeout:
                    OnPortalDetectionTimedOut?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    break;

                default:
                    OnPortalDetectionError?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    OnBridgeDetectionFailed?.Invoke(null, new DetectionErrorEventArgs(comres.data));
                    break;
                }
            }


            Dictionary <string, Bridge> bridges = newdetectedBridge.Select(kvp => new Bridge
            {
                IpAddress  = IPAddress.Parse(kvp.Key),
                Mac        = kvp.Value.mac,
                ApiVersion = kvp.Value.apiversion,
                SwVersion  = kvp.Value.swversion,
                Name       = kvp.Value.name ?? ""
            }).ToDictionary(p => p.Mac, p => p);

            // Process all bridges to get needed settings.
            e.Result = bridges;
        }