-
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.
* Feat/generic commands (#100) * generic client * add generic command client --------- Co-authored-by: ridomin <[email protected]> * Feat/generic commands (#102) * generic client * add generic command client * status in user properties * all green 1 by 1 --------- Co-authored-by: ridomin <[email protected]> * review generic commands * start 8 * fix obj serializer * fix serializer tests * not trhow birth exceptions * fix audience for gateway * fix twin serialization * reviewing generic ser (#103) Co-authored-by: ridomin <[email protected]> * change correlation to byte * rid wip * Revert "rid wip" This reverts commit e3a23ab. * dont check birth pub * add generic interfaces for commands * review object serializer * adds MqttGatewayHostName * rm status from command * fix status * force 17 --------- Co-authored-by: Rido <[email protected]>
- Loading branch information
Showing
40 changed files
with
472 additions
and
119 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
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
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
2 changes: 1 addition & 1 deletion
2
src/MQTTnet.Extensions.MultiCloud.AzureIoTClient/Untyped/GenericCommandRequest.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
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
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
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
65 changes: 34 additions & 31 deletions
65
src/MQTTnet.Extensions.MultiCloud.BrokerIoTClient/Untyped/GenericCommandBinder.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,48 +1,51 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
using System.Text; | ||
using MQTTnet.Extensions.MultiCloud.Serializers; | ||
|
||
namespace MQTTnet.Extensions.MultiCloud.BrokerIoTClient.Untyped | ||
namespace MQTTnet.Extensions.MultiCloud.BrokerIoTClient.Untyped; | ||
|
||
public class GenericCommand : IGenericCommand | ||
{ | ||
public class GenericCommand | ||
{ | ||
private readonly IMqttClient connection; | ||
public Func<GenericCommandRequest, GenericCommandResponse>? OnCmdDelegate { get; set; } | ||
private readonly IMqttClient connection; | ||
private readonly IMessageSerializer _serializer; | ||
|
||
public GenericCommand(IMqttClient c) | ||
public Func<IGenericCommandRequest, Task<IGenericCommandResponse>>? OnCmdDelegate { get; set; } | ||
|
||
public GenericCommand(IMqttClient c) | ||
{ | ||
_serializer = new Utf8JsonSerializer(); | ||
connection = c; | ||
_ = connection.SubscribeWithReplyAsync($"device/{c.Options.ClientId}/commands/+"); | ||
connection.ApplicationMessageReceivedAsync += async m => | ||
{ | ||
connection = c; | ||
_ = connection.SubscribeWithReplyAsync($"device/{c.Options.ClientId}/commands/+"); | ||
connection.ApplicationMessageReceivedAsync += async m => | ||
var topic = m.ApplicationMessage.Topic; | ||
if (topic.StartsWith($"device/{c.Options.ClientId}/commands/")) | ||
{ | ||
var topic = m.ApplicationMessage.Topic; | ||
if (topic.StartsWith($"device/{c.Options.ClientId}/commands/")) | ||
{ | ||
var segments = topic.Split('/'); | ||
var cmdName = segments[3]; | ||
string msg = Encoding.UTF8.GetString(m.ApplicationMessage.Payload); | ||
var segments = topic.Split('/'); | ||
var cmdName = segments[3]; | ||
|
||
if (_serializer.TryReadFromBytes(m.ApplicationMessage.Payload, string.Empty, out string reqPayload)) | ||
{ | ||
var responseTopic = m.ApplicationMessage.ResponseTopic ?? $"{topic}/resp"; | ||
|
||
GenericCommandRequest req = new() | ||
if (OnCmdDelegate != null) | ||
{ | ||
CommandName = cmdName, | ||
CommandPayload = msg | ||
}; | ||
if (OnCmdDelegate != null && req != null) | ||
{ | ||
var tp = TopicParser.ParseTopic(topic); | ||
GenericCommandResponse response = OnCmdDelegate.Invoke(req); | ||
GenericCommandRequest req = new() | ||
{ | ||
CommandName = cmdName, | ||
CommandPayload = reqPayload, | ||
//CorrelationId = m.ApplicationMessage.CorrelationData | ||
}; | ||
|
||
IGenericCommandResponse response = await OnCmdDelegate.Invoke(req); | ||
await connection.PublishAsync(new MqttApplicationMessageBuilder() | ||
.WithTopic(responseTopic) | ||
.WithPayload(Encoding.UTF8.GetBytes(response.ReponsePayload!)) | ||
.WithCorrelationData(m.ApplicationMessage.CorrelationData ?? Guid.Empty.ToByteArray()) | ||
.WithPayload(_serializer.ToBytes(response.ReponsePayload)) | ||
.WithUserProperty("status", response.Status.ToString()) | ||
.WithCorrelationData(m.ApplicationMessage.CorrelationData) | ||
.Build()); | ||
} | ||
} | ||
|
||
}; | ||
} | ||
// public async Task InitSubscriptionsAsync() => await connection.SubscribeWithReplyAsync("$iothub/methods/POST/#"); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.