public override async Task SendWillMsg(MqttWillMessage willMeaasge) { Topics.TryGetValue(willMeaasge.Topic, out IEnumerable <MqttChannel> mqttChannels); if (mqttChannels != null && mqttChannels.Any()) { foreach (var mqttChannel in mqttChannels) { switch (mqttChannel.SessionStatus) { case SessionStatus.CLOSE: _clientSessionService.SaveMessage(mqttChannel.ClientId, new SessionMessage { Message = Encoding.UTF8.GetBytes(willMeaasge.WillMessage), QoS = willMeaasge.Qos, Topic = willMeaasge.Topic }); break; case SessionStatus.OPEN: await _messagePushService.WriteWillMsg(mqttChannel, willMeaasge); break; } } } }
public async Task Publish(string deviceId, WillMessage message) { var willMessage = new MqttWillMessage { WillMessage = message.Message, Qos = message.Qos, Topic = message.Topic, WillRetain = message.WillRetain }; await Publish(deviceId, willMessage); await RemotePublish(deviceId, willMessage); }
/// <summary> /// Remotes the publish message. /// </summary> /// <param name="deviceId">The device identifier.</param> /// <param name="willMessage">The will message.</param> public async Task RemotePublishMessage(string deviceId, MqttWillMessage willMessage) { await _mqttRemoteInvokeService.InvokeAsync(new MqttRemoteInvokeContext { topic = willMessage.Topic, InvokeMessage = new RemoteInvokeMessage { ServiceId = _publishServiceId, Parameters = new Dictionary <string, object> { { "deviceId", deviceId }, { "message", willMessage } } } }, AppConfig.ServerOptions.ExecutionTimeoutInMilliseconds); }
public override async Task Publish(string deviceId, MqttWillMessage willMessage) { if (!string.IsNullOrEmpty(deviceId)) { var mqttChannel = GetMqttChannel(deviceId); if (mqttChannel.SessionStatus == SessionStatus.OPEN) { await _messagePushService.WriteWillMsg(mqttChannel, willMessage); } } else { await SendWillMsg(willMessage); } await RemotePublishMessage(deviceId, willMessage); }
public async Task WriteWillMsg(MqttChannel mqttChannel, MqttWillMessage willMeaasge) { switch (willMeaasge.Qos) { case 0: await SendQos0Msg(mqttChannel.Channel, willMeaasge.Topic, Encoding.Default.GetBytes(willMeaasge.WillMessage)); break; case 1: // qos1 await SendQosConfirmMsg(QualityOfService.AtLeastOnce, mqttChannel, willMeaasge.Topic, Encoding.Default.GetBytes(willMeaasge.WillMessage)); break; case 2: // qos2 await SendQosConfirmMsg(QualityOfService.ExactlyOnce, mqttChannel, willMeaasge.Topic, Encoding.Default.GetBytes(willMeaasge.WillMessage)); break; } }
public override async Task SendWillMsg(MqttWillMessage willMeaasge) { Topics.TryGetValue(willMeaasge.Topic, out IEnumerable <MqttChannel> mqttChannels); if (mqttChannels != null && mqttChannels.Any()) { foreach (var mqttChannel in mqttChannels) { if (!mqttChannel.IsOnine()) { _clientSessionService.SaveMessage(mqttChannel.ClientId, new SessionMessage { Message = Encoding.UTF8.GetBytes(willMeaasge.WillMessage), QoS = willMeaasge.Qos, Topic = willMeaasge.Topic }); } else { await _messagePushService.WriteWillMsg(mqttChannel, willMeaasge); } } } }
public override async Task Publish(string deviceId, MqttWillMessage willMessage) { if (!string.IsNullOrEmpty(deviceId)) { var mqttChannel = GetMqttChannel(deviceId); if (mqttChannel != null && mqttChannel.IsOnine()) { await _messagePushService.WriteWillMsg(mqttChannel, willMessage); } } else { await SendWillMsg(willMessage); } if (willMessage.WillRetain) { SaveRetain(willMessage.Topic, new RetainMessage { ByteBuf = Encoding.UTF8.GetBytes(willMessage.WillMessage), QoS = willMessage.Qos }, willMessage.Qos == 0 ? true : false); } }
public async Task Publish(string deviceId, MqttWillMessage willMessage) { await ServiceLocator.GetService <IChannelService>().Publish(deviceId, willMessage); }
public async Task RemotePublish(string deviceId, MqttWillMessage willMessage) { await GetService <IChannelService>().RemotePublishMessage(deviceId, willMessage); }
/// <summary> /// Publishes the specified device identifier. /// </summary> /// <param name="deviceId">The device identifier.</param> /// <param name="willMessage">The will message.</param> /// <returns>Task.</returns> public abstract Task Publish(string deviceId, MqttWillMessage willMessage);
/// <summary> /// Sends the will MSG. /// </summary> /// <param name="willMeaasge">The will meaasge.</param> /// <returns>Task.</returns> public abstract Task SendWillMsg(MqttWillMessage willMeaasge);
public void Add(string deviceid, MqttWillMessage willMessage) { willMeaasges.AddOrUpdate(deviceid, willMessage, (id, message) => willMessage); }
private async Task Init(IChannel channel, ConnectMessage mqttConnectMessage) { String deviceId = await GetDeviceId(channel); MqttChannel mqttChannel = new MqttChannel() { Channel = channel, CleanSession = mqttConnectMessage.CleanSession, ClientId = mqttConnectMessage.ClientId, SessionStatus = SessionStatus.OPEN, IsWill = mqttConnectMessage.HasWill, SubscribeStatus = SubscribeStatus.No, Messages = new ConcurrentDictionary <int, SendMqttMessage>(), Topics = new List <string>() }; if (Connect(deviceId, mqttChannel)) { if (mqttConnectMessage.HasWill) { if (mqttConnectMessage.WillMessage == null || string.IsNullOrEmpty(mqttConnectMessage.WillTopic)) { if (_logger.IsEnabled(LogLevel.Error)) { _logger.LogError($"WillMessage 和 WillTopic不能为空"); } return; } var willMessage = new MqttWillMessage { Qos = mqttConnectMessage.Qos, WillRetain = mqttConnectMessage.WillRetain, Topic = mqttConnectMessage.WillTopic, WillMessage = Encoding.UTF8.GetString(mqttConnectMessage.WillMessage) }; _willService.Add(mqttConnectMessage.ClientId, willMessage); } else { _willService.Remove(mqttConnectMessage.ClientId); if (!mqttConnectMessage.WillRetain && mqttConnectMessage.WillQualityOfService != 0) { if (_logger.IsEnabled(LogLevel.Error)) { _logger.LogError($"WillRetain 设置为false,WillQos必须设置为AtMostOnce"); } return; } } await channel.WriteAndFlushAsync(new ConnAckPacket { ReturnCode = ConnectReturnCode.Accepted, SessionPresent = !mqttConnectMessage.CleanSession }); var sessionMessages = _clientSessionService.GetMessages(mqttConnectMessage.ClientId); if (sessionMessages != null && !sessionMessages.IsEmpty) { for (; sessionMessages.TryDequeue(out SessionMessage sessionMessage) && sessionMessage != null;) { switch (sessionMessage.QoS) { case 0: await _messagePushService.SendQos0Msg(channel, sessionMessage.Topic, sessionMessage.Message); break; case 1: await _messagePushService.SendQosConfirmMsg(QualityOfService.AtLeastOnce, GetMqttChannel(deviceId), sessionMessage.Topic, sessionMessage.Message); break; case 2: await _messagePushService.SendQosConfirmMsg(QualityOfService.ExactlyOnce, GetMqttChannel(deviceId), sessionMessage.Topic, sessionMessage.Message); break; } } } } }