This repository has been archived by the owner on Mar 25, 2024. It is now read-only.
forked from WilliamBerryiii/OPCUA2AAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
96 lines (85 loc) · 3.1 KB
/
Program.cs
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
using Opc.Ua.Configuration;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Opc.Ua.Sample.Simulation
{
public class ApplicationMessageDlg : IApplicationMessageDlg
{
private string message = string.Empty;
private bool ask = false;
public override void Message(string text, bool ask)
{
this.message = text;
this.ask = ask;
}
public override async Task<bool> ShowAsync()
{
if (ask)
{
message += " (y/n, default y): ";
Console.Write(message);
}
else
{
Console.WriteLine(message);
}
if (ask)
{
try
{
ConsoleKeyInfo result = Console.ReadKey();
Console.WriteLine();
return await Task.FromResult((result.KeyChar == 'y') || (result.KeyChar == 'Y') || (result.KeyChar == '\r'));
}
catch
{
// intentionally fall through
}
}
return await Task.FromResult(true);
}
}
public class Program
{
public static void Main(string[] args)
{
try
{
ConsoleServer(args).Wait();
}
catch (Exception ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
}
private static async Task ConsoleServer(string[] args)
{
ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
ApplicationInstance application = new ApplicationInstance();
// load the application configuration.
application.ConfigSectionName = "Opc.Ua.Station";
application.ApplicationType = ApplicationType.Server;
ApplicationConfiguration config = await application.LoadApplicationConfiguration(false).ConfigureAwait(false);
// check the application certificate.
await application.CheckApplicationInstanceCertificate(false, 0).ConfigureAwait(false);
// create cert validator
config.CertificateValidator = new CertificateValidator();
config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
// start the server.
await application.Start(new FactoryStationServer()).ConfigureAwait(false);
Console.WriteLine("Server started.");
Thread.Sleep(Timeout.Infinite);
}
private static void CertificateValidator_CertificateValidation(CertificateValidator validator, CertificateValidationEventArgs e)
{
if (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted)
{
// accept all OPC UA client certificates
Console.WriteLine("Automatically trusting client certificate " + e.Certificate.Subject);
e.Accept = true;
}
}
}
}