-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathProgram.cs
More file actions
898 lines (838 loc) · 36.1 KB
/
Copy pathProgram.cs
File metadata and controls
898 lines (838 loc) · 36.1 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.CommandLine;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Opc.Ua;
using Opc.Ua.PubSub.Adapter;
using Opc.Ua.PubSub.Adapter.DependencyInjection;
using Opc.Ua.PubSub.Application;
using Opc.Ua.PubSub.Configuration;
namespace Quickstarts.ConsoleReferencePubSubClient
{
/// <summary>
/// Unified OPC UA Part 14 PubSub reference sample built on the fluent
/// <see cref="PubSubApplicationBuilder"/> + DI + .NET Generic Host surface.
/// One executable exposes three command-line-selectable modes:
/// <list type="bullet">
/// <item><description>
/// <c>publisher</c> - publishes sample DataSets over UDP/UADP or MQTT (UADP/JSON).
/// </description></item>
/// <item><description>
/// <c>subscriber</c> - receives DataSets and logs each decoded message.
/// </description></item>
/// <item><description>
/// <c>external</c> - bridges an external OPC UA server to PubSub through the
/// <c>Opc.Ua.PubSub.Adapter</c> library (publisher / subscriber / responder).
/// </description></item>
/// </list>
/// The build publishes as a NativeAOT-ready single-file executable.
/// </summary>
internal static class Program
{
private const string DefaultExternalEndpoint =
"opc.tcp://localhost:62541/Quickstarts/ReferenceServer";
private const string ExternalPublisherOptionsName = "ExternalPublisher";
private const string ExternalSubscriberOptionsName = "ExternalSubscriber";
private const string ExternalResponderOptionsName = "ExternalResponder";
public static async Task<int> Main(string[] args)
{
int exitCode = 0;
var rootCommand = new RootCommand(
"OPC UA Part 14 PubSub Reference sample. "
+ "Select a mode: publisher | subscriber | external.");
rootCommand.Subcommands.Add(BuildPublisherCommand(code => exitCode = code));
rootCommand.Subcommands.Add(BuildSubscriberCommand(code => exitCode = code));
rootCommand.Subcommands.Add(BuildExternalCommand(code => exitCode = code));
ParseResult parse = rootCommand.Parse(args);
await parse.InvokeAsync().ConfigureAwait(false);
return exitCode;
}
/// <summary>
/// Builds the <c>publisher</c> subcommand: a UDP/UADP or MQTT publisher
/// that publishes a built-in sample DataSet.
/// </summary>
private static Command BuildPublisherCommand(Action<int> setExitCode)
{
var profileOption = new Option<string>("--profile")
{
Description = "Transport profile: udp-uadp | mqtt-uadp | mqtt-json.",
DefaultValueFactory = _ => "udp-uadp"
};
var configFileOption = new Option<string?>("--config-file")
{
Description = "Optional path to a Part 14 XML PubSub configuration."
};
var publisherIdOption = new Option<ushort>("--publisher-id")
{
Description = "PublisherId published in every NetworkMessage header.",
DefaultValueFactory = _ => (ushort)1
};
var writerGroupIdOption = new Option<ushort>("--writer-group-id")
{
Description = "WriterGroupId for the single sample WriterGroup.",
DefaultValueFactory = _ => (ushort)100
};
var dataSetWriterIdOption = new Option<ushort>("--data-set-writer-id")
{
Description = "DataSetWriterId for the single sample writer.",
DefaultValueFactory = _ => (ushort)1
};
var endpointOption = new Option<string?>("--endpoint")
{
Description =
"Transport endpoint URL. Defaults: opc.udp://239.0.0.1:4840 (UDP), "
+ "mqtt://localhost:1883 (MQTT)."
};
var intervalOption = new Option<int>("--interval")
{
Description = "Publishing interval in milliseconds.",
DefaultValueFactory = _ => 1000
};
var command = new Command(
"publisher",
"Publish a sample DataSet over UDP/UADP or MQTT (UADP/JSON).")
{
profileOption,
configFileOption,
publisherIdOption,
writerGroupIdOption,
dataSetWriterIdOption,
endpointOption,
intervalOption
};
command.SetAction(async (parseResult, cancellationToken) =>
{
string? profileArg = parseResult.GetValue(profileOption);
if (!TryParsePublisherProfile(profileArg, out PublisherProfile profile))
{
await Console.Error.WriteLineAsync(
$"Unknown --profile value '{profileArg}'. "
+ "Expected one of: udp-uadp, mqtt-uadp, mqtt-json.")
.ConfigureAwait(false);
setExitCode(2);
return;
}
setExitCode(await RunPublisherAsync(
profile,
parseResult.GetValue(configFileOption),
parseResult.GetValue(publisherIdOption),
parseResult.GetValue(writerGroupIdOption),
parseResult.GetValue(dataSetWriterIdOption),
parseResult.GetValue(endpointOption),
parseResult.GetValue(intervalOption),
cancellationToken).ConfigureAwait(false));
});
return command;
}
/// <summary>
/// Builds the <c>subscriber</c> subcommand: a UDP/UADP or MQTT subscriber
/// that logs each decoded DataSetMessage.
/// </summary>
private static Command BuildSubscriberCommand(Action<int> setExitCode)
{
var profileOption = new Option<string>("--profile")
{
Description = "Transport profile: udp-uadp | mqtt-uadp | mqtt-json.",
DefaultValueFactory = _ => "udp-uadp"
};
var configFileOption = new Option<string?>("--config-file")
{
Description = "Optional path to a Part 14 XML PubSub configuration."
};
var publisherFilterOption = new Option<ushort>("--publisher-id-filter")
{
Description = "PublisherId filter applied by the reader.",
DefaultValueFactory = _ => (ushort)1
};
var writerGroupFilterOption = new Option<ushort>("--writer-group-id-filter")
{
Description = "WriterGroupId filter applied by the reader.",
DefaultValueFactory = _ => (ushort)100
};
var dataSetWriterFilterOption = new Option<ushort>("--data-set-writer-id-filter")
{
Description = "DataSetWriterId filter applied by the reader.",
DefaultValueFactory = _ => (ushort)1
};
var endpointOption = new Option<string?>("--endpoint")
{
Description = "Transport endpoint URL. Defaults: opc.udp://239.0.0.1:4840 "
+ "(UDP), mqtt://localhost:1883 (MQTT)."
};
var command = new Command(
"subscriber",
"Receive DataSets and log each decoded message.")
{
profileOption,
configFileOption,
publisherFilterOption,
writerGroupFilterOption,
dataSetWriterFilterOption,
endpointOption
};
command.SetAction(async (parseResult, cancellationToken) =>
{
string? profileArg = parseResult.GetValue(profileOption);
if (!TryParseSubscriberProfile(profileArg, out SubscriberProfile profile))
{
await Console.Error.WriteLineAsync(
$"Unknown --profile value '{profileArg}'. "
+ "Expected one of: udp-uadp, mqtt-uadp, mqtt-json.")
.ConfigureAwait(false);
setExitCode(2);
return;
}
setExitCode(await RunSubscriberAsync(
profile,
parseResult.GetValue(configFileOption),
parseResult.GetValue(publisherFilterOption),
parseResult.GetValue(writerGroupFilterOption),
parseResult.GetValue(dataSetWriterFilterOption),
parseResult.GetValue(endpointOption),
cancellationToken).ConfigureAwait(false));
});
return command;
}
/// <summary>
/// Builds the <c>external</c> subcommand: bridges an external OPC UA server
/// to PubSub via the adapter library, in publisher / subscriber / responder
/// direction.
/// </summary>
private static Command BuildExternalCommand(Action<int> setExitCode)
{
var directionOption = new Option<string>("--mode")
{
Description =
"Adapter directions to run, comma- or plus-separated: publisher | subscriber | responder.",
DefaultValueFactory = _ => "publisher"
};
var readModeOption = new Option<string>("--read-mode")
{
Description =
"Publisher source strategy: cyclic (Read each cycle) | "
+ "subscription (client Subscription cache).",
DefaultValueFactory = _ => "cyclic"
};
var affinityOption = new Option<string>("--affinity")
{
Description =
"Subscription grouping when --read-mode=subscription: "
+ "writergroup | datasetwriter.",
DefaultValueFactory = _ => "writergroup"
};
var endpointOption = new Option<string?>("--endpoint")
{
Description =
"External OPC UA server endpoint URL. Defaults to "
+ "OPCUA_EXTERNAL_ENDPOINT or " + DefaultExternalEndpoint + "."
};
var pubSubEndpointOption = new Option<string>("--pubsub-endpoint")
{
Description = "UDP/UADP PubSub transport endpoint URL.",
DefaultValueFactory = _ => ExternalServerPubSubConfiguration.DefaultPubSubEndpoint
};
var hotReloadOption = new Option<bool>("--hot-reload")
{
Description =
"Enable the external bridge hot-reload demo using appsettings.json and pubsub-config.xml."
};
var command = new Command(
"external",
"Bridge an external OPC UA server to PubSub (publisher | subscriber | responder).")
{
directionOption,
readModeOption,
affinityOption,
endpointOption,
pubSubEndpointOption,
hotReloadOption
};
command.SetAction(async (parseResult, cancellationToken) =>
{
if (!TryParseBridgeMode(parseResult.GetValue(directionOption), out BridgeMode mode))
{
await Console.Error.WriteLineAsync(
$"Unknown --mode value '{parseResult.GetValue(directionOption)}'. "
+ "Expected one or more of: publisher, subscriber, responder.")
.ConfigureAwait(false);
setExitCode(2);
return;
}
if (!TryParseReadMode(parseResult.GetValue(readModeOption), out ReadMode readMode))
{
await Console.Error.WriteLineAsync(
$"Unknown --read-mode value '{parseResult.GetValue(readModeOption)}'. "
+ "Expected one of: cyclic, subscription.")
.ConfigureAwait(false);
setExitCode(2);
return;
}
if (!TryParseAffinity(
parseResult.GetValue(affinityOption), out SubscriptionAffinity affinity))
{
await Console.Error.WriteLineAsync(
$"Unknown --affinity value '{parseResult.GetValue(affinityOption)}'. "
+ "Expected one of: writergroup, datasetwriter.")
.ConfigureAwait(false);
setExitCode(2);
return;
}
string externalEndpoint = parseResult.GetValue(endpointOption)
?? Environment.GetEnvironmentVariable("OPCUA_EXTERNAL_ENDPOINT")
?? DefaultExternalEndpoint;
setExitCode(await RunExternalAsync(
mode,
readMode,
affinity,
externalEndpoint,
parseResult.GetValue(pubSubEndpointOption)
?? ExternalServerPubSubConfiguration.DefaultPubSubEndpoint,
parseResult.GetValue(hotReloadOption),
cancellationToken).ConfigureAwait(false));
});
return command;
}
private static async Task<int> RunPublisherAsync(
PublisherProfile profile,
string? configFile,
ushort publisherId,
ushort writerGroupId,
ushort dataSetWriterId,
string? endpoint,
int intervalMs,
CancellationToken cancellationToken)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
string transportEndpoint = endpoint
?? PublisherConfigurationBuilder.DefaultEndpointFor(profile);
var sampleSource = new SampleDataSetSource();
builder.Services.AddOpcUa().AddPubSub(pubsub =>
{
IPubSubBuilder publisher = pubsub
.AddPublisher()
.AddUdpTransport()
.AddSecurityKeyProvider(SampleSecurity.CreateKeyProvider())
.AddDataSetSource(PublisherConfigurationBuilder.DataSetName, sampleSource);
if (profile == PublisherProfile.EthUadp)
{
publisher.AddEthTransport();
}
else if (profile != PublisherProfile.UdpUadp)
{
publisher.AddMqttTransport();
}
publisher.ConfigureApplication(app =>
{
app.WithApplicationId("urn:opcfoundation:ConsoleReferencePubSubClient:Publisher");
if (!string.IsNullOrEmpty(configFile))
{
app.UseConfigurationFile(configFile);
}
else
{
app.UseConfiguration(PublisherConfigurationBuilder.Build(
profile,
transportEndpoint,
publisherId,
writerGroupId,
dataSetWriterId,
intervalMs));
}
});
});
IHost host = builder.Build();
ILogger logger = host.Services
.GetRequiredService<ILoggerFactory>()
.CreateLogger("ConsoleReferencePubSubClient.Publisher");
logger.LogInformation(
"Publisher starting: profile={Profile} endpoint={Endpoint} "
+ "interval={Interval}ms publisherId={PublisherId} writerGroup={WriterGroupId}",
profile, transportEndpoint, intervalMs, publisherId, writerGroupId);
logger.LogInformation("Publisher started. Press Ctrl-C to exit.");
await host.RunAsync(cancellationToken).ConfigureAwait(false);
return 0;
}
private static async Task<int> RunSubscriberAsync(
SubscriberProfile profile,
string? configFile,
ushort publisherIdFilter,
ushort writerGroupIdFilter,
ushort dataSetWriterIdFilter,
string? endpoint,
CancellationToken cancellationToken)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
string transportEndpoint = endpoint
?? SubscriberConfigurationBuilder.DefaultEndpointFor(profile);
builder.Services.AddOpcUa().AddPubSub(pubsub =>
{
IPubSubBuilder subscriber = pubsub
.AddSubscriber()
.AddUdpTransport()
.AddSecurityKeyProvider(SampleSecurity.CreateKeyProvider())
.AddSubscribedDataSetSink(
SubscriberConfigurationBuilder.ReaderName,
sp => new ConsoleLoggingSink(
sp.GetRequiredService<ILoggerFactory>()
.CreateLogger<ConsoleLoggingSink>()));
if (profile == SubscriberProfile.EthUadp)
{
subscriber.AddEthTransport();
}
else if (profile != SubscriberProfile.UdpUadp)
{
subscriber.AddMqttTransport();
}
subscriber.ConfigureApplication(app =>
{
app.WithApplicationId("urn:opcfoundation:ConsoleReferencePubSubClient:Subscriber");
if (!string.IsNullOrEmpty(configFile))
{
app.UseConfigurationFile(configFile);
}
else
{
app.UseConfiguration(SubscriberConfigurationBuilder.Build(
profile,
transportEndpoint,
publisherIdFilter,
writerGroupIdFilter,
dataSetWriterIdFilter));
}
});
});
IHost host = builder.Build();
ILogger logger = host.Services
.GetRequiredService<ILoggerFactory>()
.CreateLogger("ConsoleReferencePubSubClient.Subscriber");
logger.LogInformation(
"Subscriber starting: profile={Profile} endpoint={Endpoint} "
+ "publisherFilter={PublisherFilter} writerGroupFilter={WriterGroupFilter}",
profile,
transportEndpoint,
publisherIdFilter,
writerGroupIdFilter);
logger.LogInformation("Subscriber started. Press Ctrl-C to exit.");
await host.RunAsync(cancellationToken).ConfigureAwait(false);
return 0;
}
private static async Task<int> RunExternalAsync(
BridgeMode mode,
ReadMode readMode,
SubscriptionAffinity affinity,
string externalEndpoint,
string pubSubEndpoint,
bool hotReload,
CancellationToken cancellationToken)
{
HostApplicationBuilder builder = hotReload
? Host.CreateApplicationBuilder(
new HostApplicationBuilderSettings { ContentRootPath = AppContext.BaseDirectory })
: Host.CreateApplicationBuilder();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
string? configFile = null;
XmlPubSubConfigurationStore? hotReloadStore = null;
string appSettingsFile = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
if (hotReload)
{
(configFile, hotReloadStore) = await ConfigureExternalBridgeHotReloadAsync(
builder,
mode,
pubSubEndpoint,
cancellationToken).ConfigureAwait(false);
}
else
{
ConfigureExternalBridge(builder, mode, readMode, affinity, externalEndpoint, pubSubEndpoint);
}
IHost host = builder.Build();
ILogger logger = host.Services
.GetRequiredService<ILoggerFactory>()
.CreateLogger("ConsoleReferencePubSubClient.External");
logger.LogInformation(
"External-server PubSub bridge starting: mode={Mode} readMode={ReadMode} "
+ "affinity={Affinity} externalServer={ExternalEndpoint} pubSub={PubSubEndpoint}",
mode, readMode, affinity, externalEndpoint, pubSubEndpoint);
if (hotReload)
{
logger.LogInformation(
"Hot reload enabled. Edit {AppSettingsFile} (for example, change "
+ "{PublisherOptionsName}:ReadMode to Subscription) or {ConfigFile} "
+ "(for example, add or remove a DataSetWriter) and save to reconfigure "
+ "the running bridge.",
appSettingsFile,
ExternalPublisherOptionsName,
configFile);
}
logger.LogInformation("Bridge started. Press Ctrl-C to exit.");
try
{
await host.RunAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
hotReloadStore?.Dispose();
}
return 0;
}
/// <summary>
/// Wires the selected external bridge directions on one UDP/UADP PubSub
/// application and one host.
/// </summary>
private static void ConfigureExternalBridge(
HostApplicationBuilder builder,
BridgeMode modes,
ReadMode readMode,
SubscriptionAffinity affinity,
string externalEndpoint,
string pubSubEndpoint)
{
builder.Services.AddOpcUa().AddPubSub(pubsub =>
{
IPubSubBuilder bridge = pubsub;
if (modes.HasFlag(BridgeMode.Publisher))
{
bridge = bridge.AddPublisher();
}
if (modes.HasFlag(BridgeMode.Subscriber) || modes.HasFlag(BridgeMode.Responder))
{
bridge = bridge.AddSubscriber();
}
bridge = bridge
.AddUdpTransport()
.ConfigureApplication(app => app.WithApplicationId(
"urn:opcfoundation:ConsoleReferencePubSubClient:ExternalBridge"))
.UseConfiguration(
ExternalServerPubSubConfiguration.BuildConfiguration(modes, pubSubEndpoint));
if (modes.HasFlag(BridgeMode.Publisher))
{
bridge = bridge.AddServerAsPublisher(options =>
{
options.Connection.EndpointUrl = externalEndpoint;
// The demo connects unsecured for zero-config interop. A
// production bridge must use SignAndEncrypt with a provisioned
// application instance certificate.
options.Connection.SecurityMode = MessageSecurityMode.None;
options.ReadMode = readMode;
options.Affinity = affinity;
});
}
if (modes.HasFlag(BridgeMode.Subscriber))
{
bridge = bridge.AddServerAsSubscriber(options =>
{
options.Connection.EndpointUrl = externalEndpoint;
options.Connection.SecurityMode = MessageSecurityMode.None;
});
}
if (modes.HasFlag(BridgeMode.Responder))
{
bridge.AddServerAsActionResponder(options =>
{
options.Connection.EndpointUrl = externalEndpoint;
options.Connection.SecurityMode = MessageSecurityMode.None;
options.AllowUnsecured = true;
// Map the "ResetCounters" action to an external method call.
options.MethodMap.Add(
"ResetCounters",
NodeId.Parse("ns=2;s=Demo.External.Methods"),
NodeId.Parse("ns=2;s=Demo.External.ResetCounters"));
options.Targets.Add(new PubSubActionTarget
{
DataSetWriterId = 1,
ActionName = "ResetCounters"
});
});
}
});
}
private static async Task<(string ConfigFile, XmlPubSubConfigurationStore Store)> ConfigureExternalBridgeHotReloadAsync(
HostApplicationBuilder builder,
BridgeMode modes,
string pubSubEndpoint,
CancellationToken cancellationToken)
{
ITelemetryContext telemetry = DefaultTelemetry.Create(logging => logging.AddConsole());
string configFile = Path.Combine(AppContext.BaseDirectory, "pubsub-config.xml");
var store = new XmlPubSubConfigurationStore(configFile, telemetry, watchForChanges: true);
try
{
await store.SaveAsync(
ExternalServerPubSubConfiguration.BuildConfiguration(modes, pubSubEndpoint),
cancellationToken).ConfigureAwait(false);
builder.Services.AddOpcUa().AddPubSub(pubsub =>
{
IPubSubBuilder bridge = pubsub;
if (modes.HasFlag(BridgeMode.Publisher))
{
bridge = bridge.AddPublisher();
}
if (modes.HasFlag(BridgeMode.Subscriber) || modes.HasFlag(BridgeMode.Responder))
{
bridge = bridge.AddSubscriber();
}
bridge = bridge
.AddUdpTransport()
.ConfigureApplication(app => app.WithApplicationId(
"urn:opcfoundation:ConsoleReferencePubSubClient:ExternalBridge"))
// WithConfigurationStore registers this externally-created singleton instance.
// The sample disposes it after the host stops instead of relying on the container.
.WithConfigurationStore(store);
if (modes.HasFlag(BridgeMode.Publisher))
{
bridge = bridge.AddServerAsPublisher(
ExternalPublisherOptionsName,
builder.Configuration.GetSection(ExternalPublisherOptionsName));
}
if (modes.HasFlag(BridgeMode.Subscriber))
{
bridge = bridge.AddServerAsSubscriber(
ExternalSubscriberOptionsName,
builder.Configuration.GetSection(ExternalSubscriberOptionsName));
}
if (modes.HasFlag(BridgeMode.Responder))
{
bridge.AddServerAsActionResponder(
ExternalResponderOptionsName,
builder.Configuration.GetSection(ExternalResponderOptionsName));
}
});
if (modes.HasFlag(BridgeMode.Responder))
{
builder.Services.Configure<ServerActionResponderOptions>(
ExternalResponderOptionsName,
options =>
{
options.MethodMap.Add(
"ResetCounters",
NodeId.Parse("ns=2;s=Demo.External.Methods"),
NodeId.Parse("ns=2;s=Demo.External.ResetCounters"));
options.Targets.Add(new PubSubActionTarget
{
DataSetWriterId = 1,
ActionName = "ResetCounters"
});
});
}
return (configFile, store);
}
catch
{
store.Dispose();
throw;
}
}
private static bool TryParsePublisherProfile(string? text, out PublisherProfile profile)
{
switch (text)
{
case "udp-uadp":
profile = PublisherProfile.UdpUadp;
return true;
case "mqtt-uadp":
profile = PublisherProfile.MqttUadp;
return true;
case "mqtt-json":
profile = PublisherProfile.MqttJson;
return true;
case "eth-uadp":
profile = PublisherProfile.EthUadp;
return true;
default:
profile = PublisherProfile.UdpUadp;
return false;
}
}
private static bool TryParseSubscriberProfile(string? text, out SubscriberProfile profile)
{
switch (text)
{
case "udp-uadp":
profile = SubscriberProfile.UdpUadp;
return true;
case "mqtt-uadp":
profile = SubscriberProfile.MqttUadp;
return true;
case "mqtt-json":
profile = SubscriberProfile.MqttJson;
return true;
case "eth-uadp":
profile = SubscriberProfile.EthUadp;
return true;
default:
profile = SubscriberProfile.UdpUadp;
return false;
}
}
private static bool TryParseBridgeMode(string? text, out BridgeMode mode)
{
mode = BridgeMode.None;
if (string.IsNullOrWhiteSpace(text))
{
mode = BridgeMode.Publisher;
return true;
}
string[] tokens = text.Split(
[',', '+'],
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (tokens.Length == 0)
{
mode = BridgeMode.Publisher;
return true;
}
foreach (string token in tokens)
{
switch (token)
{
case "publisher":
mode |= BridgeMode.Publisher;
break;
case "subscriber":
mode |= BridgeMode.Subscriber;
break;
case "responder":
mode |= BridgeMode.Responder;
break;
default:
mode = BridgeMode.Publisher;
return false;
}
}
return mode != BridgeMode.None;
}
private static bool TryParseReadMode(string? text, out ReadMode readMode)
{
switch (text)
{
case "cyclic":
readMode = ReadMode.Cyclic;
return true;
case "subscription":
readMode = ReadMode.Subscription;
return true;
default:
readMode = ReadMode.Cyclic;
return false;
}
}
private static bool TryParseAffinity(string? text, out SubscriptionAffinity affinity)
{
switch (text)
{
case "writergroup":
affinity = SubscriptionAffinity.WriterGroup;
return true;
case "datasetwriter":
affinity = SubscriptionAffinity.DataSetWriter;
return true;
default:
affinity = SubscriptionAffinity.WriterGroup;
return false;
}
}
}
/// <summary>
/// Publisher transport/message profile selected via <c>publisher --profile</c>.
/// </summary>
public enum PublisherProfile
{
/// <summary>
/// UDP transport with UADP message mapping.
/// </summary>
UdpUadp = 0,
/// <summary>
/// MQTT broker transport with UADP message mapping.
/// </summary>
MqttUadp = 1,
/// <summary>
/// MQTT broker transport with JSON message mapping.
/// </summary>
MqttJson = 2,
/// <summary>
/// Ethernet (Layer 2) transport with UADP message mapping.
/// </summary>
EthUadp = 3
}
/// <summary>
/// Subscriber transport/message profile selected via <c>subscriber --profile</c>.
/// </summary>
public enum SubscriberProfile
{
/// <summary>
/// UDP transport with UADP message mapping.
/// </summary>
UdpUadp = 0,
/// <summary>
/// MQTT broker transport with UADP message mapping.
/// </summary>
MqttUadp = 1,
/// <summary>
/// MQTT broker transport with JSON message mapping.
/// </summary>
MqttJson = 2,
/// <summary>
/// Ethernet (Layer 2) transport with UADP message mapping.
/// </summary>
EthUadp = 3
}
/// <summary>
/// The external-server adapter direction selected via <c>external --mode</c>.
/// </summary>
[Flags]
public enum BridgeMode
{
/// <summary>
/// No external bridge direction selected.
/// </summary>
None = 0,
/// <summary>
/// Read an external server and publish its data over PubSub.
/// </summary>
Publisher = 1,
/// <summary>
/// Receive PubSub data and write it back to an external server.
/// </summary>
Subscriber = 2,
/// <summary>
/// Map an inbound PubSub Action to an external server method call.
/// </summary>
Responder = 4
}
}