Example #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    var forecast = await _weatherApiClient.GetWeatherForecastAsync(stoppingToken);

                    if (forecast is object)
                    {
                        var currentWeather = new CurrentWeatherResult {
                            Description = forecast.Weather.Description
                        };

                        var cacheKey = $"current_weather_{DateTime.UtcNow:yyyy_MM_dd}";

                        _logger.LogInformation("Updating weather in cache.");

                        await _cache.SetAsync(cacheKey, currentWeather, _minutesToCache);
                    }

                    await Task.Delay(TimeSpan.FromSeconds(_refreshIntervalInSeconds), stoppingToken);
                }
            }
            catch (OperationCanceledException)
            {
                // swallow
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "An exception occurred when populating the cache");
            }
        }
Example #2
0
        public Task <CurrentWeatherResult> GetCurrentWeatherAsync()
        {
            var result = new CurrentWeatherResult
            {
                Description = "The weather forecast feature is not currently available"
            };

            return(Task.FromResult(result));
        }
Example #3
0
        public async Task <CurrentWeatherResult> GetCurrentWeatherAsync()
        {
            var currentWeather = await _weatherApiClient.GetWeatherForecastAsync();

            var result = new CurrentWeatherResult
            {
                Description = currentWeather.Weather.Description
            };

            return(result);
        }
Example #4
0
        public static SingleResult <CurrentWeatherResult> GetWeatherCurrent(JObject response)
        {
            var error = GetServerErrorFromResponse(response);

            if (!String.IsNullOrEmpty(error))
            {
                return(new SingleResult <CurrentWeatherResult>(null, false, error));
            }

            var weatherCurrent = new CurrentWeatherResult();

            if (response["sys"] != null)
            {
                weatherCurrent.Country = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["sys"]["country"])));
                weatherCurrent.Sunrise = TimestampToDateTime(Convert.ToInt64(Convert.ToString(response["sys"]["sunrise"])), true);
                weatherCurrent.Sunset  = TimestampToDateTime(Convert.ToInt64(Convert.ToString(response["sys"]["sunset"])), true);
            }

            if (response["weather"] != null)
            {
                weatherCurrent.Title       = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["main"])));
                weatherCurrent.Description = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["description"])));
                weatherCurrent.Icon        = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["icon"])));
            }

            if (response["main"] != null)
            {
                weatherCurrent.Temp    = Convert.ToDouble(response["main"]["temp"].Value <double>());
                weatherCurrent.TempMax = Convert.ToDouble(response["main"]["temp_max"].Value <double>());
                try
                {
                    weatherCurrent.TempMin = Convert.ToDouble(response["main"]["temp_min"].Value <double>());
                }
                catch
                {
                    weatherCurrent.TempMin = 0;
                }
                weatherCurrent.Humidity = Convert.ToDouble(response["main"]["humidity"].Value <double>());
            }

            if (response["wind"] != null)
            {
                weatherCurrent.WindSpeed = Convert.ToDouble(response["wind"]["speed"].Value <double>());
            }

            weatherCurrent.Date   = DateTime.UtcNow;
            weatherCurrent.City   = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["name"])));
            weatherCurrent.CityId = Convert.ToInt32(response["id"].Value <Int32>());

            return(new SingleResult <CurrentWeatherResult>(weatherCurrent, true, TimeHelper.MessageSuccess));
        }
Example #5
0
        public static void UpdateTile(PlaceInfo _PlaceInfo, CurrentWeatherResult _CurrentWeatherResult, DailyForecastResult _DailyForecastResult, string _CoverImage)
        {
            try
            {
                var _CityData = new CityData()
                {
                    PlaceInfo = _PlaceInfo, Current = _CurrentWeatherResult, DailyForecast = _DailyForecastResult, CoverImage = _CoverImage
                };
                var tileContent = SetTileContent(_CityData);

                var tileNotif = new TileNotification(tileContent.GetXml());
                TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotif);
            }
            catch (Exception) { }
        }
