Sonarr/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs

126 lines
4.3 KiB
C#
Raw Normal View History

2013-06-12 06:45:25 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
2013-06-12 06:45:25 +00:00
using Newtonsoft.Json.Linq;
using NLog;
using NzbDrone.Common.Http;
2013-08-31 01:42:30 +00:00
using NzbDrone.Common.Instrumentation;
using NzbDrone.Common.Serializer;
2013-09-19 01:09:26 +00:00
using NzbDrone.Core.Notifications.Xbmc.Model;
2013-06-12 06:45:25 +00:00
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Notifications.Xbmc
{
public interface IXbmcService
{
void Notify(XbmcSettings settings, string title, string message);
void Update(XbmcSettings settings, Series series);
void Clean(XbmcSettings settings);
XbmcVersion GetJsonVersion(XbmcSettings settings);
ValidationFailure Test(XbmcSettings settings);
2013-06-12 06:45:25 +00:00
}
public class XbmcService : IXbmcService
2013-06-12 06:45:25 +00:00
{
private readonly IHttpProvider _httpProvider;
private readonly IEnumerable<IApiProvider> _apiProviders;
private readonly Logger _logger;
2013-06-12 06:45:25 +00:00
public XbmcService(IHttpProvider httpProvider, IEnumerable<IApiProvider> apiProviders, Logger logger)
2013-06-12 06:45:25 +00:00
{
_httpProvider = httpProvider;
_apiProviders = apiProviders;
_logger = logger;
2013-06-12 06:45:25 +00:00
}
public void Notify(XbmcSettings settings, string title, string message)
{
var provider = GetApiProvider(settings);
provider.Notify(settings, title, message);
}
public void Update(XbmcSettings settings, Series series)
{
var provider = GetApiProvider(settings);
provider.Update(settings, series);
}
public void Clean(XbmcSettings settings)
{
var provider = GetApiProvider(settings);
provider.Clean(settings);
}
public XbmcVersion GetJsonVersion(XbmcSettings settings)
{
try
{
var postJson = new JObject();
postJson.Add(new JProperty("jsonrpc", "2.0"));
postJson.Add(new JProperty("method", "JSONRPC.Version"));
2014-06-09 06:03:23 +00:00
postJson.Add(new JProperty("id", 1));
2013-06-12 06:45:25 +00:00
var response = _httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
_logger.Debug("Getting version from response: " + response);
var result = Json.Deserialize<XbmcJsonResult<JObject>>(response);
2013-06-12 06:45:25 +00:00
var versionObject = result.Result.Property("version");
if (versionObject.Value.Type == JTokenType.Integer)
return new XbmcVersion((int)versionObject.Value);
if (versionObject.Value.Type == JTokenType.Object)
return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
2013-06-12 06:45:25 +00:00
throw new InvalidCastException("Unknown Version structure!: " + versionObject);
}
catch (Exception ex)
{
_logger.DebugException(ex.Message, ex);
2013-06-12 06:45:25 +00:00
}
return new XbmcVersion();
}
private IApiProvider GetApiProvider(XbmcSettings settings)
{
var version = GetJsonVersion(settings);
var apiProvider = _apiProviders.SingleOrDefault(a => a.CanHandle(version));
if (apiProvider == null)
{
var message = String.Format("Invalid API Version: {0} for {1}", version, settings.Address);
throw new InvalidXbmcVersionException(message);
}
return apiProvider;
}
public ValidationFailure Test(XbmcSettings settings)
2013-06-12 06:45:25 +00:00
{
try
{
_logger.Debug("Determining version of XBMC Host: {0}", settings.Address);
var version = GetJsonVersion(settings);
_logger.Debug("Version is: {0}", version);
2013-06-12 06:45:25 +00:00
if (version == new XbmcVersion(0))
{
throw new InvalidXbmcVersionException("Verion received from XBMC is invalid, please correct your settings.");
}
Notify(settings, "Test Notification", "Success! XBMC has been successfully configured!");
}
catch (Exception ex)
2013-06-13 02:55:11 +00:00
{
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
return new ValidationFailure("Host", "Unable to send test message");
2013-06-13 02:55:11 +00:00
}
return null;
2013-06-12 06:45:25 +00:00
}
}
}