3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
28 : base(
@"^topologyquery\s*.*$")
35 public override string Name =>
"TopologyQuery";
45 public override async Task
Execute(
ChatState State,
string[] Arguments,
string OrgMessage, Match Details,
48 string Script = OrgMessage.Substring(13).Trim();
52 if (
string.IsNullOrEmpty(Script))
55 Color = SKColors.Empty;
61 Result = Simplify(Result, out Color);
64 StringBuilder sb =
new StringBuilder();
65 string Title =
string.IsNullOrEmpty(IoTGateway.Gateway.Domain) ? IoTGateway.Gateway.XmppClient.BareJID : IoTGateway.Gateway.Domain.Value;
67 sb.AppendLine(
"```dot");
68 sb.AppendLine(
"digraph G {");
69 sb.AppendLine(
"rankdir=LR");
71 EmbedResult(sb, Title, Result, Color);
74 sb.Append(IoTGateway.Setup.XmppConfiguration.Instance.Host);
75 sb.Append(
"\" -> \"");
77 sb.AppendLine(
"\" [dir=back];");
81 await ResponseCallback(sb.ToString(),
string.Empty);
84 public static object Simplify(
object Result, out SKColor Color)
88 Color = SKColors.Empty;
101 Result = M2.GetColumn(0);
102 else if (M2.Rows == 1)
103 Result = M2.GetRow(0);
107 else if (Result is Array V2)
109 if ((c = V2.Length) == 1)
110 Result = V2.GetValue(0);
113 for (i = 0; i < c; i++)
115 if (V2.GetValue(i) is SKColor Color2)
120 object[] Elements2 =
new object[c - 1];
141 else if (Result is SKColor Color2)
151 public static void EmbedResult(StringBuilder sb,
string Title,
object Result, SKColor Color)
161 if (Color != SKColors.Empty)
163 sb.Append(
"style=filled, fillcolor=\"#");
164 sb.Append(Color.Red.ToString(
"X2"));
165 sb.Append(Color.Green.ToString(
"X2"));
166 sb.Append(Color.Blue.ToString(
"X2"));
168 if (Color.Alpha != 255)
169 sb.Append(Color.Alpha.ToString(
"X2"));
178 if (Result is
IMatrix || Result is Array || Result is
ISet)
185 sb.Append(
"shape=plaintext, margin=0");
191 sb.Append(
"label=<");
198 sb.Append(
"<table border=\"1\" cellborder=\"0\" cellspacing=\"1\">");
199 sb.Append(
"<tr><td align=\"center\" colspan=\"");
200 sb.Append(d.ToString());
203 sb.Append(
"</b></td></tr>");
205 string[] Alignment =
new string[d];
207 for (i = 0; i < c; i++)
209 for (j = 0; j < d; j++)
216 if (Obj is
double || Obj is
float || Obj is
int || Obj is
long)
218 else if (Obj is
bool)
220 else if (Obj is
string || Obj is Enum)
225 if (Alignment[j] is
null)
227 else if (Alignment[j] != s)
228 Alignment[j] =
string.Empty;
238 foreach (
string Name in OM.ColumnNames)
240 sb.Append(
"<td align=\"");
241 sb.Append(
string.IsNullOrEmpty(s = Alignment[j++]) ?
"center" : s);
244 sb.Append(
"</b></td>");
250 for (i = 0; i < c; i++)
254 for (j = 0; j < d; j++)
262 if (!
string.IsNullOrEmpty(s))
264 sb.Append(
" align=\"");
270 sb.Append(Obj?.
ToString() ??
string.Empty);
277 sb.Append(
"</table>");
279 else if (Result is Array V)
283 sb.Append(
"<table border=\"1\" cellborder=\"1\" cellspacing=\"1\">");
284 sb.Append(
"<tr><td align=\"center\" colspan=\"");
285 sb.Append(d.ToString());
288 sb.Append(
"</b></td></tr><tr>");
290 foreach (
object Obj
in V)
293 sb.Append(Obj?.
ToString() ??
string.Empty);
297 sb.Append(
"</tr></table>");
299 else if (Result is
ISet S)
301 StringBuilder sb2 =
new StringBuilder();
309 sb2.Append(Obj?.
ToString() ??
string.Empty);
315 sb.Append(
"<table border=\"1\" cellborder=\"1\" cellspacing=\"1\">");
316 sb.Append(
"<tr><td align=\"center\" colspan=\"");
317 sb.Append(d.ToString());
320 sb.Append(
"</b></td></tr><tr>");
321 sb.Append(sb2.ToString());
322 sb.Append(
"</tr></table>");
328 if (!(Result is
null))
330 sb.Append(
"<br/><FONT POINT-SIZE=\"5\"><br/></FONT><FONT POINT-SIZE=\"20\"><b>");
331 sb.Append(Result?.
ToString() ??
" ");
332 sb.Append(
"</b></FONT>");
336 sb.AppendLine(
">];");
346 new HelpItem(
"TopologyQuery[ SCRIPT]",
"Executes script (if provided), and returns the result in the form of a GraphViz graph of the current broker connected to its parent broker. Consolidating responses from multiple brokers generates a topology graph of all brokers, where each node contains the result of executing the script.")
Helps with common XML-related tasks.
static string Encode(string s)
Encodes a string for use in XML.
Base class for all types of elements.
Class managing a script expression.
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
bool HasColumnNames
If the matrix has column names defined.
ToMatrix(ScriptNode Operand, bool NullCheck, int Start, int Length, Expression Expression)
To-Matrix operator.
readonly Variables Session
Session variables
An administrative command whose syntax is validated with a regular expression.
Contains an item of information about a command.
Executes script (if provided), and returns the result in the form of a GraphViz graph of the current ...
override string Name
Command name
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
TopologyQuery()
Executes script (if provided), and returns the result in the form of a GraphViz graph of the current ...
override HelpItem[] GetHelp()
Markdown description of syntax.
Basic interface for all types of elements.
object AssociatedObjectValue
Associated object value.
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Basic interface for matrices.
Basic interface for all types of sets.
Interface for objects that can be converted into matrices.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.