Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/VB/TouchBoard/App_Code/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/VB/TouchBoard/App_Code/WeatherWidget.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.SessionState
Imports System.Xml.Linq
Imports System.ComponentModel
Imports System.IO

Partial Public Class WeatherWidget
	Public Const SessionKey As String = "WeatherWidget_List2"

	Public Const Latitude As Decimal = CDec(34.05)
	Public Const Longitude As Decimal = CDec(-118.24)
	Public Const Location As String = "Los Angeles, US"

	Private Shared random_Renamed As Random
	Private Shared ReadOnly Property Random() As Random
		Get
			If random_Renamed Is Nothing Then
				random_Renamed = New Random()
			End If
			Return random_Renamed
		End Get
	End Property
	Private Shared ReadOnly Property Session() As HttpSessionState
		Get
			Return HttpContext.Current.Session
		End Get
	End Property
	Public Shared ReadOnly Property WeatherList() As List(Of Weather)
		Get
			If Session(SessionKey) Is Nothing Then
				Session(SessionKey) = LoadActualData()
			End If
			Return CType(Session(SessionKey), List(Of Weather))
		End Get
	End Property
	Public Shared ReadOnly Property TodayWeather() As Weather
		Get
			Return WeatherList(0)
		End Get
	End Property
	Public Shared Function SelectForecast() As BindingList(Of Weather)
		Return New BindingList(Of Weather)(WeatherList.Skip(1).ToList())
	End Function

	Private Shared Function LoadActualData() As List(Of Weather)
		Dim client = New Client()
		Dim response = client.NDFDgenByDay(Latitude, Longitude, DateTime.Now, "6", unitType.m, formatType.Item24hourly)
		Dim doc As XDocument = XDocument.Parse(response)
		Dim minTemps As New List(Of Integer)()
		Dim maxTemps As New List(Of Integer)()
		Dim types As New List(Of WeatherType)()
		Dim paramNodes = doc.Root.Element("data").Element("parameters")
		For Each n In paramNodes.Elements("temperature").Where(Function(t) t.Attribute("type").Value = "maximum").Elements("value")
			minTemps.Add(GetTemperature(minTemps, n.Value))
		Next n
		For Each n In paramNodes.Elements("temperature").Where(Function(t) t.Attribute("type").Value = "minimum").Elements("value")
			maxTemps.Add(GetTemperature(maxTemps, n.Value))
		Next n
		For Each n In paramNodes.Elements("weather").Elements("weather-conditions")
			Dim typeString As String = n.Attribute("weather-summary").Value.ToLower()
			If typeString.Contains("sun") Then
				types.Add(WeatherType.Sunny)
			ElseIf typeString.Contains("clouds") Then
				types.Add(WeatherType.Cloudy)
			ElseIf typeString.Contains("rain") Then
				types.Add(WeatherType.Rain)
			ElseIf typeString.Contains("fog") Then
				types.Add(WeatherType.Fog)
			ElseIf typeString.Contains("snow") Then
				types.Add(WeatherType.Snow)
			ElseIf typeString.Contains("storm") Then
				types.Add(WeatherType.Storm)
			Else
				types.Add(WeatherType.PartlyCloudy)
			End If
		Next n

		Dim list = New List(Of Weather)()
		For i = 0 To minTemps.Count - 1
			list.Add(New Weather() With {.WeatherDate = DateTime.Now + TimeSpan.FromDays(i), .Temperature = (minTemps(i) + maxTemps(i)) / 2, .Type = types(i)})
		Next i
		Return list
	End Function

	Private Shared Function GetServiceResponse() As String
		Dim client = New Client()
		Dim response As String = Nothing
		Try
			response = client.NDFDgenByDay(Latitude, Longitude, DateTime.Now, "6", unitType.m, formatType.Item24hourly)
		Catch
		End Try
		If (Not String.IsNullOrEmpty(response)) Then
			Return response
		End If
		Return File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/off_Weather_LA.xml"))
	End Function

	Private Shared Function GetTemperature(ByVal temps As List(Of Integer), ByVal value As String) As Integer
		Dim temp As Integer = 0
		If Integer.TryParse(value, temp) Then
			Return temp
		Else
			If temps.Any() Then
				Return temps.Last()
			Else
				Return random_Renamed.Next(10, 25)
			End If
		End If
	End Function
End Class

Public Class Weather
	Private privateWeatherDate As DateTime
	Public Property WeatherDate() As DateTime
		Get
			Return privateWeatherDate
		End Get
		Set(ByVal value As DateTime)
			privateWeatherDate = value
		End Set
	End Property
	Private privateTemperature As Integer
	Public Property Temperature() As Integer
		Get
			Return privateTemperature
		End Get
		Set(ByVal value As Integer)
			privateTemperature = value
		End Set
	End Property
	Private privateType As WeatherType
	Public Property Type() As WeatherType
		Get
			Return privateType
		End Get
		Set(ByVal value As WeatherType)
			privateType = value
		End Set
	End Property
End Class

Public Enum WeatherType
	Cloudy
	Fog
	PartlyCloudy
	Rain
	Snow
	Storm
	Sunny
End Enum