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;
2using System.Collections.Generic;
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 }
174
178 protected abstract string ConfigPrivilege
179 {
180 get;
181 }
182
186 public virtual Task MakeCompleted()
187 {
188 return this.MakeCompleted(false);
189 }
190
195 protected async Task MakeCompleted(bool ReloadConfiguration)
196 {
197 this.complete = true;
198 this.completed = DateTime.Now;
199 this.updated = DateTime.Now;
200
201 if (!ReloadConfiguration)
202 {
203 if (this.Priority <= 0)
204 await Gateway.InternalDatabase.Update(this);
205 else
206 await Database.Update(this);
207 }
208
209 this.completionSource?.TrySetResult(ReloadConfiguration);
210 }
211
216 public virtual Task<bool> SimplifiedConfiguration()
217 {
218 return Task.FromResult(false);
219 }
220
225 public abstract Task<bool> EnvironmentConfiguration();
226
233 public void LogEnvironmentError(string EnvironmentVariable, object Value)
234 {
235 this.LogEnvironmentError(null, EnvironmentVariable, Value);
236 }
237
243 public void LogEnvironmentVariableMissingError(string EnvironmentVariable, object Value)
244 {
245 this.LogEnvironmentError("Value missing.", EnvironmentVariable, Value);
246 }
247
254 public void LogEnvironmentVariableInvalidBooleanError(string EnvironmentVariable, object Value)
255 {
256 this.LogEnvironmentError("Invalid Boolean Value.", EnvironmentVariable, Value);
257 }
258
265 public void LogEnvironmentVariableInvalidIntegerError(string EnvironmentVariable, object Value)
266 {
267 this.LogEnvironmentError("Invalid integer Value.", EnvironmentVariable, Value);
268 }
269
276 public void LogEnvironmentVariableInvalidTimeError(string EnvironmentVariable, object Value)
277 {
278 this.LogEnvironmentError("Invalid time Value.", EnvironmentVariable, Value);
279 }
280
287 public void LogEnvironmentVariableInvalidDateError(string EnvironmentVariable, object Value)
288 {
289 this.LogEnvironmentError("Invalid date Value.", EnvironmentVariable, Value);
290 }
291
300 public void LogEnvironmentVariableInvalidRangeError(int Min, int Max, string EnvironmentVariable, object Value)
301 {
302 if (Max == int.MaxValue)
303 {
304 this.LogEnvironmentError("Value not in valid range. Must be at least " + Min.ToString() + ".",
305 EnvironmentVariable, Value);
306 }
307 else if (Min == int.MinValue)
308 {
309 this.LogEnvironmentError("Value not in valid range. Must be at most " + Max.ToString() + ".",
310 EnvironmentVariable, Value);
311 }
312 else
313 {
314 this.LogEnvironmentError("Value not in valid range. Must be between " + Min.ToString() + " and " + Max.ToString() + ".",
315 EnvironmentVariable, Value);
316 }
317 }
318
326 public void LogEnvironmentError(string Message, string EnvironmentVariable, object Value)
327 {
328 if (string.IsNullOrEmpty(Message))
329 Message = "Environment Variable contains an invalid value.";
330 else
331 Message = "Environment Variable contains an invalid value: " + Message;
332
333 Log.Error(Message, EnvironmentVariable, string.Empty, "ConfigError",
334 new KeyValuePair<string, object>("EnvironmentVariable", EnvironmentVariable),
335 new KeyValuePair<string, object>("Value", Value));
336 }
337
345 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
346 {
347 Value = Environment.GetEnvironmentVariable(VariableName) ?? string.Empty;
348 if (!string.IsNullOrEmpty(Value))
349 return true;
350
351 if (Required)
352 this.LogEnvironmentVariableMissingError(VariableName, Value);
353
354 return false;
355 }
356
364 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out bool Value)
365 {
366 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
367 {
368 Value = default;
369 return false;
370 }
371
372 if (!CommonTypes.TryParse(s, out Value))
373 {
374 this.LogEnvironmentVariableInvalidBooleanError(VariableName, s);
375 return false;
376 }
377
378 return true;
379 }
380
388 public bool TryGetEnvironmentVariable(string VariableName, out bool Value, bool Default)
389 {
390 if (!this.TryGetEnvironmentVariable(VariableName, false, out string s))
391 {
392 Value = Default;
393 return true;
394 }
395
396 if (!CommonTypes.TryParse(s, out Value))
397 {
398 this.LogEnvironmentVariableInvalidBooleanError(VariableName, s);
399 return false;
400 }
401
402 return true;
403 }
404
412 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out int Value)
413 {
414 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
415 {
416 Value = default;
417 return false;
418 }
419
420 if (!int.TryParse(s, out Value))
421 {
422 this.LogEnvironmentVariableInvalidIntegerError(VariableName, s);
423 return false;
424 }
425
426 return true;
427 }
428
438 public bool TryGetEnvironmentVariable(string VariableName, int Min, int Max, bool Required, ref int Value)
439 {
440 if (!this.TryGetEnvironmentVariable(VariableName, Required, out int i))
441 return false;
442
443 if (i < Min || i > Max)
444 {
445 this.LogEnvironmentVariableInvalidRangeError(Min, Max, VariableName, i);
446 return false;
447 }
448
449 Value = i;
450
451 return true;
452 }
453
461 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out TimeSpan? Value)
462 {
463 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
464 {
465 Value = default;
466 return false;
467 }
468
469 if (!TimeSpan.TryParse(s, out TimeSpan TS) || TS < TimeSpan.Zero || TS.TotalHours >= 24)
470 {
471 this.LogEnvironmentVariableInvalidTimeError(VariableName, s);
472 Value = default;
473 return false;
474 }
475
476 Value = TS;
477 return true;
478 }
479
487 public bool TryGetEnvironmentVariable(string VariableName, bool Required, out DateTime? Value)
488 {
489 if (!this.TryGetEnvironmentVariable(VariableName, Required, out string s))
490 {
491 Value = default;
492 return false;
493 }
494
495 if (!XML.TryParse(s, out DateTime TP) || TP.TimeOfDay != TimeSpan.Zero)
496 {
497 this.LogEnvironmentVariableInvalidDateError(VariableName, s);
498 Value = default;
499 return false;
500 }
501
502 Value = TP;
503 return true;
504 }
505
506 }
507}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:682
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
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:3041
static IDatabaseProvider InternalDatabase
Local Internal Encrypted Object Database provider.
Definition: Gateway.cs:2408
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.
abstract string Resource
Resource to be redirected to, to perform the configuration.
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.
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:18
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
Implements an HTTP server.
Definition: HttpServer.cs:36
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1287
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1438
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
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.