3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
29 : base(
@"^topologyquery\s*.*$")
36 public override string Name =>
"TopologyQuery";
46 public override async Task
Execute(
ChatState State,
string[] Arguments,
string OrgMessage, Match Details,
49 string Script = OrgMessage.Substring(13).Trim();
53 if (
string.IsNullOrEmpty(Script))
56 Color = SKColors.Empty;
62 Result = Simplify(Result, out Color);
65 StringBuilder sb =
new StringBuilder();
66 string Title = Gateway.HasDomain ? Gateway.Domain.Value :
Gateway.
XmppClient.BareJID;
68 sb.AppendLine(
"```dot");
69 sb.AppendLine(
"digraph G {");
70 sb.AppendLine(
"rankdir=LR");
72 EmbedResult(sb, Title, Result, Color);
75 sb.Append(IoTGateway.Setup.XmppConfiguration.Instance.Host);
76 sb.Append(
"\" -> \"");
78 sb.AppendLine(
"\" [dir=back];");
82 await ResponseCallback(sb.ToString(),
string.Empty);
85 public static object Simplify(
object Result, out SKColor Color)
89 Color = SKColors.Empty;
102 Result = M2.GetColumn(0);
103 else if (M2.Rows == 1)
104 Result = M2.GetRow(0);
108 else if (Result is Array V2)
110 if ((c = V2.Length) == 1)
111 Result = V2.GetValue(0);
114 for (i = 0; i < c; i++)
116 if (V2.GetValue(i) is SKColor Color2)
121 object[] Elements2 =
new object[c - 1];
142 else if (Result is SKColor Color2)
152 public static void EmbedResult(StringBuilder sb,
string Title,
object Result, SKColor Color)
162 if (Color != SKColors.Empty)
164 sb.Append(
"style=filled, fillcolor=\"#");
165 sb.Append(Color.Red.ToString(
"X2"));
166 sb.Append(Color.Green.ToString(
"X2"));
167 sb.Append(Color.Blue.ToString(
"X2"));
169 if (Color.Alpha != 255)
170 sb.Append(Color.Alpha.ToString(
"X2"));
179 if (Result is
IMatrix || Result is Array || Result is
ISet)
186 sb.Append(
"shape=plaintext, margin=0");
192 sb.Append(
"label=<");
199 sb.Append(
"<table border=\"1\" cellborder=\"0\" cellspacing=\"1\">");
200 sb.Append(
"<tr><td align=\"center\" colspan=\"");
201 sb.Append(d.ToString());
204 sb.Append(
"</b></td></tr>");
206 string[] Alignment =
new string[d];
208 for (i = 0; i < c; i++)
210 for (j = 0; j < d; j++)
217 if (Obj is
double || Obj is
float || Obj is
int || Obj is
long)
219 else if (Obj is
bool)
221 else if (Obj is
string || Obj is Enum)
226 if (Alignment[j] is
null)
228 else if (Alignment[j] != s)
229 Alignment[j] =
string.Empty;
239 foreach (
string Name in OM.ColumnNames)
241 sb.Append(
"<td align=\"");
242 sb.Append(
string.IsNullOrEmpty(s = Alignment[j++]) ?
"center" : s);
245 sb.Append(
"</b></td>");
251 for (i = 0; i < c; i++)
255 for (j = 0; j < d; j++)
263 if (!
string.IsNullOrEmpty(s))
265 sb.Append(
" align=\"");
271 sb.Append(Obj?.
ToString() ??
string.Empty);
278 sb.Append(
"</table>");
280 else if (Result is Array V)
284 sb.Append(
"<table border=\"1\" cellborder=\"1\" cellspacing=\"1\">");
285 sb.Append(
"<tr><td align=\"center\" colspan=\"");
286 sb.Append(d.ToString());
289 sb.Append(
"</b></td></tr><tr>");
291 foreach (
object Obj
in V)
294 sb.Append(Obj?.
ToString() ??
string.Empty);
298 sb.Append(
"</tr></table>");
300 else if (Result is
ISet S)
302 StringBuilder sb2 =
new StringBuilder();
310 sb2.Append(Obj?.
ToString() ??
string.Empty);
316 sb.Append(
"<table border=\"1\" cellborder=\"1\" cellspacing=\"1\">");
317 sb.Append(
"<tr><td align=\"center\" colspan=\"");
318 sb.Append(d.ToString());
321 sb.Append(
"</b></td></tr><tr>");
322 sb.Append(sb2.ToString());
323 sb.Append(
"</tr></table>");
329 if (!(Result is
null))
331 sb.Append(
"<br/><FONT POINT-SIZE=\"5\"><br/></FONT><FONT POINT-SIZE=\"20\"><b>");
332 sb.Append(Result?.
ToString() ??
" ");
333 sb.Append(
"</b></FONT>");
337 sb.AppendLine(
">];");
347 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.
Static class managing the runtime environment of the IoT Gateway.
static XmppClient XmppClient
XMPP Client connection of gateway.
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.