-
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.
* moving serializers * copy serializers to samples * upd tests to new hub * review proto bindings
- Loading branch information
Showing
41 changed files
with
314 additions
and
339 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,18 @@ | ||
using Google.Protobuf; | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
|
||
namespace Serializers; | ||
|
||
public class CommandProtobuff<T, TResp> : CloudToDeviceBinder<T, TResp>, ICommand<T, TResp> | ||
{ | ||
public CommandProtobuff(IMqttClient client, string name, MessageParser parser) | ||
: base(client, name, new ProtobufSerializer(parser)) | ||
{ | ||
UnwrapRequest = false; | ||
RequestTopicPattern = "device/{clientId}/cmd/{name}"; | ||
SubscribeTopicPattern = "device/{clientId}/cmd/{name}"; | ||
ResponseTopicPattern = "device/{clientId}/cmd/{name}/resp"; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...tiCloud/Serializers/ProtobufSerializer.cs → ...otobuff/Serializers/ProtobufSerializer.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
3 changes: 2 additions & 1 deletion
3
...kerIoTClient/ReadOnlyPropertyProtobuff.cs → .../Serializers/ReadOnlyPropertyProtobuff.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
|
||
namespace Serializers; | ||
|
||
public class TelemetryProtobuf<T> : DeviceToCloudBinder<T>, ITelemetry<T> | ||
{ | ||
|
||
public TelemetryProtobuf(IMqttClient mqttClient) : | ||
this(mqttClient, string.Empty) | ||
{ } | ||
|
||
public TelemetryProtobuf(IMqttClient mqttClient, string name) | ||
: base(mqttClient, name, new ProtobufSerializer()) | ||
{ | ||
TopicPattern = "device/{clientId}/tel"; | ||
WrapMessage = 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Google.Protobuf; | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
|
||
namespace mqtt_grpc_device.Serializers; | ||
|
||
public class CommandProtobuff<T, TResp> : CloudToDeviceBinder<T, TResp>, ICommand<T, TResp> | ||
{ | ||
public CommandProtobuff(IMqttClient client, string name, MessageParser parser) | ||
: base(client, name, new ProtobufSerializer(parser)) | ||
{ | ||
UnwrapRequest = false; | ||
RequestTopicPattern = "device/{clientId}/cmd/{name}"; | ||
ResponseTopicPattern = "device/{clientId}/cmd/{name}/resp"; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
samples/mqtt-grpc-device/Serializers/ProtobufSerializer.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 Google.Protobuf; | ||
using MQTTnet.Extensions.MultiCloud; | ||
|
||
namespace mqtt_grpc_device.Serializers; | ||
|
||
public class ProtobufSerializer : IMessageSerializer | ||
{ | ||
private readonly MessageParser? _parser; | ||
public ProtobufSerializer() { } | ||
public ProtobufSerializer(MessageParser parser) => _parser = parser; | ||
public byte[] ToBytes<T>(T payload, string name = "") => (payload as IMessage).ToByteArray(); | ||
|
||
public bool TryReadFromBytes<T>(byte[] payload, string name, out T result) | ||
{ | ||
if (payload == null || payload.Length == 0) | ||
{ | ||
result = default!; | ||
return false; | ||
} | ||
bool found = false; | ||
IMessage msg = _parser!.ParseFrom(payload); | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
found = true; | ||
result = (T)msg; | ||
} | ||
else | ||
{ | ||
if (msg.ToString()!.Contains(name)) // find better way | ||
{ | ||
result = (T)msg; | ||
found = true; | ||
} | ||
else | ||
{ | ||
result = default!; | ||
} | ||
} | ||
return found; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
samples/mqtt-grpc-device/Serializers/ReadOnlyPropertyProtobuff.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,19 @@ | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
using MQTTnet.Extensions.MultiCloud.Serializers; | ||
|
||
namespace mqtt_grpc_device.Serializers; | ||
|
||
public class ReadOnlyPropertyProtobuff<T> : DeviceToCloudBinder<T>, IReadOnlyProperty<T> | ||
{ | ||
public ReadOnlyPropertyProtobuff(IMqttClient mqttClient) : this(mqttClient, string.Empty) { } | ||
|
||
public ReadOnlyPropertyProtobuff(IMqttClient mqttClient, string name) | ||
: base(mqttClient, name, new ProtobufSerializer()) | ||
{ | ||
TopicPattern = "device/{clientId}/props"; | ||
WrapMessage = false; | ||
Retain = true; | ||
} | ||
} |
9 changes: 7 additions & 2 deletions
9
...Cloud.AzureIoTClient/TelemetryProtobuf.cs → ...c-device/Serializers/TelemetryProtobuf.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
51 changes: 51 additions & 0 deletions
51
samples/mqtt-grpc-device/Serializers/WritablePropertyProtobuff.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,51 @@ | ||
using Google.Protobuf; | ||
using MQTTnet.Client; | ||
using MQTTnet.Extensions.MultiCloud; | ||
using MQTTnet.Extensions.MultiCloud.Binders; | ||
using MQTTnet.Extensions.MultiCloud.BrokerIoTClient; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace mqtt_grpc_device.Serializers | ||
{ | ||
public class WritablePropertyProtobuff<T, TResp> : CloudToDeviceBinder<T, TResp>, IWritableProperty<T, TResp>, IDeviceToCloud<TResp> | ||
{ | ||
readonly IMqttClient _connection; | ||
readonly string _name; | ||
public T? Value { get; set; } = default!; | ||
public int? Version { get; set; } = -1; | ||
public WritablePropertyProtobuff(IMqttClient connection, string name, MessageParser parser) | ||
: base(connection, name, new ProtobufSerializer(parser)) | ||
{ | ||
_connection = connection; | ||
_name = name; | ||
SubscribeTopicPattern = "device/{clientId}/props/{name}/set"; | ||
RequestTopicPattern = "device/{clientId}/props/{name}/set"; | ||
ResponseTopicPattern = "device/{clientId}/props/{name}/ack"; | ||
RetainResponse = true; | ||
PreProcessMessage = tp => | ||
{ | ||
Version = tp.Version; | ||
}; | ||
} | ||
|
||
public async Task SendMessageAsync(TResp payload, CancellationToken cancellationToken = default) | ||
{ | ||
var prop = new ReadOnlyProperty<TResp>(_connection, _name) | ||
{ | ||
TopicPattern = "device/{clientId}/props/{name}/ack", | ||
WrapMessage = false | ||
}; | ||
await prop.SendMessageAsync(payload, cancellationToken); | ||
} | ||
|
||
public async Task InitPropertyAsync(string intialState, TResp defaultValue, CancellationToken cancellationToken = default) | ||
{ | ||
TResp payload = default!; //TODO use generic ACK for protos | ||
await SendMessageAsync(payload, cancellationToken); | ||
|
||
} | ||
|
||
|
||
} | ||
} |
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
Oops, something went wrong.