Version number parsing for XBMC Frodo

pull/6/head
Mark McDowall 12 years ago
parent 5294928878
commit b747db6b93

@ -241,6 +241,7 @@
<Compile Include="ProviderTests\SceneMappingProviderTest.cs" />
<Compile Include="ProviderTests\SeriesProviderTest.cs" />
<Compile Include="ProviderTests\TvDbProviderTest.cs" />
<Compile Include="XbmcVersionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NzbDrone.Common\NzbDrone.Common.csproj">

@ -93,12 +93,29 @@ namespace NzbDrone.Core.Test.ProviderTests
[TestCase(3)]
[TestCase(2)]
[TestCase(0)]
public void GetJsonVersion(int number)
public void GetJsonVersionIntOnly(int number)
{
//Setup
var message = "{\"id\":10,\"jsonrpc\":\"2.0\",\"result\":{\"version\":" + number + "}}";
var fakeHttp = Mocker.GetMock<HttpProvider>();
fakeHttp.Setup(s => s.PostCommand("localhost:8080", "xbmc", "xbmc", It.IsAny<string>()))
.Returns(message);
var message = "{\"id\":10,\"jsonrpc\":\"2.0\",\"result\":{\"version\":" + number + "}}";
//Act
var result = Mocker.Resolve<XbmcProvider>().GetJsonVersion("localhost:8080", "xbmc", "xbmc");
//Assert
result.Should().Be(new XbmcVersion(number));
}
[TestCase(5, 0, 0)]
[TestCase(6, 0, 0)]
[TestCase(6, 1, 0)]
[TestCase(6, 0, 23)]
[TestCase(0, 0, 0)]
public void GetJsonVersionFrodo(int major, int minor, int patch)
{
var message = "{\"id\":10,\"jsonrpc\":\"2.0\",\"result\":{\"version\":{\"major\":" + major + ",\"minor\":" + minor + ",\"patch\":" + patch + "}}}";
var fakeHttp = Mocker.GetMock<HttpProvider>();
fakeHttp.Setup(s => s.PostCommand("localhost:8080", "xbmc", "xbmc", It.IsAny<string>()))
@ -108,15 +125,12 @@ namespace NzbDrone.Core.Test.ProviderTests
var result = Mocker.Resolve<XbmcProvider>().GetJsonVersion("localhost:8080", "xbmc", "xbmc");
//Assert
Assert.AreEqual(number, result);
result.Should().Be(new XbmcVersion(major, minor, patch));
}
[Test]
public void GetJsonVersion_error()
{
//Setup
var message = "{\"error\":{\"code\":-32601,\"message\":\"Method not found.\"},\"id\":10,\"jsonrpc\":\"2.0\"}";
var fakeHttp = Mocker.GetMock<HttpProvider>();
@ -127,7 +141,7 @@ namespace NzbDrone.Core.Test.ProviderTests
var result = Mocker.Resolve<XbmcProvider>().GetJsonVersion("localhost:8080", "xbmc", "xbmc");
//Assert
Assert.AreEqual(0, result);
result.Should().Be(new XbmcVersion(0));
}
[TestCase(false, false, false)]

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Model.Xbmc;
using NzbDrone.Core.Repository.Quality;
namespace NzbDrone.Core.Test
{
public class XbmcVersionTests
{
[TestCase(6, 0, 0)]
[TestCase(5, 1, 0)]
[TestCase(5, 0, 1)]
public void Icomparer_greater_test(int major, int minor, int patch)
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(major, minor, patch);
second.Should().BeGreaterThan(first);
}
[TestCase(4, 5, 5)]
[TestCase(5, 4, 5)]
[TestCase(5, 5, 4)]
public void Icomparer_lesser_test(int major, int minor, int patch)
{
var first = new XbmcVersion(5, 5, 5);
var second = new XbmcVersion(major, minor, patch);
second.Should().BeLessThan(first);
}
[Test]
public void equal_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(5, 0, 0);
(first == second).Should().BeTrue();
(first >= second).Should().BeTrue();
(first <= second).Should().BeTrue();
}
[Test]
public void equal_operand_false()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first == second).Should().BeFalse();
}
[Test]
public void not_equal_operand_false()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(5, 0, 0);
(first != second).Should().BeFalse();
}
[Test]
public void not_equal_operand_true()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first != second).Should().BeTrue();
}
[Test]
public void greater_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first < second).Should().BeTrue();
(first <= second).Should().BeTrue();
}
[Test]
public void lesser_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(second > first).Should().BeTrue();
(second >= first).Should().BeTrue();
}
}
}

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Xbmc
{
public class XbmcJsonResult<T>
{
public string Id { get; set; }
public string JsonRpc { get; set; }
public T Result { get; set; }
}
}

