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;
7
8namespace Waher.IoTGateway
9{
13 public static class DomainSettings
14 {
15 #region Getting settings
16
24 public static Task<string> GetSettingAsync(IHostReference HostRef, string Key, string DefaultValue)
25 {
26 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
27 }
28
36 public static async Task<string> GetSettingAsync(string Host, string Key, string DefaultValue)
37 {
38 Host = IsAlternativeDomain(Host);
39
40 if (!string.IsNullOrEmpty(Host))
41 return await HostSettings.GetAsync(Host, Key, DefaultValue);
42 else
43 return await RuntimeSettings.GetAsync(Key, DefaultValue);
44 }
45
53 public static Task<long> GetSettingAsync(IHostReference HostRef, string Key, long DefaultValue)
54 {
55 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
56 }
57
65 public static async Task<long> GetSettingAsync(string Host, string Key, long DefaultValue)
66 {
67 Host = IsAlternativeDomain(Host);
68
69 if (!string.IsNullOrEmpty(Host))
70 return await HostSettings.GetAsync(Host, Key, DefaultValue);
71 else
72 return await RuntimeSettings.GetAsync(Key, DefaultValue);
73 }
74
82 public static Task<double> GetSettingAsync(IHostReference HostRef, string Key, double DefaultValue)
83 {
84 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
85 }
86
94 public static async Task<double> GetSettingAsync(string Host, string Key, double DefaultValue)
95 {
96 Host = IsAlternativeDomain(Host);
97
98 if (!string.IsNullOrEmpty(Host))
99 return await HostSettings.GetAsync(Host, Key, DefaultValue);
100 else
101 return await RuntimeSettings.GetAsync(Key, DefaultValue);
102 }
103
111 public static Task<DateTime> GetSettingAsync(IHostReference HostRef, string Key, DateTime DefaultValue)
112 {
113 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
114 }
115
123 public static async Task<DateTime> GetSettingAsync(string Host, string Key, DateTime DefaultValue)
124 {
125 Host = IsAlternativeDomain(Host);
126
127 if (!string.IsNullOrEmpty(Host))
128 return await HostSettings.GetAsync(Host, Key, DefaultValue);
129 else
130 return await RuntimeSettings.GetAsync(Key, DefaultValue);
131 }
132
140 public static Task<TimeSpan> GetSettingAsync(IHostReference HostRef, string Key, TimeSpan DefaultValue)
141 {
142 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
143 }
144
152 public static async Task<TimeSpan> GetSettingAsync(string Host, string Key, TimeSpan DefaultValue)
153 {
154 Host = IsAlternativeDomain(Host);
155
156 if (!string.IsNullOrEmpty(Host))
157 return await HostSettings.GetAsync(Host, Key, DefaultValue);
158 else
159 return await RuntimeSettings.GetAsync(Key, DefaultValue);
160 }
161
169 public static Task<Enum> GetSettingAsync(IHostReference HostRef, string Key, Enum DefaultValue)
170 {
171 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
172 }
173
181 public static async Task<Enum> GetSettingAsync(string Host, string Key, Enum DefaultValue)
182 {
183 Host = IsAlternativeDomain(Host);
184
185 if (!string.IsNullOrEmpty(Host))
186 return await HostSettings.GetAsync(Host, Key, DefaultValue);
187 else
188 return await RuntimeSettings.GetAsync(Key, DefaultValue);
189 }
190
198 public static Task<bool> GetSettingAsync(IHostReference HostRef, string Key, bool DefaultValue)
199 {
200 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
201 }
202
210 public static async Task<bool> GetSettingAsync(string Host, string Key, bool DefaultValue)
211 {
212 Host = IsAlternativeDomain(Host);
213
214 if (!string.IsNullOrEmpty(Host))
215 return await HostSettings.GetAsync(Host, Key, DefaultValue);
216 else
217 return await RuntimeSettings.GetAsync(Key, DefaultValue);
218 }
219
227 public static Task<object> GetSettingAsync(IHostReference HostRef, string Key, object DefaultValue)
228 {
229 return GetSettingAsync(HostRef.Host, Key, DefaultValue);
230 }
231
239 public static async Task<object> GetSettingAsync(string Host, string Key, object DefaultValue)
240 {
241 Host = IsAlternativeDomain(Host);
242
243 if (!string.IsNullOrEmpty(Host))
244 return await HostSettings.GetAsync(Host, Key, DefaultValue);
245 else
246 return await RuntimeSettings.GetAsync(Key, DefaultValue);
247 }
248
249 internal static string IsAlternativeDomain(string Host)
250 {
251 if (string.IsNullOrEmpty(Host))
252 return null;
253
254 int i = Host.LastIndexOf(':');
255 if (i > 0)
256 Host = Host.Substring(0, i);
257
258 if (Gateway.IsDomain(Host, true) && !Gateway.IsDomain(Host, false))
259 return Host;
260 else
261 return null;
262 }
263
264 #endregion
265
266 #region Setting settings
267
275 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, string Value)
276 {
277 return SetSettingAsync(HostRef.Host, Key, Value);
278 }
279
287 public static async Task<bool> SetSettingAsync(string Host, string Key, string Value)
288 {
289 Host = IsAlternativeDomain(Host);
290
291 if (!string.IsNullOrEmpty(Host))
292 return await HostSettings.SetAsync(Host, Key, Value);
293 else
294 return await RuntimeSettings.SetAsync(Key, Value);
295 }
296
304 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, long Value)
305 {
306 return SetSettingAsync(HostRef.Host, Key, Value);
307 }
308
316 public static async Task<bool> SetSettingAsync(string Host, string Key, long Value)
317 {
318 Host = IsAlternativeDomain(Host);
319
320 if (!string.IsNullOrEmpty(Host))
321 return await HostSettings.SetAsync(Host, Key, Value);
322 else
323 return await RuntimeSettings.SetAsync(Key, Value);
324 }
325
333 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, double Value)
334 {
335 return SetSettingAsync(HostRef.Host, Key, Value);
336 }
337
345 public static async Task<bool> SetSettingAsync(string Host, string Key, double Value)
346 {
347 Host = IsAlternativeDomain(Host);
348
349 if (!string.IsNullOrEmpty(Host))
350 return await HostSettings.SetAsync(Host, Key, Value);
351 else
352 return await RuntimeSettings.SetAsync(Key, Value);
353 }
354
362 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, DateTime Value)
363 {
364 return SetSettingAsync(HostRef.Host, Key, Value);
365 }
366
374 public static async Task<bool> SetSettingAsync(string Host, string Key, DateTime Value)
375 {
376 Host = IsAlternativeDomain(Host);
377
378 if (!string.IsNullOrEmpty(Host))
379 return await HostSettings.SetAsync(Host, Key, Value);
380 else
381 return await RuntimeSettings.SetAsync(Key, Value);
382 }
383
391 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, TimeSpan Value)
392 {
393 return SetSettingAsync(HostRef.Host, Key, Value);
394 }
395
403 public static async Task<bool> SetSettingAsync(string Host, string Key, TimeSpan Value)
404 {
405 Host = IsAlternativeDomain(Host);
406
407 if (!string.IsNullOrEmpty(Host))
408 return await HostSettings.SetAsync(Host, Key, Value);
409 else
410 return await RuntimeSettings.SetAsync(Key, Value);
411 }
412
420 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, Enum Value)
421 {
422 return SetSettingAsync(HostRef.Host, Key, Value);
423 }
424
432 public static async Task<bool> SetSettingAsync(string Host, string Key, Enum Value)
433 {
434 Host = IsAlternativeDomain(Host);
435
436 if (!string.IsNullOrEmpty(Host))
437 return await HostSettings.SetAsync(Host, Key, Value);
438 else
439 return await RuntimeSettings.SetAsync(Key, Value);
440 }
441
449 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, bool Value)
450 {
451 return SetSettingAsync(HostRef.Host, Key, Value);
452 }
453
461 public static async Task<bool> SetSettingAsync(string Host, string Key, bool Value)
462 {
463 Host = IsAlternativeDomain(Host);
464
465 if (!string.IsNullOrEmpty(Host))
466 return await HostSettings.SetAsync(Host, Key, Value);
467 else
468 return await RuntimeSettings.SetAsync(Key, Value);
469 }
470
478 public static Task<bool> SetSettingAsync(IHostReference HostRef, string Key, object Value)
479 {
480 return SetSettingAsync(HostRef.Host, Key, Value);
481 }
482
490 public static async Task<bool> SetSettingAsync(string Host, string Key, object Value)
491 {
492 Host = IsAlternativeDomain(Host);
493
494 if (!string.IsNullOrEmpty(Host))
495 return await HostSettings.SetAsync(Host, Key, Value);
496 else
497 return await RuntimeSettings.SetAsync(Key, Value);
498 }
499
500 #endregion
501
502 #region Getting file-based settings
503
510 public static Task<string> GetTextFileSettingAsync(IHostReference HostRef, string FileName)
511 {
512 return GetTextFileSettingAsync(HostRef.Host, FileName);
513 }
514
521 public static async Task<string> GetTextFileSettingAsync(string Host, string FileName)
522 {
523 Host = IsAlternativeDomain(Host);
524
525 string Content;
526
527 if (!string.IsNullOrEmpty(Host))
528 Content = await HostSettings.GetAsync(Host, FileName, string.Empty);
529 else
530 Content = await RuntimeSettings.GetAsync(FileName, string.Empty);
531
532 if (!string.IsNullOrEmpty(Content))
533 return Content;
534
535 return await Resources.ReadAllTextAsync(FileName);
536 }
537
544 public static Task<byte[]> GetBinaryFileSettingAsync(IHostReference HostRef, string FileName)
545 {
546 return GetBinaryFileSettingAsync(HostRef.Host, FileName);
547 }
548
555 public static async Task<byte[]> GetBinaryFileSettingAsync(string Host, string FileName)
556 {
557 Host = IsAlternativeDomain(Host);
558
559 string Content;
560
561 if (!string.IsNullOrEmpty(Host))
562 Content = await HostSettings.GetAsync(Host, FileName, string.Empty);
563 else
564 Content = await RuntimeSettings.GetAsync(FileName, string.Empty);
565
566 if (!string.IsNullOrEmpty(Content))
567 {
568 try
569 {
570 return Convert.FromBase64String(Content);
571 }
572 catch (Exception ex)
573 {
574 Log.Exception(ex);
575 }
576 }
577
578 return await Resources.ReadAllBytesAsync(FileName);
579 }
580
587 public static Task<DateTime> GetFileSettingTimestampAsync(IHostReference HostRef, string FileName)
588 {
589 return GetFileSettingTimestampAsync(HostRef.Host, FileName);
590 }
591
598 public static async Task<DateTime> GetFileSettingTimestampAsync(string Host, string FileName)
599 {
600 Host = IsAlternativeDomain(Host);
601
602 DateTime Timestamp;
603
604 if (!string.IsNullOrEmpty(Host))
605 Timestamp = await HostSettings.GetAsync(Host, FileName, DateTime.MinValue);
606 else
607 Timestamp = await RuntimeSettings.GetAsync(FileName, DateTime.MinValue);
608
609 if (Timestamp == DateTime.MinValue)
610 return File.GetLastWriteTime(FileName);
611 else
612 return Timestamp;
613 }
614
615 #endregion
616 }
617}
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Resources.cs:205
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
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:126
static bool IsDomain(string DomainOrHost, bool IncludeAlternativeDomains)
If a domain or host name represents the gateway.
Definition: Gateway.cs:4258
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.