-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.cs
132 lines (112 loc) · 5.28 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
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
namespace Opc.Ua.Cloud.Commander
{
using Opc.Ua;
using Opc.Ua.Configuration;
using Serilog;
using System;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
private static int _traceMasks = 1; // default to errors only
public static async Task Main()
{
InitLogging(Directory.GetCurrentDirectory());
// create OPC UA client app
string appName = "UACloudCommander";
if (Environment.GetEnvironmentVariable("APPNAME") != null)
{
appName = Environment.GetEnvironmentVariable("APPNAME");
}
ApplicationInstance app = new ApplicationInstance
{
ApplicationName = appName,
ApplicationType = ApplicationType.ClientAndServer,
ConfigSectionName = "UA.Cloud.Commander"
};
// update app name in config file
string fileContent = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "UA.Cloud.Commander.Config.xml"));
fileContent = fileContent.Replace("UndefinedAppName", appName);
File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "UA.Cloud.Commander.Config.xml"), fileContent);
await app.LoadApplicationConfiguration(false).ConfigureAwait(false);
// hook up OPC UA stack traces
_traceMasks = app.ApplicationConfiguration.TraceConfiguration.TraceMasks;
Utils.Tracing.TraceEventHandler += new EventHandler<TraceEventArgs>(OpcStackLoggingHandler);
bool certOK = await app.CheckApplicationInstanceCertificate(false, 0).ConfigureAwait(false);
if (!certOK)
{
throw new Exception("Application instance certificate invalid!");
}
// create OPC UA cert validator
app.ApplicationConfiguration.CertificateValidator = new CertificateValidator();
app.ApplicationConfiguration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(OPCUAServerCertificateValidationCallback);
app.ApplicationConfiguration.CertificateValidator.Update(app.ApplicationConfiguration.SecurityConfiguration).GetAwaiter().GetResult();
string issuerPath = Path.Combine(Directory.GetCurrentDirectory(), "pki", "issuer", "certs");
if (!Directory.Exists(issuerPath))
{
Directory.CreateDirectory(issuerPath);
}
// start the server.
app.Start(new UAServer()).GetAwaiter().GetResult();
Serilog.Log.Logger.Information("Server started.");
MQTTClient methodHandlerMQTT = null;
KafkaClient methodHandlerKafka = null;
if (Environment.GetEnvironmentVariable("USE_KAFKA") != null)
{
methodHandlerKafka = new KafkaClient(app.ApplicationConfiguration);
methodHandlerKafka.Connect();
}
else
{
// connect to the MQTT broker
methodHandlerMQTT = new MQTTClient(app.ApplicationConfiguration);
methodHandlerMQTT.Connect();
}
Serilog.Log.Logger.Information("UA Cloud Commander is running.");
await Task.Delay(Timeout.Infinite).ConfigureAwait(false);
}
private static void OpcStackLoggingHandler(object sender, TraceEventArgs e)
{
if ((e.TraceMask & _traceMasks) != 0)
{
if (e.Arguments != null)
{
Console.WriteLine("OPC UA Stack: " + string.Format(CultureInfo.InvariantCulture, e.Format, e.Arguments).Trim());
}
else
{
Console.WriteLine("OPC UA Stack: " + e.Format.Trim());
}
}
}
private static void OPCUAServerCertificateValidationCallback(CertificateValidator validator, CertificateValidationEventArgs e)
{
// always trust the OPC UA server certificate
if (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted)
{
e.Accept = true;
}
}
private static void InitLogging(string pathToLogFile)
{
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
#if DEBUG
loggerConfiguration.MinimumLevel.Debug();
#else
loggerConfiguration.MinimumLevel.Information();
#endif
if (!Directory.Exists(pathToLogFile))
{
Directory.CreateDirectory(pathToLogFile);
}
// set logging sinks
loggerConfiguration.WriteTo.Console();
loggerConfiguration.WriteTo.File(Path.Combine(pathToLogFile, "uacloudcommander.logfile.txt"), fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true, retainedFileCountLimit: 10);
Serilog.Log.Logger = loggerConfiguration.CreateLogger();
Serilog.Log.Logger.Information($"Log file is: {Path.Combine(pathToLogFile, "uacloudcommander.logfile.txt")}");
}
}
}