Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AlertNotifier.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
7using Waher.Events;
8
10{
14 public class AlertNotifier : EventSink
15 {
20 public AlertNotifier(string ObjectID)
21 : base(ObjectID)
22 {
23 }
24
29 public override Task Queue(Event Event)
30 {
31 StringBuilder Markdown = new StringBuilder();
32
33 if (!Event.Message.Contains("=======" + Environment.NewLine))
34 {
35 Markdown.AppendLine(Event.Type.ToString());
36 Markdown.AppendLine("===============");
37 Markdown.AppendLine();
38 }
39
40 Markdown.AppendLine(Event.Message);
41
42 Markdown.AppendLine();
43 Markdown.AppendLine("| Information ||");
44 Markdown.AppendLine("|:------|:-----|");
45
46 StringBuilder sb = new StringBuilder();
47
48 sb.Append(Event.Timestamp.ToShortDateString());
49 sb.Append(", ");
50 sb.Append(Event.Timestamp.ToLongTimeString());
51
52 if (Event.Timestamp.Kind == DateTimeKind.Utc)
53 sb.Append(" (UTC)");
54
55 this.AppendLabel("Timestamp", sb.ToString(), Markdown);
56 this.AppendLabel("Event ID", Event.EventId, Markdown);
57 this.AppendLabel("Actor", Event.Actor, Markdown);
58 this.AppendLabel("Object", Event.Object, Markdown);
59 this.AppendLabel("Module", Event.Module, Markdown);
60 this.AppendLabel("Facility", Event.Facility, Markdown);
61
62 if (!(Event.Tags is null) && Event.Tags.Length > 0)
63 {
64 foreach (KeyValuePair<string, object> P in Event.Tags)
65 this.AppendLabel(P.Key, P.Value?.ToString(), Markdown);
66 }
67
68 return Gateway.SendNotification(Markdown.ToString());
69 }
70
71 private void AppendLabel(string Label, string Value, StringBuilder Markdown)
72 {
73 bool First = true;
74
75 Markdown.Append("| ");
76 Markdown.Append(MarkdownDocument.Encode(Label));
77 Markdown.Append(" | ");
78
79 if (Value.Length > 2 && Value.StartsWith(":") && Value.EndsWith(":") &&
80 EmojiUtilities.TryGetEmoji(Value[1..^1], out EmojiInfo _))
81 {
82 Markdown.Append(Value);
83 }
84 else
85 {
86 foreach (string Row in Value.Trim().Replace("\r\n", "\n").Replace("\r", "\n").Split('\n'))
87 {
88 if (First)
89 First = false;
90 else
91 Markdown.Append("<br/>");
92
93 Markdown.Append(MarkdownDocument.Encode(Row));
94 }
95 }
96
97 Markdown.AppendLine(" |");
98 }
99 }
100}
Contains information about an emoji.
Static class that provide methods for managing emojis.
static bool TryGetEmoji(string ShortName, out EmojiInfo Emoji)
Tries to get information about an emoji, given its short name.
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
Class representing an event.
Definition: Event.cs:11
string Message
Free-text event message.
Definition: Event.cs:132
EventType Type
Type of event.
Definition: Event.cs:122
string Object
Object related to the event.
Definition: Event.cs:137
string Actor
Actor responsible for the action causing the event.
Definition: Event.cs:142
string Module
Module where the event is reported.
Definition: Event.cs:157
DateTime Timestamp
Timestamp of event.
Definition: Event.cs:117
KeyValuePair< string, object >[] Tags
Variable set of tags providing event-specific information.
Definition: Event.cs:167
string EventId
Computer-readable Event ID identifying type of even.
Definition: Event.cs:147
string Facility
Facility can be either a facility in the network sense or in the system sense.
Definition: Event.cs:152
Base class for event sinks.
Definition: EventSink.cs:9
virtual string ObjectID
Object ID, used when logging events.
Definition: LogObject.cs:26
Event sink that forwards events as notification messages to administrators.
AlertNotifier(string ObjectID)
Event sink that forwards events as notification messages to administrators.
override Task Queue(Event Event)
Queues an event to be output.
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static Task SendNotification(Graph Graph)
Sends a graph as a notification message to configured notification recipients.
Definition: Gateway.cs:4677
Definition: ImplTypes.g.cs:58