-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* generic client * add generic command client --------- Co-authored-by: ridomin <[email protected]>
- Loading branch information
Showing
6 changed files
with
124 additions
and
28 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
15 changes: 7 additions & 8 deletions
15
src/MQTTnet.Extensions.MultiCloud.BrokerIoTClient/CommandClient.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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
|
||
namespace MQTTnet.Extensions.MultiCloud.BrokerIoTClient | ||
namespace MQTTnet.Extensions.MultiCloud.BrokerIoTClient; | ||
|
||
public class CommandClient<T, TResp> : RequestResponseBinder<T, TResp> | ||
{ | ||
public class CommandClient<T, TResp> : RequestResponseBinder<T, TResp> | ||
public CommandClient(IMqttClient client, string commandName) | ||
: base(client, commandName, false) | ||
{ | ||
public CommandClient(IMqttClient client, string commandName) | ||
: base(client, commandName, false) | ||
{ | ||
requestTopicPattern = "device/{clientId}/commands/{commandName}"; | ||
responseTopicSuccess = "device/{clientId}/commands/{commandName}/resp"; | ||
} | ||
requestTopicPattern = "device/{clientId}/commands/{commandName}"; | ||
responseTopicSuccess = "device/{clientId}/commands/{commandName}/resp"; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
src/MQTTnet.Extensions.MultiCloud.BrokerIoTClient/Untyped/GenericCommandClient.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,15 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
using MQTTnet.Extensions.MultiCloud.Serializers; | ||
using MQTTnet.Server; | ||
using System.Xml.Linq; | ||
|
||
namespace MQTTnet.Extensions.MultiCloud.BrokerIoTClient.Untyped; | ||
|
||
public class GenericCommandClient : RequestResponseBinder<GenericCommandRequest, GenericCommandResponse> | ||
{ | ||
public GenericCommandClient(IMqttClient client) : base(client, string.Empty, false) | ||
{ | ||
|
||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
tests/MQTTnet.Extensions.MultiCloud.IntegrationTests/e2e/GenericCommandE2EFixture.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,79 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud.BrokerIoTClient; | ||
using MQTTnet.Extensions.MultiCloud.BrokerIoTClient.Untyped; | ||
using MQTTnet.Extensions.MultiCloud.Connections; | ||
|
||
namespace MQTTnet.Extensions.MultiCloud.IntegrationTests.e2e; | ||
|
||
public class GenericCommandE2EFixture | ||
{ | ||
private static ConnectionSettings TestCS(string clientId) | ||
{ | ||
return new ConnectionSettings | ||
{ | ||
HostName = "localhost", | ||
UseTls = false, | ||
TcpPort = 1883, | ||
UserName = "user", | ||
Password = "password", | ||
ClientId = clientId | ||
}; | ||
} | ||
|
||
internal class Producer | ||
{ | ||
readonly IMqttClient mqttClient; | ||
|
||
public GenericCommand EchoCommand; | ||
|
||
public Producer(IMqttClient client) | ||
{ | ||
mqttClient = client; | ||
|
||
EchoCommand = new GenericCommand(mqttClient) | ||
{ | ||
OnCmdDelegate = async m => | ||
{ | ||
await Task.Delay(m.CommandPayload!.Length * 100); | ||
await Console.Out.WriteLineAsync("[Producer] Running Generic Command in client: " + client.Options.ClientId); | ||
return await Task.FromResult( | ||
new GenericCommandResponse() | ||
{ | ||
Status = 200, | ||
ReponsePayload = m.CommandPayload + m.CommandPayload | ||
}); | ||
} | ||
}; | ||
} | ||
} | ||
|
||
internal class Consumer | ||
{ | ||
readonly IMqttClient mqttClient; | ||
public GenericCommandClient mqttCommand; | ||
|
||
public Consumer(IMqttClient client) | ||
{ | ||
mqttClient = client; | ||
mqttCommand = new GenericCommandClient(mqttClient); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task InvokeCommandWithDefaultTopics() | ||
{ | ||
IMqttClient producerClientOne = await BrokerClientFactory.CreateFromConnectionSettingsAsync(TestCS("deviceOne")); | ||
IMqttClient producerClientTwo = await BrokerClientFactory.CreateFromConnectionSettingsAsync(TestCS("deviceTwo")); | ||
_ = new Producer(producerClientOne); | ||
_ = new Producer(producerClientTwo); | ||
|
||
IMqttClient consumerClient = await BrokerClientFactory.CreateFromConnectionSettingsAsync(TestCS("consumer")); | ||
Consumer consumer = new(consumerClient); | ||
var respOne = await consumer.mqttCommand.InvokeAsync("deviceOne", new GenericCommandRequest() { CommandName = "echo", CommandPayload = "Hello One" }); | ||
var respTwo = await consumer.mqttCommand.InvokeAsync("deviceTwo", new GenericCommandRequest() { CommandName = "echo", CommandPayload = "Hello Two Loooonger " }); | ||
|
||
Assert.Equal("Hello OneHello One", respOne.ReponsePayload); | ||
Assert.Equal("Hello Two Loooonger Hello Two Loooonger ", respTwo.ReponsePayload); | ||
|
||
} | ||
} |