-
Notifications
You must be signed in to change notification settings - Fork 10
/
Settings.cs
187 lines (132 loc) · 6.25 KB
/
Settings.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
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
namespace Opc.Ua.Cloud.Publisher
{
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Opc.Ua.Cloud.Publisher.Interfaces;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
public class Settings
{
public Settings()
{
// needed for serialization
}
public delegate IBrokerClient BrokerResolver(string key);
private static Settings _instance = null;
private static object _instanceLock = new object();
public static Settings Instance
{
get
{
if (_instance == null)
{
lock (_instanceLock)
{
if (_instance == null)
{
_instance = Load();
}
}
}
return _instance;
}
set
{
lock (_instanceLock)
{
_instance = value;
}
}
}
private static Settings Load()
{
ILoggerFactory loggerFactory = (ILoggerFactory)Program.AppHost.Services.GetService(typeof(ILoggerFactory));
ILogger logger = loggerFactory.CreateLogger("Settings");
try
{
byte[] settingsFile = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "settings", "settings.json"));
if (settingsFile == null)
{
// no file persisted yet
logger.LogError("Creating new settings file as none was persisted so far.");
Settings newInstance = new Settings();
newInstance.Save();
return newInstance;
}
else
{
return JsonConvert.DeserializeObject<Settings>(Encoding.UTF8.GetString(settingsFile));
}
}
catch (Exception ex)
{
logger.LogError(ex, "Loading settings file failed. Creating a new one.");
Settings newInstance = new Settings();
newInstance.Save();
return newInstance;
}
}
public void Save()
{
ILoggerFactory loggerFactory = (ILoggerFactory)Program.AppHost.Services.GetService(typeof(ILoggerFactory));
ILogger logger = loggerFactory.CreateLogger("Settings");
try
{
File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "settings", "settings.json"), Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this, Formatting.Indented)));
}
catch (Exception)
{
logger.LogError("Could not store settings file. Settings won't be persisted!");
}
}
public string UAClientCertThumbprint { get; set; } = string.Empty;
public DateTime UAClientCertExpiry { get; set; } = DateTime.MinValue;
public string UAIssuerCertThumbprint { get; set; } = string.Empty;
public DateTime UAIssuerCertExpiry { get; set; } = DateTime.MinValue;
public string MQTTClientCertThumbprint { get; set; } = "N/A";
public DateTime MQTTClientCertExpiry { get; set; } = DateTime.MinValue;
public string PublisherName { get; set; } = "UACloudPublisher";
public string BrokerUrl { get; set; } = string.Empty;
public uint BrokerPort { get; set; } = 8883;
public string BrokerUsername { get; set; } = string.Empty;
public string BrokerPassword { get; set; } = string.Empty;
public bool UseAltBrokerForMetadata { get; set; } = false;
public bool UseAltBrokerForReceivingUAOverMQTT { get; set; } = false;
public string AltBrokerUrl { get; set; } = string.Empty;
public uint AltBrokerPort { get; set; } = 8883;
public string AltBrokerUsername { get; set; } = string.Empty;
public string AltBrokerPassword { get; set; } = string.Empty;
public string BrokerMessageTopic { get; set; } = string.Empty;
public string BrokerMetadataTopic { get; set; } = string.Empty;
public bool SendUAMetadata { get; set; } = false;
public bool SendUAStatus { get; set; } = false;
public uint MetadataSendInterval { get; set; } = 30; // seconds
public string BrokerCommandTopic { get; set; } = string.Empty;
public string BrokerDataReceivedTopic { get; set; } = string.Empty;
public string BrokerResponseTopic { get; set; } = string.Empty;
public uint BrokerMessageSize { get; set; } = HubMessageSizeMax;
public bool UseKafka { get; set; } = false;
public bool CreateBrokerSASToken { get; set; } = false;
public bool UseTLS { get; set; } = true;
public bool UseUACertAuth { get; set; } = false;
public bool UseCustomCertAuth { get; set; } = false;
public bool UseReverseConnect { get; set; } = false;
public bool PushCertsBeforePublishing { get; set; } = false;
public uint InternalQueueCapacity { get; set; } = 1000; // records
public uint DefaultSendIntervalSeconds { get; set; } = 1;
public uint DiagnosticsLoggingInterval { get; set; } = 30; // seconds
public uint DefaultOpcSamplingInterval { get; set; } = 500;
public uint DefaultOpcPublishingInterval { get; set; } = 1000;
public int UAStackTraceMask { get; set; } = 645; // Error, Trace, StartStop, Security
public bool ReversiblePubSubEncoding { get; set; } = false;
public bool AutoLoadPersistedNodes { get; set; } = false;
public string AzureOpenAIAPIEndpoint { get; set; } = string.Empty;
public string AzureOpenAIAPIKey { get; set; } = string.Empty;
public string AzureOpenAIDeploymentName { get; set; } = string.Empty;
public const int MaxResponsePayloadLength = (128 * 1024) - 256;
public const uint HubMessageSizeMax = 256 * 1024;
}
}