reworked code
This commit is contained in:
parent
c68f616377
commit
95b733ad4c
|
@ -106,8 +106,10 @@ namespace Emby.Dlna
|
||||||
throw new ArgumentNullException(nameof(deviceInfo));
|
throw new ArgumentNullException(nameof(deviceInfo));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
var profile = GetProfiles()
|
var profile = GetProfiles()
|
||||||
.FirstOrDefault(i => i.Identification != null && IsMatch(i.Identification, deviceInfo));
|
.FirstOrDefault(i => i.Identification != null && IsMatch(deviceInfo, i.Identification));
|
||||||
|
|
||||||
if (profile != null)
|
if (profile != null)
|
||||||
{
|
{
|
||||||
|
@ -120,6 +122,13 @@ namespace Emby.Dlna
|
||||||
|
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
catch (ArgumentException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error in profile comparison.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void LogUnmatchedProfile(DeviceIdentification profile)
|
private void LogUnmatchedProfile(DeviceIdentification profile)
|
||||||
{
|
{
|
||||||
|
@ -138,85 +147,82 @@ namespace Emby.Dlna
|
||||||
_logger.LogInformation(builder.ToString());
|
_logger.LogInformation(builder.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
|
/// <summary>
|
||||||
|
/// Attempts to match a device with a profile.
|
||||||
|
/// Rules:
|
||||||
|
/// - If the profile field has no value, the field matches irregardless of its contents.
|
||||||
|
/// - the profile field can be an exact match, or a reg exp.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="deviceInfo">The <see cref="DeviceIdentification"/> of the device.</param>
|
||||||
|
/// <param name="profileInfo">The <see cref="DeviceIdentification"/> of the profile.</param>
|
||||||
|
/// <returns><b>True</b> if they match.</returns>
|
||||||
|
public static bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
|
if (!IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
|
||||||
{
|
|
||||||
if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
|
if (!IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
|
||||||
{
|
|
||||||
if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
|
if (!IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
|
||||||
{
|
|
||||||
if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
|
if (!IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
|
||||||
{
|
|
||||||
if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelName))
|
if (!IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
|
||||||
{
|
|
||||||
if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
|
if (!IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
|
||||||
{
|
|
||||||
if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
|
if (!IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
|
||||||
{
|
|
||||||
if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
|
if (!IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
|
||||||
{
|
|
||||||
if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsRegexOrSubstringMatch(string input, string pattern)
|
public static bool IsRegexOrSubstringMatch(string input, string pattern)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrEmpty(pattern))
|
||||||
|
{
|
||||||
|
// In profile identification: An empty pattern matches anything.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(input))
|
||||||
|
{
|
||||||
|
// The profile contains a value, and the device doesn't.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
return input.Equals(pattern, StringComparison.OrdinalIgnoreCase)
|
||||||
|
|| Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
||||||
}
|
}
|
||||||
catch (ArgumentException ex)
|
catch (ArgumentException ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error evaluating regex pattern {Pattern}", pattern);
|
throw new ArgumentException("Error evaluating regex pattern " + pattern, ex);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Emby.Dlna;
|
||||||
using Emby.Dlna.PlayTo;
|
using Emby.Dlna.PlayTo;
|
||||||
using MediaBrowser.Model.Dlna;
|
using MediaBrowser.Model.Dlna;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
@ -12,92 +13,23 @@ namespace Jellyfin.Dlna.Tests
|
||||||
{
|
{
|
||||||
public class ProfileTester
|
public class ProfileTester
|
||||||
{
|
{
|
||||||
private bool IsMatch(DeviceIdentification deviceInfo, DeviceIdentification profileInfo)
|
|
||||||
{
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.FriendlyName))
|
|
||||||
{
|
|
||||||
if (deviceInfo.FriendlyName == null || !IsRegexOrSubstringMatch(deviceInfo.FriendlyName, profileInfo.FriendlyName))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.Manufacturer))
|
|
||||||
{
|
|
||||||
if (deviceInfo.Manufacturer == null || !IsRegexOrSubstringMatch(deviceInfo.Manufacturer, profileInfo.Manufacturer))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ManufacturerUrl))
|
|
||||||
{
|
|
||||||
if (deviceInfo.ManufacturerUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ManufacturerUrl, profileInfo.ManufacturerUrl))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelDescription))
|
|
||||||
{
|
|
||||||
if (deviceInfo.ModelDescription == null || !IsRegexOrSubstringMatch(deviceInfo.ModelDescription, profileInfo.ModelDescription))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelName))
|
|
||||||
{
|
|
||||||
if (deviceInfo.ModelName == null || !IsRegexOrSubstringMatch(deviceInfo.ModelName, profileInfo.ModelName))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelNumber))
|
|
||||||
{
|
|
||||||
if (deviceInfo.ModelNumber == null || !IsRegexOrSubstringMatch(deviceInfo.ModelNumber, profileInfo.ModelNumber))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.ModelUrl))
|
|
||||||
{
|
|
||||||
if (deviceInfo.ModelUrl == null || !IsRegexOrSubstringMatch(deviceInfo.ModelUrl, profileInfo.ModelUrl))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(profileInfo.SerialNumber))
|
|
||||||
{
|
|
||||||
if (deviceInfo.SerialNumber == null || !IsRegexOrSubstringMatch(deviceInfo.SerialNumber, profileInfo.SerialNumber))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool IsRegexOrSubstringMatch(string input, string pattern)
|
|
||||||
{
|
|
||||||
return input.Contains(pattern, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Test_Profile_Matches()
|
public void Test_Profile_Matches()
|
||||||
{
|
{
|
||||||
var source = new DeviceInfo()
|
var device = new DeviceInfo()
|
||||||
{
|
{
|
||||||
Name = "HelloWorld"
|
Name = "My Device",
|
||||||
|
Manufacturer = "LG Electronics",
|
||||||
|
ManufacturerUrl = "http://www.lge.com",
|
||||||
|
ModelDescription = "LG WebOSTV DMRplus",
|
||||||
|
ModelName = "LG TV",
|
||||||
|
ModelNumber = "1.0",
|
||||||
};
|
};
|
||||||
|
|
||||||
var dest = new DeviceProfile()
|
var profile = new DeviceProfile()
|
||||||
{
|
{
|
||||||
Name = "Test Subject 1",
|
Name = "Test Profile",
|
||||||
FriendlyName = "HelloWorld",
|
FriendlyName = "My Device",
|
||||||
Manufacturer = "LG Electronics",
|
Manufacturer = "LG Electronics",
|
||||||
ManufacturerUrl = "http://www.lge.com",
|
ManufacturerUrl = "http://www.lge.com",
|
||||||
ModelDescription = "LG WebOSTV DMRplus",
|
ModelDescription = "LG WebOSTV DMRplus",
|
||||||
|
@ -105,7 +37,7 @@ namespace Jellyfin.Dlna.Tests
|
||||||
ModelNumber = "1.0",
|
ModelNumber = "1.0",
|
||||||
Identification = new DeviceIdentification()
|
Identification = new DeviceIdentification()
|
||||||
{
|
{
|
||||||
FriendlyName = "HelloWorld",
|
FriendlyName = "My Device",
|
||||||
Manufacturer = "LG Electronics",
|
Manufacturer = "LG Electronics",
|
||||||
ManufacturerUrl = "http://www.lge.com",
|
ManufacturerUrl = "http://www.lge.com",
|
||||||
ModelDescription = "LG WebOSTV DMRplus",
|
ModelDescription = "LG WebOSTV DMRplus",
|
||||||
|
@ -114,7 +46,51 @@ namespace Jellyfin.Dlna.Tests
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.True(IsMatch(dest.Identification, source.ToDeviceIdentification()));
|
Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));
|
||||||
|
|
||||||
|
var profile2 = new DeviceProfile()
|
||||||
|
{
|
||||||
|
Name = "Test Profile",
|
||||||
|
FriendlyName = "My Device",
|
||||||
|
Identification = new DeviceIdentification()
|
||||||
|
{
|
||||||
|
FriendlyName = "My Device",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.True(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile2.Identification));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_Profile_NoMatch()
|
||||||
|
{
|
||||||
|
var device = new DeviceInfo()
|
||||||
|
{
|
||||||
|
Name = "My Device",
|
||||||
|
Manufacturer = "JVC"
|
||||||
|
};
|
||||||
|
|
||||||
|
var profile = new DeviceProfile()
|
||||||
|
{
|
||||||
|
Name = "Test Profile",
|
||||||
|
FriendlyName = "My Device",
|
||||||
|
Manufacturer = "LG Electronics",
|
||||||
|
ManufacturerUrl = "http://www.lge.com",
|
||||||
|
ModelDescription = "LG WebOSTV DMRplus",
|
||||||
|
ModelName = "LG TV",
|
||||||
|
ModelNumber = "1.0",
|
||||||
|
Identification = new DeviceIdentification()
|
||||||
|
{
|
||||||
|
FriendlyName = "My Device",
|
||||||
|
Manufacturer = "LG Electronics",
|
||||||
|
ManufacturerUrl = "http://www.lge.com",
|
||||||
|
ModelDescription = "LG WebOSTV DMRplus",
|
||||||
|
ModelName = "LG TV",
|
||||||
|
ModelNumber = "1.0",
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.False(DlnaManager.IsMatch(device.ToDeviceIdentification(), profile.Identification));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user