Example #6
0
        public static SingleResult<CurrentWeatherResult> GetWeatherCurrent(JObject response)
        {
            var error = GetServerErrorFromResponse(response);
            if (!String.IsNullOrEmpty(error))
                return new SingleResult<CurrentWeatherResult>(null, false, error);

            var weatherCurrent = new CurrentWeatherResult();

            if (response["sys"] != null)
            {
                weatherCurrent.Country = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["sys"]["country"])));
                weatherCurrent.Sunrise = TimestampToDateTime(Convert.ToInt64(Convert.ToString(response["sys"]["sunrise"])), true);
                weatherCurrent.Sunset = TimestampToDateTime(Convert.ToInt64(Convert.ToString(response["sys"]["sunset"])), true);
            }

            if (response["weather"] != null)
            {
                weatherCurrent.Title = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["main"])));
                weatherCurrent.Description = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["description"])));
                weatherCurrent.Icon = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["icon"])));
            }

            if (response["main"] != null)
            {
                weatherCurrent.Temp = Convert.ToDouble(response["main"]["temp"].Value<double>());
                weatherCurrent.TempMax = Convert.ToDouble(response["main"]["temp_max"].Value<double>());
                try
                {
                    weatherCurrent.TempMin = Convert.ToDouble(response["main"]["temp_min"].Value<double>());
                }
                catch
                {
                    weatherCurrent.TempMin = 0;
                }
                weatherCurrent.Humidity = Convert.ToDouble(response["main"]["humidity"].Value<double>());
            }

            if (response["wind"] != null)
            {
                weatherCurrent.WindSpeed = Convert.ToDouble(response["wind"]["speed"].Value<double>());
            }

            weatherCurrent.Date = DateTime.UtcNow;
            weatherCurrent.City = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["name"])));
            weatherCurrent.CityId = Convert.ToInt32(response["id"].Value<Int32>());

            return new SingleResult<CurrentWeatherResult>(weatherCurrent, true, TimeHelper.MessageSuccess);
        }
Example #7
0
        public static async Task <bool> UpdateCustomTile(PlaceInfo _PlaceInfo, CurrentWeatherResult _CurrentWeatherResult, DailyForecastResult _DailyForecastResult, string _CoverImage)
        {
            var _CityData = new CityData()
            {
                PlaceInfo = _PlaceInfo, Current = _CurrentWeatherResult, DailyForecast = _DailyForecastResult, CoverImage = _CoverImage
            };

            if (!SecondaryTile.Exists(_PlaceInfo.PlaceId))
            {
                string tileId      = _CityData.PlaceInfo.PlaceId;
                string displayName = "Wecond";
                string arguments   = "PlaceId=" + _CityData.PlaceInfo.PlaceId + "&Latitude=" + _CityData.PlaceInfo.Latitude + "&Longitude=" + _CityData.PlaceInfo.Longitude + "&DisplayName=" + _CityData.PlaceInfo.DisplayName;

                SecondaryTile tile = new SecondaryTile(
                    tileId,
                    displayName,
                    arguments,
                    new Uri("ms-appx:///Assets/Square150x150Logo.png"),
                    TileSize.Default);

                tile.VisualElements.Wide310x150Logo   = new Uri("ms-appx:///Assets/Wide310x150Logo.png");
                tile.VisualElements.Square310x310Logo = new Uri("ms-appx:///Assets/Square310x310Logo.png");
                tile.VisualElements.Square71x71Logo   = new Uri("ms-appx:///Assets/Square71x71Logo.png");
                tile.VisualElements.Square44x44Logo   = new Uri("ms-appx:///Assets/Square44x44Logo.png");

                tile.VisualElements.ShowNameOnSquare150x150Logo = true;
                tile.VisualElements.ShowNameOnWide310x150Logo   = true;
                tile.VisualElements.ShowNameOnSquare310x310Logo = true;

                bool isPinned = await tile.RequestCreateAsync();

                if (isPinned != true)
                {
                    return(false);
                }
            }

            var tileContent = SetTileContent(_CityData);

            Debug.Write(tileContent.ToString());
            var tileNotif = new TileNotification(tileContent.GetXml());

            TileUpdateManager.CreateTileUpdaterForSecondaryTile(_CityData.PlaceInfo.PlaceId).Update(tileNotif);
            return(true);
        }
        public void ReturnsExpectedViewModel_WhenWeatherIsRain()
        {
            var mockWeatherForecaster = new Mock <IWeatherForecaster>();
            var temp = new CurrentWeatherResult
            {
                Description = "Rain"
            };

            mockWeatherForecaster.Setup(w => w.GetCurrentWeatherAsync()).Returns(
                Task.FromResult(temp)
                );
            var sut = new HomeController(mockWeatherForecaster.Object);

            var result = sut.Index();

            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <HomeViewModel>(viewResult.ViewData.Model);

            Assert.Contains("We're sorry but it's raining here.", model.WeatherDescription);
        }
