Exemplo n.º 1
0
        public ClimateItem CurrentReading(AppKeyConfig AppConfig)
        {
            ClimateItem item = null;

            // Get the access and refresh tokens if they exist, if they don't exist we cannot proceceed and return null
            NibeAuth auth = getNibeAuthJson();

            if (auth == null)
            {
                return(null);
            }

            // Access and refresh token exist,  try to access using the access token
            item = GetReadingWithAccessCode(auth.access_token, AppConfig);

            // If we get null back, it didn't work and we try to refresh with the refresh token
            if (item == null)
            {
                // If it didn't work to get a new access token we bail out and return null
                auth = Refresh(AppConfig);


                if (auth == null)
                {
                    return(null);
                }

                // It worked to get a new access token, try agin to get data using the new access token
                item = GetReadingWithAccessCode(auth.access_token, AppConfig);
            }

            // If get an item back we return it, it we get null back we can't still access data and return null
            return(item);
        }
Exemplo n.º 2
0
 private NibeAuth getNibeAuthJson()
 {
     if (File.Exists("data/nibeauth.json"))
     {
         string nibeAuthJson = File.ReadAllText("data/nibeauth.json");
         nibeAuth = JsonConvert.DeserializeObject <NibeAuth>(nibeAuthJson);
         return(nibeAuth);
     }
     else
     {
         return(null);
     }
 }
Exemplo n.º 3
0
        public void init(AppKeyConfig config)
        {
            // Check to see if we have a code
            if (code == null)
            {
                throw new Exception("Code is null");
            }

            //Login
            var pairs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("client_id", config.NibeClientId),
                new KeyValuePair <string, string>("client_secret", config.NibeClientSecret),
                new KeyValuePair <string, string>("code", this.code),
                new KeyValuePair <string, string>("redirect_uri", config.NibeRedirectURI),
                new KeyValuePair <string, string>("scope", "READSYSTEM")
            };

            HttpClient client     = new HttpClient();
            var        outcontent = new FormUrlEncodedContent(pairs);
            var        uri        = new UriBuilder(config.NibeHost)
            {
                Path  = "/oauth/token",
                Query = "parameterIds=outdoor_temperature&parameterIds=indoor_temperature"
            }.Uri;

            HttpResponseMessage response = client.PostAsync(uri, outcontent).Result;

            if (!response.IsSuccessStatusCode)
            {
                int statusCode = (int)response.StatusCode;
                throw new Exception(statusCode + " " + response.ReasonPhrase);
            }


            string contentResult = response.Content.ReadAsStringAsync().Result;

            nibeAuth = JsonConvert.DeserializeObject <NibeAuth>(contentResult);

            string nibeAuthJson = JsonConvert.SerializeObject(nibeAuth);

            File.WriteAllText("data/nibeauth.json", nibeAuthJson);
        }
Exemplo n.º 4
0
        public NibeAuth Refresh(AppKeyConfig AppConfig)
        {
            string nibeAuthJson = File.ReadAllText("data/nibeauth.json");

            nibeAuth = JsonConvert.DeserializeObject <NibeAuth>(nibeAuthJson);

            //Login
            var pairs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("grant_type", "refresh_token"),
                new KeyValuePair <string, string>("client_id", AppConfig.NibeClientId),
                new KeyValuePair <string, string>("client_secret", AppConfig.NibeClientSecret),
                new KeyValuePair <string, string>("refresh_token", nibeAuth.refresh_token)
            };
            HttpClient client     = new HttpClient();
            var        outcontent = new FormUrlEncodedContent(pairs);
            var        uri        = new UriBuilder(AppConfig.NibeHost)
            {
                Path  = "/oauth/token",
                Query = "parameterIds=outdoor_temperature&parameterIds=indoor_temperature"
            }.Uri;

            var response = client.PostAsync(uri, outcontent).Result;

            // If we could not refresh
            if (!response.IsSuccessStatusCode)
            {
                int statusCode = (int)response.StatusCode;
                return(null);
            }

            // Replace the access and refresh token in the file with the new values
            string contentResult = response.Content.ReadAsStringAsync().Result;

            nibeAuth     = JsonConvert.DeserializeObject <NibeAuth>(contentResult);
            nibeAuthJson = JsonConvert.SerializeObject(nibeAuth);
            File.WriteAllText("data/nibeauth.json", nibeAuthJson);

            // Success, a new access and refresh token is in the file
            return(nibeAuth);
        }