SatIpDiscovery.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Common.Net;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Dlna;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Controller.Plugins;
  13. using MediaBrowser.Model.Extensions;
  14. using MediaBrowser.Model.LiveTv;
  15. using MediaBrowser.Model.Logging;
  16. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
  17. {
  18. public class SatIpDiscovery : IServerEntryPoint
  19. {
  20. private readonly IDeviceDiscovery _deviceDiscovery;
  21. private readonly IServerConfigurationManager _config;
  22. private readonly ILogger _logger;
  23. private readonly ILiveTvManager _liveTvManager;
  24. private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  25. private readonly IHttpClient _httpClient;
  26. public SatIpDiscovery(IDeviceDiscovery deviceDiscovery, IServerConfigurationManager config, ILogger logger, ILiveTvManager liveTvManager, IHttpClient httpClient)
  27. {
  28. _deviceDiscovery = deviceDiscovery;
  29. _config = config;
  30. _logger = logger;
  31. _liveTvManager = liveTvManager;
  32. _httpClient = httpClient;
  33. }
  34. public void Run()
  35. {
  36. _deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
  37. }
  38. void _deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
  39. {
  40. //string server = null;
  41. //if (e.Headers.TryGetValue("SERVER", out server) && server.IndexOf("HDHomeRun", StringComparison.OrdinalIgnoreCase) != -1)
  42. //{
  43. // string location;
  44. // if (e.Headers.TryGetValue("Location", out location))
  45. // {
  46. // //_logger.Debug("HdHomerun found at {0}", location);
  47. // // Just get the beginning of the url
  48. // Uri uri;
  49. // if (Uri.TryCreate(location, UriKind.Absolute, out uri))
  50. // {
  51. // var apiUrl = location.Replace(uri.LocalPath, String.Empty, StringComparison.OrdinalIgnoreCase)
  52. // .TrimEnd('/');
  53. // //_logger.Debug("HdHomerun api url: {0}", apiUrl);
  54. // AddDevice(apiUrl);
  55. // }
  56. // }
  57. //}
  58. }
  59. private async void AddDevice(string url)
  60. {
  61. await _semaphore.WaitAsync().ConfigureAwait(false);
  62. try
  63. {
  64. var options = GetConfiguration();
  65. if (options.TunerHosts.Any(i =>
  66. string.Equals(i.Type, SatIpHost.DeviceType, StringComparison.OrdinalIgnoreCase) &&
  67. UriEquals(i.Url, url)))
  68. {
  69. return;
  70. }
  71. // Strip off the port
  72. url = new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped).TrimEnd('/');
  73. await TestUrl(url).ConfigureAwait(false);
  74. await _liveTvManager.SaveTunerHost(new TunerHostInfo
  75. {
  76. Type = SatIpHost.DeviceType,
  77. Url = url
  78. }).ConfigureAwait(false);
  79. }
  80. catch (Exception ex)
  81. {
  82. _logger.ErrorException("Error saving device", ex);
  83. }
  84. finally
  85. {
  86. _semaphore.Release();
  87. }
  88. }
  89. private async Task TestUrl(string url)
  90. {
  91. // Test it by pulling down the lineup
  92. using (await _httpClient.Get(new HttpRequestOptions
  93. {
  94. Url = string.Format("{0}/lineup.json", url),
  95. CancellationToken = CancellationToken.None
  96. }))
  97. {
  98. }
  99. }
  100. private bool UriEquals(string savedUri, string location)
  101. {
  102. return string.Equals(NormalizeUrl(location), NormalizeUrl(savedUri), StringComparison.OrdinalIgnoreCase);
  103. }
  104. private string NormalizeUrl(string url)
  105. {
  106. if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  107. {
  108. url = "http://" + url;
  109. }
  110. url = url.TrimEnd('/');
  111. // Strip off the port
  112. return new Uri(url).GetComponents(UriComponents.AbsoluteUri & ~UriComponents.Port, UriFormat.UriEscaped);
  113. }
  114. private LiveTvOptions GetConfiguration()
  115. {
  116. return _config.GetConfiguration<LiveTvOptions>("livetv");
  117. }
  118. public void Dispose()
  119. {
  120. }
  121. }
  122. }