2013-06-12 06:45:25 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
using FluentValidation.Results;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using NLog;
|
2014-08-01 06:51:34 +00:00
|
|
|
|
using NzbDrone.Common.Cache;
|
2013-09-19 00:24:50 +00:00
|
|
|
|
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);
|
2015-04-25 16:02:17 +00:00
|
|
|
|
ValidationFailure Test(XbmcSettings settings, string message);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
public class XbmcService : IXbmcService
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2014-08-01 06:30:30 +00:00
|
|
|
|
private readonly IXbmcJsonApiProxy _proxy;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
private readonly IEnumerable<IApiProvider> _apiProviders;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
private readonly Logger _logger;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
private readonly ICached<XbmcVersion> _xbmcVersionCache;
|
|
|
|
|
|
|
|
|
|
public XbmcService(IXbmcJsonApiProxy proxy,
|
|
|
|
|
IEnumerable<IApiProvider> apiProviders,
|
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
Logger logger)
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2014-08-01 06:30:30 +00:00
|
|
|
|
_proxy = proxy;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
_apiProviders = apiProviders;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
_logger = logger;
|
2014-08-01 06:51:34 +00:00
|
|
|
|
|
|
|
|
|
_xbmcVersionCache = cacheManager.GetCache<XbmcVersion>(GetType());
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-03 23:29:52 +00:00
|
|
|
|
private XbmcVersion GetJsonVersion(XbmcSettings settings)
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-08-01 06:51:34 +00:00
|
|
|
|
return _xbmcVersionCache.Get(settings.Address, () =>
|
|
|
|
|
{
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
var response = _proxy.GetJsonVersion(settings);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
_logger.Debug("Getting version from response: " + response);
|
|
|
|
|
var result = Json.Deserialize<XbmcJsonResult<JObject>>(response);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
var versionObject = result.Result.Property("version");
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
if (versionObject.Value.Type == JTokenType.Integer) return new XbmcVersion((int) versionObject.Value);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
|
if (versionObject.Value.Type == JTokenType.Object) return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
|
|
|
|
|
|
|
|
|
|
throw new InvalidCastException("Unknown Version structure!: " + versionObject);
|
|
|
|
|
}, TimeSpan.FromHours(12));
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-07-04 08:09:48 +00:00
|
|
|
|
_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)
|
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var message = string.Format("Invalid API Version: {0} for {1}", version, settings.Address);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
throw new InvalidXbmcVersionException(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return apiProvider;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 16:02:17 +00:00
|
|
|
|
public ValidationFailure Test(XbmcSettings settings, string message)
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2014-08-01 06:51:34 +00:00
|
|
|
|
_xbmcVersionCache.Clear();
|
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-04-25 16:02:17 +00:00
|
|
|
|
_logger.Debug("Determining version of Host: {0}", settings.Address);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
var version = GetJsonVersion(settings);
|
|
|
|
|
_logger.Debug("Version is: {0}", version);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
if (version == new XbmcVersion(0))
|
|
|
|
|
{
|
2015-03-31 21:52:27 +00:00
|
|
|
|
throw new InvalidXbmcVersionException("Version received from XBMC is invalid, please correct your settings.");
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-25 16:02:17 +00:00
|
|
|
|
Notify(settings, "Test Notification", message);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2013-06-13 02:55:11 +00:00
|
|
|
|
{
|
2014-07-04 08:09:48 +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
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
return null;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|