jellyfin-server/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunDiscovery.cs

160 lines
5.8 KiB
C#
Raw Normal View History

2015-07-23 16:32:34 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Logging;
using System;
using System.Linq;
using System.Threading;
2015-10-04 16:15:08 +00:00
using MediaBrowser.Common.Net;
2016-10-29 18:46:56 +00:00
using MediaBrowser.Model.Dlna;
2016-09-11 07:33:53 +00:00
using MediaBrowser.Model.Events;
2016-03-08 05:00:03 +00:00
using MediaBrowser.Model.Serialization;
2015-07-23 16:32:34 +00:00
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
2015-07-23 16:32:34 +00:00
{
public class HdHomerunDiscovery : IServerEntryPoint
{
private readonly IDeviceDiscovery _deviceDiscovery;
private readonly IServerConfigurationManager _config;
private readonly ILogger _logger;
private readonly ILiveTvManager _liveTvManager;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
2015-10-04 16:15:08 +00:00
private readonly IHttpClient _httpClient;
2016-03-13 07:34:17 +00:00
private readonly IJsonSerializer _json;
2015-07-23 16:32:34 +00:00
2016-03-13 07:34:17 +00:00
public HdHomerunDiscovery(IDeviceDiscovery deviceDiscovery, IServerConfigurationManager config, ILogger logger, ILiveTvManager liveTvManager, IHttpClient httpClient, IJsonSerializer json)
2015-07-23 16:32:34 +00:00
{
_deviceDiscovery = deviceDiscovery;
_config = config;
_logger = logger;
_liveTvManager = liveTvManager;
2015-10-04 16:15:08 +00:00
_httpClient = httpClient;
2016-03-13 07:34:17 +00:00
_json = json;
2015-07-23 16:32:34 +00:00
}
public void Run()
{
_deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
}
2016-09-11 07:33:53 +00:00
void _deviceDiscovery_DeviceDiscovered(object sender, GenericEventArgs<UpnpDeviceInfo> e)
2015-07-23 16:32:34 +00:00
{
string server = null;
2016-09-11 07:33:53 +00:00
var info = e.Argument;
if (info.Headers.TryGetValue("SERVER", out server) && server.IndexOf("HDHomeRun", StringComparison.OrdinalIgnoreCase) != -1)
2015-07-23 16:32:34 +00:00
{
string location;
2016-09-11 07:33:53 +00:00
if (info.Headers.TryGetValue("Location", out location))
2015-07-23 16:32:34 +00:00
{
2015-07-31 02:34:15 +00:00
//_logger.Debug("HdHomerun found at {0}", location);
2015-07-23 16:32:34 +00:00
// Just get the beginning of the url
Uri uri;
if (Uri.TryCreate(location, UriKind.Absolute, out uri))
{
var apiUrl = location.Replace(uri.LocalPath, String.Empty, StringComparison.OrdinalIgnoreCase)
.TrimEnd('/');
2015-07-31 02:34:15 +00:00
//_logger.Debug("HdHomerun api url: {0}", apiUrl);
2015-07-23 16:32:34 +00:00
AddDevice(apiUrl);
}
}
}
}
private async void AddDevice(string url)
{
await _semaphore.WaitAsync().ConfigureAwait(false);
try
{
var options = GetConfiguration();
if (options.TunerHosts.Any(i =>
string.Equals(i.Type, HdHomerunHost.DeviceType, StringComparison.OrdinalIgnoreCase) &&
UriEquals(i.Url, url)))
{
return;
}
2015-07-23 23:40:54 +00:00
// Strip off the port
2015-07-25 18:11:46 +00:00
url = new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped).TrimEnd('/');
2015-07-23 23:40:54 +00:00
2015-10-04 16:15:08 +00:00
// Test it by pulling down the lineup
2016-03-08 05:00:03 +00:00
using (var stream = await _httpClient.Get(new HttpRequestOptions
2015-10-04 16:15:08 +00:00
{
2016-03-08 05:00:03 +00:00
Url = string.Format("{0}/discover.json", url),
2016-10-06 18:55:01 +00:00
CancellationToken = CancellationToken.None,
BufferContent = false
2015-10-04 16:15:08 +00:00
}))
{
2016-03-08 05:00:03 +00:00
var response = _json.DeserializeFromStream<HdHomerunHost.DiscoverResponse>(stream);
var existing = GetConfiguration().TunerHosts
.FirstOrDefault(i => string.Equals(i.Type, HdHomerunHost.DeviceType, StringComparison.OrdinalIgnoreCase) && string.Equals(i.DeviceId, response.DeviceID, StringComparison.OrdinalIgnoreCase));
if (existing == null)
{
await _liveTvManager.SaveTunerHost(new TunerHostInfo
{
Type = HdHomerunHost.DeviceType,
Url = url,
DataVersion = 1,
DeviceId = response.DeviceID
2015-07-23 16:32:34 +00:00
2016-03-08 05:00:03 +00:00
}).ConfigureAwait(false);
}
else
{
if (!string.Equals(existing.Url, url, StringComparison.OrdinalIgnoreCase))
{
existing.Url = url;
await _liveTvManager.SaveTunerHost(existing).ConfigureAwait(false);
}
}
}
2015-07-23 16:32:34 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error saving device", ex);
}
finally
{
_semaphore.Release();
}
}
private bool UriEquals(string savedUri, string location)
{
2015-07-23 17:58:20 +00:00
return string.Equals(NormalizeUrl(location), NormalizeUrl(savedUri), StringComparison.OrdinalIgnoreCase);
}
private string NormalizeUrl(string url)
{
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
2015-07-23 16:32:34 +00:00
{
2015-07-23 17:58:20 +00:00
url = "http://" + url;
2015-07-23 16:32:34 +00:00
}
2015-07-23 17:58:20 +00:00
url = url.TrimEnd('/');
2015-07-23 23:40:54 +00:00
// Strip off the port
return new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped);
2015-07-23 16:32:34 +00:00
}
private LiveTvOptions GetConfiguration()
{
return _config.GetConfiguration<LiveTvOptions>("livetv");
}
public void Dispose()
{
}
}
}