@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NzbDrone.Core.Model.Xbmc
{
public class XbmcVersion : IComparable<XbmcVersion>
{
public XbmcVersion()
{
}
public XbmcVersion(int major)
{
Major = major;
}
public XbmcVersion(int major, int minor, int patch)
{
Major = major;
Minor = minor;
Patch = patch;
}
public int Major { get; set; }
public int Minor { get; set; }
public int Patch { get; set; }
public int CompareTo(XbmcVersion other)
{
if(other.Major > Major)
return -1;
if(other.Major < Major)
return 1;
if (other.Minor > Minor)
return -1;
if (other.Minor < Minor)
return 1;
if (other.Patch > Patch)
return -1;
if (other.Patch < Patch)
return 1;
return 0;
}
public static bool operator !=(XbmcVersion x, XbmcVersion y)
{
return !(x == y);
}
public static bool operator ==(XbmcVersion x, XbmcVersion y)
{
var xObj = (Object)x;
var yObj = (object)y;
if (xObj == null || yObj == null)
{
return xObj == yObj;
}
return x.CompareTo(y) == 0;
}
public static bool operator >(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) > 0;
}
public static bool operator <(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) < 0;
}
public static bool operator <=(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) <= 0;
}
public static bool operator >=(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) >= 0;
}
public override string ToString()
{
return String.Format("{0}.{1}.{2}", Major, Minor, Patch);
}
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + Major.GetHashCode();
hash = hash * 23 + Minor.GetHashCode();
hash = hash * 23 + Patch.GetHashCode();
return hash;
}
}
public bool Equals(XbmcVersion other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return (Equals(other.Major, Major) && Equals(other.Minor, Minor) && Equals(other.Patch, Patch));
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(XbmcVersion)) return false;
return Equals((XbmcVersion)obj);
}
}
}

@ -299,6 +299,8 @@
<Compile Include="Model\Xbmc\TvShowResult.cs" />
<Compile Include="Model\Xbmc\ErrorResult.cs" />
<Compile Include="Model\Xbmc\IconType.cs" />
<Compile Include="Model\Xbmc\XbmcJsonResult.cs" />
<Compile Include="Model\Xbmc\XbmcVersion.cs" />
<Compile Include="Model\Xem\XemResult.cs" />
<Compile Include="Model\Xem\XemSceneTvdbMapping.cs" />
<Compile Include="Model\Xem\XemValues.cs" />

@ -60,7 +60,7 @@ namespace NzbDrone.Core.Providers
var version = GetJsonVersion(host, username, password);
//If Dharma
if (version == 2)
if (version == new XbmcVersion(2))
{
//Check for active player only when we should skip updates when playing
if (!_configProvider.XbmcUpdateWhenPlaying)
@ -80,7 +80,26 @@ namespace NzbDrone.Core.Providers
}
//If Eden or newer (attempting to make it future compatible)
else if (version >= 3)
else if (version == new XbmcVersion(3) || version == new XbmcVersion(4))
{
//Check for active player only when we should skip updates when playing
if (!_configProvider.XbmcUpdateWhenPlaying)
{
Logger.Trace("Determining if there are any active players on XBMC host: {0}", host);
var activePlayers = GetActivePlayersEden(host, username, password);
//If video is currently playing, then skip update
if(activePlayers.Any(a => a.Type.Equals("video")))
{
Logger.Debug("Video is currently playing, skipping library update");
continue;
}
}
UpdateWithJson(series, host, username, password);
}
else if (version >= new XbmcVersion(5))
{
//Check for active player only when we should skip updates when playing
if (!_configProvider.XbmcUpdateWhenPlaying)
@ -239,12 +258,11 @@ namespace NzbDrone.Core.Providers
return field.Value;
}
public virtual int GetJsonVersion(string host, string username, string password)
public virtual XbmcVersion GetJsonVersion(string host, string username, string password)
{
//2 = Dharma
//3 = Eden/Nightly (as of July 2011)
var version = 0;
//3 & 4 = Eden
//5 & 6 = Frodo
try
{
@ -256,11 +274,20 @@ namespace NzbDrone.Core.Providers
var response = _httpProvider.PostCommand(host, username, password, postJson.ToString());
if (CheckForJsonError(response))
return version;
return new XbmcVersion();
Logger.Trace("Getting version from response");
var result = JsonConvert.DeserializeObject<VersionResult>(response);
result.Result.TryGetValue("version", out version);
var result = JsonConvert.DeserializeObject<XbmcJsonResult<JObject>>(response);
var versionObject = result.Result.Property("version");
if (versionObject.Value.Type == JTokenType.Integer)
return new XbmcVersion((int)versionObject.Value);
if(versionObject.Value.Type == JTokenType.Object)
return JsonConvert.DeserializeObject<XbmcVersion>(versionObject.Value.ToString());
throw new InvalidCastException("Unknown Version structure!: " + versionObject);
}
catch (Exception ex)
@ -268,7 +295,7 @@ namespace NzbDrone.Core.Providers
Logger.DebugException(ex.Message, ex);
}
return version;
return new XbmcVersion();
}
public virtual Dictionary<string, bool> GetActivePlayersDharma(string host, string username, string password)
@ -391,7 +418,7 @@ namespace NzbDrone.Core.Providers
{
Logger.Trace("Sending Test Notifcation to XBMC Host: {0}", host);
var version = GetJsonVersion(host, username, password);
if (version == 0)
if (version == new XbmcVersion())
throw new Exception("Failed to get JSON version in test");
}
}

Loading…
Cancel
Save