Class MqttClient
- Namespace
- NominalSystems.External
- Assembly
- NominalSystems.External.dll
This class is a wrapper for the MQTT client and allows a particular node to communicate over the network, via MQTT, to a server
public class MqttClient
- Inheritance
-
MqttClient
Constructors
MqttClient()
Default constructor for the client
public MqttClient()
Fields
ServerPort
The port of the server the client has connected to. The value is less than zero if unconfigured or disconnected function has been called
public int ServerPort
Field Value
ServerString
The server the client has connected to. The Length is zero when unconfigured or the disconnect function has been called
public string ServerString
Field Value
Properties
ClientID
The Client ID of the current client
public string ClientID { get; }
Property Value
Connected
Defines whether or not the client is connected to a server or not
public bool Connected { get; }
Property Value
NumMessages
The total number of messages across all topics
public int NumMessages { get; }
Property Value
NumSubscriptions
The number of topics that the client is currently subscribed to
public int NumSubscriptions { get; }
Property Value
Methods
ClearAllMessages()
Clears all messages that exist for all topics stored locally
public void ClearAllMessages()
ClearMessages(string)
Clears all messages stored locally on a particular topic
public void ClearMessages(string topic)
Parameters
topic
stringThe topic to clear messages
Connect(string, int, string)
Connects to a TCP broker and starts a connection with the service for publishing and subscribing
public Task Connect(string server, int port = 0, string id = null)
Parameters
server
stringThe server to connect to
port
intThe port of the server. If the port is empty, a default will be used.
id
stringThe String ID of the port
Returns
- Task
A task completion object
Disconnect(string)
Disconnects the client from the current connection
public Task Disconnect(string reason = "User Requested")
Parameters
reason
string
Returns
- Task
A task completion
GetNumTopicMessages(string)
Returns the number of messages current received over a particular topic. If the topic has not yet been created, it will return 0
public int GetNumTopicMessages(string topic)
Parameters
topic
stringThe topic that will be checked
Returns
- int
The number of messages in a topic
PublishByteArray(string, byte[], MqttQualityOfService)
Publishes a message over a MQTT connection with some Quality of Service level and some data in a byte array format
public Task PublishByteArray(string topic, byte[] data, MqttQualityOfService qos = MqttQualityOfService.AT_MOST_ONCE)
Parameters
topic
stringThe topic to publish the data to
data
byte[]The data that is being published
qos
MqttQualityOfServiceThe Quality of Service level
Returns
- Task
A task completion object
PublishMessage(string, Message, double, MqttQualityOfService)
A function to publish a particular Nominal message over the network which can be accessed by Mono
public void PublishMessage(string topic, Message msg, double time = 0, MqttQualityOfService qos = MqttQualityOfService.AT_MOST_ONCE)
Parameters
topic
stringThe topic to publish the data to
msg
MessageThe message to publish
time
double[s] The current time of the simulation
qos
MqttQualityOfServiceThe Quality of Service level
PublishSerializedMessage<T>(string, T, double, MqttQualityOfService)
Publishes a payload message over an MQTT connection with a particular message and some data on a particular topic. This is a private method and handles the serialization of the message data before publishing
public Task PublishSerializedMessage<T>(string topic, T data, double time = 0, MqttQualityOfService qos = MqttQualityOfService.AT_MOST_ONCE)
Parameters
topic
stringThe topic to publish the data to
data
TThe data that is being published
time
double[s] The current time of the simulation
qos
MqttQualityOfServiceThe Quality of Service level
Returns
Type Parameters
T
The type of data to publish
PublishString(string, string, MqttQualityOfService)
Publishes a message over a MQTT connection with some Quality of Service level and some data in a string format
public Task PublishString(string topic, string data, MqttQualityOfService qos = MqttQualityOfService.AT_MOST_ONCE)
Parameters
topic
stringThe topic to publish the data to
data
stringThe data that is being published
qos
MqttQualityOfServiceThe Quality of Service level
Returns
- Task
A task completion object
Publish<T>(string, T, double, MqttQualityOfService)
Publishes a payload message over an MQTT connection with a particular message and some data on a particular topic
public Task Publish<T>(string topic, T data, double time = 0, MqttQualityOfService qos = MqttQualityOfService.AT_MOST_ONCE)
Parameters
topic
stringThe topic to publish the data to
data
TThe data that is being published
time
double[s] The current time of the simulation
qos
MqttQualityOfServiceThe Quality of Service level
Returns
Type Parameters
T
The type of data to publish
ReadByteArray(string, bool)
Reads and pops a message off the stack of messages that currently exists on the message stack. Once the message is read, it is removed from the list
public byte[] ReadByteArray(string topic, bool wait = false)
Parameters
topic
stringThe topic to read the message from if it exists
wait
boolA flag to wait for a message to appear
Returns
- byte[]
The value of the message if it exists on the topic
ReadString(string, bool)
Reads and pops a message off the stack of messages that currently exists on the message stack. Once the message is read, it is removed from the list
public string ReadString(string topic, bool wait = false)
Parameters
topic
stringThe topic to read the message from if it exists
wait
boolA flag to wait for a message to appear
Returns
- string
The value of the message if it exists on the topic as a string
ReadToMessage(string, Message)
Attempts to read data from a topic and write it into a particular message
public bool ReadToMessage(string topic, Message msg)
Parameters
Returns
- bool
A success flag
Read<T>(string, bool)
Attempts to read the most recent message from the subscription stack and pop it from the system. This will remove it from the topic collection. This is for reading structs or messages that come through the system
public T Read<T>(string topic, bool wait = false)
Parameters
Returns
- T
A data struct that contains the data from the message
Type Parameters
T
The type of struct or class expected
Read<T>(ref T, string, bool)
Attempts to read the most recent message from the subscription stack and pop it from the system. This will remove it from the topic collection. This is for reading structs or messages that come through the system
public bool Read<T>(ref T input, string topic, bool wait = false)
Parameters
input
TThe data being updated
topic
stringThe topic to read from
wait
boolA flag to wait for a message to appear
Returns
- bool
A data struct that contains the data from the message
Type Parameters
T
The type of struct or class expected
Subscribe(string)
Subscribes to a particular topic and will start to listen to messages that appear on this topic
public Task Subscribe(string topic)
Parameters
topic
stringThe name of the topic
Returns
- Task
A task completion object
TopicExists(string)
Returns whether or not a particular topic exists and has been subscribed to already
public bool TopicExists(string topic)
Parameters
topic
stringThe name of the topic
Returns
- bool
Whether the topic has been subscribed to
Unsubscribe(string)
Unsubscribes from a topic and removes it from the topic list
public Task Unsubscribe(string topic)
Parameters
topic
stringThe name of the topic to unsubscribe from
Returns
- Task
A task completion object
WaitForCompletion(double)
Attempts to wait until all messages have completed publishing that exist in the current publishing queue. This can be run at the end of a scenario to ensure all messages get published correctly
public void WaitForCompletion(double timeout = 0)
Parameters
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
WaitForConnection(double)
Waits for a connection with the server to have been created. If no connection has attempted to be started, then it will return. This will pause the current thread until a connection is made Returns the time spent waiting for the connection in milliseconds.
public uint WaitForConnection(double timeout = 0)
Parameters
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
Returns
WaitForDisconnection(double)
Waits for the disconnection form the server to have occured. This will pause the current thread until the disconnection has occured. Returns the time spent waiting for the disconnection in milliseconds.
public uint WaitForDisconnection(double timeout = 0)
Parameters
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
Returns
WaitForMessage(string, double)
Attempts to wait until a particular message from a topic exists. Once it exists, it will return from the function
public void WaitForMessage(string topic, double timeout = 0)
Parameters
topic
stringThe topic name to wait for
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
WaitForMessages(string, int, double)
Attempts to wait until a number of messages has appeared on the topic. Once at least this number appears (or the timeout is exceeded), the loop will break
public void WaitForMessages(string topic, int num, double timeout = 0)
Parameters
topic
stringThe topic name to wait for
num
intThe number of messages to wait for
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
WaitForNumberOfSubscriptions(int, double)
Waits for the target number of topics subscribed to by the client. This will pause the current thread until the target is reached or the timeout is triggered
public bool WaitForNumberOfSubscriptions(int targetNumber, double timeout = 0)
Parameters
targetNumber
int[-] The target number of topics to be subscribed to by the client
timeout
double[s] The timeout to wait for. If the timeout is exceeded, the loop will break
Returns
- bool
Returns if the target number of subscribed topics was reached