예제 #1
0
        /// <summary>
        /// Get the bridge Settings.
        /// </summary>
        /// <returns>The Settings of the bridge or null.</returns>
        public BridgeSettings GetBridgeSettings()
        {
            BridgeSettings bridgeSettings = new BridgeSettings();

            CommResult comres = Communication.SendRequest(new Uri(BridgeUrl + "/config"), WebRequestType.GET);

            switch (comres.status)
            {
            case WebExceptionStatus.Success:
                bridgeSettings = Serializer.DeserializeToObject <BridgeSettings>(comres.data);
                if (bridgeSettings != null)
                {
                    return(bridgeSettings);
                }
                bridgeSettings = new BridgeSettings();
                List <Message> lstmsg = Serializer.DeserializeToObject <List <Message> >(Communication.lastjson);
                lastMessages = lstmsg != null ? new MessageCollection(lstmsg) : new MessageCollection {
                    new UnkownError(comres)
                };
                break;

            case WebExceptionStatus.Timeout:
                lastMessages = new MessageCollection {
                    _bridgeNotResponding
                };
                BridgeNotResponding?.Invoke(this, _e);
                break;

            default:
                lastMessages = new MessageCollection {
                    new UnkownError(comres)
                };
                break;
            }

            return(bridgeSettings);
        }
예제 #2
0
파일: Hue.cs 프로젝트: abhaypsingh/WinHue3
        private static void _ipscanBgw_DoWork(object sender, DoWorkEventArgs e)
        {
            IPAddress ip = IPAddress.Parse(GetLocalIPAddress());

            byte[] ipArray   = ip.GetAddressBytes();
            byte   currentip = ipArray[3];
            ObservableCollection <Bridge> newlist = new ObservableCollection <Bridge>();

            Ping pingSender = new Ping()
            {
            };
            BridgeSettings desc = new BridgeSettings();

            for (byte x = 2; x <= 254; x++)
            {
                if (_ipscanBgw.CancellationPending)
                {
                    break;
                }
                _ipscanBgw.ReportProgress(0, x);
                if (x == currentip)
                {
                    continue;
                }
                ipArray[3] = x;

                Communication.Timeout = 50;
                if (_ipscanBgw.CancellationPending)
                {
                    break;
                }

                CommResult comres = Communication.SendRequest(new Uri($@"http://{new IPAddress(ipArray)}/api/config"), WebRequestType.GET);

                switch (comres.status)
                {
                case WebExceptionStatus.Success:
                    desc = Serializer.DeserializeToObject <BridgeSettings>(comres.data);    // try to deserialize the received message.
                    if (desc == null)
                    {
                        continue;                  // if the deserialisation didn't work it means this is not a bridge continue with next ip.
                    }
                    if (newlist.Count > 0)
                    {
                        if (!newlist.Any(y => Equals(y.IpAddress, ipArray)))
                        {
                            newlist.Add(new Bridge()
                            {
                                IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                            });
                        }
                    }
                    else
                    {
                        newlist.Add(new Bridge()
                        {
                            IpAddress = new IPAddress(ipArray), ApiVersion = desc.apiversion, Mac = desc.mac
                        });
                    }
                    break;

                case WebExceptionStatus.Timeout:

                    break;

                default:

                    break;
                }
            }

            e.Result = newlist;
        }