Connector.cs File Reference
Data Structures
struct MQttConnection::Connector
<summary>
CLass Connector to connect MQTT Server
</summary>
Namespaces
struct MQttConnection
struct MQTTnet
struct MQTTnet::Client
struct MQTTnet::Client::Options
struct MQTTnet::Client::Disconnecting
struct MQTTnet::Client::Connecting
struct System::Text
Detailed Description
Definition in file C:/Users/siegf/Documents/c#projects/MQttConnection/Connector.cs
1 using MQTTnet;
2 using MQTTnet.Client;
3 using MQTTnet.Client.Options;
4 using MQTTnet.Client.Disconnecting;
5 using MQTTnet.Client.Connecting;
6
7 using System.Text;
8
9 namespace MQttConnection
10 {
14 public class Connector
15 {
16 private string _host;
17 private int _port;
18 private bool _tls = true;
19 private string? _username;
20 private string? _password;
21 private string _payload;
22 private string _topic;
23 private string _clientid = "Client";
24 public event MessageHandler messagehandler;
25 public delegate void MessageHandler(Connector c, Message e);
26 public static IMqttClient client = new MqttFactory().CreateMqttClient();
27
28 public string Host { get { return _host; } set { _host = value; } }
29 public int Port { get { return _port; } set { _port = value; } }
30 public string Payload { get { return _payload; } set { _payload = value; } }
31 public bool Tls { get { return _tls; } set { _tls = value; } }
32 public string topic { get { return _topic; } set { _topic = value; } }
33 public string clientid { get { return _clientid; } set { _clientid = value; } }
34
35
36 private static MqttClientOptionsBuilder mqttClientOptionsBuilder = new MqttClientOptionsBuilder();
37
41 public Connector()
42 {
43 _host = "localhost";
44 _port = 1883;
45 _payload = "";
46 }
47
53 public Connector(string host, int port)
54 {
55 _host = host;
56 _port = port;
57 _payload = "";
58 }
59
67 public Connector(string host, int port, string username, string password)
68 {
69 _host = host;
70 _port = port;
71 _username = username;
72 _password = password;
73 _payload = "";
74 }
75
80 public async Task Initialize()
81 {
82 try
83 {
84 client.UseApplicationMessageReceivedHandler(MessageRecieved);
85 client.UseConnectedHandler(ConnectionHandler);
86 client.UseDisconnectedHandler(DisconnectHandler);
87
88 var options = BuildClientOptions();
89 await client.ConnectAsync(options, CancellationToken.None);
90 }
91 catch (Exception ex)
92 {
93 Console.WriteLine(ex.Message);
94 }
95
96 }
97
102 public async Task<bool> CloseConnection()
103 {
104 await client.DisconnectAsync();
105 return true;
106 }
107
112 public async void UnsubscribeTopic(string topic)
113 {
114 await client.UnsubscribeAsync(topic);
115
116 }
117
122 public async void SubscribeTopic(string topic)
123 {
124 await client.SubscribeAsync(topic);
125 }
126
135 public async Task PublishAsync(string topic, string payload, bool retainFlag = true, int qos = 1) =>
136 await client.PublishAsync(new MqttApplicationMessageBuilder()
137 .WithTopic(topic)
138 .WithPayload(payload)
139 .WithQualityOfServiceLevel((MQTTnet.Protocol.MqttQualityOfServiceLevel)qos)
140 .WithRetainFlag(retainFlag)
141 .Build());
142
147 private void ConnectionHandler(MqttClientConnectedEventArgs e)
148 {
149
150 Message message = new ();
151 message.EventMessage = e.ConnectResult.ResultCode.ToString();
152 messagehandler(this, message);
153
154 }
155
160 private void DisconnectHandler(MqttClientDisconnectedEventArgs e)
161 {
162 Message message = new ();
163 message.EventMessage = e.Exception.Message;
164 messagehandler(this, message);
165 }
166
171 private void MessageRecieved(MqttApplicationMessageReceivedEventArgs mq_message)
172 {
173 Payload = Encoding.UTF8.GetString(mq_message.ApplicationMessage.Payload);
174
175 Message message = new ();
176 message.EventMessage = Payload;
177 messagehandler(this, message);
178 }
179
185 private IMqttClientOptions BuildClientOptions()
186 {
187 if (_host == null || _port == 0)
188 {
189 throw new ArgumentException("No Host or Port");
190 }
191 mqttClientOptionsBuilder.WithClientId(_clientid);
192 mqttClientOptionsBuilder.WithTcpServer(_host, _port);
193 mqttClientOptionsBuilder.WithCleanSession();
194 if (_username != null && _password != null)
195 {
196 mqttClientOptionsBuilder.WithCredentials(_username, _password);
197 }
198 if (_tls)
199 {
200 mqttClientOptionsBuilder.WithTls();
201 }
202
203 var options = mqttClientOptionsBuilder.Build();
204 return options;
205 }
206
207 }
208 }