Ejemplo n.º 1
0
        /// <summary>
        /// Sucht im Netzwerk nach Bridges
        /// </summary>
        /// <returns>Gefundene Bridges</returns>
        public async Task <BridgeInfo[]> ScanBridges()
        {
            Logger.WriteToLog("Bridge-Suche gestartet", LogLevel.Info);

            if (SimMode)
            {
                Logger.WriteToLog("Bridge-Simulation", LogLevel.Simulation);
                return(new BridgeInfo[1] {
                    new BridgeInfo {
                        Name = "Simulated Bridge", BridgeId = "simulation", IpAddress = "255.255.255.255"
                    }
                });
            }
            var bridgeInfos = new List <BridgeInfo>();

            IBridgeLocator locator = new HttpBridgeLocator();
            var            bridges = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));


            foreach (LocatedBridge bridge in bridges)
            {
                try
                {
                    string json = string.Empty;
                    string url  = @"http://" + bridge.IpAddress + "/api/config";

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "GET";
                    //request.AutomaticDecompression = DecompressionMethods.GZip;

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        using (Stream stream = response.GetResponseStream())
                            using (StreamReader reader = new StreamReader(stream))
                            {
                                json = reader.ReadToEnd();
                            }

                    var info = new BridgeInfo(JsonConvert.DeserializeObject <PublicConfig>(json));
                    info.IpAddress = bridge.IpAddress;

                    Logger.WriteToLog("Gefundene Bridge: " + info.GetNameString(BridgeNameDisplay.IpAndName), LogLevel.Info);

                    bridgeInfos.Add(info);
                }
                catch (Exception)
                {
                    //Ohne weitere Infos fortfahren
                    bridgeInfos.Add(new BridgeInfo(bridge));

                    Logger.WriteToLog("Erweiterete Bridge-Info (description.xml) für Bridge (" + bridge.IpAddress + ") nicht verfügbar", LogLevel.Warning);
                }
            }

            return(bridgeInfos.ToArray());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Verbindung zu einer bekannten Bridge herstellen
        /// </summary>
        /// <param name="bridgeIp">IP Adresse der zu verbindenden Bridge</param>
        /// <returns></returns>
        public BridgeResult ConnectBridge(BridgeInfo bridge)
        {
            if (SimMode)
            {
                Logger.WriteToLog("Bridge-Verbindung wurde simuliert", LogLevel.Simulation);
                ConnectedBridge = bridge;
                return(BridgeResult.SuccessfulConnected);
            }
            else
            {
                Logger.WriteToLog("Verbindungsaufbau mit Bridge (" + bridge.IpAddress + ")");
            }

            Version apiVersion;

            if (String.IsNullOrWhiteSpace(bridge.ApiVersion))
            {
                Logger.WriteToLog("API-Version unbekannt", LogLevel.Warning);
                return(BridgeResult.Error);
            }
            else
            {
                if (!Version.TryParse(bridge.ApiVersion, out apiVersion))
                {
                    Logger.WriteToLog("API-Version unbekannt; Versionsstring (" + bridge.ApiVersion + ") kann nicht geparst werden", LogLevel.Error);
                    return(BridgeResult.Error);
                }
            }

            if (apiVersion < new Version(API_VERSION_MIN))
            {
                Logger.WriteToLog("API-Version (" + apiVersion.ToString() + ") nicht unterstützt; Es wird eine Version ab " + API_VERSION_MIN + " benötigt", LogLevel.Error);
                return(BridgeResult.Error);
            }


            Logger.WriteToLog("API-Version = " + apiVersion.ToString());



            if (m_AppKeyManager.TryGetKey(bridge.BridgeId, out string appKey))
            {
                try
                {
                    Logger.WriteToLog("Verbindungsaufbau zu Bridge (" + bridge.IpAddress + ") initiieren");
                    m_HueClient = new LocalHueClient(bridge.IpAddress);
                    if (DevMode)
                    {
                        Logger.WriteToLog("AppKey = " + appKey);
                    }
                    m_HueClient.Initialize(appKey);

                    ConnectedBridge = bridge;
                    ConnectedBridge.CurrentAppKey = appKey;

                    //Init own API caller
                    ApiCaller.Host   = bridge.IpAddress;
                    ApiCaller.AppKey = appKey;

                    return(BridgeResult.SuccessfulConnected);
                }
                catch (Exception ex)
                {
                    //TODO: Antwort bei falschen AppKey?
                    if (ex.Message.Contains("Link button not pressed"))
                    {
                        return(BridgeResult.UnauthorizedUser);
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                return(BridgeResult.MissingUser);
            }
        }