Skip to content

Commit

Permalink
Merge pull request #51 from zxbmmmmmmmmm/feat/historical-weather
Browse files Browse the repository at this point in the history
历史天气支持
  • Loading branch information
zxbmmmmmmmmm authored Jun 10, 2024
2 parents a029f8d + dc11423 commit eff2b04
Show file tree
Hide file tree
Showing 31 changed files with 927 additions and 60 deletions.
13 changes: 13 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"messagepack.generator": {
"version": "2.5.140",
"commands": [
"mpc"
],
"rollForward": false
}
}
}
8 changes: 8 additions & 0 deletions .idea/.idea.FluentWeather/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.FluentWeather/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions .idea/.idea.FluentWeather/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FluentWeather.Abstraction.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace FluentWeather.Abstraction.Interfaces.WeatherProvider;

public interface IHistoricalWeatherProvider
{
public Task<List<WeatherDailyBase>> GetHistoricalDailyWeather(double lon, double lat, DateTime startTime, DateTime endTime);
}
29 changes: 29 additions & 0 deletions FluentWeather.Abstraction/Models/HistoricalDailyWeatherBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace FluentWeather.Abstraction.Models;

public class HistoricalDailyWeatherBase
{
public DateTime Date { get; set; }

public WeatherCode Weather { get; set; }

public int HistoricalMaxTemperature { get; set; }

public DateTime HistoricalMaxTemperatureDate { get; set; }

public int HistoricalMinTemperature { get; set; }

public DateTime HistoricalMinTemperatureDate { get; set; }

public int AverageMaxTemperature { get; set; }

public int AverageMinTemperature { get; set; }

public double? AveragePrecipitation { get; set; }

public double? MaxPrecipitation { get; set; }

public DateTime? MaxPrecipitationDate { get; set; }
public double? AveragePrecipitationHours { get; set; }
}
8 changes: 7 additions & 1 deletion FluentWeather.Abstraction/Models/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return base.GetHashCode();
unchecked
{
int hash = 17;
hash = hash * 23 + Latitude.GetHashCode();
hash = hash * 23 + Longitude.GetHashCode();
return hash;
}
}
}
6 changes: 4 additions & 2 deletions FluentWeather.Abstraction/Models/WeatherDailyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace FluentWeather.Abstraction.Models;

