aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/Weather
diff options
context:
space:
mode:
authorLukePulverenti <luke.pulverenti@gmail.com>2013-02-21 01:02:10 -0500
committerLukePulverenti <luke.pulverenti@gmail.com>2013-02-21 01:02:10 -0500
commitae4ffa75be378497d8db210c6864cde40f7c75f9 (patch)
tree14d7a0610ff5117f3ea92ddaee71c219bbbc4865 /MediaBrowser.Controller/Weather
parentacf5b0b6ed173a3a9540d0585bd491a119d524cf (diff)
isolated weather and moved drawing classes up to the controller project
Diffstat (limited to 'MediaBrowser.Controller/Weather')
-rw-r--r--MediaBrowser.Controller/Weather/BaseWeatherProvider.cs37
-rw-r--r--MediaBrowser.Controller/Weather/IWeatherProvider.cs20
-rw-r--r--MediaBrowser.Controller/Weather/WeatherProvider.cs311
3 files changed, 20 insertions, 348 deletions
diff --git a/MediaBrowser.Controller/Weather/BaseWeatherProvider.cs b/MediaBrowser.Controller/Weather/BaseWeatherProvider.cs
deleted file mode 100644
index 4ae7a3991..000000000
--- a/MediaBrowser.Controller/Weather/BaseWeatherProvider.cs
+++ /dev/null
@@ -1,37 +0,0 @@
-using MediaBrowser.Model.Weather;
-using System;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace MediaBrowser.Controller.Weather
-{
- /// <summary>
- /// Class BaseWeatherProvider
- /// </summary>
- public abstract class BaseWeatherProvider : IDisposable
- {
- /// <summary>
- /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- /// </summary>
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- /// <summary>
- /// Releases unmanaged and - optionally - managed resources.
- /// </summary>
- /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
- protected virtual void Dispose(bool dispose)
- {
- }
-
- /// <summary>
- /// Gets the weather info async.
- /// </summary>
- /// <param name="location">The location.</param>
- /// <returns>Task{WeatherInfo}.</returns>
- public abstract Task<WeatherInfo> GetWeatherInfoAsync(string location, CancellationToken cancellationToken);
- }
-}
diff --git a/MediaBrowser.Controller/Weather/IWeatherProvider.cs b/MediaBrowser.Controller/Weather/IWeatherProvider.cs
new file mode 100644
index 000000000..9060e5b9c
--- /dev/null
+++ b/MediaBrowser.Controller/Weather/IWeatherProvider.cs
@@ -0,0 +1,20 @@
+using MediaBrowser.Model.Weather;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Controller.Weather
+{
+ /// <summary>
+ /// Interface IWeatherProvider
+ /// </summary>
+ public interface IWeatherProvider
+ {
+ /// <summary>
+ /// Gets the weather info async.
+ /// </summary>
+ /// <param name="location">The location.</param>
+ /// <param name="cancellationToken">The cancellation token.</param>
+ /// <returns>Task{WeatherInfo}.</returns>
+ Task<WeatherInfo> GetWeatherInfoAsync(string location, CancellationToken cancellationToken);
+ }
+}
diff --git a/MediaBrowser.Controller/Weather/WeatherProvider.cs b/MediaBrowser.Controller/Weather/WeatherProvider.cs
deleted file mode 100644
index 1560aa92c..000000000
--- a/MediaBrowser.Controller/Weather/WeatherProvider.cs
+++ /dev/null
@@ -1,311 +0,0 @@
-using MediaBrowser.Common.Logging;
-using MediaBrowser.Common.Serialization;
-using MediaBrowser.Model.Weather;
-using System;
-using System.ComponentModel.Composition;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace MediaBrowser.Controller.Weather
-{
- /// <summary>
- /// Based on http://www.worldweatheronline.com/free-weather-feed.aspx
- /// The classes in this file are a reproduction of the json output, which will then be converted to our weather model classes
- /// </summary>
- [Export(typeof(BaseWeatherProvider))]
- public class WeatherProvider : BaseWeatherProvider
- {
- /// <summary>
- /// The _weather semaphore
- /// </summary>
- private readonly SemaphoreSlim _weatherSemaphore = new SemaphoreSlim(10, 10);
-
- /// <summary>
- /// Gets the weather info async.
- /// </summary>
- /// <param name="location">The location.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task{WeatherInfo}.</returns>
- /// <exception cref="System.ArgumentNullException">location</exception>
- public override async Task<WeatherInfo> GetWeatherInfoAsync(string location, CancellationToken cancellationToken)
- {
- if (string.IsNullOrWhiteSpace(location))
- {
- throw new ArgumentNullException("location");
- }
-
- if (cancellationToken == null)
- {
- throw new ArgumentNullException("cancellationToken");
- }
-
- const int numDays = 5;
- const string apiKey = "24902f60f1231941120109";
-
- var url = "http://free.worldweatheronline.com/feed/weather.ashx?q=" + location + "&format=json&num_of_days=" + numDays + "&key=" + apiKey;
-
- Logger.LogInfo("Accessing weather from " + url);
-
- using (var stream = await Kernel.Instance.HttpManager.Get(url, _weatherSemaphore, cancellationToken).ConfigureAwait(false))
- {
- var data = JsonSerializer.DeserializeFromStream<WeatherResult>(stream).data;
-
- return GetWeatherInfo(data);
- }
- }
-
- /// <summary>
- /// Converst the json output to our WeatherInfo model class
- /// </summary>
- /// <param name="data">The data.</param>
- /// <returns>WeatherInfo.</returns>
- private WeatherInfo GetWeatherInfo(WeatherData data)
- {
- var info = new WeatherInfo();
-
- if (data.current_condition != null)
- {
- var condition = data.current_condition.FirstOrDefault();
-
- if (condition != null)
- {
- info.CurrentWeather = condition.ToWeatherStatus();
- }
- }
-
- if (data.weather != null)
- {
- info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray();
- }
-
- return info;
- }
- }
-
- /// <summary>
- /// Class WeatherResult
- /// </summary>
- class WeatherResult
- {
- /// <summary>
- /// Gets or sets the data.
- /// </summary>
- /// <value>The data.</value>
- public WeatherData data { get; set; }
- }
-
- /// <summary>
- /// Class WeatherData
- /// </summary>
- public class WeatherData
- {
- /// <summary>
- /// Gets or sets the current_condition.
- /// </summary>
- /// <value>The current_condition.</value>
- public WeatherCondition[] current_condition { get; set; }
- /// <summary>
- /// Gets or sets the weather.
- /// </summary>
- /// <value>The weather.</value>
- public DailyWeatherInfo[] weather { get; set; }
- }
-
- /// <summary>
- /// Class WeatherCondition
- /// </summary>
- public class WeatherCondition
- {
- /// <summary>
- /// Gets or sets the temp_ C.
- /// </summary>
- /// <value>The temp_ C.</value>
- public string temp_C { get; set; }
- /// <summary>
- /// Gets or sets the temp_ F.
- /// </summary>
- /// <value>The temp_ F.</value>
- public string temp_F { get; set; }
- /// <summary>
- /// Gets or sets the humidity.
- /// </summary>
- /// <value>The humidity.</value>
- public string humidity { get; set; }
- /// <summary>
- /// Gets or sets the weather code.
- /// </summary>
- /// <value>The weather code.</value>
- public string weatherCode { get; set; }
-
- /// <summary>
- /// To the weather status.
- /// </summary>
- /// <returns>WeatherStatus.</returns>
- public WeatherStatus ToWeatherStatus()
- {
- return new WeatherStatus
- {
- TemperatureCelsius = int.Parse(temp_C),
- TemperatureFahrenheit = int.Parse(temp_F),
- Humidity = int.Parse(humidity),
- Condition = DailyWeatherInfo.GetCondition(weatherCode)
- };
- }
- }
-
- /// <summary>
- /// Class DailyWeatherInfo
- /// </summary>
- public class DailyWeatherInfo
- {
- /// <summary>
- /// Gets or sets the date.
- /// </summary>
- /// <value>The date.</value>
- public string date { get; set; }
- /// <summary>
- /// Gets or sets the precip MM.
- /// </summary>
- /// <value>The precip MM.</value>
- public string precipMM { get; set; }
- /// <summary>
- /// Gets or sets the temp max C.
- /// </summary>
- /// <value>The temp max C.</value>
- public string tempMaxC { get; set; }
- /// <summary>
- /// Gets or sets the temp max F.
- /// </summary>
- /// <value>The temp max F.</value>
- public string tempMaxF { get; set; }
- /// <summary>
- /// Gets or sets the temp min C.
- /// </summary>
- /// <value>The temp min C.</value>
- public string tempMinC { get; set; }
- /// <summary>
- /// Gets or sets the temp min F.
- /// </summary>
- /// <value>The temp min F.</value>
- public string tempMinF { get; set; }
- /// <summary>
- /// Gets or sets the weather code.
- /// </summary>
- /// <value>The weather code.</value>
- public string weatherCode { get; set; }
- /// <summary>
- /// Gets or sets the winddir16 point.
- /// </summary>
- /// <value>The winddir16 point.</value>
- public string winddir16Point { get; set; }
- /// <summary>
- /// Gets or sets the winddir degree.
- /// </summary>
- /// <value>The winddir degree.</value>
- public string winddirDegree { get; set; }
- /// <summary>
- /// Gets or sets the winddirection.
- /// </summary>
- /// <value>The winddirection.</value>
- public string winddirection { get; set; }
- /// <summary>
- /// Gets or sets the windspeed KMPH.
- /// </summary>
- /// <value>The windspeed KMPH.</value>
- public string windspeedKmph { get; set; }
- /// <summary>
- /// Gets or sets the windspeed miles.
- /// </summary>
- /// <value>The windspeed miles.</value>
- public string windspeedMiles { get; set; }
-
- /// <summary>
- /// To the weather forecast.
- /// </summary>
- /// <returns>WeatherForecast.</returns>
- public WeatherForecast ToWeatherForecast()
- {
- return new WeatherForecast
- {
- Date = DateTime.Parse(date),
- HighTemperatureCelsius = int.Parse(tempMaxC),
- HighTemperatureFahrenheit = int.Parse(tempMaxF),
- LowTemperatureCelsius = int.Parse(tempMinC),
- LowTemperatureFahrenheit = int.Parse(tempMinF),
- Condition = GetCondition(weatherCode)
- };
- }
-
- /// <summary>
- /// Gets the condition.
- /// </summary>
- /// <param name="weatherCode">The weather code.</param>
- /// <returns>WeatherConditions.</returns>
- public static WeatherConditions GetCondition(string weatherCode)
- {
- switch (weatherCode)
- {
- case "362":
- case "365":
- case "320":
- case "317":
- case "182":
- return WeatherConditions.Sleet;
- case "338":
- case "335":
- case "332":
- case "329":
- case "326":
- case "323":
- case "377":
- case "374":
- case "371":
- case "368":
- case "395":
- case "392":
- case "350":
- case "227":
- case "179":
- return WeatherConditions.Snow;
- case "314":
- case "311":
- case "308":
- case "305":
- case "302":
- case "299":
- case "296":
- case "293":
- case "284":
- case "281":
- case "266":
- case "263":
- case "359":
- case "356":
- case "353":
- case "185":
- case "176":
- return WeatherConditions.Rain;
- case "260":
- case "248":
- return WeatherConditions.Fog;
- case "389":
- case "386":
- case "200":
- return WeatherConditions.Thunderstorm;
- case "230":
- return WeatherConditions.Blizzard;
- case "143":
- return WeatherConditions.Mist;
- case "122":
- return WeatherConditions.Overcast;
- case "119":
- return WeatherConditions.Cloudy;
- case "115":
- return WeatherConditions.PartlyCloudy;
- default:
- return WeatherConditions.Sunny;
- }
- }
- }
-}