Skip to content

Commit b940549

Browse files
authored
Merge pull request #30 from HHCBakker/Return-make-model-on-discovery
Added DiscoverAsyncWithMake to return the make and model from the ONV…
2 parents 7960669 + 1b52007 commit b940549

File tree

8 files changed

+234
-90
lines changed

8 files changed

+234
-90
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
55
<Nullable>disable</Nullable>
66
<Title>$(ProjectName)</Title>
7-
<Version>0.3.0</Version>
7+
<Version>0.4.1</Version>
88
<Authors>Lukas Volf</Authors>
99
<Copyright>MIT</Copyright>
1010
<PackageProjectUrl>https://github.com/jimm98y/SharpOnvif</PackageProjectUrl>

src/OnvifClient/Program.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,27 @@ public static async Task Main(string[] args)
1919
static async Task MainAsync(string[] args)
2020
{
2121
var devices = await OnvifDiscoveryClient.DiscoverAsync();
22-
var device = devices.FirstOrDefault(x => x.Contains("localhost"));
22+
23+
if (devices == null || devices.Count == 0)
24+
{
25+
Console.WriteLine("No ONVIF devices found. Please check your network connection and try again.");
26+
return;
27+
}
28+
29+
foreach (var onvifDevice in devices)
30+
{
31+
Console.WriteLine($"Found device: Manufacturer = {onvifDevice.Manufacturer}, Model = {onvifDevice.Hardware}");
32+
}
33+
34+
var device = devices.FirstOrDefault(x => x.Addresses.First().Contains("localhost"));
2335

2436
if (device == null)
2537
{
2638
Console.WriteLine("Please run OnvifService on the localhost as Administrator, or use a different camera URL and credentials.");
2739
}
2840
else
2941
{
30-
using (var client = new SimpleOnvifClient(device, "admin", "password", true))
42+
using (var client = new SimpleOnvifClient(device.Addresses.First(), "admin", "password", true))
3143
{
3244
var services = await client.GetServicesAsync(true);
3345
var cameraDateTime = await client.GetSystemDateAndTimeUtcAsync();

src/OnvifService/appsettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
}
4747
],
4848
"MAC": null,
49-
"Hardware": null,
49+
"Manufacturer": "Lukas Volf",
50+
"Hardware": "OnvifService",
5051
"Name": null,
51-
"City": null
52+
"City": null,
53+
"Country": null
5254
}
5355
}

src/SharpOnvifClient/OnvifDiscoveryClient.cs

Lines changed: 134 additions & 77 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace SharpOnvifClient
2+
{
3+
/// <summary>
4+
/// Discovered device.
5+
/// </summary>
6+
public class OnvifDiscoveryResult
7+
{
8+
/// <summary>
9+
/// Raw SOAP message for advanced processing.
10+
/// </summary>
11+
public string Raw { get; set; }
12+
13+
/// <summary>
14+
/// Onvif addresses.
15+
/// </summary>
16+
public string[] Addresses { get; set; }
17+
18+
/// <summary>
19+
/// Onvif scopes.
20+
/// </summary>
21+
public string[] Scopes { get; set; }
22+
23+
/// <summary>
24+
/// Location - city.
25+
/// </summary>
26+
public string City { get; set; }
27+
28+
/// <summary>
29+
/// Location - country.
30+
/// </summary>
31+
public string Country { get; set; }
32+
33+
/// <summary>
34+
/// Hardware.
35+
/// </summary>
36+
public string Hardware { get; set; }
37+
38+
/// <summary>
39+
/// MAC address.
40+
/// </summary>
41+
public string MAC { get; set; }
42+
43+
/// <summary>
44+
/// Device manufacturer.
45+
/// </summary>
46+
public string Manufacturer { get; set; }
47+
48+
/// <summary>
49+
/// Device name.
50+
/// </summary>
51+
public string Name { get; set; }
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SharpOnvifCommon.Discovery
2+
{
3+
public static class Scopes
4+
{
5+
public const string MAC = "onvif://www.onvif.org/MAC/";
6+
public const string Manufacturer = "onvif://www.onvif.org/manufacturer/";
7+
public const string Hardware = "onvif://www.onvif.org/hardware/";
8+
public const string Name = "onvif://www.onvif.org/name/";
9+
public const string City = "onvif://www.onvif.org/location/city/";
10+
public const string Country = "onvif://www.onvif.org/location/country/";
11+
}
12+
}

src/SharpOnvifServer/Discovery/DiscoveryService.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,21 +268,27 @@ private static string BuildAddresses(OnvifDiscoveryOptions options, string fallb
268268

269269
private static string BuildScopes(OnvifDiscoveryOptions options)
270270
{
271-
List<string> scopes = options.Scopes;
271+
List<string> scopes = options.Scopes.ToList(); // because we are adding additional scopes here, we need a copy of the collection
272272
if (scopes == null)
273273
return string.Empty;
274274

275-
if (!string.IsNullOrEmpty(options.MAC))
276-
scopes.Add($"onvif://www.onvif.org/MAC/{Uri.EscapeDataString(options.MAC)}");
275+
if (!string.IsNullOrEmpty(options.City))
276+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.City}{Uri.EscapeDataString(options.City)}");
277+
278+
if (!string.IsNullOrEmpty(options.Country))
279+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.Country}{Uri.EscapeDataString(options.Country)}");
277280

278281
if (!string.IsNullOrEmpty(options.Hardware))
279-
scopes.Add($"onvif://www.onvif.org/hardware/{Uri.EscapeDataString(options.Hardware)}");
282+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.Hardware}{Uri.EscapeDataString(options.Hardware)}");
280283

281-
if (!string.IsNullOrEmpty(options.Name))
282-
scopes.Add($"onvif://www.onvif.org/name/{Uri.EscapeDataString(options.Name)}");
284+
if (!string.IsNullOrEmpty(options.MAC))
285+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.MAC}{Uri.EscapeDataString(options.MAC)}");
283286

284-
if (!string.IsNullOrEmpty(options.City))
285-
scopes.Add($"onvif://www.onvif.org/location/city/{Uri.EscapeDataString(options.City)}");
287+
if (!string.IsNullOrEmpty(options.Manufacturer))
288+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.Manufacturer}{Uri.EscapeDataString(options.Manufacturer)}");
289+
290+
if (!string.IsNullOrEmpty(options.Name))
291+
scopes.Add($"{SharpOnvifCommon.Discovery.Scopes.Name}{Uri.EscapeDataString(options.Name)}");
286292

287293
return string.Join(' ', scopes);
288294
}

src/SharpOnvifServer/Discovery/OnvifDiscoveryOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ public class OnvifDiscoveryOptions
99
public List<string> Scopes { get; set; }
1010
public List<OnvifType> Types { get; set; }
1111
public string MAC { get; set; }
12+
public string Manufacturer { get; set; }
1213
public string Hardware { get; set; }
1314
public string Name { get; set; }
1415
public string City { get; set; }
16+
public string Country { get; set; }
1517
}
1618
}

0 commit comments

Comments
 (0)