Ejemplo n.º 1
0
        // == CRUD operations section ==

        public void AddCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var newRecord = new Table_1()
            {
                city_name   = city_nameTextBox.Text,
                city_id     = city_idTextBox.Text,
                temperature = double.Parse(temperatureTextBox.Text),
            };

            context.Table_1.Add(newRecord);
            context.SaveChanges();
            tabViewSource.View.Refresh();
        }
Ejemplo n.º 2
0
        // == Downloading data from the OpenWeatherAPI section ==

        private async void GetApiData()
        {
            Random random     = new Random();
            string apiKey     = "747fe0e760b5276cc648effd30d3a2a8";
            string apiBaseUrl = "https://api.openweathermap.org/data/2.5/weather";

            await Task.Run(() =>
            {
                using (var client = new WebClient())
                {
                    while (true)
                    {
                        double lat = random.Next(35, 71);
                        double lon = random.Next(-9, 68);

                        string apiCall = apiBaseUrl + "?lat=" + lat + "&lon=" + lon + "&apikey=" + apiKey +
                                         "&mode=json&units=metric";

                        string jsonString = client.DownloadString(apiCall);
                        var jsonObject    = JsonConvert.DeserializeObject <JObject>(jsonString);
                        string j_name     = jsonObject["name"].ToString();
                        string j_id       = jsonObject["id"].ToString();
                        string j_temp     = jsonObject["main"]["temp"].ToString();

                        if (j_name == "")
                        {
                            j_name = "Ocean or See";
                        }

                        var newRecord = new Table_1()
                        {
                            city_name   = j_name,
                            city_id     = j_id,
                            temperature = double.Parse(j_temp),
                        };

                        Dispatcher.Invoke(() =>
                        {
                            context.Table_1.Add(newRecord);
                            context.SaveChanges();
                            tabViewSource.View.Refresh();
                        });

                        Thread.Sleep(5000);
                    }
                }
            });
        }