Beginning of the Implementing multple cameras into the AiQ GUI
Features added: - Onvif discoverable - New Camera Type combo box - Access has be changed to be more scalable and dynamic in preparation for more cameras
This commit is contained in:
83
Network.cs
83
Network.cs
@@ -1,4 +1,6 @@
|
||||
using System.Net;
|
||||
using OnvifDiscovery;
|
||||
using OnvifDiscovery.Models;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
@@ -71,15 +73,15 @@ namespace AiQ_GUI
|
||||
}
|
||||
catch (TaskCanceledException ex)
|
||||
{
|
||||
return $"HTTP error calling {url}: {ex.Message}{Level.ERROR}";
|
||||
return $"HTTP error calling {url}: {ex.Message}";
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
return $"HTTP error calling {url}: {ex.Message}{Level.ERROR}";
|
||||
return $"HTTP error calling {url}: {ex.Message}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return $"Unexpected error calling {url}: {ex.Message}{Level.ERROR}";
|
||||
return $"Unexpected error calling {url}: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,9 +90,15 @@ namespace AiQ_GUI
|
||||
const int sendPort = 6666;
|
||||
const int receivePort = 6667;
|
||||
const int discoveryTimeoutMs = 1000;
|
||||
IList<string> FoundCams = [];
|
||||
|
||||
byte[] discoveryPacket = [0x50, 0x4f, 0x4c, 0x4c, 0xaf, 0xb0, 0xb3, 0xb3, 0xb6, 0x01, 0xa8, 0xc0, 0x0b, 0x1a, 0x00, 0x00];
|
||||
List<string> FoundCams = [];
|
||||
HashSet<string> discoveredIPs = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
byte[] discoveryPacket =
|
||||
[
|
||||
0x50, 0x4f, 0x4c, 0x4c, 0xaf, 0xb0, 0xb3, 0xb3,
|
||||
0xb6, 0x01, 0xa8, 0xc0, 0x0b, 0x1a, 0x00, 0x00
|
||||
];
|
||||
|
||||
async Task SendAndListen(IPAddress localIp)
|
||||
{
|
||||
@@ -101,41 +109,42 @@ namespace AiQ_GUI
|
||||
sender.Send(discoveryPacket, discoveryPacket.Length);
|
||||
}
|
||||
|
||||
using UdpClient receiver = new(receivePort); // Listen for replies on fixed port
|
||||
using UdpClient receiver = new(receivePort);
|
||||
receiver.Client.ReceiveTimeout = discoveryTimeoutMs;
|
||||
|
||||
DateTime timeout = DateTime.Now.AddMilliseconds(discoveryTimeoutMs);
|
||||
try
|
||||
|
||||
while (DateTime.Now < timeout)
|
||||
{
|
||||
while (DateTime.Now < timeout)
|
||||
if (receiver.Available > 0)
|
||||
{
|
||||
if (receiver.Available > 0)
|
||||
UdpReceiveResult result = await receiver.ReceiveAsync();
|
||||
byte[] recvBuffer = result.Buffer;
|
||||
|
||||
if (recvBuffer.Length >= 52)
|
||||
{
|
||||
UdpReceiveResult result = await receiver.ReceiveAsync();
|
||||
byte[] recvBuffer = result.Buffer;
|
||||
byte[] ipBytes = recvBuffer
|
||||
.Skip(recvBuffer.Length - 52)
|
||||
.Take(4)
|
||||
.Reverse()
|
||||
.ToArray();
|
||||
|
||||
if (recvBuffer.Length >= 52) // Safety check
|
||||
{
|
||||
byte[] ipBytes = recvBuffer.Skip(recvBuffer.Length - 52).Take(4).Reverse().ToArray();
|
||||
string ipToAdd = string.Join(".", ipBytes);
|
||||
string ip = string.Join(".", ipBytes);
|
||||
|
||||
if (!FoundCams.Contains(ipToAdd))
|
||||
FoundCams.Add(ipToAdd);
|
||||
}
|
||||
if (discoveredIPs.Add(ip))
|
||||
FoundCams.Add(ip);
|
||||
}
|
||||
|
||||
await Task.Delay(50); // brief wait to allow data in
|
||||
}
|
||||
}
|
||||
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
|
||||
{
|
||||
// No data received in time — normal case
|
||||
|
||||
await Task.Delay(50);
|
||||
}
|
||||
}
|
||||
|
||||
// Get first IPv4 interface (non-loopback)
|
||||
foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
|
||||
{
|
||||
if (ip.AddressFamily != AddressFamily.InterNetwork)
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
await SendAndListen(ip);
|
||||
@@ -143,9 +152,31 @@ namespace AiQ_GUI
|
||||
catch { }
|
||||
}
|
||||
|
||||
//ONVIF discovery
|
||||
try
|
||||
{
|
||||
Discovery onvifDiscovery = new();
|
||||
|
||||
await foreach (DiscoveryDevice device in onvifDiscovery.DiscoverAsync(5))
|
||||
{
|
||||
string ip = device.Address;
|
||||
|
||||
// Already found via UDP → skip
|
||||
if (!discoveredIPs.Add(ip))
|
||||
continue;
|
||||
|
||||
// ONVIF-only camera
|
||||
FoundCams.Add($"{ip} - Onvif");
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return FoundCams;
|
||||
}
|
||||
|
||||
|
||||
// Ping to make sure devices are connected to the network, be aware it isn't consistant across subnets.
|
||||
public async static Task<bool> PingIP(string ipAddress)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user