/// <summary>
        /// Get average, high, low temperature for a time period
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewButton_Click(object sender, RoutedEventArgs e)
        {
            WeatherServiceHW04 clientSdk = new WeatherServiceHW04();
            Task <HttpOperationResponse <string> > resultTask = null;
            //check whick radio button is selected within the same stackpanel
            List <RadioButton> radioButtons = DegreeTypeStackPanel.Children.OfType <RadioButton>().ToList();
            RadioButton        rbTarget     = radioButtons.First(r => r.IsChecked != null && (bool)r.IsChecked);
            string             whichType    = (string)rbTarget.Content;

            try
            {
                StatusTextBlock.Text       = "GET Request Made, waiting for response...";
                StatusTextBlock.Foreground = new SolidColorBrush(Colors.White);
                StatusBorder.Background    = new SolidColorBrush(Colors.Blue);
                //init result text box
                OutTextBlock.Text = string.Empty;

                // Let the user know something happening
                progressBar.IsIndeterminate = true;

                switch (whichType)
                {
                case "Average":
                    resultTask =
                        clientSdk.Temperatures.GetAverageTemperatureByTypeAndPeriodWithOperationResponseAsync(
                            IntervalType, int.Parse(TimeTextBox.Text));
                    break;

                case "High":
                    resultTask =
                        clientSdk.Temperatures.GetHighTemperatureByTypeAndPeriodWithOperationResponseAsync(
                            IntervalType, int.Parse(TimeTextBox.Text));
                    break;

                case "Low":
                    resultTask =
                        clientSdk.Temperatures.GetLowTemperatureByTypeAndPeriodWithOperationResponseAsync(
                            IntervalType, int.Parse(TimeTextBox.Text));
                    break;
                }
                // Wait until task completes
                if (resultTask != null)
                {
                    resultTask.Wait();
                    StatusBorder.Background = new SolidColorBrush(Colors.Green);
                    StatusTextBlock.Text    = "Request completed!";

                    // Display results
                    if (resultTask.Result.Response.IsSuccessStatusCode)
                    {
                        // Extract the value from the result
                        var messageResult = resultTask.Result.Body;
                        // Set the text block with the result
                        OutTextBlock.Text = messageResult;
                    }
                    else
                    {
                        StatusBorder.Background    = new SolidColorBrush(Colors.Orange);
                        StatusTextBlock.Foreground = new SolidColorBrush(Colors.Black);
                        OutTextBlock.Text          = "Nothing returned!";
                    }
                }
            }
            catch (Exception ex)
            {
                // Display the exception message
                StatusTextBlock.Text    = ex.Message;
                StatusBorder.Background = new SolidColorBrush(Colors.Red);
            }
            finally
            {
                progressBar.IsIndeterminate = false;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Submit value to REST API
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            //new REST API Client  object
            WeatherServiceHW04 clientSdk = new WeatherServiceHW04();

            DateTime dateTime;
            decimal? value;
            string   date = DateTime.Parse(valueDate.Date.ToString()).ToString("MM/dd/yyyy");
            string   time = DateTime.Parse(valueTime.Time.ToString()).ToString("h:mm tt");
            //combine date and time from datepicker and timepicker
            var dateTimeString = date + " " + time;

            dateTime = DateTime.Parse(dateTimeString);

            //POST temperature
            if (TemperatureRadioButton.IsChecked != null && (bool)TemperatureRadioButton.IsChecked)
            {
                //if value is in Celsius convert it to Fahrenheit
                if ((string)DegreeType.SelectedValue == "Celsius")
                {
                    value = (decimal?)(9.0 / 5.0 * (double)(decimal.Parse(Value.Text))) + 32;
                }
                else
                {
                    value = Decimal.Parse(Value.Text);
                }

                Temperature temp = new Temperature()
                {
                    Degree        = value.ToString(),
                    RecorDateTime = dateTime
                };

                StatusTextBlock.Text       = "POST Request Made, waiting for response...";
                StatusTextBlock.Foreground = new SolidColorBrush(Colors.White);
                StatusBorder.Background    = new SolidColorBrush(Colors.Blue);
                // Let the user know something happening
                progressBar.IsIndeterminate = true;

                try
                {
                    Task <HttpOperationResponse <Temperature> > resultTask =
                        clientSdk.Temperatures.PostTemperatureByTemperatureWithOperationResponseAsync(temp);

                    // Wait until task completes
                    if (resultTask != null)
                    {
                        try
                        {
                            resultTask.Wait();
                        }
                        catch (AggregateException ex)
                        {
                            StatusTextBlock.Text    = ex.Message;
                            StatusBorder.Background = new SolidColorBrush(Colors.Red);
                        }
                        StatusBorder.Background     = new SolidColorBrush(Colors.Green);
                        StatusTextBlock.Text        = "Request completed!";
                        progressBar.IsIndeterminate = false;
                    }
                }
                catch (HttpOperationException ex)
                {
                    // Display the exception message
                    StatusTextBlock.Text    = ex.Message;
                    StatusBorder.Background = new SolidColorBrush(Colors.Red);
                }
                finally
                {
                    progressBar.IsIndeterminate = false;
                }
            }

            //POST Humidity
            if (HumidityRadioButton.IsChecked != null && (bool)HumidityRadioButton.IsChecked)
            {
                value = Decimal.Parse(Value.Text);
                Humidity humi = new Humidity()
                {
                    Percentage    = value.ToString(),
                    RecorDateTime = dateTime
                };
                try
                {
                    Task <HttpOperationResponse <Humidity> > resultTask =
                        clientSdk.Humidities.PostHumidityByHumidityWithOperationResponseAsync(humi);
                    // Wait until task completes
                    if (resultTask != null)
                    {
                        try
                        {
                            resultTask.Wait();
                        }
                        catch (AggregateException ex)
                        {
                            StatusTextBlock.Text    = ex.Message;
                            StatusBorder.Background = new SolidColorBrush(Colors.Red);
                        }
                        StatusBorder.Background     = new SolidColorBrush(Colors.Green);
                        StatusTextBlock.Text        = "Request completed!";
                        progressBar.IsIndeterminate = false;
                    }
                }
                catch (HttpOperationException ex)
                {
                    // Display the exception message
                    StatusTextBlock.Text    = ex.Message;
                    StatusBorder.Background = new SolidColorBrush(Colors.Red);
                }
                finally
                {
                    progressBar.IsIndeterminate = false;
                }
            }

            //POST Pressure
            if (PressureRadioButton.IsChecked != null && (bool)PressureRadioButton.IsChecked)
            {
                value = Decimal.Parse(Value.Text);
                Pressure pres = new Pressure()
                {
                    Millibar      = value.ToString(),
                    RecorDateTime = dateTime
                };
                try
                {
                    Task <HttpOperationResponse <Pressure> > resultTask = clientSdk.Pressures.PostPressureByPressureWithOperationResponseAsync(pres);
                    // Wait until task completes
                    if (resultTask != null)
                    {
                        try
                        {
                            resultTask.Wait();
                        }
                        catch (AggregateException ex)
                        {
                            StatusTextBlock.Text    = ex.Message;
                            StatusBorder.Background = new SolidColorBrush(Colors.Red);
                        }
                        StatusBorder.Background     = new SolidColorBrush(Colors.Green);
                        StatusTextBlock.Text        = "Request completed!";
                        progressBar.IsIndeterminate = false;
                    }
                }
                catch (HttpOperationException ex)
                {
                    // Display the exception message
                    StatusTextBlock.Text    = ex.Message;
                    StatusBorder.Background = new SolidColorBrush(Colors.Red);
                }
                finally
                {
                    progressBar.IsIndeterminate = false;
                }
            }
        }