Example #1
0
        public override async Task <MqttMessage> ProcessAsync()
        {
            if (!Session.IsConnected)
            {
                Session.Disconnect(Message);
                return(null);
            }

            Session.IncrementKeepAlive();
            List <QualityOfServiceLevelType> list = new List <QualityOfServiceLevelType>();
            SubscribeMessage msg = Message as SubscribeMessage;

            List <string> validSubs = Session.Subscribe(Message);
            IEnumerator <KeyValuePair <string, QualityOfServiceLevelType> > en = msg.Topics.GetEnumerator();

            while (en.MoveNext())
            {
                MqttUri uri = new MqttUri(en.Current.Key);
                QualityOfServiceLevelType qos = validSubs.Contains(uri.ToString())
                    ? en.Current.Value
                    : QualityOfServiceLevelType.Failure;
                list.Add(qos);
                Session.AddQosLevel(uri.Resource, qos);
            }

            return(await Task.FromResult <MqttMessage>(new SubscriptionAckMessage(Message.MessageId, list)));
        }
Example #2
0
        private void Session_OnUnsubscribe(object sender, MqttMessageEventArgs args)
        {
            try
            {
                UnsubscribeMessage msg = (UnsubscribeMessage)args.Message;
                foreach (var item in msg.Topics)
                {
                    MqttUri uri = new MqttUri(item.ToLowerInvariant());

                    if (EventValidator.Validate(false, uri.Resource, Channel, graphManager, context).Validated)
                    {
                        adapter.UnsubscribeAsync(uri.Resource).GetAwaiter();
                        logger?.LogInformationAsync($"MQTT adapter unsubscribed {uri.ToString()}");
                    }
                }
            }
            catch (Exception ex)
            {
                logger?.LogErrorAsync(ex, $"MQTT adapter Session_OnUnsubscribe error on channel '{Channel.Id}'.").GetAwaiter();
                OnError?.Invoke(this, new ProtocolAdapterErrorEventArgs(Channel.Id, ex));
            }
        }