private WeatherDataPoint CreateWeatherDataPoint(Forecast item, CancellationToken token) { if (item == null) { throw new ArgumentNullException(nameof(item)); } var itemCode = item.Code; var date = ConvertStringToDateTime(item.Date, DateTimeUniversalStyles); if (!date.HasValue) { throw new FormatException("Can't define forecast date."); } var astronomy = GetSunInfo(date, token); var result = new WeatherDataPoint { Date = date, Astronomy = astronomy, Temperature = (item.Low + item.High) / 2.0, //HARDCODE: MaxTemperature = item.High, MinTemperature = item.Low, Condition = item.Text, WeatherCode = ConvertToWeatherCode(itemCode), Icon = string.Format(IconFormat, itemCode) }; if (result.WeatherCode == WeatherCodes.Error) { result.WeatherUnrecognizedCode = itemCode.HasValue ? itemCode.ToString() : null; } return(result); }
private WeatherDataPoint CreateWeatherDataPoint(WeatherResponseBase item, CancellationToken token) { if (item == null) { throw new ArgumentNullException(nameof(item)); } DateTime?date = null; switch (item) { case WeatherResponse weatherResponse: date = ConvertUnixTimeToUtcDateTime(weatherResponse.DateUnixTime); //sunrise = ConvertUnixTimeToUtcDateTime(weatherResponse.WeatherSys?.SunriseUnixTime); //sunset = ConvertUnixTimeToUtcDateTime(weatherResponse.WeatherSys?.SunsetUnixTime); break; case WeatherForecastItem weatherForecastItem: date = ConvertStringToDateTime(weatherForecastItem.Date, DateTimeUniversalStyles); break; } if (!date.HasValue) { throw new FormatException("Can't define forecast date."); } var astronomy = GetSunInfo(date, token); var weatherInfo = item.WeatherInfos?.FirstOrDefault(); var conditionId = weatherInfo?.ConditionId; var result = new WeatherDataPoint { Date = date, Astronomy = astronomy, Temperature = item.WeatherMain?.Temperature, MaxTemperature = item.WeatherMain?.MaxTemperature, MinTemperature = item.WeatherMain?.MinTemperature, Pressure = item.WeatherMain?.Pressure, Humidity = item.WeatherMain?.Humidity / 100, Condition = weatherInfo?.Description, Icon = string.Format(IconFormat, weatherInfo?.Icon), WindDirection = item.Wind?.DirectionDegrees, WindSpeed = item.Wind?.Speed, WeatherCode = ConvertToWeatherCode(conditionId) }; if (result.WeatherCode == WeatherCodes.Error) { result.WeatherUnrecognizedCode = conditionId.HasValue ? conditionId.ToString() : null; } return(result); }
private List <WeatherDataPoint> getListOfPoints(RootObject weatherRootObject) { List <WeatherDataPoint> _ListOfPoints = new List <WeatherDataPoint>(); foreach (var item in weatherRootObject.list) { WeatherDataPoint _tempWeatherPoint = new WeatherDataPoint() { TempMax = getTempMax(item.main.temp_max), TempMin = getTempMin(item.main.temp_min), Date = getDate(item.dt_txt) }; _ListOfPoints.Add(_tempWeatherPoint); } return(_ListOfPoints); }
private WeatherDataPoint CreateWeatherDataPoint(DataPoint item, CancellationToken token) { if (item == null) { throw new ArgumentNullException(nameof(item)); } var date = ConvertUnixTimeToUtcDateTime(item.Time); if (!date.HasValue) { throw new FormatException("Can't define forecast date."); } //var sunrise = ConvertUnixTimeToUtcDateTime(item.SunriseTime); //var sunset = ConvertUnixTimeToUtcDateTime(item.SunsetTime); var astronomy = GetSunInfo(date, token); var itemIcon = item.Icon; var result = new WeatherDataPoint { Date = date, Astronomy = astronomy, ApparentTemperature = item.ApparentTemperature, Temperature = item.Temperature, MaxTemperature = item.TemperatureMax, MinTemperature = item.TemperatureMin, Pressure = item.Pressure, Humidity = item.Humidity, Condition = item.Summary, Icon = itemIcon, WeatherCode = ConvertToWeatherCode(itemIcon), Visibility = item.Visibility, WindDirection = item.WindBearing, WindSpeed = item.WindSpeed, DewPoint = item.DewPoint }; if (result.WeatherCode == WeatherCodes.Error) { result.WeatherUnrecognizedCode = itemIcon; } return(result); }
protected override WeatherForecast OnProcessResponse(object responseResult, CancellationToken token) { if (IfCancellationRequested(token)) { return(null); } if (responseResult == null) { return(null); } var json = responseResult as string; if (string.IsNullOrEmpty(json) || string.IsNullOrWhiteSpace(json)) { return(null); } var forecast = JsonConvert.DeserializeObject <WeatherForecastResponse>(json); if (forecast?.City == null || forecast.Items == null) { return(null); } if (!Latitude.HasValue && !Longitude.HasValue && forecast.City?.Coord?.Latitude != null && forecast.City?.Coord?.Longitude != null) { Latitude = forecast.City.Coord.Latitude; Longitude = forecast.City.Coord.Longitude; } var currentlyResponse = GetCurrently(token); if (currentlyResponse == null) { return(null); } // weather forecast var weather = new WeatherForecast(providerId: ProviderId, latitude: Latitude, longitude: Longitude, units: ResponseUnits, languageCode: OptionalParameters?.LanguageCode, link: string.Format("http://openweathermap.org/city/{0}", forecast.City.Id), hasIcons: true) { Currently = CreateWeatherDataPoint(currentlyResponse, token), Hourly = CreateItems(forecast.Items.Cast <WeatherResponseBase>().ToArray(), token) }; if (IfCancellationRequested(token)) { return(null); } // daily if (weather.Hourly != null && weather.Hourly.Length > 0) { var daily = new List <WeatherDataPoint>(); // ReSharper disable once PossibleInvalidOperationException var hourlyGroup = weather.Hourly.Where(p => p.Date.HasValue).GroupBy(key => key.Date.Value.Date).ToArray(); foreach (var group in hourlyGroup) { if (IfCancellationRequested(token)) { return(null); } var date = group.Key; var astronomy = group.FirstOrDefault()?.Astronomy; var point = new WeatherDataPoint { Date = date, Astronomy = astronomy, MinTemperature = group.Min(p => p.MinTemperature), MaxTemperature = group.Max(p => p.MaxTemperature) }; daily.Add(point); } if (daily.Count > 0) { weather.Daily = daily.ToArray(); } } // error if (!ValidateForecast(weather)) { return(null); } if (IfCancellationRequested(token)) { return(null); } weather.UpdatedDate = DateTime.UtcNow; return(weather); }
protected virtual void UpdateWeatherPart(Context context, RemoteViews view, AppWidgetSettings settings, bool isDemoMode) { var weather = settings.Weather; var utcNow = DateTime.UtcNow; if (isDemoMode && weather == null) { var weatherDataPoint = new WeatherDataPoint { Date = utcNow, Temperature = 0, MinTemperature = -1, MaxTemperature = 1, WeatherCode = WeatherCodes.ClearSky, Condition = context.GetString(Resource.String.DemoCondition) }; weather = new WeatherForecast(providerId: settings.WeatherProviderId, latitude: null, longitude: null, units: AppSettings.Default.Units, languageCode: AppSettings.Default.Language, link: null, hasIcons: false) { Currently = weatherDataPoint, Daily = new[] { weatherDataPoint } }; } if (weather?.MaxPublishedDate == null) { return; } var actualUtcDate = utcNow > weather.MaxPublishedDate ? weather.MaxPublishedDate.Value : utcNow; var currently = weather.FindActualCurrentlyAsync(actualUtcDate).Result; if (currently == null) { return; } var temperature = string.Empty; using (var weatherTools = new WeatherTools { ProviderUnits = weather.Units, }) { var degree = weatherTools.DegreeString; if (currently.Temperature.HasValue) { temperature = weatherTools.ConvertTemperatureToString(currently.Temperature.Value, "{0:f0}{1}", degree); } SetTextFormatted(view, Resource.Id.txtTemp, temperature, weatherTools.IsTemperatureAlerted(currently.Temperature)); var minTempText = string.Empty; var maxTempText = string.Empty; var visibility = ViewStates.Gone; var minTemp = weatherTools .CalculateMinTemperatureAsync(actualUtcDate, weather, currently.MinTemperature).Result; var maxTemp = weatherTools .CalculateMaxTemperatureAsync(actualUtcDate, weather, currently.MaxTemperature).Result; if (minTemp.HasValue && maxTemp.HasValue) { minTempText = weatherTools.ConvertTemperatureToString(minTemp.Value, "{0:f0}{1}", degree); maxTempText = weatherTools.ConvertTemperatureToString(maxTemp.Value, "{0:f0}{1}", degree); visibility = ViewStates.Visible; } SetTextFormatted(view, Resource.Id.txtLow, minTempText, weatherTools.IsTemperatureAlerted(minTemp)); view.SetViewVisibility(Resource.Id.txtLowIndicator, visibility); SetTextFormatted(view, Resource.Id.txtHigh, maxTempText, weatherTools.IsTemperatureAlerted(maxTemp)); view.SetViewVisibility(Resource.Id.txtHighIndicator, visibility); var conditionIconId = weatherTools.GetConditionIconId(WidgetTypes.AppWidget, settings.WidgetIconStyle, currently, true, true); view.SetImageViewResource(Resource.Id.imgCondition, conditionIconId); //"Небольшой снегопад " SetTextFormatted(view, Resource.Id.txtCondition, (currently.Condition ?? string.Empty).Trim().ToCapital(), weatherTools.IsConditionExtreme(currently.WeatherCode)); } var locality = (settings.LocationAddress?.GetDisplayLocality() ?? string.Empty).Trim(); if (string.IsNullOrEmpty(locality) && isDemoMode) { locality = context.GetString(settings.UseTrackCurrentLocation ? Resource.String.CurrentLocationEmpty : Resource.String.DemoLocality); } view.SetTextViewText(Resource.Id.txtLocation, locality); }
public void WeatherDataPointTest() { WeatherDataPoint t = new WeatherDataPoint(1, 80, 50); Approvals.Verify(t.ToString()); }
protected override WeatherForecast OnProcessResponse(object responseResult, CancellationToken token) { if (IfCancellationRequested(token)) { return(null); } if (responseResult == null) { return(null); } var json = responseResult as string; if (string.IsNullOrEmpty(json) || string.IsNullOrWhiteSpace(json)) { return(null); } var forecastResponse = JsonConvert.DeserializeObject <WeatherForecastResponse>(json); if (forecastResponse?.Query?.Results?.Channel?.Item?.Forecast == null) { //if (forecastResponse.Query?.CreatedDate != null) // throw new HttpRequestException(string.Format("Post empty forecast request. Weather provider is {0}.", ProviderId)); return(null); } var forecast = forecastResponse.Query.Results.Channel; var forecastLink = forecast.Link; if (!string.IsNullOrEmpty(forecastLink)) { var index = forecastLink.LastIndexOf("http", StringComparison.OrdinalIgnoreCase); //HARDCODE: if (index > 0) { forecastLink = forecastLink.Substring(index); } } if (IfCancellationRequested(token)) { return(null); } if (!Latitude.HasValue && !Longitude.HasValue && forecast?.Item?.Latitude != null && forecast.Item?.Longitude != null) { Latitude = forecast.Item.Latitude; Longitude = forecast.Item.Longitude; } // weather forecast if (forecast.Item == null) { return(null); } var weather = new WeatherForecast(providerId: ProviderId, units: ResponseUnits, latitude: Latitude, longitude: Longitude, languageCode: OptionalParameters?.LanguageCode, link: forecastLink, hasIcons: true); // Currently var currentlyDateOffset = ConvertRfc822ToDateTimeOffset(forecast.Item.Condition?.Date); if (currentlyDateOffset.HasValue) { var currentlyDateTime = currentlyDateOffset.Value.UtcDateTime; // sunrise, sunset //var currentlySunrise = GetUtcDateTime(forecast.Astronomy.Sunrise, currentlyDateOffset); //var currentlySunset = GetUtcDateTime(forecast.Astronomy.Sunset, currentlyDateOffset); var astronomy = GetSunInfo(currentlyDateTime, token); var temperature = forecast.Item.Condition?.Temp; var conditionCode = forecast.Item.Condition?.Code; var currently = new WeatherDataPoint { Date = currentlyDateTime, Astronomy = astronomy, ApparentTemperature = forecast.Wind?.Chill, Temperature = temperature, Pressure = forecast.Atmosphere?.Pressure, Humidity = forecast.Atmosphere?.Humidity / 100, Icon = string.Format(IconFormat, conditionCode), Visibility = forecast.Atmosphere?.Visibility, WindDirection = forecast.Wind?.Direction, WindSpeed = forecast.Wind?.Speed, Condition = forecast.Item.Condition?.Text, WeatherCode = ConvertToWeatherCode(conditionCode) }; if (currently.WeatherCode == WeatherCodes.Error) { currently.WeatherUnrecognizedCode = conditionCode.HasValue ? conditionCode.ToString() : null; } weather.Currently = currently; } else { throw new FormatException("Can't define currently forecast date."); } if (IfCancellationRequested(token)) { return(null); } // daily var daily = CreateItems(forecast.Item.Forecast, token); weather.Daily = daily; // error if (!ValidateForecast(weather)) { return(null); } // validate weather forecast properties var currentlyDate = weather.Currently.Date.Value.Date; var currentDaily = weather.Daily.FirstOrDefault(p => p.Date.HasValue && p.Date.Value.Date == currentlyDate); if (currentDaily != null) { if (!currentDaily.WindSpeed.HasValue) { currentDaily.WindSpeed = weather.Currently.WindSpeed; } if (!currentDaily.WindDirection.HasValue) { currentDaily.WindDirection = weather.Currently.WindDirection; } } if (IfCancellationRequested(token)) { return(null); } weather.UpdatedDate = DateTime.UtcNow; return(weather); }