Example #1
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);
        }
Example #4
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);
         }
     }
 }
Example #5
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);
         }
     }
 }
Example #6
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));
        }
Example #7
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);
                    }
                }
            }
        }
Example #8
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);
             }
         }
     }
 }