Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add modules support for hub client #79

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Process trx reports with default
if: always()
uses: im-open/[email protected].1
uses: im-open/[email protected].2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MQTTnet.Extensions.MultiCloud.AzureIoTClient
public class HubMqttClient : IHubMqttClient
{
public IMqttClient Connection { get; set; }

public string InitialState { get; set; } = String.Empty;

private readonly TwinRequestResponseBinder twinOperationsBinder;
Expand Down Expand Up @@ -38,12 +37,17 @@ public Func<JsonNode, GenericPropertyAck> OnPropertyUpdateReceived

public Task<string> GetTwinAsync(CancellationToken cancellationToken = default) => twinOperationsBinder.GetTwinAsync(cancellationToken);
public Task<int> UpdateTwinAsync(object payload, CancellationToken cancellationToken = default) => twinOperationsBinder.UpdateTwinAsync(payload, cancellationToken);
public Task<MqttClientPublishResult> SendTelemetryAsync(object payload, CancellationToken t = default) =>
Connection.PublishBinaryAsync($"devices/{Connection.Options.ClientId}/messages/events/",
public async Task<MqttClientPublishResult> SendTelemetryAsync(object payload, CancellationToken t = default)
{
string clientSegment = Connection.Options.ClientId;
if (clientSegment.Contains("/")) //should be a module
{
clientSegment = clientSegment.Replace("/", "/modules/");
}
return await Connection.PublishBinaryAsync($"devices/{clientSegment}/messages/events/",
new UTF8JsonSerializer().ToBytes(payload),
Protocol.MqttQualityOfServiceLevel.AtLeastOnce,
false, t);


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ public async Task SendTelemetry()
Assert.Equal("devices/mock/messages/events/", mqttClient.topicRecceived);
Assert.Equal("{\"temp\":2}", mqttClient.payloadReceived);
}

[Fact]
public async Task SendTelemetryToModule()
{
var mqttClient = new MockMqttClient("mock/myModule");
var hubMqttClient = new HubMqttClient(mqttClient);
//var telemetryBinder = new Telemetry<int>(mqttClient, "temp");
await hubMqttClient.SendTelemetryAsync(new { temp = 2});
Assert.Equal("devices/mock/modules/myModule/messages/events/", mqttClient.topicRecceived);
Assert.Equal("{\"temp\":2}", mqttClient.payloadReceived);
}
}
}
13 changes: 11 additions & 2 deletions tests/MQTTnet.Extensions.MultiCloud.UnitTests/MockMqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@

namespace MQTTnet.Extensions.MultiCloud.UnitTests
{

internal class MockMqttClient : IMqttClient
{
string _clientId = "";


#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public MockMqttClient()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
_clientId = "mock";
}

public MockMqttClient(string clientId)
{
_clientId = clientId;
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public bool IsConnected => throw new NotImplementedException();


public MqttClientOptions Options => new() { ClientId = "mock" };
public MqttClientOptions Options => new() { ClientId = _clientId };

public string payloadReceived;
public string topicRecceived;
Expand Down