1
+ using System . Text ;
2
+ using System . Text . Json ;
3
+ using OpenMeteo ;
4
+
5
+ namespace SkPluginLibrary . Plugins ;
6
+
7
+ public class WeatherPlugin ( )
8
+ {
9
+ private OpenMeteoClient _client = new ( ) ;
10
+
11
+ [ KernelFunction , Description ( "Gets the current weather for a location." ) ]
12
+ public async Task < string > GetWeather (
13
+ [ Description (
14
+ "Pass US Zipcode, UK Postcode, Canada Postalcode, IP address, Latitude/Longitude (decimal degree) or city name to get current weather for that location" ) ]
15
+ string location )
16
+ {
17
+ location = Uri . EscapeDataString ( location ) ;
18
+ var weatherData = await _client . QueryAsync ( location , new WeatherForecastOptions ( ) { Temperature_Unit = TemperatureUnitType . fahrenheit , Current = CurrentOptions . All , Daily = DailyOptions . All } ) ;
19
+ var weather = weatherData . ToMarkdown ( location ) ;
20
+ return weather ;
21
+ }
22
+ }
23
+ internal static class WeatherPluginExtensions
24
+ {
25
+ public static string ToMarkdown ( this WeatherForecast weatherForecast , string location )
26
+ {
27
+ var weather = weatherForecast . Current ;
28
+ var weatherForecastCurrentUnits = weatherForecast . CurrentUnits ! ;
29
+ var sb = new StringBuilder ( ) ;
30
+ sb . AppendLine ( $ "The weather in { location } is:") ;
31
+ sb . AppendLine ( $ "- Weather Condition: { weather . Weathercode ? . WeathercodeDescription ( ) } ") ;
32
+ sb . AppendLine ( $ "- Temperature: { weather . Temperature } { weatherForecastCurrentUnits . Temperature } ") ;
33
+ sb . AppendLine ( $ "- Feels Like: { weather . Apparent_temperature } { weatherForecastCurrentUnits . Apparent_temperature } ") ;
34
+ sb . AppendLine ( $ "- Wind Speed: { weather . Windspeed_10m } { weatherForecastCurrentUnits . Windspeed_10m } ") ;
35
+ sb . AppendLine ( $ "- Wind Direction: { weather . Winddirection_10m } { weatherForecastCurrentUnits . Winddirection_10m } ") ;
36
+ sb . AppendLine ( $ "- Humidity: { weather . Relativehumidity_2m } { weatherForecastCurrentUnits . Relativehumidity_2m } ") ;
37
+ sb . AppendLine ( $ "- Precipitation: { weather . Precipitation } { weatherForecastCurrentUnits . Precipitation } ") ;
38
+ sb . AppendLine ( $ "- Pressure: { weather . Pressure_msl } { weatherForecastCurrentUnits . Pressure_msl } ") ;
39
+ sb . AppendLine ( "" ) ;
40
+ sb . AppendLine ( $ "{ weatherForecast . ConvertWeatherForecastToMarkdownTable ( ) } ") ;
41
+ return sb . ToString ( ) ;
42
+ }
43
+ public static string ConvertWeatherForecastToMarkdownTable ( this WeatherForecast forecast )
44
+ {
45
+ var sb = new StringBuilder ( ) ;
46
+ sb . AppendLine ( "| Date | Condition | Max Temp (°F) | Min Temp (°F) | Apparent Max Temp (°F) | Apparent Min Temp (°F) | Precipitation (mm) | Wind Speed (mph) | Sunrise | Sunset |" ) ;
47
+ sb . AppendLine ( "|------------|---------------|---------------|---------------|------------------------|------------------------|--------------------|------------------|----------|---------|" ) ;
48
+
49
+ for ( int i = 0 ; i < forecast . Daily . Time . Length ; i ++ )
50
+ {
51
+ sb . AppendLine ( $ "| { forecast . Daily . Time [ i ] } | { forecast . Daily . Weathercode ? [ i ] . WeathercodeDescription ( ) } | { forecast . Daily . Temperature_2m_max ? [ i ] } | { forecast . Daily . Temperature_2m_min ? [ i ] } | { forecast . Daily . Apparent_temperature_max ? [ i ] } | { forecast . Daily . Apparent_temperature_min ? [ i ] } | { forecast . Daily . Precipitation_sum ? [ i ] } | { forecast . Daily . Windspeed_10m_max ? [ i ] } | { ( forecast . Daily . Sunrise ? [ i ] ) ? [ 11 ..] } | { ( forecast . Daily . Sunset ? [ i ] ) ? [ 11 ..] } |") ;
52
+ }
53
+
54
+ return sb . ToString ( ) ;
55
+ }
56
+ public static float ToFahrenheit ( this float ? celsius )
57
+ {
58
+ if ( celsius == null )
59
+ return 0 ;
60
+ return celsius . Value * 9 / 5 + 32 ;
61
+ }
62
+
63
+ public static string WeathercodeDescription ( this float weathercode )
64
+ {
65
+ if ( weathercode == null )
66
+ return "Invalid weathercode" ;
67
+ var code = ( int ) weathercode ;
68
+ return code . WeathercodeDescription ( ) ;
69
+ }
70
+ public static string WeathercodeDescription ( this int weathercode )
71
+ {
72
+ //var weathercode = current.Weathercode;
73
+ switch ( weathercode )
74
+ {
75
+ case 0 :
76
+ return "Clear sky" ;
77
+ case 1 :
78
+ return "Mainly clear" ;
79
+ case 2 :
80
+ return "Partly cloudy" ;
81
+ case 3 :
82
+ return "Overcast" ;
83
+ case 45 :
84
+ return "Fog" ;
85
+ case 48 :
86
+ return "Depositing rime Fog" ;
87
+ case 51 :
88
+ return "Light drizzle" ;
89
+ case 53 :
90
+ return "Moderate drizzle" ;
91
+ case 55 :
92
+ return "Dense drizzle" ;
93
+ case 56 :
94
+ return "Light freezing drizzle" ;
95
+ case 57 :
96
+ return "Dense freezing drizzle" ;
97
+ case 61 :
98
+ return "Slight rain" ;
99
+ case 63 :
100
+ return "Moderate rain" ;
101
+ case 65 :
102
+ return "Heavy rain" ;
103
+ case 66 :
104
+ return "Light freezing rain" ;
105
+ case 67 :
106
+ return "Heavy freezing rain" ;
107
+ case 71 :
108
+ return "Slight snow fall" ;
109
+ case 73 :
110
+ return "Moderate snow fall" ;
111
+ case 75 :
112
+ return "Heavy snow fall" ;
113
+ case 77 :
114
+ return "Snow grains" ;
115
+ case 80 :
116
+ return "Slight rain showers" ;
117
+ case 81 :
118
+ return "Moderate rain showers" ;
119
+ case 82 :
120
+ return "Violent rain showers" ;
121
+ case 85 :
122
+ return "Slight snow showers" ;
123
+ case 86 :
124
+ return "Heavy snow showers" ;
125
+ case 95 :
126
+ return "Thunderstorm" ;
127
+ case 96 :
128
+ return "Thunderstorm with light hail" ;
129
+ case 99 :
130
+ return "Thunderstorm with heavy hail" ;
131
+ default :
132
+ return "Invalid weathercode" ;
133
+ }
134
+ }
135
+ }
0 commit comments