Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DomainSettings.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Events;
8
9namespace Waher.IoTGateway
10{
14 public static class DomainSettings
15 {
16 #region Getting settings
17
25 public static Task<string> GetSettingAsync(IHostReference HostRef, string Key, string DefaultValue)
26 {
27 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
28 }
29
37 public static async Task<string> GetSettingAsync(string Host, string Key, string DefaultValue)
38 {
39 Host = IsAlternativeDomain(Host);
40
41 if (!string.IsNullOrEmpty(Host))
42 return await HostSettings.GetAsync(Host, Key, DefaultValue);
43 else
44 return await RuntimeSettings.GetAsync(Key, DefaultValue);
45 }
46
54 public static Task<long> GetSettingAsync(IHostReference HostRef, string Key, long DefaultValue)
55 {
56 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
57 }
58
66 public static async Task<long> GetSettingAsync(string Host, string Key, long DefaultValue)
67 {
68 Host = IsAlternativeDomain(Host);
69
70 if (!string.IsNullOrEmpty(Host))
71 return await HostSettings.GetAsync(Host, Key, DefaultValue);
72 else
73 return await RuntimeSettings.GetAsync(Key, DefaultValue);
74 }
75
83 public static Task<double> GetSettingAsync(IHostReference HostRef, string Key, double DefaultValue)
84 {
85 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
86 }
87
95 public static async Task<double> GetSettingAsync(string Host, string Key, double DefaultValue)
96 {
97 Host = IsAlternativeDomain(Host);
98
99 if (!string.IsNullOrEmpty(Host))
100 return await HostSettings.GetAsync(Host, Key, DefaultValue);
101 else
102 return await RuntimeSettings.GetAsync(Key, DefaultValue);
103 }
104
112 public static Task<DateTime> GetSettingAsync(IHostReference HostRef, string Key, DateTime DefaultValue)
113 {
114 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
115 }
116
124 public static async Task<DateTime> GetSettingAsync(string Host, string Key, DateTime DefaultValue)
125 {
126 Host = IsAlternativeDomain(Host);
127
128 if (!string.IsNullOrEmpty(Host))
129 return await HostSettings.GetAsync(Host, Key, DefaultValue);
130 else
131 return await RuntimeSettings.GetAsync(Key, DefaultValue);
132 }
133
141 public static Task<TimeSpan> GetSettingAsync(IHostReference HostRef, string Key, TimeSpan DefaultValue)
142 {
143 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
144 }
145
153 public static async Task<TimeSpan> GetSettingAsync(string Host, string Key, TimeSpan DefaultValue)
154 {
155 Host = IsAlternativeDomain(Host);
156
157 if (!string.IsNullOrEmpty(Host))
158 return await HostSettings.GetAsync(Host, Key, DefaultValue);
159 else
160 return await RuntimeSettings.GetAsync(Key, DefaultValue);
161 }
162
170 public static Task<Enum> GetSettingAsync(IHostReference HostRef, string Key, Enum DefaultValue)
171 {
172 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
173 }
174
182 public static async Task<Enum> GetSettingAsync(string Host, string Key, Enum DefaultValue)
183 {
184 Host = IsAlternativeDomain(Host);
185
186 if (!string.IsNullOrEmpty(Host))
187 return await HostSettings.GetAsync(Host, Key, DefaultValue);
188 else
189 return await RuntimeSettings.GetAsync(Key, DefaultValue);
190 }
191
199 public static Task<bool> GetSettingAsync(IHostReference HostRef, string Key, bool DefaultValue)
200 {
201 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
202 }
203
211 public static async Task<bool> GetSettingAsync(string Host, string Key, bool DefaultValue)
212 {
213 Host = IsAlternativeDomain(Host);
214
215 if (!string.IsNullOrEmpty(Host))
216 return await HostSettings.GetAsync(Host, Key, DefaultValue);
217 else
218 return await RuntimeSettings.GetAsync(Key, DefaultValue);
219 }
220
228 public static Task<object> GetSettingAsync(IHostReference HostRef, string Key, object DefaultValue)
229 {
230 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
231 }
232
240 public static async Task<object> GetSettingAsync(string Host, string Key, object DefaultValue)
241 {
242 Host = IsAlternativeDomain(Host);
243
244 if (!string.IsNullOrEmpty(Host))
245 return await HostSettings.GetAsync(Host, Key, DefaultValue);
246 else
247 return await RuntimeSettings.GetAsync(Key, DefaultValue);
248 }
249
250 internal static string IsAlternativeDomain(string Host)
251 {
252 if (string.IsNullOrEmpty(Host))
253 return null;
254
255 Host = Host.RemovePortNumber();
256
257 if (Gateway.IsDomain(Host, true) && !Gateway.IsDomain(Host, false))
258 return Host;
259 else
260 return null;
261 }
262
263 #endregion
264
265 #region Setting settings
266
274 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, string Value)
275 {
276 return SetSettingAsync(HostRef.Host, Key, Value);
277 }
278
286 public static async Task<bool> SetSettingAsync(string Host, string Key, string Value)
287 {
288 Host = IsAlternativeDomain(Host);
289
290 if (!string.IsNullOrEmpty(Host))
291 return await HostSettings.SetAsync(Host, Key, Value);
292 else
293 return await RuntimeSettings.SetAsync(Key, Value);
294 }
295
303 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, long Value)
304 {
305 return SetSettingAsync(HostRef.Host, Key, Value);
306 }
307
315 public static async Task<bool> SetSettingAsync(string Host, string Key, long Value)
316 {
317 Host = IsAlternativeDomain(Host);
318
319 if (!string.IsNullOrEmpty(Host))
320 return await HostSettings.SetAsync(Host, Key, Value);
321 else
322 return await RuntimeSettings.SetAsync(Key, Value);
323 }
324
332 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, double Value)
333 {
334 return SetSettingAsync(HostRef.Host, Key, Value);
335 }
336
344 public static async Task<bool> SetSettingAsync(string Host, string Key, double Value)
345 {
346 Host = IsAlternativeDomain(Host);
347
348 if (!string.IsNullOrEmpty(Host))
349 return await HostSettings.SetAsync(Host, Key, Value);
350 else
351 return await RuntimeSettings.SetAsync(Key, Value);
352 }
353
361 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, DateTime Value)
362 {
363 return SetSettingAsync(HostRef.Host, Key, Value);
364 }
365
373 public static async Task<bool> SetSettingAsync(string Host, string Key, DateTime Value)
374 {
375 Host = IsAlternativeDomain(Host);
376
377 if (!string.IsNullOrEmpty(Host))
378 return await HostSettings.SetAsync(Host, Key, Value);
379 else
380 return await RuntimeSettings.SetAsync(Key, Value);
381 }
382
390 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, TimeSpan Value)
391 {
392 return SetSettingAsync(HostRef.Host, Key, Value);
393 }
394
402 public static async Task<bool> SetSettingAsync(string Host, string Key, TimeSpan Value)
403 {
404 Host = IsAlternativeDomain(Host);
405
406 if (!string.IsNullOrEmpty(Host))
407 return await HostSettings.SetAsync(Host, Key, Value);
408 else
409 return await RuntimeSettings.SetAsync(Key, Value);
410 }
411
419 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, Enum Value)
420 {
421 return SetSettingAsync(HostRef.Host, Key, Value);
422 }
423
431 public static async Task<bool> SetSettingAsync(string Host, string Key, Enum Value)
432 {
433 Host = IsAlternativeDomain(Host);
434
435 if (!string.IsNullOrEmpty(Host))
436 return await HostSettings.SetAsync(Host, Key, Value);
437 else
438 return await RuntimeSettings.SetAsync(Key, Value);
439 }
440
448 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, bool Value)
449 {
450 return SetSettingAsync(HostRef.Host, Key, Value);
451 }
452
460 public static async Task<bool> SetSettingAsync(string Host, string Key, bool Value)
461 {
462 Host = IsAlternativeDomain(Host);
463
464 if (!string.IsNullOrEmpty(Host))
465 return await HostSettings.SetAsync(Host, Key, Value);
466 else
467 return await RuntimeSettings.SetAsync(Key, Value);
468 }
469
477 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, object Value)
478 {
479 return SetSettingAsync(HostRef.Host, Key, Value);
480 }
481
489 public static async Task<bool> SetSettingAsync(string Host, string Key, object Value)
490 {
491 Host = IsAlternativeDomain(Host);
492
493 if (!string.IsNullOrEmpty(Host))
494 return await HostSettings.SetAsync(Host, Key, Value);
495 else
496 return await RuntimeSettings.SetAsync(Key, Value);
497 }
498
499 #endregion
500
501 #region Getting file-based settings
502
509 public static Task<string> GetTextFileSettingAsync(IHostReference HostRef, string FileName)
510 {
511 return GetTextFileSettingAsync(HostRef.Host, FileName);
512 }
513
520 public static async Task<string> GetTextFileSettingAsync(string Host, string FileName)
521 {
522 Host = IsAlternativeDomain(Host);
523
524 string Content;
525
526 if (!string.IsNullOrEmpty(Host))
527 Content = await HostSettings.GetAsync(Host, FileName, string.Empty);
528 else
529 Content = await RuntimeSettings.GetAsync(FileName, string.Empty);
530
531 if (!string.IsNullOrEmpty(Content))
532 return Content;
533
534 return await Files.ReadAllTextAsync(FileName);
535 }
536
543 public static Task<byte[]> GetBinaryFileSettingAsync(IHostReference HostRef, string FileName)
544 {
545 return GetBinaryFileSettingAsync(HostRef.Host, FileName);
546 }
547
554 public static async Task<byte[]> GetBinaryFileSettingAsync(string Host, string FileName)
555 {
556 Host = IsAlternativeDomain(Host);
557
558 string Content;
559
560 if (!string.IsNullOrEmpty(Host))
561 Content = await HostSettings.GetAsync(Host, FileName, string.Empty);
562 else
563 Content = await RuntimeSettings.GetAsync(FileName, string.Empty);
564
565 if (!string.IsNullOrEmpty(Content))
566 {
567 try
568 {
569 return Convert.FromBase64String(Content);
570 }
571 catch (Exception ex)
572 {
573 Log.Exception(ex);
574 }
575 }
576
577 return await Runtime.IO.Files.ReadAllBytesAsync(FileName);
578 }
579
586 public static Task<DateTime> GetFileSettingTimestampAsync(IHostReference HostRef, string FileName)
587 {
588 return GetFileSettingTimestampAsync(HostRef.Host, FileName);
589 }
590
597 public static async Task<DateTime> GetFileSettingTimestampAsync(string Host, string FileName)
598 {
599 Host = IsAlternativeDomain(Host);
600
601 DateTime Timestamp;
602
603 if (!string.IsNullOrEmpty(Host))
604 Timestamp = await HostSettings.GetAsync(Host, FileName, DateTime.MinValue);
605 else
606 Timestamp = await RuntimeSettings.GetAsync(FileName, DateTime.MinValue);
607
608 if (Timestamp == DateTime.MinValue)
609 return File.GetLastWriteTimeUtc(FileName);
610 else
611 return Timestamp;
612 }
613
614 #endregion
615 }
616}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
static async Task< long > GetSettingAsync(string Host, string Key, long DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< double > GetSettingAsync(string Host, string Key, double DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< DateTime > GetFileSettingTimestampAsync(string Host, string FileName)
Gets the timestamp of contents of a configurable domain-sensitive file.
static Task< DateTime > GetSettingAsync(IHostReference HostRef, string Key, DateTime DefaultValue)
Gets a setting that may vary depending on domain.
static Task< long > GetSettingAsync(IHostReference HostRef, string Key, long DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, object Value)
Sets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, double Value)
Sets a setting that may vary depending on domain.
static Task< Enum > GetSettingAsync(IHostReference HostRef, string Key, Enum DefaultValue)
Gets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, Enum Value)
Sets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, bool Value)
Sets a setting that may vary depending on domain.
static Task< string > GetSettingAsync(IHostReference HostRef, string Key, string DefaultValue)
Gets a setting that may vary depending on domain.
static Task< string > GetTextFileSettingAsync(IHostReference HostRef, string FileName)
Gets the contents of a configurable domain-sensitive text file.
static async Task< object > GetSettingAsync(string Host, string Key, object DefaultValue)
Gets a setting that may vary depending on domain.
static Task< bool > GetSettingAsync(IHostReference HostRef, string Key, bool DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< byte[]> GetBinaryFileSettingAsync(string Host, string FileName)
Gets the contents of a configurable domain-sensitive binary file.
static async Task< bool > SetSettingAsync(string Host, string Key, TimeSpan Value)
Sets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, string Value)
Sets a setting that may vary depending on domain.
static Task< byte[]> GetBinaryFileSettingAsync(IHostReference HostRef, string FileName)
Gets the contents of a configurable domain-sensitive binary file.
static async Task< bool > SetSettingAsync(string Host, string Key, long Value)
Sets a setting that may vary depending on domain.
static async Task< string > GetSettingAsync(string Host, string Key, string DefaultValue)
Gets a setting that may vary depending on domain.
static Task< object > GetSettingAsync(IHostReference HostRef, string Key, object DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< string > GetTextFileSettingAsync(string Host, string FileName)
Gets the contents of a configurable domain-sensitive text file.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, long Value)
Sets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, bool Value)
Sets a setting that may vary depending on domain.
static async Task< Enum > GetSettingAsync(string Host, string Key, Enum DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< TimeSpan > GetSettingAsync(string Host, string Key, TimeSpan DefaultValue)
Gets a setting that may vary depending on domain.
static Task< double > GetSettingAsync(IHostReference HostRef, string Key, double DefaultValue)
Gets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, DateTime Value)
Sets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, Enum Value)
Sets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, TimeSpan Value)
Sets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, double Value)
Sets a setting that may vary depending on domain.
static Task< bool > SetSettingAsync(IHostReference HostRef, string Key, object Value)
Sets a setting that may vary depending on domain.
static Task< DateTime > GetFileSettingTimestampAsync(IHostReference HostRef, string FileName)
Gets the timestamp of contents of a configurable domain-sensitive file.
static Task< TimeSpan > GetSettingAsync(IHostReference HostRef, string Key, TimeSpan DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< bool > GetSettingAsync(string Host, string Key, bool DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< DateTime > GetSettingAsync(string Host, string Key, DateTime DefaultValue)
Gets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, DateTime Value)
Sets a setting that may vary depending on domain.
static async Task< bool > SetSettingAsync(string Host, string Key, string Value)
Sets a setting that may vary depending on domain.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:5174
Contains static methods
Definition: Files.cs:14
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Files.cs:84
Static class managing persistent host settings. Host settings default to runtime settings if host-spe...
Definition: HostSettings.cs:18
static async Task< string > GetAsync(string Host, string Key, string DefaultValue)
Gets a string-valued setting.
Definition: HostSettings.cs:40
static async Task< bool > SetAsync(string Host, string Key, string Value)
Sets a string-valued setting.
Definition: HostSettings.cs:77
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Interface for objects that contain a reference to a host.
string Host
Host reference.