Skip to content

Commit

Permalink
add modules support for hub client (#79)
Browse files Browse the repository at this point in the history
* add modules support for hub client

* add module tests

* upd trx to 2.2.2

Co-authored-by: ridomin <[email protected]>
  • Loading branch information
rido-min and ridomin authored Nov 4, 2022
1 parent dd0240e commit 9f0c80c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
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

0 comments on commit 9f0c80c

Please sign in to comment.