jellyfin-server/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/SatIp/SatIpDiscovery.cs

140 lines
4.6 KiB
C#
Raw Normal View History

2016-01-25 21:33:36 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
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;
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
{
public class SatIpDiscovery : 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);
private readonly IHttpClient _httpClient;
public SatIpDiscovery(IDeviceDiscovery deviceDiscovery, IServerConfigurationManager config, ILogger logger, ILiveTvManager liveTvManager, IHttpClient httpClient)
{
_deviceDiscovery = deviceDiscovery;
_config = config;
_logger = logger;
_liveTvManager = liveTvManager;
_httpClient = httpClient;
}
public void Run()
{
_deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
}
void _deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
{
2016-02-15 05:47:23 +00:00
string st = null;
2016-02-17 21:24:01 +00:00
string nt = null;
e.Headers.TryGetValue("ST", out st);
e.Headers.TryGetValue("NT", out nt);
if (string.Equals(st, "urn:ses-com:device:SatIPServer:1", StringComparison.OrdinalIgnoreCase) ||
string.Equals(nt, "urn:ses-com:device:SatIPServer:1", StringComparison.OrdinalIgnoreCase))
2016-02-15 05:47:23 +00:00
{
string location;
if (e.Headers.TryGetValue("Location", out location) && !string.IsNullOrWhiteSpace(location))
{
_logger.Debug("SAT IP found at {0}", location);
2016-01-25 21:33:36 +00:00
2016-02-15 05:47:23 +00:00
// Just get the beginning of the url
AddDevice(location);
}
}
2016-01-25 21:33:36 +00:00
}
2016-02-15 05:47:23 +00:00
private async void AddDevice(string location)
2016-01-25 21:33:36 +00:00
{
await _semaphore.WaitAsync().ConfigureAwait(false);
try
{
var options = GetConfiguration();
2016-02-15 05:47:23 +00:00
//if (options.TunerHosts.Any(i =>
// string.Equals(i.Type, SatIpHost.DeviceType, StringComparison.OrdinalIgnoreCase) &&
// UriEquals(i.Url, url)))
//{
// return;
//}
2016-01-25 21:33:36 +00:00
2016-02-15 05:47:23 +00:00
//// Strip off the port
//url = new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped).TrimEnd('/');
2016-01-25 21:33:36 +00:00
2016-02-15 05:47:23 +00:00
//await TestUrl(url).ConfigureAwait(false);
//await _liveTvManager.SaveTunerHost(new TunerHostInfo
//{
// Type = SatIpHost.DeviceType,
// Url = url
2016-01-25 21:33:36 +00:00
2016-02-15 05:47:23 +00:00
//}).ConfigureAwait(false);
2016-01-25 21:33:36 +00:00
}
catch (Exception ex)
{
_logger.ErrorException("Error saving device", ex);
}
finally
{
_semaphore.Release();
}
}
private async Task TestUrl(string url)
{
// Test it by pulling down the lineup
using (await _httpClient.Get(new HttpRequestOptions
{
Url = string.Format("{0}/lineup.json", url),
CancellationToken = CancellationToken.None
}))
{
}
}
private bool UriEquals(string savedUri, string location)
{
return string.Equals(NormalizeUrl(location), NormalizeUrl(savedUri), StringComparison.OrdinalIgnoreCase);
}
private string NormalizeUrl(string url)
{
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
url = "http://" + url;
}
url = url.TrimEnd('/');
// Strip off the port
return new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped);
}
private LiveTvOptions GetConfiguration()
{
return _config.GetConfiguration<LiveTvOptions>("livetv");
}
public void Dispose()
{
}
}
}