public class WeatherDailyBase : WeatherBase, IWeatherNight, ITemperatureRange, IWind, ITime, IAstronomic,ICloudAmount,IVisibility,IPressure,IHumidity
{
public int MaxTemperature { get; set; }
public int MinTemperature { get; set; }
public int MaxTemperature { get; set; }
public int MinTemperature { get; set; }
public DateTime Time { get; set; }
public WeatherBase? WeatherNight { get; set; }

Expand All @@ -30,4 +30,6 @@ public string WindDirectionDescription
public int? Visibility { get; set; }
public int? Pressure { get; set; }
public int? Humidity { get; set ; }
public double? Precipitation { get; set; }
public double? PrecipitationHours { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OpenMeteoApi" Version="1.1.0" />
<PackageReference Include="OpenMeteoApi" Version="1.2.0" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public static OpenMeteoWeatherNow MapToOpenMeteoWeatherNow(this CurrentWeather i
return new OpenMeteoWeatherNow
{
//Description = WeatherCodeHelper.GetWeatherDescription(item.WeatherCode!.Value),
WeatherType = WeatherCodeHelper.GetWeatherType(item.WeatherCode.Value),
WeatherType = WeatherCodeHelper.GetWeatherType(item.WeatherCode!.Value),
WindDirection = UnitConverter.GetWindDirectionFromAngle(item.WindDirection10m!.Value),
WindScale = UnitConverter.GetWindScaleFromKM((int)item.WindSpeed10m!).ToString(),
WindScale = UnitConverter.GetWindScaleFromKM((int)item.WindSpeed10m!.Value).ToString(),
WindSpeed = (int)item.WindSpeed10m,
ApparentTemperature = (int)item.ApparentTemperature!.Value,
Humidity = item.RelativeHumidity2m!.Value,
Temperature = (int)item.Temperature2m!,
Pressure = (int)item.SurfacePressure!,
Temperature = (int)Math.Round(item.Temperature2m!.Value),
Pressure = item.SurfacePressure is null ? null : (int)Math.Round(item.SurfacePressure.Value),
//Visibility = int.Parse(item.),
CloudAmount = item.CloudCover

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static OpenMeteoDailyForecast MapToOpenMeteoDailyForecast(this DailyForec
//Description = WeatherCodeHelper.GetWeatherDescription(item.WeatherCode!.Value),
WeatherType = WeatherCodeHelper.GetWeatherType(item.WeatherCode!.Value),
WindDirection = UnitConverter.GetWindDirectionFromAngle(item.WindDirection10mDominant!.Value),
WindSpeed = (int)item.WindSpeed10mMax!,
WindScale = UnitConverter.GetWindScaleFromKM((int)item.WindSpeed10mMax!.Value).ToString(),
WindSpeed = (int)Math.Round(item.WindSpeed10mMax!.Value),
WindScale = UnitConverter.GetWindScaleFromKM((int)Math.Round(item.WindSpeed10mMax!.Value)).ToString(),
//Humidity = int.Parse(item.),
MaxTemperature = (int)item.Temperature2mMax!,
MinTemperature = (int)item.Temperature2mMin!,
MaxTemperature = (int)Math.Round(item.Temperature2mMax!.Value),
MinTemperature = (int)Math.Round(item.Temperature2mMin!.Value),
//Pressure = int.Parse(item),
Time = item.Time!.Value,
//Visibility = int.Parse(item.),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public static WeatherHourlyBase MapToHourlyForecast(this HourlyForecastItem item
WindDirection = UnitConverter.GetWindDirectionFromAngle(item.WindDirection10m!.Value),
//Description = WeatherCodeHelper.GetWeatherDescription(item.WeatherCode!.Value),
WindScale = UnitConverter.GetWindScaleFromKM((int)item.WindSpeed10m!.Value).ToString(),
WindSpeed = (int)item.WindSpeed10m!.Value,
WindSpeed = (int)Math.Round(item.WindSpeed10m!.Value),
Humidity = item.RelativeHumidity2m,
Pressure = (int?)item.SurfacePressure,
Temperature = (int)item.Temperature2m!,
Pressure = item.SurfacePressure is null ? null : (int)Math.Round(item.SurfacePressure.Value),
Temperature = (int)Math.Round(item.Temperature2m!.Value),
Time = item.Time!.Value,
PrecipitationProbability = item.PrecipitationProbability,
CloudAmount = item.CloudCover,
Visibility = (int?)item.Visibility/1000,
Visibility = item.Visibility is null ? null : (int)Math.Round(item.Visibility.Value/1000),
};
}
}
26 changes: 24 additions & 2 deletions FluentWeather.OpenMeteoProvider/OpenMeteoProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
using FluentWeather.OpenMeteoProvider.Models;
using OpenMeteoApi.Variables;
using System;
using System.Linq;

namespace FluentWeather.OpenMeteoProvider;

public sealed class OpenMeteoProvider : ProviderBase, ICurrentWeatherProvider, IAirConditionProvider, IDailyForecastProvider, IHourlyForecastProvider , IPrecipitationProvider
public sealed class OpenMeteoProvider : ProviderBase, ICurrentWeatherProvider, IAirConditionProvider, IDailyForecastProvider, IHourlyForecastProvider, IPrecipitationProvider, IHistoricalWeatherProvider
{
public override string Name => "OpenMeteo";
public override string Id => "open-meteo";
Expand Down Expand Up @@ -48,7 +49,7 @@ public async Task<List<WeatherDailyBase>> GetDailyForecasts(double lon, double l
public async Task<List<WeatherHourlyBase>> GetHourlyForecasts(double lon, double lat)
{
var result = await Client.GetHourlyForecasts(lat, lon);
return result.ConvertAll<WeatherHourlyBase>(p => p.MapToHourlyForecast());
return result.ConvertAll(p => p.MapToHourlyForecast());
}

public async Task<PrecipitationBase> GetPrecipitations(double lon, double lat)
Expand All @@ -60,4 +61,25 @@ public async Task<PrecipitationBase> GetPrecipitations(double lon, double lat)
};
return precip;
}

public async Task<List<WeatherDailyBase>> GetHistoricalDailyWeather(double lon, double lat, DateTime startTime, DateTime endTime)
{
var list = new List<WeatherDailyBase>();
var data = await Client.GetHistoricalWeatherData(lat, lon, startTime, endTime,
dailyVariables: [DailyVariables.WeatherCode,DailyVariables.Temperature2mMax,DailyVariables.Temperature2mMin,DailyVariables.PrecipitationSum,DailyVariables.PrecipitationHours]
);
for (var i = 0; i < data.DailyForecast!.Time!.Count(); i++)
{
var item = new WeatherDailyBase
{
Time = data.DailyForecast.Time![i]!.Value,
Precipitation = data.DailyForecast.PrecipitationSum![i],
PrecipitationHours = data.DailyForecast.PrecipitationHours![i],
MaxTemperature = (int)Math.Round(data.DailyForecast.Temperature2mMax![i]!.Value),
MinTemperature = (int)Math.Round(data.DailyForecast.Temperature2mMin![i]!.Value),
};
list.Add(item);
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ public static string GetImageNameNight(this WeatherCode weatherType)
_ => "",
};
}

public static int Subtract(this int num1,int num2)
{
return num1 - num2;
}
}

6 changes: 3 additions & 3 deletions FluentWeather.Uwp/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
xmlns:local="using:FluentWeather.Uwp"
xmlns:local="using:FluentWeather.Uwp"
xmlns:themes="using:FluentWeather.Uwp.Themes"
xmlns:valueConverters2="using:ValueConverters"
xmlns:valueConverters="using:FluentWeather.Uwp.Shared.Helpers.ValueConverters">
xmlns:valueConverters="using:FluentWeather.Uwp.Shared.Helpers.ValueConverters"
xmlns:valueConverters2="using:ValueConverters">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ private List<ITemperature> GetHourly(ObservableCollection<WeatherHourlyBase> wea
private string GetTextFirst(List<WeatherHourlyBase> weatherList)
{
if (weatherList is null) return null;
return weatherList.Count is 0 ? null : weatherList?.ConvertAll(p => (ITime)p).ToList().First().Time.ToShortTimeString();
return weatherList.Count is 0 ? null : weatherList?.First().Time.ToShortTimeString();
}
private string GetTextLast(List<WeatherHourlyBase> weatherList)
{
if (weatherList is null) return null;
return weatherList.Count is 0 ? null : weatherList?.ConvertAll(p => (ITime)p).ToList().Last().Time.ToShortTimeString();
return weatherList.Count is 0 ? null : weatherList?.Last().Time.ToShortTimeString();
}
private void ForecastGridView_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
Expand Down
Loading

0 comments on commit eff2b04

Please sign in to comment.