69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.NetworkInformation;
|
|
using System.Net.Sockets;
|
|
|
|
namespace SddpViewer;
|
|
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading.Tasks;
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
using Rssdp;
|
|
|
|
public partial class MainWindowViewModel : ObservableObject
|
|
{
|
|
public MainWindowViewModel()
|
|
{
|
|
NetworkAdapters = NetworkAdapter.GetAvailableNetworkAdapter().ToArray();
|
|
SelectedNetworkAdapter = NetworkAdapters.FirstOrDefault();
|
|
}
|
|
|
|
[ObservableProperty]
|
|
private IReadOnlyList<NetworkAdapter> _networkAdapters;
|
|
|
|
[ObservableProperty]
|
|
private NetworkAdapter? _selectedNetworkAdapter;
|
|
|
|
[ObservableProperty]
|
|
private string _deviceIpAddress = "192.168.42.193";
|
|
|
|
[ObservableProperty]
|
|
private string _notificationFilter = "upnp:rootdevice";
|
|
|
|
public ObservableCollection<DiscoveredDeviceViewModel> SddpDevices { get; } = new();
|
|
|
|
[RelayCommand(AllowConcurrentExecutions = false)]
|
|
private async Task SearchDevicesNowAsync()
|
|
{
|
|
SddpDevices.Clear();
|
|
if (SelectedNetworkAdapter is null)
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
var locator = new SsdpDeviceLocator(SelectedNetworkAdapter.IpAddress.ToString());
|
|
if (!string.IsNullOrWhiteSpace(NotificationFilter))
|
|
{
|
|
locator.NotificationFilter = NotificationFilter;
|
|
}
|
|
var availableDevices = await locator.SearchAsync().ConfigureAwait(true);
|
|
foreach (var ssdpDevice in availableDevices)
|
|
{
|
|
var discoveredDeviceViewModel = new DiscoveredDeviceViewModel(ssdpDevice);
|
|
SddpDevices.Add(discoveredDeviceViewModel);
|
|
}
|
|
await Parallel
|
|
.ForEachAsync(
|
|
SddpDevices,
|
|
async (device, token) =>
|
|
{
|
|
await device.GetFurtherInformationAsync().ConfigureAwait(true);
|
|
}
|
|
)
|
|
.ConfigureAwait(true);
|
|
}
|
|
}
|