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> /// <summary>
/// Sets or returns the universal service name (USN) of the device. /// Sets or returns the universal service name (USN) of the device.
/// </summary> /// </summary>
public string Usn => _device.Usn; public string? Usn => _device.Usn;
/// <summary> /// <summary>
/// Sets or returns a URL pointing to the device description document for this device. /// 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) 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() public void Dispose()

View File

@ -29,6 +29,11 @@
<Button Command="{Binding SearchDevicesNowCommand}" Content="Search now" /> <Button Command="{Binding SearchDevicesNowCommand}" Content="Search now" />
<Button Command="{Binding ResearchCommand}" Content="Search more" /> <Button Command="{Binding ResearchCommand}" Content="Search more" />
<Button Command="{Binding IpScanCommand}" Content="Scan ip" /> <Button Command="{Binding IpScanCommand}" Content="Scan ip" />
<TextBlock VerticalAlignment="Center">
<Run Text="Found" />
<Run Text="{Binding SddpDevices.Count}" />
<Run Text="Items" />
</TextBlock>
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>
<Grid Grid.Column="1" Grid.Row="0"> <Grid Grid.Column="1" Grid.Row="0">
@ -64,6 +69,11 @@
<DataGridTextColumn Binding="{Binding MacAddress}" Header="Mac address" /> <DataGridTextColumn Binding="{Binding MacAddress}" Header="Mac address" />
<DataGridTextColumn Binding="{Binding Version}" Header="Version" /> <DataGridTextColumn Binding="{Binding Version}" Header="Version" />
<DataGridTextColumn Binding="{Binding DiscoveredAt}" Header="Discovered at" /> <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.Columns>
</DataGrid> </DataGrid>
</Grid> </Grid>

View File

@ -24,25 +24,32 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
private static async void SddpDevices_OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) private static async void SddpDevices_OnCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{ {
switch (e.Action) try
{ {
case NotifyCollectionChangedAction.Add: switch (e.Action)
await AddAction(e.NewItems); {
break; case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Remove: await AddAction(e.NewItems);
RemoveAction(e.OldItems); break;
break; case NotifyCollectionChangedAction.Remove:
case NotifyCollectionChangedAction.Replace: RemoveAction(e.OldItems);
RemoveAction(e.OldItems); break;
await AddAction(e.NewItems); case NotifyCollectionChangedAction.Replace:
break; RemoveAction(e.OldItems);
case NotifyCollectionChangedAction.Reset: await AddAction(e.NewItems);
RemoveAction(sender as ObservableCollection<DiscoveredDeviceViewModel>); break;
break; case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Move: RemoveAction(sender as ObservableCollection<DiscoveredDeviceViewModel>);
break; break;
default: case NotifyCollectionChangedAction.Move:
throw new ArgumentOutOfRangeException(); 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 interNetwork = new IPAddress(ip);
var cidr = $"{interNetwork}/{SelectedNetworkAdapter.PrefixLength}"; var cidr = $"{interNetwork}/{SelectedNetworkAdapter.PrefixLength}";
var range = CidrHelper.GetIpRangeFromCidr(cidr); 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>(); var collection = new ConcurrentBag<DiscoveredDeviceViewModel>();
using var httpClient = new HttpClient(); using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(1); httpClient.Timeout = TimeSpan.FromSeconds(1);
@ -158,7 +165,7 @@ public sealed partial class MainWindowViewModel : ObservableObject, IDisposable
try try
{ {
token.ThrowIfCancellationRequested(); token.ThrowIfCancellationRequested();
DiscoveredSsdpDevice device = new DiscoveredSsdpDevice() DiscoveredSsdpDevice device = new()
{ {
CacheLifetime = TimeSpan.Zero, CacheLifetime = TimeSpan.Zero,
DescriptionLocation = new Uri($"http://{address.ToString()}:8080/discovery/ssdp"), 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 false;
}
return true; return true;
} }