Skip to content

Commit e9daecc

Browse files
authored
Implements less permissive iodd search (#18)
* Implements less permissive iodd search Adds test * Fixes test --------- Co-authored-by: Dominik Deschner <domdeger@gmail.com>
1 parent cd4fb88 commit e9daecc

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed
+24-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
21
using System.IO.Compression;
32
using System.Xml.Linq;
4-
53
using IOLinkNET.IODD.Structure;
64

75
namespace IOLinkNET.IODD.Provider;
@@ -10,25 +8,42 @@ public class DeviceDefinitionProvider : IDeviceDefinitionProvider
108
{
119
private readonly IIODDProvider _ioddProvider;
1210
private readonly IODDParser _ioddParser = new IODDParser();
11+
1312
public DeviceDefinitionProvider(IIODDProvider ioddProvider)
1413
{
1514
_ioddProvider = ioddProvider;
1615
}
1716

18-
public async Task<IODevice> GetDeviceDefinitionAsync(ushort vendorId, uint deviceId, string productId, CancellationToken cancellationToken = default)
17+
public async Task<IODevice> GetDeviceDefinitionAsync(
18+
ushort vendorId,
19+
uint deviceId,
20+
string productId,
21+
CancellationToken cancellationToken = default
22+
)
1923
{
20-
var ioddPackage = await _ioddProvider.GetIODDPackageAsync(vendorId, deviceId, productId, cancellationToken);
24+
var ioddPackage = await _ioddProvider.GetIODDPackageAsync(
25+
vendorId,
26+
deviceId,
27+
productId,
28+
cancellationToken
29+
);
2130

2231
using var zipArchive = new ZipArchive(ioddPackage, ZipArchiveMode.Read);
2332
var ioddXml = await FindMainIoddEntryAsync(zipArchive, cancellationToken);
2433

25-
26-
return _ioddParser.Parse(ioddXml.Root ?? throw new InvalidOperationException("No root element found"));
34+
return _ioddParser.Parse(
35+
ioddXml.Root ?? throw new InvalidOperationException("No root element found")
36+
);
2737
}
2838

29-
private async Task<XDocument> FindMainIoddEntryAsync(ZipArchive zipArchive, CancellationToken cancellationToken)
39+
private async Task<XDocument> FindMainIoddEntryAsync(
40+
ZipArchive zipArchive,
41+
CancellationToken cancellationToken
42+
)
3043
{
31-
var xmlFiles = zipArchive.Entries.Where(e => e.Name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase));
44+
var xmlFiles = zipArchive.Entries.Where(e =>
45+
e.Name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)
46+
);
3247

3348
foreach (var xmlFile in xmlFiles)
3449
{
@@ -42,4 +57,4 @@ private async Task<XDocument> FindMainIoddEntryAsync(ZipArchive zipArchive, Canc
4257

4358
throw new InvalidOperationException("No matching IODD file found");
4459
}
45-
}
60+
}

src/IODD.Provider/IODDFinderPublicClient.cs

+24-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Net.Http;
22
using System.Net.Http.Json;
3-
43
using IOLinkNET.IODD.Provider.Data;
54

65
namespace IOLinkNET.IODD.Provider;
@@ -22,34 +21,48 @@ public IODDFinderPublicClient(Uri baseUrl, bool shouldValidateSSLCertificate = t
2221
ServerCertificateCustomValidationCallback = (request, cert, chain, errors) =>
2322
{
2423
return true;
25-
}
24+
},
2625
};
2726
_httpClient = new(handler) { BaseAddress = baseUrl };
2827
}
2928
}
3029

31-
public IODDFinderPublicClient() : this(new Uri("https://ioddfinder.io-link.com/"))
32-
{
33-
}
30+
public IODDFinderPublicClient()
31+
: this(new Uri("https://ioddfinder.io-link.com/")) { }
3432

35-
public async Task<Stream> GetIODDPackageAsync(ushort vendorId, uint deviceId, string productId, CancellationToken cancellationToken = default)
33+
public async Task<Stream> GetIODDPackageAsync(
34+
ushort vendorId,
35+
uint deviceId,
36+
string productId,
37+
CancellationToken cancellationToken = default
38+
)
3639
{
37-
var entries = await _httpClient.GetFromJsonAsync<IODDFinderSearchResponse>($"api/drivers?status=APPROVED&status=UPLOADED&vendorId={vendorId}&deviceId={deviceId}&productId={productId}");
40+
var entries = await _httpClient.GetFromJsonAsync<IODDFinderSearchResponse>(
41+
$"api/drivers?status=APPROVED&status=UPLOADED&vendorId={vendorId}&deviceId={deviceId}&productId={productId}"
42+
);
3843
if (entries is null)
3944
{
4045
throw new InvalidOperationException("Could not deserialize response");
4146
}
4247

4348
if (entries.Content.Count() < 1)
4449
{
45-
throw new InvalidOperationException("No entries found");
46-
}
50+
entries = await _httpClient.GetFromJsonAsync<IODDFinderSearchResponse>(
51+
$"api/drivers?status=APPROVED&status=UPLOADED&vendorId={vendorId}&deviceId={deviceId}"
52+
);
4753

54+
if (entries?.Content.Count() < 1)
55+
{
56+
throw new InvalidOperationException("No entries found");
57+
}
58+
}
4859

4960
var entry = entries.Content.OrderByDescending(x => x.IoLinkRev).First();
5061

51-
var zipStream = await _httpClient.GetStreamAsync($"api/vendors/{vendorId}/iodds/{entry.IoddId}/files/zip/rated");
62+
var zipStream = await _httpClient.GetStreamAsync(
63+
$"api/vendors/{vendorId}/iodds/{entry.IoddId}/files/zip/rated"
64+
);
5265

5366
return zipStream;
5467
}
55-
}
68+
}

src/Tests/IODD.Provider.Tests/DeviceDefinitionProviderTests.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace IODD.Provider.Tests;
55
public class DeviceDefinitionProviderTests
66
{
77
private readonly Uri _baseUrl = new("https://ioddfinder.io-link.com/");
8+
89
[Fact]
910
public async Task DoesLoadDeviceDefinitionAsync()
1011
{
@@ -14,4 +15,15 @@ public async Task DoesLoadDeviceDefinitionAsync()
1415

1516
definition.ProfileBody.DeviceIdentity.VendorId.Should().Be(888);
1617
}
17-
}
18+
19+
[Fact]
20+
public async Task DoesLoadDeviceDefinitionForIfmDeviceAsync()
21+
{
22+
var client = new IODDFinderPublicClient(_baseUrl);
23+
var provider = new DeviceDefinitionProvider(client);
24+
var definition = await provider.GetDeviceDefinitionAsync(310, 1367, "VVB001 Status B");
25+
26+
definition.ProfileBody.DeviceIdentity.VendorId.Should().Be(310);
27+
definition.ProfileBody.DeviceIdentity.DeviceId.Should().Be(1367);
28+
}
29+
}

0 commit comments

Comments
 (0)