Ejemplo n.º 1
0
 private void btnFind_Click(object sender, EventArgs e)
 {
     if (Discovery.DiscoveryFinished)
     {
         Discovery.FindDevices();
         SetPanel(pnlStep1);
         btnFind.Text = "...";
     }
 }
Ejemplo n.º 2
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.");
        }
Ejemplo n.º 3
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;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 初始化 UPnP 设备信息列表。
        /// </summary>
        /// <param name="discoveryTimeout">用于发现设备所用的超时时间。</param>
        public static void InitUPnP(TimeSpan discoveryTimeout)
        {
            if (!UseUPnP)
            {
                return;
            }
            //WindowsFirewall.CheckUPnPFirewallRules(form);
            bool searchCompleted;

            _upnpDevices = Discovery.FindDevices(string.Empty, (int)discoveryTimeout.TotalMilliseconds, 0, out searchCompleted, AddressFamilyFlags.IPv4);
            //_upnpDevices = Discovery.FindDevices("WANConnectionDevice:1", AddressFamilyFlags.IPv4);
            //bool found = false;
            //FindPortMapping(_upnpDevices, ref found);
            Services services = _upnpDevices.FindServices(string.Empty, true);
            bool     afound = false, dfound = false;

            foreach (var serv in services)
            {
                if (serv.FriendlyServiceTypeIdentifier == "WANIPConnection:1")
                {
                    var sd = serv.Description();
                    var ac = sd.Actions;
                    afound          = dfound = false;
                    _addPortMapping = _deletePortMapping = null;
                    if (ac.Count > 0)
                    {
                        foreach (var item in ac)
                        {
                            if (item.Key == "AddPortMapping")
                            {
                                _addPortMapping = serv.InvokeAction;
                                //System.Diagnostics.Debug.Print("Name of action: " + item.Value.Name);
                                //foreach (var x in item.Value.Arguments)
                                //{
                                //    System.Diagnostics.Debug.Print(x.Key + " is " + x.Value.RelatedStateVariableDescription.DataTypeValue.BaseType().ToString());
                                //}
                                afound = true;
                                if (dfound)
                                {
                                    break;
                                }
                            }
                            else if (item.Key == "DeletePortMapping")
                            {
                                _deletePortMapping = serv.InvokeAction;
                                //System.Diagnostics.Debug.Print("Name of action: " + item.Value.Name);
                                //foreach (var x in item.Value.Arguments)
                                //{
                                //    System.Diagnostics.Debug.Print(x.Key + " is " + x.Value.RelatedStateVariableDescription.DataTypeValue.BaseType().ToString());
                                //}
                                //dfound = true;
                                if (afound)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (afound && dfound)
                    {
                        break;
                    }
                }
            }
        }