Mini Kabibi Habibi

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Xml.Linq;
using System.ComponentModel;
using System.IO;

public partial class WeatherWidget {
    public const string SessionKey = "WeatherWidget_List2";

    public const decimal Latitude = (decimal)34.05;
    public const decimal Longitude = (decimal)-118.24;
    public const string Location = "Los Angeles, US";

    static Random random;
    static Random Random {
        get {
            if(random == null)
                random = new Random();
            return random;
        }
    }
    static HttpSessionState Session { get { return HttpContext.Current.Session; } }
    public static List<Weather> WeatherList {
        get {
            if(Session[SessionKey] == null) {
                Session[SessionKey] = LoadActualData();
            }
            return (List<Weather>)Session[SessionKey];
        }
    }
    public static Weather TodayWeather {
        get { return WeatherList[0]; }
    }
    public static BindingList<Weather> SelectForecast() {
        return new BindingList<Weather>(WeatherList.Skip(1).ToList());
    }

    static List<Weather> LoadActualData() {
        var client = new Client();
        var response = client.NDFDgenByDay(Latitude, Longitude, DateTime.Now, "6", unitType.m, formatType.Item24hourly);
        XDocument doc = XDocument.Parse(response);
        List<int> minTemps = new List<int>();
        List<int> maxTemps = new List<int>();
        List<WeatherType> types = new List<WeatherType>();
        var paramNodes = doc.Root.Element("data").Element("parameters");
        foreach(var n in paramNodes.Elements("temperature").Where(t => t.Attribute("type").Value == "maximum").Elements("value")) {
            minTemps.Add(GetTemperature(minTemps, n.Value));
        }
        foreach(var n in paramNodes.Elements("temperature").Where(t => t.Attribute("type").Value == "minimum").Elements("value")) {
            maxTemps.Add(GetTemperature(maxTemps, n.Value));
        }
        foreach(var n in paramNodes.Elements("weather").Elements("weather-conditions")) {
            string typeString = n.Attribute("weather-summary").Value.ToLower();
            if(typeString.Contains("sun"))
                types.Add(WeatherType.Sunny);
            else if(typeString.Contains("clouds"))
                types.Add(WeatherType.Cloudy);
            else if(typeString.Contains("rain"))
                types.Add(WeatherType.Rain);
            else if(typeString.Contains("fog"))
                types.Add(WeatherType.Fog);
            else if(typeString.Contains("snow"))
                types.Add(WeatherType.Snow);
            else if(typeString.Contains("storm"))
                types.Add(WeatherType.Storm);
            else
                types.Add(WeatherType.PartlyCloudy);
        }

        var list = new List<Weather>();
        for(var i = 0; i < minTemps.Count; i++) {
            list.Add(new Weather() {
                WeatherDate = DateTime.Now + TimeSpan.FromDays(i),
                Temperature = (minTemps[i] + maxTemps[i]) / 2,
                Type = types[i]
            });
        }
        return list;
    }

    static string GetServiceResponse() {
        var client = new Client();
        string response = null;
        try {
            response = client.NDFDgenByDay(Latitude, Longitude, DateTime.Now, "6", unitType.m, formatType.Item24hourly);
        } catch { }
        if(!string.IsNullOrEmpty(response))
            return response;
        return File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/off_Weather_LA.xml"));
    }

    static int GetTemperature(List<int> temps, string value) {
        int temp = 0;
        if(int.TryParse(value, out temp))
            return temp;
        else
            return temps.Any() ? temps.Last() : random.Next(10, 25);
    }
}

public class Weather {
    public DateTime WeatherDate { get; set; }
    public int Temperature { get; set; }
    public WeatherType Type { get; set; }
}

public enum WeatherType {
    Cloudy,
    Fog,
    PartlyCloudy,
    Rain,
    Snow,
    Storm,
    Sunny
}