From 3c473752298e2bc91d3cb10a13c236cfaa2bc11e Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 2 Sep 2012 13:34:12 -0400 Subject: [PATCH] Weather updates --- MediaBrowser.ApiInteraction/ApiClient.cs | 18 +++++++++++++----- MediaBrowser.Common/Kernel/BaseKernel.cs | 10 +++++++++- .../Net/Handlers/BaseSerializationHandler.cs | 2 +- .../Weather/WeatherClient.cs | 12 +++++++++--- .../Configuration/ServerConfiguration.cs | 8 +++++--- MediaBrowser.Model/MediaBrowser.Model.csproj | 1 + MediaBrowser.Model/Weather/WeatherUnits.cs | 9 +++++++++ 7 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 MediaBrowser.Model/Weather/WeatherUnits.cs diff --git a/MediaBrowser.ApiInteraction/ApiClient.cs b/MediaBrowser.ApiInteraction/ApiClient.cs index bc06aa9a2e..3de40f6a86 100644 --- a/MediaBrowser.ApiInteraction/ApiClient.cs +++ b/MediaBrowser.ApiInteraction/ApiClient.cs @@ -573,9 +573,9 @@ namespace MediaBrowser.ApiInteraction { string url = ApiUrl + "/ServerConfiguration"; - using (Stream stream = await GetSerializedStreamAsync(url).ConfigureAwait(false)) + using (Stream stream = await GetSerializedStreamAsync(url, ApiInteraction.SerializationFormat.Json).ConfigureAwait(false)) { - return DeserializeFromStream(stream); + return DeserializeFromStream(stream, ApiInteraction.SerializationFormat.Json); } } @@ -622,19 +622,27 @@ namespace MediaBrowser.ApiInteraction /// This is a helper around getting a stream from the server that contains serialized data /// private Task GetSerializedStreamAsync(string url) + { + return GetSerializedStreamAsync(url, SerializationFormat); + } + + /// + /// This is a helper around getting a stream from the server that contains serialized data + /// + private Task GetSerializedStreamAsync(string url, SerializationFormat serializationFormat) { if (url.IndexOf('?') == -1) { - url += "?dataformat=" + SerializationFormat.ToString().ToLower(); + url += "?dataformat=" + serializationFormat.ToString().ToLower(); } else { - url += "&dataformat=" + SerializationFormat.ToString().ToLower(); + url += "&dataformat=" + serializationFormat.ToString().ToLower(); } return GetStreamAsync(url); } - + private T DeserializeFromStream(Stream stream) { return DeserializeFromStream(stream, SerializationFormat); diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 44569cbadf..cdde17b24a 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -41,6 +41,14 @@ namespace MediaBrowser.Common.Kernel /// public HttpServer HttpServer { get; private set; } + protected virtual string HttpServerUrlPrefix + { + get + { + return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/"; + } + } + /// /// Gets the kernel context. The UI kernel will have to override this. /// @@ -181,7 +189,7 @@ namespace MediaBrowser.Common.Kernel { DisposeHttpServer(); - HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/"); + HttpServer = new HttpServer(HttpServerUrlPrefix); } /// diff --git a/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs index a0696d4a61..dd3020e035 100644 --- a/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs +++ b/MediaBrowser.Common/Net/Handlers/BaseSerializationHandler.cs @@ -62,7 +62,7 @@ namespace MediaBrowser.Common.Net.Handlers protected async override Task WriteResponseToOutputStream(Stream stream) { - await EnsureObjectToSerialize(); + await EnsureObjectToSerialize().ConfigureAwait(false); switch (SerializationFormat) { diff --git a/MediaBrowser.Controller/Weather/WeatherClient.cs b/MediaBrowser.Controller/Weather/WeatherClient.cs index c49c504773..e2aada34f8 100644 --- a/MediaBrowser.Controller/Weather/WeatherClient.cs +++ b/MediaBrowser.Controller/Weather/WeatherClient.cs @@ -53,12 +53,18 @@ namespace MediaBrowser.Controller.Weather { WeatherInfo info = new WeatherInfo(); - if (data.current_condition.Any()) + if (data.current_condition != null) { - info.CurrentWeather = data.current_condition.First().ToWeatherStatus(); + if (data.current_condition.Any()) + { + info.CurrentWeather = data.current_condition.First().ToWeatherStatus(); + } } - info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray(); + if (data.weather != null) + { + info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray(); + } return info; } diff --git a/MediaBrowser.Model/Configuration/ServerConfiguration.cs b/MediaBrowser.Model/Configuration/ServerConfiguration.cs index 480cf3adb0..c229ca5568 100644 --- a/MediaBrowser.Model/Configuration/ServerConfiguration.cs +++ b/MediaBrowser.Model/Configuration/ServerConfiguration.cs @@ -1,17 +1,19 @@ - +using MediaBrowser.Model.Weather; + namespace MediaBrowser.Model.Configuration { public class ServerConfiguration : BaseApplicationConfiguration { public bool EnableInternetProviders { get; set; } - public string WeatherZipCode { get; set; } public bool EnableUserProfiles { get; set; } + public string WeatherZipCode { get; set; } + public WeatherUnits WeatherUnit { get; set; } + public ServerConfiguration() : base() { EnableUserProfiles = true; - WeatherZipCode = "02116"; } } } diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index b91e7ea173..668fd8f5b5 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -61,6 +61,7 @@ + diff --git a/MediaBrowser.Model/Weather/WeatherUnits.cs b/MediaBrowser.Model/Weather/WeatherUnits.cs new file mode 100644 index 0000000000..3bea67b9a4 --- /dev/null +++ b/MediaBrowser.Model/Weather/WeatherUnits.cs @@ -0,0 +1,9 @@ + +namespace MediaBrowser.Model.Weather +{ + public enum WeatherUnits + { + Fahrenheit, + Celsius + } +}