smaller fixes

This commit is contained in:
Holger Börchers 2025-04-07 15:53:45 +02:00
parent cb5ec30b99
commit 9811211bd0
3 changed files with 59 additions and 29 deletions

View File

@ -69,7 +69,7 @@ public partial class DiscoveredDeviceViewModel : ObservableObject, IDisposable
/// <summary>
/// Sets or returns the universal service name (USN) of the device.
/// </summary>
public string Usn => _device.Usn;
public string? Usn => _device.Usn;
/// <summary>
/// Sets or returns a URL pointing to the device description document for this device.
@ -138,7 +138,15 @@ public partial class DiscoveredDeviceViewModel : ObservableObject, IDisposable
public override bool Equals(object? obj)
{
return obj is DiscoveredDeviceViewModel viewModel && Equals(viewModel.Usn, Usn);
if (obj is not DiscoveredDeviceViewModel viewModel)
return false;
if (viewModel.Usn != null && !Equals(viewModel.Usn, Usn))
return false;
if (!Equals(viewModel.MacAddress, MacAddress))
return false;
if (!Equals(viewModel.IpAddress, IpAddress))
return false;
return true;
}
public void Dispose()

View File

@ -29,6 +29,11 @@
<Button Command="{Binding SearchDevicesNowCommand}" Content="Search now" />
<Button Command="{Binding ResearchCommand}" Content="Search more" />
<Button Command="{Binding IpScanCommand}" Content="Scan ip" />
<TextBlock VerticalAlignment="Center">
<Run Text="Found" />
<Run Text="{Binding SddpDevices.Count}" />
<Run Text="Items" />
</TextBlock>
</StackPanel>
</StackPanel>
<Grid Grid.Column="1" Grid.Row="0">
@ -64,6 +69,11 @@
<DataGridTextColumn Binding="{Binding MacAddress}" Header="Mac address" />
<DataGridTextColumn Binding="{Binding Version}" Header="Version" />
<DataGridTextColumn Binding="{Binding DiscoveredAt}" Header="Discovered at" />
<DataGridTemplateColumn Header="Presentation Page">
<DataTemplate DataType="sddpViewer:DiscoveredDeviceViewModel">
<HyperlinkButton Content="Open" NavigateUri="{Binding PresentationUrl}" ></HyperlinkButton>
</DataTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>

View File

@ -24,25 +24,32 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
private static async void SddpDevices_OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
try
{
case NotifyCollectionChangedAction.Add:
await AddAction(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveAction(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
RemoveAction(e.OldItems);
await AddAction(e.NewItems);
break;
case NotifyCollectionChangedAction.Reset:
RemoveAction(sender as ObservableCollection<DiscoveredDeviceViewModel>);
break;
case NotifyCollectionChangedAction.Move:
break;
default:
throw new ArgumentOutOfRangeException();
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
await AddAction(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
RemoveAction(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
RemoveAction(e.OldItems);
await AddAction(e.NewItems);
break;
case NotifyCollectionChangedAction.Reset:
RemoveAction(sender as ObservableCollection<DiscoveredDeviceViewModel>);
break;
case NotifyCollectionChangedAction.Move:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
@ -145,7 +152,7 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
var interNetwork = new IPAddress(ip);
var cidr = $"{interNetwork}/{SelectedNetworkAdapter.PrefixLength}";
var range = CidrHelper.GetIpRangeFromCidr(cidr);
var ipRange = IpRangeHelper.GetIPRange(range.Start, range.End).Where(IpAddressFilter);
var ipRange = IpRangeHelper.GetIPRange(range.Start, range.End).Where(address => IpAddressFilter(address, range));
var collection = new ConcurrentBag<DiscoveredDeviceViewModel>();
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(1);
@ -158,7 +165,7 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
try
{
token.ThrowIfCancellationRequested();
DiscoveredSsdpDevice device = new DiscoveredSsdpDevice()
DiscoveredSsdpDevice device = new()
{
CacheLifetime = TimeSpan.Zero,
DescriptionLocation = new Uri($"http://{address.ToString()}:8080/discovery/ssdp"),
@ -177,19 +184,24 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
}
);
foreach (DiscoveredDeviceViewModel viewModel in collection)
Dispatcher.UIThread.Invoke(() =>
{
Dispatcher.UIThread.Invoke(() => SddpDevices.Add(viewModel));
}
foreach (DiscoveredDeviceViewModel viewModel in collection)
{
SddpDevices.Add(viewModel);
}
});
}
}
private bool IpAddressFilter(IPAddress arg)
private bool IpAddressFilter(IPAddress arg, (IPAddress Start, IPAddress End) range)
{
if (this.SddpDevices.Any(x => Equals(x.IpAddress, arg)))
{
if (Equals(arg, range.Start))
return false;
if (Equals(arg, range.End))
return false;
if (this.SddpDevices.Any(x => Equals(x.IpAddress, arg)))
return false;
}
return true;
}