示例#1
0
        public override bool Connect(string deviceId, MqttChannel channel)
        {
            var mqttChannel = GetMqttChannel(deviceId);

            if (mqttChannel != null)
            {
                if (mqttChannel.IsOnine())
                {
                    return(false);
                }
                else if (!mqttChannel.IsOnine())
                {
                    if (mqttChannel.SubscribeStatus == SubscribeStatus.Yes)
                    {
                        var topics = RemoveSubTopic(mqttChannel);
                        foreach (var topic in topics)
                        {
                            Topics.TryGetValue(topic, out IEnumerable <MqttChannel> comparisonValue);
                            var newValue = comparisonValue.Concat(new[] { channel });
                            Topics.AddOrUpdate(topic, newValue, (key, value) => newValue);
                        }
                    }
                }
            }
            MqttChannels.AddOrUpdate(deviceId, channel, (k, v) => channel);
            return(true);
        }
示例#2
0
 public override async ValueTask PingReq(IChannel channel)
 {
     if (MqttChannels.TryGetValue(await this.GetDeviceId(channel), out MqttChannel mqttChannel))
     {
         if (mqttChannel.IsLogin())
         {
             mqttChannel.PingReqTime = DateTime.Now;
         }
     }
 }
        /// <summary>
        /// 获取mqtt通道
        /// </summary>
        /// <param name="deviceId">The device identifier.</param>
        /// <returns>MqttChannel.</returns>
        public MqttChannel GetMqttChannel(string deviceId)
        {
            MqttChannel channel = null;

            if (!string.IsNullOrEmpty(deviceId))
            {
                MqttChannels.TryGetValue(deviceId, out channel);
            }

            return(channel);
        }
        public async ValueTask <bool> GetDeviceIsOnine(string deviceId)
        {
            bool result = false;

            if (!string.IsNullOrEmpty(deviceId))
            {
                MqttChannels.TryGetValue(deviceId, out MqttChannel mqttChannel);
                result = mqttChannel == null?false: await mqttChannel.IsOnine();
            }
            return(result);
        }
示例#5
0
 public override async Task UnSubscribe(string deviceId, params string[] topics)
 {
     if (MqttChannels.TryGetValue(deviceId, out MqttChannel mqttChannel))
     {
         foreach (var topic in topics)
         {
             RemoveChannel(topic, mqttChannel);
             await BrokerCancellationReg(topic);
         }
     }
 }
示例#6
0
 public override async Task Pubrel(IChannel channel, int messageId)
 {
     if (MqttChannels.TryGetValue(await this.GetDeviceId(channel), out MqttChannel mqttChannel))
     {
         if (mqttChannel.IsLogin())
         {
             mqttChannel.RemoveMqttMessage(messageId);
             await _messagePushService.SendToPubComp(channel, messageId);
         }
     }
 }
示例#7
0
        public ValueTask <SessionStatus?> GetDeviceStatus(string deviceId)
        {
            SessionStatus?result = null;

            if (!string.IsNullOrEmpty(deviceId))
            {
                MqttChannels.TryGetValue(deviceId, out MqttChannel mqttChannel);
                result = mqttChannel?.SessionStatus;
            }
            return(new ValueTask <SessionStatus?>(result));
        }
示例#8
0
        /// <summary>
        /// Closes the specified device identifier.
        /// </summary>
        /// <param name="deviceId">The device identifier.</param>
        /// <param name="isDisconnect">if set to <c>true</c> [is disconnect].</param>
        public override async Task Close(string deviceId, bool isDisconnect)
        {
            if (!string.IsNullOrEmpty(deviceId))
            {
                MqttChannels.TryGetValue(deviceId, out var mqttChannel);
                if (mqttChannel != null)
                {
                    mqttChannel.SessionStatus = SessionStatus.CLOSE;
                    await mqttChannel.Close();

                    mqttChannel.Channel = null;
                }

                if (!mqttChannel.CleanSession)
                {
                    var messages = mqttChannel.Messages;
                    if (messages != null)
                    {
                        foreach (var sendMqttMessage in messages.Values)
                        {
                            if (sendMqttMessage.ConfirmStatus == ConfirmStatus.PUB)
                            {
                                _clientSessionService.SaveMessage(mqttChannel.ClientId, new SessionMessage
                                {
                                    Message = sendMqttMessage.ByteBuf,
                                    QoS     = sendMqttMessage.Qos,
                                    Topic   = sendMqttMessage.Topic
                                });
                            }
                        }
                    }
                }
                else
                {
                    MqttChannels.TryRemove(deviceId, out var channel);
                    if (mqttChannel.SubscribeStatus == SubscribeStatus.Yes)
                    {
                        RemoveSubTopic(mqttChannel);
                    }

                    mqttChannel.Topics.ForEach(async topic => { await BrokerCancellationReg(topic); });
                }

                if (mqttChannel.IsWill)
                {
                    if (!isDisconnect)
                    {
                        await _willService.SendWillMessage(deviceId);
                    }
                }
            }
        }
示例#9
0
 /// <summary>
 /// Suscribes the specified device identifier.
 /// </summary>
 /// <param name="deviceId">The device identifier.</param>
 /// <param name="topics">The topics.</param>
 public override async Task Suscribe(string deviceId, params string[] topics)
 {
     if (!string.IsNullOrEmpty(deviceId))
     {
         MqttChannels.TryGetValue(deviceId, out var mqttChannel);
         mqttChannel.SubscribeStatus = SubscribeStatus.Yes;
         mqttChannel.AddTopic(topics);
         if (mqttChannel.IsLogin())
         {
             foreach (var topic in topics)
             {
                 AddChannel(topic, mqttChannel);
                 await RegisterMqttBroker(topic);
                 await SendRetain(topic, mqttChannel);
             }
         }
     }
 }