This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Unsubscribe and LastWill functions in C# MQTT SDK (#2)
* Implement Unsubscribe and LastWill functions in C# MQTT SDK * Make setters internal and add topic ID and client ID to exception messages * Change exception message for unsubscribe and add extra test for subscribe and unsubscribe. Also commit example MQTT project * Fix Example Project path * Add tests for Scenarios when Connection cant be established and unsubscribe throws exception
- Loading branch information
Showing
14 changed files
with
430 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Cumulocity.SDK.MQTT\Cumulocity.SDK.MQTT.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Cumulocity.SDK.MQTT.Model; | ||
using Cumulocity.SDK.MQTT.Model.ConnectionOptions; | ||
using Cumulocity.SDK.MQTT.Model.MqttMessage; | ||
using MqttClient = Cumulocity.SDK.MQTT.MqttClient; | ||
|
||
namespace MQTTApp | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("The application has started. Press Ctrl-C to stop it."); | ||
|
||
var cSource = new CancellationTokenSource(); | ||
var myTask = Task.Factory.StartNew(() => RunJsonViaMqttClientAsync(cSource.Token), cSource.Token); | ||
Console.CancelKeyPress += (sender, eventArgs) => cSource.Cancel(); | ||
myTask.Wait(cSource.Token); | ||
|
||
Console.WriteLine("Now shutting down"); | ||
} | ||
|
||
private static async Task RunJsonViaMqttClientAsync(CancellationToken cToken) | ||
{ | ||
const string serverUrl = "<server url>"; | ||
const string clientId = "my_mqtt_cs_client"; | ||
const string device_name = "My CS MQTT device"; | ||
const string user = "<tenant>/<user>"; | ||
const string password = "<password>"; | ||
|
||
// connections details | ||
var cDetails = new ConnectionDetailsBuilder() | ||
.WithClientId(clientId) | ||
.WithHost(serverUrl) | ||
.WithCredentials(user, password) | ||
.WithCleanSession(false) | ||
.WithProtocol(TransportType.Tcp) | ||
.Build(); | ||
|
||
// Configure MQTT connection with details provided above and connect eith Cumulocity. | ||
MqttClient client = new MqttClient(cDetails); | ||
client.MessageReceived += Client_MessageReceived; | ||
client.Connected += Client_Connected; | ||
client.ConnectionFailed += Client_ConnectionFailed; | ||
await client.EstablishConnectionAsync(); | ||
|
||
// Crate new device with name device_name and type c8y_MQTDevice | ||
string topic = "s/us"; | ||
string payload = $"100,{device_name}, c8y_MQTTDevice"; | ||
var message = new MqttMessageRequestBuilder() | ||
.WithTopicName(topic) | ||
.WithQoS(QoS.EXACTLY_ONCE) | ||
.WithMessageContent(payload) | ||
.Build(); | ||
|
||
await client.PublishAsync(message); | ||
|
||
// set device's hardware information | ||
var deviceMessage = new MqttMessageRequestBuilder() | ||
.WithTopicName("s/us") | ||
.WithQoS(QoS.EXACTLY_ONCE) | ||
.WithMessageContent("110, S123456789, MQTT test model, Rev0.1") | ||
.Build(); | ||
|
||
await client.PublishAsync(deviceMessage); | ||
|
||
// add restart operation | ||
await client.SubscribeAsync(new MqttMessageRequest() { TopicName = "s/ds" }); | ||
await client.SubscribeAsync(new MqttMessageRequest() { TopicName = "s/e" }); | ||
|
||
await client.PublishAsync(new MqttMessageRequestBuilder() | ||
.WithTopicName("s/us") | ||
.WithQoS(QoS.EXACTLY_ONCE) | ||
.WithMessageContent("114,c8y_Restart") | ||
.Build()); | ||
|
||
// generate a random temperature (10º-20º) measurement and send it every second | ||
Random rnd = new Random(); | ||
while (!cToken.IsCancellationRequested) | ||
{ | ||
int temp = rnd.Next(10, 20); | ||
Console.WriteLine("Sending temperature measurement (" + temp + "º) ..."); | ||
await client.PublishAsync(new MqttMessageRequestBuilder() | ||
.WithTopicName("s/us") | ||
.WithQoS(QoS.EXACTLY_ONCE) | ||
.WithMessageContent("211," + temp) | ||
.Build()); | ||
Thread.Sleep(1000); | ||
} | ||
} | ||
|
||
private static void Client_ConnectionFailed(object sender, ProcessFailedEventArgs e) | ||
{ | ||
Console.WriteLine("Connection failed"); | ||
} | ||
|
||
private static void Client_Connected(object sender, ClientConnectedEventArgs e) | ||
{ | ||
Console.WriteLine("Client connected."); | ||
} | ||
|
||
private static void Client_MessageReceived(object sender, IMqttMessageResponse e) | ||
{ | ||
var content = e.MessageContent; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
MQTT-SDK/src/Cumulocity.SDK.MQTT/Model/ConnectionOptions/ILastWillDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Cumulocity.SDK.MQTT.Model | ||
{ | ||
public interface ILastWillDetails | ||
{ | ||
/// <summary> | ||
/// The topic to publish to | ||
/// </summary> | ||
|
||
string Topic { get; } | ||
|
||
/// <summary> | ||
/// The quality of service to publish the message at (0, 1 or 2) | ||
/// </summary> | ||
QoS qoS { get; } | ||
|
||
/// <summary> | ||
/// Message content | ||
/// </summary> | ||
string Message { get; } | ||
|
||
/// <summary> | ||
/// Whether or not the message should be retained | ||
/// </summary> | ||
bool Retained { get; } | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
MQTT-SDK/src/Cumulocity.SDK.MQTT/Model/ConnectionOptions/LastWillDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Cumulocity.SDK.MQTT.Model.ConnectionOptions | ||
{ | ||
public class LastWillDetails : ILastWillDetails | ||
{ | ||
private string _topic; | ||
/// <summary> | ||
/// The topic to publish to | ||
/// </summary> | ||
public string Topic { get => _topic; } | ||
|
||
internal void SetTopic(string topic) { _topic = topic; } | ||
|
||
private QoS _qos; | ||
/// <summary> | ||
/// The quality of service to publish the message at (0, 1 or 2) | ||
/// </summary> | ||
public QoS qoS { get => _qos; } | ||
|
||
internal void SetQoS(QoS qos) { _qos = qos; } | ||
|
||
private string _message; | ||
/// <summary> | ||
/// Message content | ||
/// </summary> | ||
public string Message { get => _message; } | ||
|
||
internal void SetMessage(string message) { _message = message; } | ||
|
||
private bool _retained; | ||
/// <summary> | ||
/// Whether or not the message should be retained | ||
/// </summary> | ||
public bool Retained { get => _retained; } | ||
|
||
internal void SetRetained(bool retained) { _retained = retained; } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
MQTT-SDK/src/Cumulocity.SDK.MQTT/Model/ConnectionOptions/LastWillDetailsBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Cumulocity.SDK.MQTT.Model.ConnectionOptions | ||
{ | ||
public class LastWillDetailsBuilder | ||
{ | ||
private readonly LastWillDetails _lastWillDetails = new LastWillDetails(); | ||
|
||
public LastWillDetailsBuilder WithTopic(string topic) | ||
{ | ||
_lastWillDetails.SetTopic(topic); | ||
return this; | ||
} | ||
|
||
public LastWillDetailsBuilder WithQoS(QoS qos) | ||
{ | ||
_lastWillDetails.SetQoS(qos); | ||
return this; | ||
} | ||
|
||
public LastWillDetailsBuilder WithMessage(string message) | ||
{ | ||
_lastWillDetails.SetMessage(message); | ||
return this; | ||
} | ||
|
||
public LastWillDetailsBuilder WithRetained(bool retained) | ||
{ | ||
_lastWillDetails.SetRetained(retained); | ||
return this; | ||
} | ||
|
||
public ILastWillDetails Build() | ||
{ | ||
return _lastWillDetails; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.