public MqttClientConnectedEventArgs(MqttClientAuthenticateResult authenticateResult)
 {
     AuthenticateResult = authenticateResult ?? throw new ArgumentNullException(nameof(authenticateResult));
 }
Exemplo n.º 2
0
        public async Task <MQTTnet.Client.Connecting.MqttClientAuthenticateResult> ConnectAsync(CancellationToken cancellationToken)
        {
            var cancelToken = cancellationToken != null ? cancellationToken : CancellationToken.None;

            MQTTnet.Client.Connecting.MqttClientAuthenticateResult connectPrimary = null;
            if (!LocalClient.IsConnected)
            {
                LocalClient.UseConnectedHandler(async e =>
                {
                    var primaryFilters = Options.LocalFilters != null && Options.LocalFilters.Any()
                        ? Options.LocalFilters
                        : new TopicFilter[] { new TopicFilterBuilder().WithTopic("#").Build() };
                    await LocalClient.SubscribeAsync(primaryFilters);
                });

                connectPrimary = await LocalClient.ConnectAsync(Options.LocalOptions, cancelToken);

                if (connectPrimary.ResultCode != MqttClientConnectResultCode.Success)
                {
                    throw new ArgumentException("LocalOptions, could not connect to primary server.");
                }
            }

            LocalClient.UseDisconnectedHandler(async e =>
            {
                var connectRetry = Policy
                                   .Handle <Exception>()
                                   .WaitAndRetryForeverAsync(
                    retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                    (exception, timespan) =>
                {
                    Console.WriteLine("### DISCONNECTED FROM SERVER ###");
                });
                await connectRetry.ExecuteAsync(async() => {
                    var response = await LocalClient.ConnectAsync(Options.LocalOptions, cancelToken);
                    Console.Write($"{response.ResultCode} {response.ReasonString}");
                });
            });

            LocalClient.UseApplicationMessageReceivedHandler(e =>
            {
                var message = new MqttApplicationMessageBuilder()
                              .WithTopic(e.ApplicationMessage.Topic)
                              .WithPayload(e.ApplicationMessage.Payload);
                if (e.ApplicationMessage.Retain)
                {
                    message = message.WithRetainFlag();
                }
                switch (e.ApplicationMessage.QualityOfServiceLevel)
                {
                case MqttQualityOfServiceLevel.ExactlyOnce:
                    message = message.WithExactlyOnceQoS();
                    break;

                case MqttQualityOfServiceLevel.AtLeastOnce:
                    message = message.WithAtLeastOnceQoS();
                    break;

                case MqttQualityOfServiceLevel.AtMostOnce:
                    message = message.WithAtMostOnceQoS();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                Task.Run(async() => await SendHubEventAsync(e.ClientId, message.Build()), cancelToken);
            });

            await ReceiveHubMessagesAsync().ConfigureAwait(false);

            return(connectPrimary);
        }