File size: 9,206 Bytes
b1b3bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
Imports System.Net.Http
Imports Newtonsoft.Json
Public Class WeatherProvider
Implements IWeatherProvider
Sub New()
End Sub
Public Function GetCurrentWeather(latitude As Double, longitude As Double) As IWeatherData Implements IWeatherProvider.GetCurrentWeather
Dim getdata = GetWeather(latitude, longitude)
getdata.Wait()
If getdata.Result.results Is Nothing Then
Throw New Exception("Unable to get current weather conditions for the specified location.")
End If
Dim result = getdata.Result.results(0)
Dim formattedresult As New WeatherData
With formattedresult
.AtmosphericPressure_Pa = result.pressure.value / 1000.0 * 101325
Select Case result.phrase.ToLower()
Case "cloudy", "partly sunny", "clouds and sun"
.CurrentCondition = WeatherCondition.Cloudy
Case "clear", "sunny"
.CurrentCondition = WeatherCondition.Sunny
Case "rainy", "light rain", "rain"
.CurrentCondition = WeatherCondition.Rainy
End Select
.Latitude = latitude
.Longitude = longitude
.RelativeHumidity_pct = result.relativeHumidity
.Temperature_C = result.temperature.value
.WindSpeed_km_h = result.wind.speed.value
End With
'calculate solar irradiation
'references:
'https://www.pveducation.org/pvcdrom/properties-of-sunlight/calculation-of-solar-insolation
'https://www.pveducation.org/pvcdrom/properties-of-sunlight/the-suns-position
'https://www.pveducation.org/pvcdrom/properties-of-sunlight/air-mass
Dim currdata = DateTime.Now
Dim DT = currdata.ToLocalTime() - currdata.ToUniversalTime()
Dim LSTM = 15 * DT.TotalHours
Dim d = currdata.DayOfYear
Dim B = 360.0 / 365.0 * (d - 81)
Dim EOT = 9.87 * Math.Sin(2 * B) - 7.53 * Math.Cos(B) - 1.5 * Math.Sin(B)
Dim TC = 4 * (longitude - LSTM) + EOT
Dim LST = currdata.Hour + TC / 60.0
Dim HRA = 15 * (LST - 12)
Dim decl = 23.45 * Math.Sin(360.0 / 365.0 * (d - 81)) * Math.PI / 180.0
Dim elev = Math.Asin(Math.Sin(decl) * Math.Sin(latitude) + Math.Cos(decl) * Math.Cos(latitude) * Math.Cos(HRA)) * 180 / Math.PI
Dim sunrise = 12 - 1.0 / 15.0 * Math.Acos(-Math.Sin(latitude) * Math.Sin(decl) / Math.Cos(latitude) / Math.Cos(decl)) * 180 / Math.PI
Dim sunset = 12 + 1.0 / 15.0 * Math.Acos(-Math.Sin(latitude) * Math.Sin(decl) / Math.Cos(latitude) / Math.Cos(decl)) * 180 / Math.PI
Dim AM = 1 / Math.Cos(elev)
Dim ID = 1.353 * 0.7 ^ (AM * 0.678)
If formattedresult.CurrentCondition = WeatherCondition.Cloudy Then
ID *= 0.5
End If
formattedresult.SolarIrradiation_kWh_m2 = ID
If currdata.Hour > sunset Or currdata.Hour < sunrise Then
formattedresult.CurrentCondition = WeatherCondition.Night
formattedresult.SolarIrradiation_kWh_m2 = 0.0
End If
Return formattedresult
End Function
Private Async Function GetWeather(latitude As Double, longitude As Double) As Task(Of WeatherResult)
Using client = New HttpClient()
client.Timeout = New TimeSpan(0, 0, 10)
Using request = New HttpRequestMessage()
request.Method = HttpMethod.Get
request.RequestUri = New Uri(String.Format("https://dwsimwebserver.azurewebsites.net/getweather?latitude={0}&longitude={1}", latitude, longitude))
Dim response As HttpResponseMessage = Await client.SendAsync(request).ConfigureAwait(False)
Dim result As String = Await response.Content.ReadAsStringAsync()
Return JsonConvert.DeserializeObject(Of WeatherResult)(result)
End Using
End Using
End Function
End Class
Public Class Temperature
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class RealFeelTemperature
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class RealFeelTemperatureShade
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class DewPoint
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Direction
Public Property degrees As Double
Public Property localizedDescription As String
End Class
Public Class Speed
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Wind
Public Property direction As Direction
Public Property speed As Speed
End Class
Public Class WindGust
Public Property speed As Speed
End Class
Public Class Visibility
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Ceiling
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Pressure
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class PressureTendency
Public Property localizedDescription As String
Public Property code As String
End Class
Public Class Past24HourTemperatureDeparture
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class ApparentTemperature
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class WindChillTemperature
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class WetBulbTemperature
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class PastHour
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past3Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past6Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past9Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past12Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past18Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class Past24Hours
Public Property value As Double
Public Property unit As String
Public Property unitType As Integer
End Class
Public Class PrecipitationSummary
Public Property pastHour As PastHour
Public Property past3Hours As Past3Hours
Public Property past6Hours As Past6Hours
Public Property past9Hours As Past9Hours
Public Property past12Hours As Past12Hours
Public Property past18Hours As Past18Hours
Public Property past24Hours As Past24Hours
End Class
Public Class TemperatureSummary
Public Property past6Hours As Past6Hours
Public Property past12Hours As Past12Hours
Public Property past24Hours As Past24Hours
End Class
Public Class Result
Public Property dateTime As DateTime
Public Property phrase As String
Public Property iconCode As Integer
Public Property hasPrecipitation As Boolean
Public Property isDayTime As Boolean
Public Property temperature As Temperature
Public Property realFeelTemperature As RealFeelTemperature
Public Property realFeelTemperatureShade As RealFeelTemperatureShade
Public Property relativeHumidity As Integer
Public Property dewPoint As DewPoint
Public Property wind As Wind
Public Property windGust As WindGust
Public Property uvIndex As Integer
Public Property uvIndexPhrase As String
Public Property visibility As Visibility
Public Property obstructionsToVisibility As String
Public Property cloudCover As Integer
Public Property ceiling As Ceiling
Public Property pressure As Pressure
Public Property pressureTendency As PressureTendency
Public Property past24HourTemperatureDeparture As Past24HourTemperatureDeparture
Public Property apparentTemperature As ApparentTemperature
Public Property windChillTemperature As WindChillTemperature
Public Property wetBulbTemperature As WetBulbTemperature
Public Property precipitationSummary As PrecipitationSummary
Public Property temperatureSummary As TemperatureSummary
End Class
Public Class WeatherResult
Public Property results As Result()
End Class
|