Repository navigation
Expand file tree
/
Copy pathMachineToolRuntimeTests.cs
More file actions
404 lines (378 loc) · 20.6 KB
/
Copy pathMachineToolRuntimeTests.cs
File metadata and controls
404 lines (378 loc) · 20.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Machinery.Server;
using Opc.Ua.MachineTool.Server;
using Opc.Ua.Server;
using Opc.Ua.Server.TestFramework;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Exercises MachineTool behavior through its public provider, builder and runtime handles.
/// </summary>
[TestFixture]
[Category("MachineTool")]
[NonParallelizable]
public sealed class MachineToolRuntimeTests
{
/// <summary>
/// Mandatory-only hosting preserves the nameplate, real sample quality, and legacy active program.
/// </summary>
[Test]
public async Task MandatoryMachinePublishesProviderSamplesWithoutOptionalFacetsAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
DateTimeUtc acquired = new DateTime(2026, 2, 3, 4, 5, 6, DateTimeKind.Utc);
var snapshot = new MachineToolSnapshot
{
Operation = new MachineToolOperation
{
Mode = MachineOperationMode.Automatic,
Sample = new MachineToolSampleInfo(acquired, StatusCodes.UncertainLastUsableValue)
},
ActiveProgram = new MachineToolProgram
{
Name = "MAIN.NC",
Phase = MachineToolProductionPhase.Running,
Sample = new MachineToolSampleInfo(acquired, StatusCodes.Good)
}
};
var provider = new InMemoryMachineToolProvider(snapshot);
await using MachineToolRuntime<MachineToolState> runtime = await fixture.Context
.AddMachineTool("Mill")
.WithProvider(provider)
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Test builder");
identity.SerialNumber = "MT-1";
identity.ProductInstanceUri = "urn:machinetool-tests:mt-1";
})
.BuildAsync().ConfigureAwait(false);
Assert.That(runtime.State.Identification!.SerialNumber!.Value, Is.EqualTo("MT-1"));
Assert.That(runtime.State.Identification.TypeDefinitionId,
Is.EqualTo(NodeId.Create(11, Namespaces.MachineTool, fixture.Context.Context.NamespaceUris)));
Assert.That(runtime.State.Monitoring!.MachineTool!.OperationMode!.Value,
Is.EqualTo(MachineOperationMode.Automatic));
Assert.That(runtime.State.Monitoring.MachineTool.OperationMode.StatusCode,
Is.EqualTo(StatusCodes.UncertainLastUsableValue));
Assert.That(runtime.State.Monitoring.MachineTool.OperationMode.Timestamp, Is.EqualTo(acquired));
Assert.That(runtime.State.Production!.ActiveProgram!.Name!.Value, Is.EqualTo("MAIN.NC"));
Assert.That(runtime.State.Production.ActiveProgram.NumberInList!.Value, Is.Zero);
Assert.That(runtime.State.Production.ActiveProgram.State!.CurrentState!.Value.Text, Is.EqualTo("Running"));
Assert.That(runtime.State.Equipment!.Tools, Is.Null);
Assert.That(runtime.State.Notification!.Messages, Is.Null);
Assert.That(runtime.State.Notification.Prognoses, Is.Null);
Assert.That(runtime.State.Production.ProductionPlan, Is.Null);
Assert.That(runtime.State.FileSystem, Is.Null);
Assert.That(fixture.Context.Context.NamespaceUris.GetIndex(WMTP.Namespaces.WMTP), Is.EqualTo(-1));
}
/// <summary>
/// Failed nameplate validation releases the staged name and identifiers so corrected setup can retry.
/// </summary>
[Test]
public async Task InvalidIdentificationDoesNotReserveTheMachineNameAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolBuilder<MachineToolState> builder = fixture.Context.AddMachineTool("Retry")
.WithProvider(new InMemoryMachineToolProvider(new MachineToolSnapshot()))
.WithIdentification(identity => identity.Manufacturer = new LocalizedText("Manufacturer"));
ServiceResultException invalid = (await Assert.ThrowsAsync<ServiceResultException>(async () =>
await builder.BuildAsync().ConfigureAwait(false)))!;
Assert.That(invalid.StatusCode, Is.EqualTo(StatusCodes.BadConfigurationError));
await using MachineToolRuntime<MachineToolState> runtime = await builder
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Retry-1";
identity.ProductInstanceUri = "urn:test:retry-1";
}).BuildAsync().ConfigureAwait(false);
Assert.That(runtime.State.Identification!.SerialNumber!.Value, Is.EqualTo("Retry-1"));
}
/// <summary>
/// Combined channels use the specified state priority and modifier union while keeping HMI overrides.
/// </summary>
[Test]
public async Task CombinedChannelsAggregateReportedStateWithoutCommandingEquipmentAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
DateTimeUtc acquired = new DateTime(2026, 2, 3, 4, 5, 6, DateTimeKind.Utc);
var sample = new MachineToolSampleInfo(acquired, StatusCodes.Good);
var provider = new InMemoryMachineToolProvider(new MachineToolSnapshot
{
Channels =
[
new MachineToolChannel
{
Name = "Left", State = ChannelState.Active, Mode = ChannelMode.Automatic,
FeedOverride = 90, Modifiers = new(true, false, false), Sample = sample
},
new MachineToolChannel
{
Name = "Right", State = ChannelState.Reset, Mode = ChannelMode.JogManual,
FeedOverride = 100, Modifiers = new(false, true, true), Sample = sample
},
new MachineToolChannel
{
Name = "Combined", CombinedChannels = ["Left", "Right"],
FeedOverride = 75, RapidOverride = 80, Sample = sample
}
]
});
await using MachineToolRuntime<MachineToolState> runtime = await fixture.Context.AddMachineTool("Channels")
.WithProvider(provider).WithFeatures(MachineToolFeatures.Channels)
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Channel-1";
identity.ProductInstanceUri = "urn:test:channel-1";
}).BuildAsync().ConfigureAwait(false);
var children = new List<BaseInstanceState>();
runtime.State.Monitoring!.GetChildren(fixture.Context.Context, children);
var combined = children.Find(child => child.BrowseName.Name == "Combined") as CombinedChannelMonitoringState;
Assert.That(combined, Is.Not.Null);
Assert.That(combined!.ChannelState!.Value, Is.EqualTo(ChannelState.Reset));
Assert.That(combined.ChannelMode!.Value, Is.EqualTo(ChannelMode.JogManual));
Assert.That(combined.FeedOverride!.Value, Is.EqualTo(75));
Assert.That(combined.FeedOverride.EngineeringUnits!.Value!.UnitId, Is.EqualTo(20529));
Assert.That(combined.RapidOverride!.Value, Is.EqualTo(80));
Assert.That(combined.ChannelModifiers!.DryRun!.Value, Is.True);
Assert.That(combined.ChannelModifiers.OptionalStop!.Value, Is.True);
Assert.That(combined.ChannelModifiers.SingleStep!.Value, Is.True);
}
/// <summary>
/// Optional working units, counters and responsibility retain their modeled types and observations.
/// </summary>
[Test]
public async Task WorkingUnitsCountersAndObligationUseProviderObservationsAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var sample = new MachineToolSampleInfo(
new DateTime(2026, 2, 3, 4, 5, 6, DateTimeKind.Utc), StatusCodes.Good);
var provider = new InMemoryMachineToolProvider(new MachineToolSnapshot
{
Operation = new MachineToolOperation
{
Mode = MachineOperationMode.Service,
Sample = sample,
PowerOnDurationMilliseconds = 9_000_000,
PartsProducedInLifetime = 412,
Obligation = new MachineToolObligation(false, true),
ItemState = MachineToolItemState.NotExecuting,
MachineryMode = MachineToolMachineryMode.Maintenance,
MaintenanceMode = MachineToolMaintenanceMode.Repair
},
Software = [new MachineToolSoftware("NC", "1.2.3")],
Spindles = [new MachineToolSpindle("Spindle", true, sample, 95, false)],
Lasers = [new MachineToolLaser("Laser", true, LaserState.Ready, sample)],
EdmGenerators = [new MachineToolEdmGenerator("Generator", false, EDMGeneratorState.Ready, sample)]
});
await using MachineToolRuntime<MachineToolState> runtime = await fixture.Context.AddMachineTool("Observed")
.WithProvider(provider).WithFeatures(
MachineToolFeatures.SoftwareIdentification | MachineToolFeatures.WorkingUnits |
MachineToolFeatures.LegacyPowerOnDuration | MachineToolFeatures.OperationCounters |
MachineToolFeatures.PartsProducedInLifetime | MachineToolFeatures.Obligation |
MachineToolFeatures.MachineryState | MachineToolFeatures.MaintenanceMode)
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Observed-1";
identity.ProductInstanceUri = "urn:test:observed-1";
}).BuildAsync().ConfigureAwait(false);
MachineOperationMonitoringState operation = runtime.State.Monitoring!.MachineTool!;
Assert.That(operation.PowerOnDuration, Is.Not.Null);
Assert.That(operation.PowerOnDuration!.Value, Is.EqualTo(2));
Assert.That(operation.OperationCounters!.PowerOnDuration!.Value, Is.EqualTo(9_000_000));
Assert.That(operation.Obligation!.EndUserObligated!.Value, Is.False);
Assert.That(operation.Obligation.MachineBuilderObligated!.Value, Is.True);
Assert.That(operation.MachineryOperationMode!.CurrentState!.Value.Text, Is.EqualTo("Maintenance"));
Assert.That(operation.MachineryOperationMode.MaintenanceMode!.CurrentState!.Value.Text, Is.EqualTo("Repair"));
var children = new List<BaseInstanceState>();
runtime.State.Monitoring.GetChildren(fixture.Context.Context, children);
Assert.That(children.Find(child => child.BrowseName.Name == "Spindle"), Is.TypeOf<SpindleMonitoringState>());
Assert.That(children.Find(child => child.BrowseName.Name == "Laser"), Is.TypeOf<LaserMonitoringState>());
Assert.That(children.Find(child => child.BrowseName.Name == "Generator"), Is.TypeOf<EDMGeneratorMonitoringState>());
}
/// <summary>
/// Stacklights use segmented IA nodes with ordered lights and propagate their provider quality.
/// </summary>
[Test]
public async Task StacklightsUseSegmentedOrderedLightElementsAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var sample = new MachineToolSampleInfo(
new DateTime(2026, 2, 3, 4, 5, 6, DateTimeKind.Utc), StatusCodes.UncertainLastUsableValue);
var provider = new InMemoryMachineToolProvider(new MachineToolSnapshot
{
Stacklights =
[
new MachineToolStacklight("Main",
[
new MachineToolLight(true, IA.SignalColor.Red, IA.SignalModeLight.Flashing, sample),
new MachineToolLight(false, IA.SignalColor.Green, IA.SignalModeLight.Continuous, sample)
])
]
});
await using MachineToolRuntime<MachineToolState> runtime = await fixture.Context.AddMachineTool("Lights")
.WithProvider(provider).WithFeatures(MachineToolFeatures.Stacklights)
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Lights-1";
identity.ProductInstanceUri = "urn:test:lights-1";
}).BuildAsync().ConfigureAwait(false);
IA.BasicStacklightState? stacklight = runtime.State.Monitoring!.Stacklight;
Assert.That(stacklight, Is.Not.Null);
Assert.That(stacklight!.StacklightMode!.Value, Is.EqualTo(IA.StacklightOperationMode.Segmented));
var children = new List<BaseInstanceState>();
stacklight.GetChildren(fixture.Context.Context, children);
var lights = children.FindAll(static child => child is IA.StackElementLightState);
Assert.That(lights, Has.Count.EqualTo(2));
var first = (IA.StackElementLightState)lights[0];
Assert.That(first.NumberInList!.WrappedValue.TryGetValue(out ushort order), Is.True);
Assert.That(order, Is.Zero);
Assert.That(first.SignalOn!.Value, Is.True);
Assert.That(first.SignalColor!.Value, Is.EqualTo(IA.SignalColor.Red));
Assert.That(first.SignalMode!.Value, Is.EqualTo(IA.SignalModeLight.Flashing));
Assert.That(first.SignalOn.StatusCode, Is.EqualTo(StatusCodes.UncertainLastUsableValue));
}
/// <summary>
/// Selecting the additional Machinery nameplate unit requires its data rather than empty successful nodes.
/// </summary>
[Test]
public async Task AdditionalNameplateSelectionRequiresAllAdditionalValuesAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolBuilder<MachineToolState> builder = fixture.Context.AddMachineTool("Additional")
.WithProvider(new InMemoryMachineToolProvider(new MachineToolSnapshot()))
.WithFeatures(MachineToolFeatures.AdditionalIdentification)
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Additional-1";
identity.ProductInstanceUri = "urn:test:additional-1";
});
ServiceResultException missing = (await Assert.ThrowsAsync<ServiceResultException>(async () =>
await builder.BuildAsync().ConfigureAwait(false)))!;
Assert.That(missing.StatusCode, Is.EqualTo(StatusCodes.BadConfigurationError));
await using MachineToolRuntime<MachineToolState> runtime = await builder.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Manufacturer");
identity.SerialNumber = "Additional-1";
identity.ProductInstanceUri = "urn:test:additional-1";
identity.Model = new LocalizedText("Milling centre");
identity.ComponentName = new LocalizedText("Mill A");
identity.DeviceClass = "Milling machine";
identity.YearOfConstruction = 2025;
identity.MonthOfConstruction = 7;
}).BuildAsync().ConfigureAwait(false);
Assert.That(runtime.State.Identification!.DeviceClass!.Value, Is.EqualTo("Milling machine"));
Assert.That(runtime.State.Identification.MonthOfConstruction!.Value, Is.EqualTo(7));
}
}
/// <summary>
/// Hosts the real MachineTool model on the stock Machinery owner for runtime and wire tests.
/// </summary>
internal sealed class MachineToolRuntimeFixture : IAsyncDisposable
{
/// <summary>
/// Selects additional models before server construction while preserving the default MachineTool fixture.
/// </summary>
internal MachineToolRuntimeFixture(ArrayOf<IMachineryModelProvider> additionalProviders = default)
{
m_additionalProviders = additionalProviders;
}
/// <summary>
/// Gets the live MachineTool server.
/// </summary>
public RuntimeServer Server { get; private set; } = null!;
/// <summary>
/// Gets the shared build context.
/// </summary>
public IMachineryBuildContext Context { get; private set; } = null!;
/// <summary>
/// Gets the endpoint chosen by the server fixture.
/// </summary>
public Uri Endpoint => new($"opc.tcp://localhost:{m_fixture!.Port}");
/// <summary>
/// Gets the physical artifact root used for this fixture's certificate stores and file bindings.
/// </summary>
public string Root { get; } = Path.Combine(
AppContext.BaseDirectory, "opc-mt", Guid.NewGuid().ToString("N"));
/// <summary>
/// Starts an isolated real server and a direct build context.
/// </summary>
public async ValueTask StartAsync()
{
m_fixture = new ServerFixture<RuntimeServer>(
telemetry => new RuntimeServer(telemetry, m_additionalProviders))
{
AutoAccept = true,
SecurityNone = true
};
await m_fixture.LoadConfigurationAsync(Root).ConfigureAwait(false);
Server = await m_fixture.StartAsync().ConfigureAwait(false);
Context = Server.Manager.CreateMachineryBuildContext();
}
/// <inheritdoc/>
public async ValueTask DisposeAsync()
{
if (m_fixture != null)
{
await m_fixture.StopAsync().ConfigureAwait(false);
}
if (Directory.Exists(Root))
{
Directory.Delete(Root, recursive: true);
}
}
/// <summary>
/// Exposes the shared node manager to the fixture.
/// </summary>
internal sealed class RuntimeServer : StandardServer
{
/// <summary>
/// Creates the server.
/// </summary>
public RuntimeServer(
ITelemetryContext telemetry, ArrayOf<IMachineryModelProvider> additionalProviders = default)
: base(telemetry)
{
var options = new MachineryServerOptions
{
Parts = MachineryParts.BuildingBlocks | MachineryParts.Jobs
};
AddNodeManager(new MachineryNodeManagerFactory(
[new MachineryModelProvider(options.Parts), new MachineToolModelProvider(), .. additionalProviders],
options));
}
/// <summary>
/// Gets the namespace owner.
/// </summary>
public MachineryNodeManager Manager { get; private set; } = null!;
/// <inheritdoc/>
protected override async ValueTask OnNodeManagerStartedAsync(
IServerInternal server,
CancellationToken cancellationToken = default)
{
Manager = server.NodeManager.AsyncNodeManagers.OfType<MachineryNodeManager>().Single();
await base.OnNodeManagerStartedAsync(server, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>Additional model providers supplied before server construction.</summary>
private readonly ArrayOf<IMachineryModelProvider> m_additionalProviders;
/// <summary>The real server fixture, disposed asynchronously.</summary>
private ServerFixture<RuntimeServer>? m_fixture;
}
}