forked from cloudevents/sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMqttExtensions.cs
More file actions
71 lines (65 loc) · 3.7 KB
/
MqttExtensions.cs
File metadata and controls
71 lines (65 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) Cloud Native Foundation.
// Licensed under the Apache 2.0 license.
// See LICENSE file in the project root for full license information.
using CloudNative.CloudEvents.Core;
using MQTTnet;
using System;
using System.Collections.Generic;
namespace CloudNative.CloudEvents.Mqtt
{
/// <summary>
/// Extension methods to convert between CloudEvents and MQTT messages.
/// </summary>
public static class MqttExtensions
{
/// <summary>
/// Converts this MQTT message into a CloudEvent object.
/// </summary>
/// <param name="message">The MQTT message to convert. Must not be null.</param>
/// <param name="formatter">The event formatter to use to parse the CloudEvent. Must not be null.</param>
/// <param name="extensionAttributes">The extension attributes to use when parsing the CloudEvent. May be null.</param>
/// <returns>A reference to a validated CloudEvent instance.</returns>
public static CloudEvent ToCloudEvent(this MqttApplicationMessage message,
CloudEventFormatter formatter, params CloudEventAttribute[]? extensionAttributes) =>
ToCloudEvent(message, formatter, (IEnumerable<CloudEventAttribute>?) extensionAttributes);
/// <summary>
/// Converts this MQTT message into a CloudEvent object.
/// </summary>
/// <param name="message">The MQTT message to convert. Must not be null.</param>
/// <param name="formatter">The event formatter to use to parse the CloudEvent. Must not be null.</param>
/// <param name="extensionAttributes">The extension attributes to use when parsing the CloudEvent. May be null.</param>
/// <returns>A reference to a validated CloudEvent instance.</returns>
public static CloudEvent ToCloudEvent(this MqttApplicationMessage message,
CloudEventFormatter formatter, IEnumerable<CloudEventAttribute>? extensionAttributes)
{
Validation.CheckNotNull(formatter, nameof(formatter));
Validation.CheckNotNull(message, nameof(message));
// TODO: Determine if there's a sensible content type we should apply.
return formatter.DecodeStructuredModeMessage(message.PayloadSegment, contentType: null, extensionAttributes);
}
// TODO: Support both binary and structured mode.
/// <summary>
/// Converts a CloudEvent to <see cref="MqttApplicationMessage"/>.
/// </summary>
/// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
/// <param name="contentMode">Content mode. Currently only structured mode is supported.</param>
/// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
/// <param name="topic">The MQTT topic for the message. May be null.</param>
public static MqttApplicationMessage ToMqttApplicationMessage(this CloudEvent cloudEvent, ContentMode contentMode, CloudEventFormatter formatter, string? topic)
{
Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
Validation.CheckNotNull(formatter, nameof(formatter));
switch (contentMode)
{
case ContentMode.Structured:
return new MqttApplicationMessage
{
Topic = topic,
PayloadSegment = BinaryDataUtilities.GetArraySegment(formatter.EncodeStructuredModeMessage(cloudEvent, out _))
};
default:
throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
}
}
}
}