Example #9
0
        public static async Task <CurrentWeatherResult> GetCurrentData(string url)
        {
            CurrentWeatherResult result = null;

            try
            {
                var client   = new HttpClient();
                var response = await client.GetStringAsync(string.Format(url));

                result = JsonConvert.DeserializeObject <CurrentWeatherResult>(response);
            }
            catch (Exception)
            {
                result = new CurrentWeatherResult()
                {
                    cod = 404
                };
            }
            return(result);
        }
Example #10
0
        public static SingleResult <CurrentWeatherResult> GetWeatherCurrent(JObject response)
        {
            string error = getServerErrorFromResponse(response);

            if (!string.IsNullOrEmpty(error))
            {
                return(new SingleResult <CurrentWeatherResult>(null, false, error));
            }

            CurrentWeatherResult weatherCurrent = new CurrentWeatherResult();

            if (response["sys"] != null)
            {
                weatherCurrent.Country = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["sys"]["country"])));
                weatherCurrent.Sunrise = DateTimeOffset.FromUnixTimeSeconds((long)Convert.ToDouble(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["sys"]["sunrise"]))))).LocalDateTime;
                weatherCurrent.Sunset  = DateTimeOffset.FromUnixTimeSeconds((long)Convert.ToDouble(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["sys"]["sunset"]))))).LocalDateTime;
            }

            if (response["weather"] != null)
            {
                weatherCurrent.Title       = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["main"])));
                weatherCurrent.Description = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["description"])));
                weatherCurrent.Icon        = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["weather"][0]["icon"])));
            }

            if (response["main"] != null)
            {
                weatherCurrent.Temp                = Math.Round(Convert.ToDouble(response["main"]["temp"].Value <double>()), 1);
                weatherCurrent.TempMax             = Math.Round(Convert.ToDouble(response["main"]["temp_max"].Value <double>()), 1);
                weatherCurrent.TempMin             = Math.Round(Convert.ToDouble(response["main"]["temp_min"].Value <double>()), 1);
                weatherCurrent.Humidity            = Convert.ToDouble(response["main"]["humidity"].Value <double>());
                weatherCurrent.Pressure            = Convert.ToDouble(response["main"]["pressure"].Value <double>());
                weatherCurrent.SeaLevelPressure    = Convert.ToDouble(response["main"]["sea_level"]?.Value <double>());
                weatherCurrent.GroundLevelPressure = Convert.ToDouble(response["main"]["grnd_level"]?.Value <double>());
            }

            if (response["wind"] != null)
            {
                weatherCurrent.WindSpeed  = Math.Round(Convert.ToDouble(response["wind"]["speed"].Value <double>()), 1);
                weatherCurrent.WindDegree = Convert.ToDouble(response["wind"]["deg"]?.Value <double>());
            }

            if (response["clouds"] != null)
            {
                weatherCurrent.Cloudinesss = Convert.ToInt32(response["clouds"]["all"].Value <int>());
            }

            try
            {
                if (response["rain"]?["3h"] != null)
                {
                    weatherCurrent.Rain = Math.Round(Convert.ToDouble(response["rain"]["3h"].Value <double>()), 2);
                }

                if (response["snow"]?["3h"] != null)
                {
                    weatherCurrent.Snow = Math.Round(Convert.ToDouble(response["snow"]["3h"].Value <double>()), 2);
                }
            }
            catch (Exception)
            {
                weatherCurrent.Rain = 0;
                weatherCurrent.Snow = 0;
            }

            weatherCurrent.Date   = DateTime.UtcNow;
            weatherCurrent.City   = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Convert.ToString(response["name"])));
            weatherCurrent.CityId = Convert.ToInt32(response["id"].Value <int>());

            return(new SingleResult <CurrentWeatherResult>(weatherCurrent, true, TimeHelper.MessageSuccess));
        }