Repository navigation
Expand file tree
/
Copy pathComtradeWorkspaceWindow.xaml.cs
More file actions
214 lines (196 loc) · 7.26 KB
/
Copy pathComtradeWorkspaceWindow.xaml.cs
File metadata and controls
214 lines (196 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using ArIED61850Tester.Controls;
using ArIED61850Tester.Services;
namespace ArIED61850Tester;
public partial class ComtradeWorkspaceWindow : Window
{
private const int ExactSignalFrameLimit = 500_000;
private const int FullRecordAnalogBuckets = 4_096;
private const int FullRecordDigitalTransitionCap = 100_000;
private readonly ArdIrecNativeRecord _record;
private readonly SemaphoreSlim _nativeGate = new(1, 1);
private ComtradeSignalItem? _activeSignal;
internal ComtradeWorkspaceWindow(ArdIrecNativeRecord record)
{
_record = record;
InitializeComponent();
ResetViewButton.IsEnabled = false;
FullRecordButton.IsEnabled = false;
PopulateHeader();
PopulateSignals();
Closed += ComtradeWorkspaceWindow_Closed;
}
private async void ComtradeWorkspaceWindow_Closed(object? sender, EventArgs e)
{
try
{
await _nativeGate.WaitAsync().ConfigureAwait(true);
try
{
_record.Dispose();
}
finally
{
_nativeGate.Release();
}
}
finally
{
_nativeGate.Dispose();
}
}
private void PopulateHeader()
{
var info = _record.Info;
var fileName = Path.GetFileNameWithoutExtension(_record.CfgPath);
RecordIdentityText.Text = $"{fileName} • COMTRADE {info.RevisionYear} • {FormatDataFormat(info.DataFormat)} • {info.StartTime}";
StationText.Text = string.IsNullOrWhiteSpace(info.RecorderId)
? info.StationName
: $"{info.StationName} • {info.RecorderId}";
ChannelsText.Text = $"{info.AnalogCount} analog • {info.StatusCount} digital";
FrequencyText.Text = info.NominalFrequency > 0 ? $"{info.NominalFrequency:G5} Hz" : "—";
FramesText.Text = $"{info.FrameCount:N0}";
StatusTextBlock.Text = $"Loaded {Path.GetFileName(_record.CfgPath)} • trigger {info.TriggerTime}";
}
private void PopulateSignals()
{
var signals = new List<ComtradeSignalItem>(_record.AnalogChannels.Count + _record.StatusChannels.Count);
for (var i = 0; i < _record.AnalogChannels.Count; i++)
{
var channel = _record.AnalogChannels[i];
_record.TryReadAnalogSemantics(checked((uint)i), out var semantics);
var section = ResolveAnalogSection(channel.Units, semantics);
var sectionOrder = section switch
{
"Voltage" => 0,
"Current" => 1,
_ => 2
};
var phaseOrder = ResolvePhaseOrder(channel.Phase, channel.Id, semantics);
var context = string.Join(" • ", new[] { channel.Phase, channel.Circuit, channel.Units }
.Where(value => !string.IsNullOrWhiteSpace(value)));
signals.Add(new ComtradeSignalItem(
true,
checked((uint)i),
channel.Id,
context,
FrozenSignalBrush(ResolveSignalColor(channel.Phase, channel.Id, false)),
section,
sectionOrder,
phaseOrder));
}
for (var i = 0; i < _record.StatusChannels.Count; i++)
{
var channel = _record.StatusChannels[i];
var context = string.Join(" • ", new[] { channel.Phase, channel.Circuit, $"normal {channel.NormalState}" }
.Where(value => !string.IsNullOrWhiteSpace(value)));
signals.Add(new ComtradeSignalItem(
false,
checked((uint)i),
channel.Id,
context,
FrozenSignalBrush(ResolveSignalColor(channel.Phase, channel.Id, true)),
"Digital Events",
3,
ResolvePhaseOrder(channel.Phase, channel.Id, null)));
}
var ordered = signals
.OrderBy(item => item.SectionOrder)
.ThenBy(item => item.PhaseOrder)
.ThenBy(item => item.Index)
.ToList();
SignalList.ItemsSource = ordered;
var view = CollectionViewSource.GetDefaultView(ordered);
view.GroupDescriptions.Add(new PropertyGroupDescription(nameof(ComtradeSignalItem.Section)));
SignalCountText.Text = $"{signals.Count} total";
if (signals.Count > 0)
SignalList.SelectedIndex = 0;
else
{
NavigationTextBlock.Text = "No COMTRADE channels.";
ResetViewButton.IsEnabled = false;
FullRecordButton.IsEnabled = false;
WaveformView.ShowMessage("No channels", "The COMTRADE record contains no analog or digital channels.");
}
}
private static string ResolveAnalogSection(string units, ComtradeAnalogSemantics? semantics)
{
if (semantics is not null)
{
return semantics.Role switch
{
1 => "Voltage",
2 => "Current",
_ => "Other Analog"
};
}
var normalized = (units ?? string.Empty).Trim().ToUpperInvariant().Replace(" ", string.Empty, StringComparison.Ordinal);
if (normalized is "V" or "KV" or "MV" or "UV") return "Voltage";
if (normalized is "A" or "KA" or "MA" or "UA") return "Current";
return "Other Analog";
}
private static int ResolvePhaseOrder(string phase, string title, ComtradeAnalogSemantics? semantics)
{
if (semantics is not null)
{
return semantics.PhaseRole switch
{
1 => 0,
2 => 1,
3 => 2,
4 => 3,
_ => 4
};
}
return NormalizePhase(phase, title) switch
{
"L1" => 0,
"L2" => 1,
"L3" => 2,
"N" or "E" => 3,
_ => 4
};
}
private static Brush FrozenSignalBrush(Color color)
{
var brush = new SolidColorBrush(color);
brush.Freeze();
return brush;
}
private void SignalList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SignalList.SelectedItem is ComtradeSignalItem signal)
_activeSignal = signal;
}
private static long ToSignedDelta(ulong frameCount, double fraction)
{
if (!double.IsFinite(fraction) || fraction == 0 || frameCount == 0)
return 0;
var magnitude = Math.Abs(fraction) * frameCount;
if (!double.IsFinite(magnitude) || magnitude >= long.MaxValue)
return fraction < 0 ? long.MinValue + 1 : long.MaxValue;
var rounded = checked((long)Math.Round(magnitude, MidpointRounding.AwayFromZero));
return fraction < 0 ? -rounded : rounded;
}
private static string FormatDataFormat(int format) => format switch
{
0 => "ASCII",
1 => "BINARY",
2 => "BINARY32",
3 => "FLOAT32",
_ => "UNKNOWN"
};
private sealed record ComtradeSignalItem(
bool IsAnalog,
uint Index,
string Title,
string Subtitle,
Brush Accent,
string Section,
int SectionOrder,
int PhaseOrder);
}