2using System.Collections.Generic;
3using System.Threading.Tasks;
24 public const string MqttNamespace =
"http://lab.tagroot.io/Schema/ComSim/MQTT.xsd";
29 public const string MqttSchema =
"TAG.Simulator.MQTT.Schema.ComSimMqtt.xsd";
31 private KeyValuePair<string, MqttQualityOfService>[] subscriptions;
35 private string domain;
36 private string userName;
37 private string password;
39 private bool encrypted;
40 private bool trustServer;
41 private bool isOnline =
false;
42 private TaskCompletionSource<bool> connected;
116 public override Task
FromXml(XmlElement Definition)
119 this.encrypted =
XML.
Attribute(Definition,
"encrypted",
false);
120 this.port =
XML.
Attribute(Definition,
"port", this.encrypted ? 8883 : 1883);
123 this.trustServer =
XML.
Attribute(Definition,
"trustServer",
false);
125 return base.FromXml(Definition);
140 domain = this.domain,
141 encrypted = this.encrypted,
144 password = this.password,
145 trustServer = this.trustServer
150 List<KeyValuePair<string, MqttQualityOfService>> Topics =
new List<KeyValuePair<string, MqttQualityOfService>>();
157 Topics.Add(
new KeyValuePair<string, MqttQualityOfService>(Topic,
Subscribe.
QoS));
161 Result.subscriptions = Topics.ToArray();
175 if (this.credentials is
null)
180 UserName = this.userName,
181 Password =
string.IsNullOrEmpty(this.password) ? string.Empty : await this.
Model.
GetKey(this.password, this.userName)
187 if (this.sniffer is
null)
188 this.client =
new MqttClient(this.domain, this.port, this.encrypted, this.userName, this.credentials.
Password);
190 this.client =
new MqttClient(this.domain, this.port, this.encrypted, this.userName, this.credentials.
Password,
this.sniffer);
192 this.client.TrustServer = this.trustServer;
194 this.client.OnStateChanged += this.Client_OnStateChanged;
195 this.client.OnConnectionError += this.Client_OnConnectionError;
196 this.client.OnError += this.Client_OnError;
197 this.client.OnContentReceived += this.Client_OnContentReceived;
198 this.client.OnPing += this.Client_OnPing;
199 this.client.OnPingResponse += this.Client_OnPingResponse;
200 this.client.OnPublished += this.Client_OnPublished;
201 this.client.OnSubscribed += this.Client_OnSubscribed;
202 this.client.OnUnsubscribed += this.Client_OnUnsubscribed;
204 this.connected =
new TaskCompletionSource<bool>();
207 private Task Client_OnUnsubscribed(
object Sender, ushort PacketIdentifier)
210 new KeyValuePair<string, object>(
"PacketIdentifier", PacketIdentifier),
211 new KeyValuePair<string, object>(
"Client", this.client));
213 return Task.CompletedTask;
216 private Task Client_OnSubscribed(
object Sender, ushort PacketIdentifier)
219 new KeyValuePair<string, object>(
"PacketIdentifier", PacketIdentifier),
220 new KeyValuePair<string, object>(
"Client", this.client));
222 return Task.CompletedTask;
225 private Task Client_OnPublished(
object Sender, ushort PacketIdentifier)
228 new KeyValuePair<string, object>(
"PacketIdentifier", PacketIdentifier),
229 new KeyValuePair<string, object>(
"Client", this.client));
231 return Task.CompletedTask;
234 private Task Client_OnPingResponse(
object Sender, EventArgs e)
237 new KeyValuePair<string, object>(
"e", e),
238 new KeyValuePair<string, object>(
"Client", this.client));
240 return Task.CompletedTask;
243 private Task Client_OnPing(
object Sender, EventArgs e)
246 new KeyValuePair<string, object>(
"e", e),
247 new KeyValuePair<string, object>(
"Client", this.client));
249 return Task.CompletedTask;
252 private Task Client_OnContentReceived(
object Sender,
MqttContent Content)
255 new KeyValuePair<string, object>(
"Content", Content),
256 new KeyValuePair<string, object>(
"Client", this.client));
258 return Task.CompletedTask;
261 private Task Client_OnError(
object Sender, Exception Exception)
264 new KeyValuePair<string, object>(
"Exception", Exception),
265 new KeyValuePair<string, object>(
"Client", this.client));
267 return Task.CompletedTask;
270 private Task Client_OnConnectionError(
object Sender, Exception Exception)
273 new KeyValuePair<string, object>(
"Exception", Exception),
274 new KeyValuePair<string, object>(
"Client", this.client));
276 return Task.CompletedTask;
279 private Task Client_OnStateChanged(
object Sender,
MqttState NewState)
284 this.isOnline =
true;
286 if (
string.IsNullOrEmpty(this.credentials.
ObjectId))
289 this.client.
SUBSCRIBE(this.subscriptions);
291 this.connected?.TrySetResult(
true);
296 this.isOnline =
false;
297 this.connected?.TrySetResult(
false);
302 new KeyValuePair<string, object>(
"NewState", NewState),
303 new KeyValuePair<string, object>(
"Client", this.client));
305 return Task.CompletedTask;
313 if (!(await this.connected.Task))
314 throw new Exception(
"Unable to connect " + this.userName +
"@" + this.domain);
316 this.connected =
null;
327 if (!(this.sniffer is
null))
329 if (this.sniffer is IDisposable Disposable)
330 Disposable.Dispose();
335 return Task.CompletedTask;
348 UserName = this.userName,
string Password
Password hash
Object used in simulation activities.
MQTT Actor connecting to the MQTT network using traditional TCP.
override async Task InitializeInstance()
Initializes an instance of an actor.
override Task FinalizeInstance()
Finalizes an instance of an actor.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
MqttActorTcp(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Abstract base class for MQTT actors.
MqttClient Client
MQTT Client
override string Namespace
XML Namespace where the element is defined.
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
override async Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
bool IsOnline
If instance is online.
const string MqttSchema
TAG.Simulator.MQTT.Schema.ComSimMqtt.xsd
MqttActorTcp(ISimulationNode Parent, Model Model)
MQTT Actor connecting to the MQTT network using traditional TCP.
const string MqttNamespace
http://lab.tagroot.io/Schema/ComSim/MQTT.xsd
bool TrustServer
If server is to be trusted, regardless of state of certificate.
override string LocalName
Local name of XML element defining contents of class.
override async Task StartInstance()
Starts an instance of an actor.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
override object ActivityObject
Returns the object that will be used by the actor for actions during an activity.
Represents a topic subscription
MqttQualityOfService QoS
Quality of Service
Root node of a simulation model
async Task< string > GetKey(string KeyName, string LookupValue)
Gets a key from the database. If it does not exist, it prompts the user for input.
ISniffer GetSniffer(string Actor)
Gets a sniffer, if sniffer output is desired.
bool ExternalEvent(IExternalEventsNode Source, string Name, params KeyValuePair< string, object >[] Arguments)
Method called when an external event has been received.
Abstract base class for actors
string InstanceId
ID of actor instance.
int InstanceIndex
Actor instance index.
Variables Variables
Collection of actor-variables.
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Manages an MQTT connection. Implements MQTT v3.1.1, as defined in http://docs.oasis-open....
async void Dispose()
Closes the connection and disposes of all resources.
Task< ushort > SUBSCRIBE(string Topic, MqttQualityOfService QoS)
Subscribes to information from a topic. Topics can include wildcards.
Information about content received from the MQTT server.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that conform to all child-filters provided.
This filter selects objects that have a named field equal to a given value.
Class managing a script expression.
static Task< string > TransformAsync(string s, string StartDelimiter, string StopDelimiter, Variables Variables)
Transforms a string by executing embedded script.
ISimulationNode[] Children
Child nodes.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
MqttState
State of MQTT connection.