|
| 1 | +// Copyright (c) The LEGO Group. All rights reserved. |
| 2 | + |
| 3 | +namespace LEGO.AsyncAPI.Bindings.MQTT |
| 4 | +{ |
| 5 | + using System; |
| 6 | + using LEGO.AsyncAPI.Models; |
| 7 | + using LEGO.AsyncAPI.Readers.ParseNodes; |
| 8 | + using LEGO.AsyncAPI.Writers; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Binding class for MQTT channel settings. |
| 12 | + /// </summary> |
| 13 | + public class MQTTServerBinding : ServerBinding<MQTTServerBinding> |
| 14 | + { |
| 15 | + /// <summary> |
| 16 | + /// The client identifier. |
| 17 | + /// </summary> |
| 18 | + public string ClientId { get; set; } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Whether to create a persistent connection or not. |
| 22 | + /// When false, the connection will be persistent. |
| 23 | + /// This is called clean start in MQTTv5. |
| 24 | + /// </summary> |
| 25 | + public bool? CleanSession { get; set; } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Last Will and Testament configuration. |
| 29 | + /// </summary> |
| 30 | + public LastWill LastWill { get; set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Interval in seconds of the longest period of time |
| 34 | + /// the broker and the client can endure without sending a message. |
| 35 | + /// </summary> |
| 36 | + public int? KeepAlive { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Interval in seconds the broker maintains a session |
| 40 | + /// for a disconnected client until this interval expires. |
| 41 | + /// </summary> |
| 42 | + public int? SessionExpiryInterval { get; set; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Number of bytes representing the maximum packet size |
| 46 | + /// the client is willing to accept. |
| 47 | + /// </summary> |
| 48 | + public int? MaximumPacketSize { get; set; } |
| 49 | + |
| 50 | + public override string BindingKey => "mqtt"; |
| 51 | + |
| 52 | + protected override FixedFieldMap<MQTTServerBinding> FixedFieldMap => new () |
| 53 | + { |
| 54 | + { "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } }, |
| 55 | + { "clientId", (a, n) => { a.ClientId = n.GetScalarValue(); } }, |
| 56 | + { "cleanSession", (a, n) => { a.CleanSession = n.GetBooleanValueOrDefault(); } }, |
| 57 | + { "lastWill", (a, n) => { a.LastWill = n.ParseMap(LastWillFixedFields); } }, |
| 58 | + { "keepAlive", (a, n) => { a.KeepAlive = n.GetIntegerValueOrDefault(); } }, |
| 59 | + { "sessionExpiryInterval", (a, n) => { a.SessionExpiryInterval = n.GetIntegerValueOrDefault(); } }, |
| 60 | + { "maximumPacketSize", (a, n) => { a.MaximumPacketSize = n.GetIntegerValueOrDefault(); } }, |
| 61 | + }; |
| 62 | + |
| 63 | + private static FixedFieldMap<LastWill> LastWillFixedFields = new () |
| 64 | + { |
| 65 | + { "topic", (a, n) => { a.Topic = n.GetScalarValue(); } }, |
| 66 | + { "qos", (a, n) => { a.QoS = (uint?)n.GetIntegerValueOrDefault(); } }, |
| 67 | + { "message", (a, n) => { a.Message = n.GetScalarValue(); } }, |
| 68 | + { "retain", (a, n) => { a.Retain = n.GetBooleanValue(); } }, |
| 69 | + }; |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Serialize to AsyncAPI V2 document without using reference. |
| 73 | + /// </summary> |
| 74 | + public override void SerializeProperties(IAsyncApiWriter writer) |
| 75 | + { |
| 76 | + if (writer is null) |
| 77 | + { |
| 78 | + throw new ArgumentNullException(nameof(writer)); |
| 79 | + } |
| 80 | + |
| 81 | + writer.WriteStartObject(); |
| 82 | + writer.WriteRequiredProperty("clientId", this.ClientId); |
| 83 | + writer.WriteOptionalProperty("cleanSession", this.CleanSession); |
| 84 | + writer.WriteOptionalObject("lastWill", this.LastWill, (w, l) => l.Serialize(w)); |
| 85 | + writer.WriteOptionalProperty("keepAlive", this.KeepAlive); |
| 86 | + writer.WriteOptionalProperty("sessionExpiryInterval", this.SessionExpiryInterval); |
| 87 | + writer.WriteOptionalProperty("maximumPacketSize", this.MaximumPacketSize); |
| 88 | + writer.WriteOptionalProperty(AsyncApiConstants.BindingVersion, this.BindingVersion); |
| 89 | + writer.WriteExtensions(this.Extensions); |
| 90 | + writer.WriteEndObject(); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments