Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SystemConfiguration.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
6using Waher.Events;
11
13{
17 [CollectionName("SystemConfigurations")]
18 [ArchivingTime]
20 {
21 private TaskCompletionSource<bool> completionSource = null;
22 private HttpResource configResource = null;
23
24 private Guid objectId = Guid.Empty;
25 private DateTime created = DateTime.MinValue;
26 private DateTime updated = DateTime.MinValue;
27 private DateTime completed = DateTime.MinValue;
28 private bool complete = false;
29
33 [ObjectId]
34 public Guid ObjectId
35 {
36 get => this.objectId;
37 set => this.objectId = value;
38 }
39
43 [DefaultValue(false)]
44 public bool Complete
45 {
46 get => this.complete;
47 set => this.complete = value;
48 }
49
53 [DefaultValueDateTimeMinValue]
54 public DateTime Created
55 {
56 get => this.created;
57 set => this.created = value;
58 }
59
63 [DefaultValueDateTimeMinValue]
64 public DateTime Updated
65 {
66 get => this.updated;
67 set => this.updated = value;
68 }
69
73 [DefaultValueDateTimeMinValue]
74 public DateTime Completed
75 {
76 get => this.completed;
77 set => this.completed = value;
78 }
79
83 public abstract string Resource
84 {
85 get;
86 }
87
91 public abstract int Priority
92 {
93 get;
94 }
95
101 public abstract Task<string> Title(Language Language);
102
106 public abstract Task ConfigureSystem();
107
112 public abstract void SetStaticInstance(ISystemConfiguration Configuration);
113
118 public virtual Task InitSetup(HttpServer WebServer)
119 {
120 return Task.CompletedTask;
121 }
122
127 public virtual Task UnregisterSetup(HttpServer WebServer)
128 {
129 return Task.CompletedTask;
130 }
131
137 public virtual Task<bool> SetupConfiguration(HttpServer WebServer)
138 {
139 this.completionSource = new TaskCompletionSource<bool>();
140
141 this.configResource = WebServer.Register("/Settings/ConfigComplete", null, this.ConfigComplete, true, false, true);
142
143 return this.completionSource.Task;
144 }
145
150 public virtual Task CleanupAfterConfiguration(HttpServer WebServer)
151 {
152 if (!(this.configResource is null))
153 {
154 WebServer.Unregister(this.configResource);
155 this.configResource = null;
156 }
157
158 return Task.CompletedTask;
159 }
160
166 protected virtual async Task ConfigComplete(HttpRequest Request, HttpResponse Response)
167 {
168 Gateway.AssertUserAuthenticated(Request, new string[] { this.ConfigPrivilege });
169
170 await this.MakeCompleted();
171
172 Response.StatusCode = 200;
173 Response.StatusMessage = "OK";
174 }
175
179 protected abstract string ConfigPrivilege
180 {
181 get;
182 }
183
187 public virtual Task MakeCompleted()
188 {
189 return this.MakeCompleted(false);
190 }
191
196 protected async Task MakeCompleted(bool ReloadConfiguration)
197 {
198 this.complete = true;
199 this.completed = DateTime.Now;
200 this.updated = DateTime.Now;
201
202 if (!ReloadConfiguration)
203 {
204 if (this.Priority <= 0)
205 await Gateway.InternalDatabase.Update(this);
206 else
207 await Database.Update(this);
208 }
209
210 this.completionSource?.TrySetResult(ReloadConfiguration);
211 }
212
217 public virtual Task<bool> SimplifiedConfiguration()
218 {
219 return Task.FromResult(false);
220 }
221
226 public abstract Task<bool> EnvironmentConfiguration();
227
234 public void LogEnvironmentError(string EnvironmentVariable, object Value)
235 {
236 this.LogEnvironmentError(null, EnvironmentVariable, Value);
237 }
238
244 public void LogEnvironmentVariableMissingError(string EnvironmentVariable, object Value)
245 {
246 this.LogEnvironmentError("Value missing.", EnvironmentVariable, Value);
247 }
248
255 public void LogEnvironmentVariableInvalidBooleanError(string EnvironmentVariable, object Value)
256 {
257 this.LogEnvironmentError("Invalid Boolean Value.", EnvironmentVariable, Value);
258 }
259
266 public void LogEnvironmentVariableInvalidIntegerError(string EnvironmentVariable, object Value)
267 {
268 this.LogEnvironmentError("Invalid integer Value.", EnvironmentVariable, Value);
269 }
270
277 public void LogEnvironmentVariableInvalidTimeError(string EnvironmentVariable, object Value)
278 {
279 this.LogEnvironmentError("Invalid time Value.", EnvironmentVariable, Value);
280 }
281
288 public void LogEnvironmentVariableInvalidDateError(string EnvironmentVariable, object Value)
289 {
290 this.LogEnvironmentError("Invalid date Value.", EnvironmentVariable, Value);
291 }
292
301 public void LogEnvironmentVariableInvalidRangeError(int Min, int Max, string EnvironmentVariable, object Value)
302 {
303 if (Max == int.MaxValue)
304 {
305 this.LogEnvironmentError("Value not in valid range. Must be at least " + Min.ToString() + ".",
306 EnvironmentVariable, Value);
307 }
308 else if (Min == int.MinValue)
309 {
310 this.LogEnvironmentError("Value not in valid range. Must be at most " + Max.ToString() + ".",
311 EnvironmentVariable, Value);
312 }
313 else
314 {
315 this.LogEnvironmentError("Value not in valid range. Must be between " + Min.ToString() + " and " + Max.ToString() + ".",
316 EnvironmentVariable, Value);
317 }
318 }
319
327 public void LogEnvironmentError(string Message, string EnvironmentVariable, object Value)
328 {
329 if (string.IsNullOrEmpty(Message))
330 Message = "Environment Variable contains an invalid value.";
331 else
332 Message = "Environment Variable contains an invalid value: " + Message;
333
334 Log.Error(Message, EnvironmentVariable, string.Empty, "ConfigError",
335 new KeyValuePair<string, object>("EnvironmentVariable", EnvironmentVariable),
336 new KeyValuePair<string, object>("Value", Value));
337 }
338
346 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
347 {
348 Value = Environment.GetEnvironmentVariable(VariableName) ?? string.Empty;
349 if (!string.IsNullOrEmpty(Value))
350 return true;
351
352 if (Required)
353 this.LogEnvironmentVariableMissingError(VariableName, Value);
354
355 return false;
356 }
357
365 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out bool Value)
366 {
367 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
368 {
369 Value = default;
370 return false;
371 }
372
373 if (!CommonTypes.TryParse(s, out Value))
374 {
375 this.LogEnvironmentVariableInvalidBooleanError(VariableName, s);
376 return false;
377 }
378
379 return true;
380 }
381
389 public bool TryGetEnvironmentVariable(string VariableName, out bool Value, bool Default)
390 {
391 if (!this.TryGetEnvironmentVariable(VariableName, false, out string s))
392 {
393 Value = Default;
394 return true;
395 }
396
397 if (!CommonTypes.TryParse(s, out Value))
398 {
399 this.LogEnvironmentVariableInvalidBooleanError(VariableName, s);
400 return false;
401 }
402
403 return true;
404 }
405
413 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out int Value)
414 {
415 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
416 {
417 Value = default;
418 return false;
419 }
420
421 if (!int.TryParse(s, out Value))
422 {
423 this.LogEnvironmentVariableInvalidIntegerError(VariableName, s);
424 return false;
425 }
426
427 return true;
428 }
429
439 public bool TryGetEnvironmentVariable(string VariableName, int Min, int Max, bool Required, ref int Value)
440 {
441 if (!this.TryGetEnvironmentVariable(VariableName, Required, out int i))
442 return false;
443
444 if (i < Min || i > Max)
445 {
446 this.LogEnvironmentVariableInvalidRangeError(Min, Max, VariableName, i);
447 return false;
448 }
449
450 Value = i;
451
452 return true;
453 }
454
462 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out TimeSpan? Value)
463 {
464 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
465 {
466 Value = default;
467 return false;
468 }
469
470 if (!TimeSpan.TryParse(s, out TimeSpan TS) || TS < TimeSpan.Zero || TS.TotalHours >= 24)
471 {
472 this.LogEnvironmentVariableInvalidTimeError(VariableName, s);
473 Value = default;
474 return false;
475 }
476
477 Value = TS;
478 return true;
479 }
480
488 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out DateTime? Value)
489 {
490 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
491 {
492 Value = default;
493 return false;
494 }
495
496 if (!XML.TryParse(s, out DateTime TP) || TP.TimeOfDay != TimeSpan.Zero)
497 {
498 this.LogEnvironmentVariableInvalidDateError(VariableName, s);
499 Value = default;
500 return false;
501 }
502
503 Value = TP;
504 return true;
505 }
506
514 public virtual Task DeferredConfiguration(HttpServer WebServer)
515 {
516 return Task.CompletedTask; // No deferred configuration, by default.
517 }
518 }
519}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:692
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3868
static IDatabaseProvider InternalDatabase
Local Internal Encrypted Object Database provider.
Definition: Gateway.cs:3176
Abstract base class for system configurations.
virtual Task InitSetup(HttpServer WebServer)
Initializes the setup object.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out bool Value)
Tries to get a Boolean-valued environment variable.
bool Complete
If the configuration is complete.
void LogEnvironmentVariableInvalidRangeError(int Min, int Max, string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not within a va...
bool TryGetEnvironmentVariable(string VariableName, out bool Value, bool Default)
Tries to get a Boolean-valued environment variable.
DateTime Updated
When the object was updated.
abstract Task< string > Title(Language Language)
Gets a title for the system configuration.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out TimeSpan? Value)
Tries to get a time-valued environment variable.
void LogEnvironmentVariableInvalidDateError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid dat...
virtual Task< bool > SetupConfiguration(HttpServer WebServer)
Waits for the user to provide configuration.
abstract Task ConfigureSystem()
Is called during startup to configure the system.
abstract int Priority
Priority of the setting. Configurations are sorted in ascending order.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out int Value)
Tries to get a integer-valued environment variable.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out DateTime? Value)
Tries to get a date-valued environment variable.
abstract string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
async Task MakeCompleted(bool ReloadConfiguration)
Sets the configuration task as completed.
void LogEnvironmentVariableMissingError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is missing.
virtual Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
DateTime Created
When the object was created.
abstract Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
void LogEnvironmentVariableInvalidBooleanError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid Boo...
virtual Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
void LogEnvironmentVariableInvalidIntegerError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid int...
bool TryGetEnvironmentVariable(string VariableName, int Min, int Max, bool Required, ref int Value)
Tries to get a integer-valued environment variable within a range.
virtual async Task ConfigComplete(HttpRequest Request, HttpResponse Response)
Method is called when the user completes the current configuration task.
void LogEnvironmentError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
virtual Task MakeCompleted()
Sets the configuration task as completed.
virtual Task DeferredConfiguration(HttpServer WebServer)
Performs a deferred configuration. Deferred configurations are such that could not be performed durin...
abstract void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
void LogEnvironmentError(string Message, string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
DateTime Completed
When the configuration was completed.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
void LogEnvironmentVariableInvalidTimeError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value is not a valid tim...
virtual Task CleanupAfterConfiguration(HttpServer WebServer)
Cleans up after configuration has been performed.
Represents an HTTP request.
Definition: HttpRequest.cs:22
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Implements an HTTP server.
Definition: HttpServer.cs:41
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1568
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1743
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
Contains information about a language.
Definition: Language.cs:17
Interface for system configurations. The gateway will scan all module for system configuration classe...
Task Update(object Object)
Updates an object in the database.