Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Publish.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using System.Threading.Tasks;
11using Waher.Script;
13using System.IO;
14using Waher.Content;
15
17{
22 {
23 private IValue value;
24 private MqttQualityOfService qos;
25 private StringAttribute actor;
26 private StringAttribute topic;
27 private bool retain;
28
35 : base(Parent, Model)
36 {
37 }
38
42 public override string LocalName => nameof(Publish);
43
47 public override string SchemaResource => MqttActorTcp.MqttSchema;
48
52 public override string Namespace => MqttActorTcp.MqttNamespace;
53
61 {
62 return new Publish(Parent, Model);
63 }
64
69 public override Task FromXml(XmlElement Definition)
70 {
71 this.actor = new StringAttribute(XML.Attribute(Definition, "actor"));
72 this.topic = new StringAttribute(XML.Attribute(Definition, "topic"));
73 this.qos = (MqttQualityOfService)XML.Attribute(Definition, "qos", MqttQualityOfService.AtMostOnce);
74 this.retain = XML.Attribute(Definition, "retain", false);
75
76 return base.FromXml(Definition);
77 }
78
83 public void Register(IValue Value)
84 {
85 if (this.value is null)
86 this.value = Value;
87 else
88 throw new Exception("Value already registered.");
89 }
90
96 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
97 {
98 string Topic = await this.topic.GetValueAsync(Variables);
99 object Content = this.value is null ? string.Empty : await this.value.EvaluateAsync(Variables) ?? string.Empty;
100
101 if (!(Content is byte[] Bin))
102 {
103 KeyValuePair<byte[], string> P = await InternetContent.EncodeAsync(Content, Encoding.UTF8);
104 Bin = P.Key;
105 }
106
107 if (!(await this.GetActorObjectAsync(this.actor, Variables) is MqttActivityObject MqttActor))
108 throw new Exception("Actor not an MQTT actor.");
109
110 await MqttActor.Client.PUBLISH(Topic, this.qos, this.retain, Bin);
111
112 return null;
113 }
114
121 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
122 {
123 base.ExportPlantUml(Output, Indentation, QuoteChar);
124
125 Indent(Output, Indentation);
126 Output.Write(':');
127 Output.Write(this.actor.Value);
128 Output.Write(".Publish");
129 Output.Write("(");
130
131 Indentation++;
132
133 this.AppendArgument(Output, Indentation, "Topic", this.topic.Value, true, QuoteChar);
134 this.AppendArgument(Output, Indentation, "QoS", this.qos.ToString(), false, QuoteChar);
135
136 if (!this.retain)
137 this.AppendArgument(Output, Indentation, "Retain", "true", false, QuoteChar);
138
139 if (!(this.value is null))
140 {
141 if (this.value is Xml Xml && !string.IsNullOrEmpty(Xml.RootName))
142 this.AppendArgument(Output, Indentation, "Content", Xml.RootName, false, QuoteChar);
143 else
144 this.AppendArgument(Output, Indentation, "Content", this.value, QuoteChar);
145 }
146
147 Output.WriteLine(");");
148 }
149
150 private void AppendArgument(StreamWriter Output, int Indentation, string Name, string Value, bool Quotes, char QuoteChar)
151 {
152 this.AppendArgument(Output, Indentation, Name);
153
154 if (Quotes)
155 Eval.ExportPlantUml("\"" + Value.Replace("\"", "\\\"") + "\"", Output, Indentation, QuoteChar, false);
156 else
157 Eval.ExportPlantUml(Value, Output, Indentation, QuoteChar, false);
158 }
159
160 private void AppendArgument(StreamWriter Output, int Indentation, string Name, IValue Value, char QuoteChar)
161 {
162 this.AppendArgument(Output, Indentation, Name);
163 Value.ExportPlantUml(Output, Indentation, QuoteChar);
164 }
165
166 private void AppendArgument(StreamWriter Output, int Indentation, string Name)
167 {
168 Output.WriteLine();
169 Indent(Output, Indentation);
170
171 Output.Write(Name);
172 Output.Write(": ");
173 }
174
175 }
176}
Publishes content to a topic.
Definition: Publish.cs:22
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Publish.cs:96
void Register(IValue Value)
Registers a value for the argument.
Definition: Publish.cs:83
Publish(ISimulationNode Parent, Model Model)
Publishes content to a topic.
Definition: Publish.cs:34
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: Publish.cs:47
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: Publish.cs:60
override string Namespace
XML Namespace where the element is defined.
Definition: Publish.cs:52
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Publish.cs:121
override string LocalName
Local name of XML element defining contents of class.
Definition: Publish.cs:42
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: Publish.cs:69
Object used in simulation activities.
MQTT Actor connecting to the MQTT network using traditional TCP.
Definition: MqttActorTcp.cs:20
const string MqttSchema
TAG.Simulator.MQTT.Schema.ComSimMqtt.xsd
Definition: MqttActorTcp.cs:29
const string MqttNamespace
http://lab.tagroot.io/Schema/ComSim/MQTT.xsd
Definition: MqttActorTcp.cs:24
Root node of a simulation model
Definition: Model.cs:49
Abstract base class for activity nodes
Definition: ActivityNode.cs:15
Executes script in an activity.
Definition: Eval.cs:14
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Eval.cs:88
async Task< object > GetActorObjectAsync(StringAttribute Actor, Variables Variables)
Gets an actor object, given a string representation, possibly containing script, of the actor.
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
Contains the value of a string attribute, possibly with embedded script.
async Task< string > GetValueAsync(Variables Variables)
Gets the value of the attribute.
string Value
String value, from definition
Abstract base class for values
Definition: Value.cs:11
abstract void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
string RootName
Root name
Definition: Xml.cs:34
Static class managing encoding and decoding of internet content.
static Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Task< object > EvaluateAsync(Variables Variables)
Evaluates the value.
Interface for nodes holding a value node
MqttQualityOfService
MQTT Quality of Service level.