Search code examples
c#firebasenest-api

FirebaseSharp nest-api set thermostat value


I am trying to set thermostat using FirebaseSharp with no success. I've downloaded padmore's example from http://www.codeproject.com/Articles/818265/NET-Works-with-Nest-Guide-to-calling-Nest-API-fro?msg=5010020#xx5010020xx. I am getting event when i change the value from the virtual device. I set thermostat to read/write as well. This is what is do: The problem is that firebaseClient.Post and firebaseClient.Put are called and never returned. also there is no exception.

    private void buttonPost_Click( object sender, RoutedEventArgs e )
    {
        string data = "30";
        string fullPath = "//devices//thermostats//<device id>//target_temperature_high_c";;

        string t;            
        try
        {
            t = firebaseClient.Post( fullPath, data );
            string i = string.Format( "firebaseClient.Post: {0}", t );
            OutMessage( i );
            return;
        }
        catch(Exception ex)
        {
            string i = string.Format( "firebaseClient.Post: exception {0}", ex.Message );
            OutMessage( i );
        }

        try
        {
            t = firebaseClient.Put( fullPath, data );
            string i = string.Format( "firebaseClient.Put: {0}", t );
            OutMessage( i );
        }
        catch(Exception ex)
        {
            string i = string.Format( "firebaseClient.Put: exception {0}", ex.Message );
            OutMessage( i );
        }
    }

So what am I doing wrong? I appreciate your help.


Solution

  • Ok, just got it.

    Using following sample: Need Working Example of Nest REST API without using Firebase API.

        private async void changeAway( string structureid, string thermostateId)
        {
            using(HttpClient http = new HttpClient())
            {
                string url =   "https://developer-api.nest.com/";
                StringContent content = null;
    
                if((string)comboBoxDataType.SelectedValue == "Structures")
                {
                    string away = comboBoxDataAway.SelectedValue.ToString();
                    url += "structures/" + structureid + "/?auth=" + _accessToken;
                    content = new StringContent( "{\"away\":\"" + away + "\"}" );
                }
                else
                {
                    if(textBoxPostData.Text == string.Empty )
                    {
                        OutMessage( "data can not be empty" );
                        return;
                    }
    
                    int data = 0;
                    if(int.TryParse( textBoxPostData.Text, out data ) == false)
                    {
                        OutMessage( "data must be number" );
                        return;                    
                    }
    
                    url += "devices/thermostats/" + thermostateId + "/?auth=" + _accessToken;
                    content = new StringContent( "{\"target_temperature_high_c\":" + textBoxPostData.Text + "}" );
                }
    
                HttpResponseMessage rep = await http.PutAsync( new Uri( url ), content );
                if(null != rep)
                {
                    OutMessage( "http.PutAsync2=" + rep.ToString() );
                }
            }
        }