Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 115 additions & 1 deletion FaultRecordWindow.Diagnostics.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;

namespace ArIED61850Tester;

public partial class FaultRecordWindow
{
private bool _diagnosticHooksInstalled;
private string _selectedTransferDiagnostic =
"No transfer failure has been recorded. Run a download, then select a failed row.";

static FaultRecordWindow()
{
EventManager.RegisterClassHandler(
Expand All @@ -15,6 +22,12 @@ static FaultRecordWindow()
handledEventsToo: true);
}

public string SelectedTransferDiagnostic
{
get => _selectedTransferDiagnostic;
private set => Set(ref _selectedTransferDiagnostic, value ?? string.Empty);
}

private static void OnFaultRecordRowLoaded(object sender, RoutedEventArgs e)
{
if (sender is not DataGridRow row ||
Expand All @@ -34,4 +47,105 @@ private static void OnFaultRecordRowLoaded(object sender, RoutedEventArgs e)
});
ToolTipService.SetShowDuration(row, 30_000);
}
}

private void Diagnostics_ContentRendered(object? sender, EventArgs e)
{
if (_diagnosticHooksInstalled)
return;

_diagnosticHooksInstalled = true;
Records.CollectionChanged += DiagnosticRecords_CollectionChanged;
foreach (var row in Records)
AttachDiagnosticRow(row);
}

private void DiagnosticRecords_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var item in e.NewItems.OfType<FaultRecordRow>())
AttachDiagnosticRow(item);
}

if (e.OldItems != null)
{
foreach (var item in e.OldItems.OfType<FaultRecordRow>())
item.PropertyChanged -= DiagnosticRow_PropertyChanged;
}
}

private void AttachDiagnosticRow(FaultRecordRow row)
{
row.PropertyChanged -= DiagnosticRow_PropertyChanged;
row.PropertyChanged += DiagnosticRow_PropertyChanged;
}

private void DiagnosticRow_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not FaultRecordRow row ||
e.PropertyName != nameof(FaultRecordRow.Detail) ||
string.IsNullOrWhiteSpace(row.Detail) ||
!row.Status.Equals("Failed", StringComparison.OrdinalIgnoreCase))
{
return;
}

ShowTransferDiagnostic(row, expand: true);
}

private void FaultRecordsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (FaultRecordsGrid.SelectedItem is FaultRecordRow row &&
!string.IsNullOrWhiteSpace(row.Detail))
{
ShowTransferDiagnostic(
row,
expand: row.Status.Equals("Failed", StringComparison.OrdinalIgnoreCase));
}
}

private void ShowTransferDiagnostic(FaultRecordRow row, bool expand)
{
void Apply()
{
SelectedTransferDiagnostic = BuildVisibleDiagnostic(row);
if (!ReferenceEquals(FaultRecordsGrid.SelectedItem, row))
FaultRecordsGrid.SelectedItem = row;
FaultRecordsGrid.ScrollIntoView(row);
if (expand)
TransferDiagnosticsExpander.IsExpanded = true;
}

if (Dispatcher.CheckAccess())
Apply();
else
Dispatcher.BeginInvoke(DispatcherPriority.DataBind, new Action(Apply));
}

private string BuildVisibleDiagnostic(FaultRecordRow row)
=> $"Device : {DeviceName}\r\n" +
$"Endpoint : {EndpointText}\r\n" +
$"Record : {row.RecordName}\r\n" +
$"Remote directory : {row.RemoteDirectory}\r\n" +
$"Files : {row.FilesText}\r\n" +
$"Status : {row.Status}\r\n" +
$"Captured local : {DateTimeOffset.Now:O}\r\n" +
new string('=', 76) + "\r\n" +
row.Detail.Trim();

private void CopyDiagnostic_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(SelectedTransferDiagnostic))
return;

try
{
Clipboard.SetText(SelectedTransferDiagnostic);
StatusText = "Transfer diagnostic copied to the clipboard.";
}
catch (Exception ex)
{
StatusText = $"Could not copy the transfer diagnostic: {ex.Message}";
}
}
}
46 changes: 41 additions & 5 deletions FaultRecordWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="IEC 61850 Fault Records"
Width="1120" Height="720" MinWidth="900" MinHeight="560"
Width="1120" Height="760" MinWidth="900" MinHeight="600"
WindowStartupLocation="CenterOwner"
Background="#F4F7FB"
FontFamily="Aptos, Segoe UI Variable Text, Segoe UI, Calibri"
Loaded="Window_Loaded"
ContentRendered="Diagnostics_ContentRendered"
Closed="Window_Closed">
<Window.Resources>
<Style x:Key="PanelStyle" TargetType="Border">
Expand Down Expand Up @@ -80,6 +81,8 @@
<RowDefinition Height="*"/>
<RowDefinition Height="10"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Border Grid.Row="0" Style="{StaticResource PanelStyle}">
Expand Down Expand Up @@ -144,10 +147,11 @@
<TextBlock Text="Discovered COMTRADE records" FontSize="13.5" FontWeight="SemiBold" Foreground="#26364D"/>
<TextBlock Grid.Column="1" Text="{Binding SelectionSummary, Mode=OneWay}" FontSize="11.5" Foreground="#68778D" VerticalAlignment="Center"/>
</Grid>
<DataGrid Grid.Row="1" ItemsSource="{Binding Records, Mode=OneWay}" AutoGenerateColumns="False"
<DataGrid x:Name="FaultRecordsGrid" Grid.Row="1" ItemsSource="{Binding Records, Mode=OneWay}" AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False"
HeadersVisibility="Column" GridLinesVisibility="All" BorderThickness="0"
SelectionMode="Extended" SelectionUnit="FullRow" Background="White">
SelectionMode="Single" SelectionUnit="FullRow" Background="White"
SelectionChanged="FaultRecordsGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Get" Width="48">
<DataGridTemplateColumn.CellTemplate>
Expand All @@ -169,7 +173,39 @@
</Grid>
</Border>

<Border Grid.Row="6" Style="{StaticResource PanelStyle}">
<Expander x:Name="TransferDiagnosticsExpander" Grid.Row="6" IsExpanded="False"
Background="White" BorderBrush="#D9E2EF" BorderThickness="1" Padding="12,8">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Transfer diagnostics" FontSize="12.5" FontWeight="SemiBold" Foreground="#26364D"/>
<TextBlock Text=" • automatically opens on failure" FontSize="11.5" Foreground="#78869A" VerticalAlignment="Center"/>
</StackPanel>
</Expander.Header>
<Grid Margin="0,9,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="7"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Exact failing stage, invoke ID, FRSM, FileRead block, session state, and MMS request/response hex."
FontSize="11.5" Foreground="#607089" VerticalAlignment="Center"/>
<Button Grid.Column="1" Content="Copy diagnostics" Click="CopyDiagnostic_Click"
Style="{StaticResource SecondaryButtonStyle}" Height="30" Padding="12,3"/>
</Grid>
<TextBox Grid.Row="2" Text="{Binding SelectedTransferDiagnostic, Mode=OneWay}"
Height="170" IsReadOnly="True" AcceptsReturn="True" TextWrapping="NoWrap"
VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"
FontFamily="Cascadia Mono, Consolas, Courier New" FontSize="11"
Padding="9" Background="#F8FAFD" BorderBrush="#CBD8E8"/>
</Grid>
</Expander>

<Border Grid.Row="8" Style="{StaticResource PanelStyle}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
Expand All @@ -194,4 +230,4 @@
</Grid>
</Border>
</Grid>
</Window>
</Window>
Loading