-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainWindow.IoTesting.AutoConnect.cs
More file actions
367 lines (327 loc) · 15.6 KB
/
Copy pathMainWindow.IoTesting.AutoConnect.cs
File metadata and controls
367 lines (327 loc) · 15.6 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using ArIED61850Tester.Models;
using ArIED61850Tester.Models.IoTesting;
using ArIED61850Tester.Services.IoTesting;
namespace ArIED61850Tester;
public partial class MainWindow
{
private readonly IoTestSignalSelectionService _ioTestSignalSelectionService = new();
/// <summary>
/// Prepares one imported IO-list IED for a monitoring-only FAT session. The workbook
/// supplies the endpoint and exact signal scope, so the operator does not need to
/// duplicate Add IED and signal-selection work in the engineering window.
/// </summary>
internal async Task<IoTestSessionActionResult> PrepareIoTestIedForFatAsync(
IoTestProject project,
IoTestIedPlan ied,
IProgress<string>? progress = null)
{
ArgumentNullException.ThrowIfNull(project);
ArgumentNullException.ThrowIfNull(ied);
var requestedPoints = ied.TestPoints
.Where(point => point.TestEnabled && point.ImportReady)
.ToList();
if (requestedPoints.Count == 0)
return IoTestSessionActionResult.Failure("No import-ready IO-list signal is enabled for this IED.");
void ReportProgress(string message)
{
ied.SetPreparationState(true, message);
progress?.Report(message);
}
ied.SetPreparationState(true, $"Connecting {ied.IedName} · {ied.IpAddress}:102");
var device = ResolveIoTestDevice(ied.LiveDeviceId)
?? ResolveIoTestDevice(ied.IpAddress)
?? ResolveIoTestDevice(ied.IedName);
var createdFromWorkbook = device == null;
if (device == null)
{
device = new Iec61850MonitorDevice
{
Name = ied.IedName,
SclIedName = ied.IedName,
IdentitySource = "IO List workbook",
IpAddress = ied.IpAddress,
Port = 102,
AllowDynamicDataSetWrites = true,
Status = "IO FAT ready to connect",
Detail = "Connect & Start will discover the live model and arm report-first acquisition for the imported FAT scope."
};
Devices.Add(device);
RaiseWorkspaceCounts();
}
if (device.IsBusy)
{
ied.SetPreparationState(false, "IED is busy in another connection workflow");
return IoTestSessionActionResult.Failure($"{ied.IedName} is already busy with another connection or discovery workflow.");
}
if (!device.IpAddress.Equals(ied.IpAddress, StringComparison.OrdinalIgnoreCase))
{
if (device.IsConnected || device.IsMonitoring)
{
ied.SetPreparationState(false, "Endpoint mismatch");
return IoTestSessionActionResult.Failure(
$"The loaded {ied.IedName} workspace is connected to {device.IpAddress}, but the IO list requires {ied.IpAddress}. Stop that engineering session or correct the workbook endpoint before FAT.");
}
device.IpAddress = ied.IpAddress;
device.Port = 102;
}
if (string.IsNullOrWhiteSpace(device.Name) || device.Name.Equals(device.IpAddress, StringComparison.OrdinalIgnoreCase))
device.Name = ied.IedName;
if (string.IsNullOrWhiteSpace(device.SclIedName))
device.SclIedName = ied.IedName;
// Monitoring-only toward the process: no control commands are executed. Use
// configured RCB/DataSet coverage first, then an association-scoped temporary
// dynamic DataSet/URCB when exact coverage is missing, with bounded MMS
// verification/fallback last. Temporary report resources are released when the
// native monitoring session stops.
device.AllowDynamicDataSetWrites = true;
try
{
var usedSavedModel = false;
if (!device.IsConnected)
{
var canUseSavedModel = device.HasDiscoveryCache && device.Signals.Count > 0;
var connected = false;
if (canUseSavedModel)
{
ReportProgress($"Fast reconnect {ied.IedName} · saved endpoint and discovery model");
connected = await ConnectUsingSavedModelAsync(device, selectDevice: false);
usedSavedModel = connected;
if (!connected)
{
ReportProgress("Saved-model reconnect failed · running one full live discovery");
connected = await ConnectAndConfigureDeviceAsync(device, openWizard: false, selectDevice: false);
usedSavedModel = false;
}
}
else
{
ReportProgress($"Connecting {ied.IedName} · {ied.IpAddress}:102");
connected = await ConnectAndConfigureDeviceAsync(device, openWizard: false, selectDevice: false);
}
if (!connected)
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure(
$"ARSAS could not connect to {ied.IedName} at {ied.IpAddress}:102. Open Diagnostics for the MMS association or discovery error.");
}
}
else if (device.Signals.Count == 0)
{
ReportProgress($"{ied.IedName} connected · discovering live model");
await StopDeviceConnectionAsync(device);
if (!await ConnectAndConfigureDeviceAsync(device, openWizard: false, selectDevice: false))
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure($"Full live-model discovery failed for {ied.IedName}.");
}
}
else
{
ReportProgress($"{ied.IedName} association ready · reusing the loaded model");
}
ReportProgress($"Matching {requestedPoints.Count} workbook signal(s)");
var selection = _ioTestSignalSelectionService.Resolve(ied, device);
if (!selection.Succeeded && selection.CanRetryWithFreshDiscovery)
{
ReportProgress("Refreshing live model once · saved model missed workbook points");
if (device.IsMonitoring)
await StopDeviceMonitorAsync(device);
if (device.IsConnected)
await StopDeviceConnectionAsync(device);
if (!await ConnectAndConfigureDeviceAsync(device, openWizard: false, selectDevice: false))
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure($"Live-model refresh failed for {ied.IedName}.");
}
usedSavedModel = false;
selection = _ioTestSignalSelectionService.Resolve(ied, device);
}
if (!selection.Succeeded)
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure(
$"ARSAS could not prepare the imported FAT scope safely. {selection.Message}");
}
var selectionChanged = false;
foreach (var match in selection.Matches)
{
if (match.Signal.IsSelected)
continue;
match.Signal.IsSelected = true;
selectionChanged = true;
}
device.RecountSelectedSignals();
device.RefreshComputed();
RaiseWorkspaceCounts();
_ioTestLiveBindingService.Bind(project, Devices);
var allRequestedPointsLive = requestedPoints.All(point =>
point.LiveBindingState == IoTestLiveBindingState.LivePointReady);
if (device.IsMonitoring && (selectionChanged || !allRequestedPointsLive))
{
ReportProgress("Refreshing report acquisition for the workbook scope");
await StopDeviceMonitorAsync(device);
}
if (!device.IsMonitoring)
{
ReportProgress("Arming static RCB first · dynamic DataSet/URCB for uncovered points");
if (!await StartDeviceMonitorAsync(device, navigateToExplorer: false))
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure(
$"{ied.IedName} connected, but ARSAS could not start live acquisition for the imported FAT scope.");
}
}
var acquisition = await SettleIoFatReportPriorityAsync(
project,
requestedPoints,
device,
ReportProgress,
allowSingleRestart: true);
var binding = _ioTestLiveBindingService.Bind(project, Devices);
var liveCount = requestedPoints.Count(point =>
point.LiveBindingState == IoTestLiveBindingState.LivePointReady);
if (liveCount != requestedPoints.Count)
{
var unresolved = requestedPoints
.Where(point => point.LiveBindingState != IoTestLiveBindingState.LivePointReady)
.Take(4)
.Select(point => $"{point.TestPointId} ({point.ObjectReference})");
return IoTestSessionActionResult.Failure(
$"{ied.IedName} is connected and monitoring, but only {liveCount}/{requestedPoints.Count} imported signal(s) became live. Unresolved: {string.Join(", ", unresolved)}.");
}
SaveSignalSelectionMemory(device);
var modelText = usedSavedModel ? "saved model" : "live model";
var acquisitionText = acquisition.PollingCount == 0
? $"report-backed {acquisition.ReportCount}/{requestedPoints.Count}"
: $"report-backed {acquisition.ReportCount}/{requestedPoints.Count} · MMS fallback {acquisition.PollingCount}";
var message = $"{ied.IedName} · {liveCount}/{requestedPoints.Count} live · {acquisitionText}";
SetStatus(message);
AddLog(
acquisition.PollingCount == 0 ? "INFO" : "WARN",
"IO Testing",
$"{message}. Acquisition policy: configured RCB → temporary dynamic DataSet/URCB → bounded MMS verification/fallback. No process control commands are enabled. Project live-bound={binding.LivePointCount}; model={modelText}; mode={device.AcquisitionMode}.");
ReportProgress(message);
return IoTestSessionActionResult.Success(message);
}
catch (OperationCanceledException)
{
_ioTestLiveBindingService.Bind(project, Devices);
return IoTestSessionActionResult.Failure($"Connection preparation for {ied.IedName} was cancelled.");
}
catch (Exception ex)
{
_ioTestLiveBindingService.Bind(project, Devices);
AddLog("ERROR", "IO Testing", $"{ied.IedName} automatic preparation failed: {ex}");
MarkDiagnosticAlert();
return IoTestSessionActionResult.Failure(
$"Automatic connection preparation failed for {ied.IedName}: {ex.Message}");
}
finally
{
ied.SetPreparationState(false, ied.LiveStatusText);
if (createdFromWorkbook)
RaiseWorkspaceCounts();
}
}
private async Task<IoFatAcquisitionSummary> SettleIoFatReportPriorityAsync(
IoTestProject project,
IReadOnlyCollection<IoTestPointPlan> requestedPoints,
Iec61850MonitorDevice device,
Action<string> reportProgress,
bool allowSingleRestart)
{
var first = await ObserveIoFatAcquisitionAsync(
project,
requestedPoints,
device,
reportProgress,
TimeSpan.FromSeconds(8));
if (!allowSingleRestart ||
first.PollingCount == 0 ||
!device.AllowDynamicDataSetWrites ||
!device.IsMonitoring)
{
return first;
}
reportProgress($"{first.PollingCount} point(s) still on MMS fallback · rebuilding the report plan once");
await StopDeviceMonitorAsync(device);
await Task.Delay(180);
if (!await StartDeviceMonitorAsync(device, navigateToExplorer: false))
return first;
return await ObserveIoFatAcquisitionAsync(
project,
requestedPoints,
device,
reportProgress,
TimeSpan.FromSeconds(10));
}
private async Task<IoFatAcquisitionSummary> ObserveIoFatAcquisitionAsync(
IoTestProject project,
IReadOnlyCollection<IoTestPointPlan> requestedPoints,
Iec61850MonitorDevice device,
Action<string> reportProgress,
TimeSpan timeout)
{
var deadline = DateTime.UtcNow + timeout;
var last = ReadIoFatAcquisitionSummary(requestedPoints, device);
var announced = false;
while (DateTime.UtcNow < deadline && device.IsMonitoring)
{
await Task.Delay(120);
_ioTestLiveBindingService.Bind(project, Devices);
last = ReadIoFatAcquisitionSummary(requestedPoints, device);
if (!announced)
{
reportProgress("Validating static/dynamic report coverage · MMS is fallback only");
announced = true;
}
var plannerSettled = !ContainsPlannerStage(device.AcquisitionMode);
if (last.LiveCount == requestedPoints.Count &&
plannerSettled &&
last.UnknownCount == 0 &&
(last.PollingCount == 0 || DateTime.UtcNow >= deadline - TimeSpan.FromSeconds(2)))
{
break;
}
}
return last;
}
private static IoFatAcquisitionSummary ReadIoFatAcquisitionSummary(
IEnumerable<IoTestPointPlan> requestedPoints,
Iec61850MonitorDevice device)
{
var live = 0;
var report = 0;
var polling = 0;
var unknown = 0;
foreach (var point in requestedPoints)
{
if (point.LiveBindingState == IoTestLiveBindingState.LivePointReady)
live++;
var source = point.Runtime.CurrentSource ?? string.Empty;
if (IsReportSource(source))
report++;
else if (source.Contains("poll", StringComparison.OrdinalIgnoreCase) ||
source.Contains("MMS", StringComparison.OrdinalIgnoreCase))
polling++;
else
unknown++;
}
return new IoFatAcquisitionSummary(live, report, polling, unknown, device.AcquisitionMode ?? string.Empty);
}
private static bool IsReportSource(string source)
=> source.Contains("BRCB", StringComparison.OrdinalIgnoreCase) ||
source.Contains("URCB", StringComparison.OrdinalIgnoreCase) ||
source.Contains("report", StringComparison.OrdinalIgnoreCase);
private static bool ContainsPlannerStage(string? mode)
=> (mode ?? string.Empty).Contains("arming", StringComparison.OrdinalIgnoreCase) ||
(mode ?? string.Empty).Contains("live start", StringComparison.OrdinalIgnoreCase) ||
(mode ?? string.Empty).Contains("preparing", StringComparison.OrdinalIgnoreCase) ||
(mode ?? string.Empty).Contains("settling", StringComparison.OrdinalIgnoreCase);
private sealed record IoFatAcquisitionSummary(
int LiveCount,
int ReportCount,
int PollingCount,
int UnknownCount,
string Mode);
}