public MqttIotHubAdapter( string deviceId, string iotHubHostName, string password, MqttTransportSettings mqttTransportSettings, IWillMessage willMessage, Action onConnected, Action <Message> onMessageReceived, Action <Exception> onError) { Contract.Requires(deviceId != null); Contract.Requires(iotHubHostName != null); Contract.Requires(password != null); Contract.Requires(mqttTransportSettings != null); Contract.Requires(!mqttTransportSettings.HasWill || willMessage != null); this.deviceId = deviceId; this.iotHubHostName = iotHubHostName; this.password = password; this.mqttTransportSettings = mqttTransportSettings; this.willMessage = willMessage; this.onConnected = onConnected; this.onError = onError; this.onMessageReceived = onMessageReceived; this.pingRequestInterval = this.mqttTransportSettings.KeepAliveInSeconds > 0 ? TimeSpan.FromSeconds(this.mqttTransportSettings.KeepAliveInSeconds / 2d) : TimeSpan.MaxValue; this.deviceBoundOneWayProcessor = new SimpleWorkQueue <PublishPacket>(this.AcceptMessageAsync); this.deviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue <int, PublishPacket>(this.AcceptMessageAsync, p => p.PacketId, this.SendAckAsync); this.serviceBoundOneWayProcessor = new SimpleWorkQueue <PublishWorkItem>(this.SendMessageToServerAsync); this.serviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue <int, PublishWorkItem>(this.SendMessageToServerAsync, p => p.Value.PacketId, this.ProcessAckAsync); }
public MqttIotHubAdapter( string deviceId, string iotHubHostName, IAuthorizationProvider passwordProvider, MqttTransportSettings mqttTransportSettings, IWillMessage willMessage, IMqttIotHubEventHandler mqttIotHubEventHandler, ProductInfo productInfo) { Contract.Requires(deviceId != null); Contract.Requires(iotHubHostName != null); Contract.Requires(passwordProvider != null); Contract.Requires(mqttTransportSettings != null); Contract.Requires(!mqttTransportSettings.HasWill || willMessage != null); Contract.Requires(productInfo != null); this.deviceId = deviceId; this.iotHubHostName = iotHubHostName; this.passwordProvider = passwordProvider; this.mqttTransportSettings = mqttTransportSettings; this.willMessage = willMessage; this.mqttIotHubEventHandler = mqttIotHubEventHandler; this.pingRequestInterval = this.mqttTransportSettings.KeepAliveInSeconds > 0 ? TimeSpan.FromSeconds(this.mqttTransportSettings.KeepAliveInSeconds / 4d) : TimeSpan.MaxValue; this.productInfo = productInfo; this.deviceBoundOneWayProcessor = new SimpleWorkQueue <PublishPacket>(this.AcceptMessageAsync); this.deviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue <int, PublishPacket>(this.AcceptMessageAsync, p => p.PacketId, this.SendAckAsync); this.serviceBoundOneWayProcessor = new SimpleWorkQueue <PublishWorkItem>(this.SendMessageToServerAsync); this.serviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue <int, PublishWorkItem>(this.SendMessageToServerAsync, p => p.Value.PacketId, this.ProcessAckAsync); }
public MqttIotHubAdapter( string deviceId, string iotHubHostName, string password, MqttTransportSettings mqttTransportSettings, IWillMessage willMessage, Action onConnected, Action<Message> onMessageReceived, Action<Exception> onError) { Contract.Requires(deviceId != null); Contract.Requires(iotHubHostName != null); Contract.Requires(password != null); Contract.Requires(mqttTransportSettings != null); Contract.Requires(!mqttTransportSettings.HasWill || willMessage != null); this.deviceId = deviceId; this.iotHubHostName = iotHubHostName; this.password = password; this.mqttTransportSettings = mqttTransportSettings; this.willMessage = willMessage; this.onConnected = onConnected; this.onError = onError; this.onMessageReceived = onMessageReceived; this.pingRequestInterval = this.mqttTransportSettings.KeepAliveInSeconds > 0 ? TimeSpan.FromSeconds(this.mqttTransportSettings.KeepAliveInSeconds / 2d) : TimeSpan.MaxValue; this.deviceBoundOneWayProcessor = new SimpleWorkQueue<PublishPacket>(this.AcceptMessageAsync); this.deviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue<int, PublishPacket>(this.AcceptMessageAsync, p => p.PacketId, this.SendAckAsync); this.serviceBoundOneWayProcessor = new SimpleWorkQueue<PublishWorkItem>(this.SendMessageToServerAsync); this.serviceBoundTwoWayProcessor = new OrderedTwoPhaseWorkQueue<int, PublishWorkItem>(this.SendMessageToServerAsync, p => p.Value.PacketId, this.ProcessAckAsync); }
public MqttIotHubAdapter Create( IMqttIotHubEventHandler mqttIotHubEventHandler, IotHubConnectionString iotHubConnectionString, MqttTransportSettings mqttTransportSettings) { IWillMessage willMessage = mqttTransportSettings.HasWill ? this.settings.WillMessage : null; return(new MqttIotHubAdapter( iotHubConnectionString.DeviceId, iotHubConnectionString.HostName, mqttTransportSettings.ClientCertificate != null ? null : iotHubConnectionString.GetPassword(), mqttTransportSettings, willMessage, mqttIotHubEventHandler)); }
CreateObservableMQTTClient( IClientOptions options, IWillMessage willMessage = null, params ITopicFilter[] topicFilters) { ClientOptions = options; WillMessage = willMessage; _wrappedClient = new MQTTClient(this, topicFilters); var observable = Observable.Create <IMQTTMessage>( obs => { var disposableConnect = _wrappedClient.ObservableConnect .Subscribe(_ => { }, obs.OnError, obs.OnCompleted); var disposableMessage = _wrappedClient.ObservableMessage .Subscribe( obs.OnNext, obs.OnError, obs.OnCompleted); var disposableDisconnect = _wrappedClient.ObservableDisconnect .Where(disconnect => disconnect == true) .Select(x => Observable.FromAsync(() => _wrappedClient.DisconnectAsync()).Timeout(TimeSpan.FromSeconds(5))) .Concat() .Subscribe(d => { Debug.WriteLine("Disconnected"); obs.OnCompleted(); }, obs.OnError, obs.OnCompleted); return(new CompositeDisposable( disposableMessage, disposableConnect, disposableDisconnect)); }) .FinallyAsync(async() => { await _wrappedClient?.DisconnectAsync(); }) .Publish().RefCount(); return(observable, _wrappedClient); }
private static MqttApplicationMessage WrapWillMessage(IWillMessage message) { if (message != null) { var applicationMessage = new MqttApplicationMessageBuilder(); applicationMessage .WithTopic(message.Topic) .WithPayload(message.Payload) .WithRetainFlag(message.Retain); ConvertToQualityOfServiceLevel(applicationMessage, message.QualityOfServiceLevel); return(applicationMessage.Build()); } return(null); }
public MqttIotHubAdapter Create( Action onConnected, Action <Message> onMessageReceived, Action <Exception> onError, IotHubConnectionString iotHubConnectionString, MqttTransportSettings mqttTransportSettings) { IWillMessage willMessage = mqttTransportSettings.HasWill ? this.settings.WillMessage : null; return(new MqttIotHubAdapter( iotHubConnectionString.DeviceId, iotHubConnectionString.HostName, iotHubConnectionString.GetPassword(), mqttTransportSettings, willMessage, onConnected, onMessageReceived, onError)); }
public MqttIotHubAdapter Create( IMqttIotHubEventHandler mqttIotHubEventHandler, IotHubConnectionString iotHubConnectionString, MqttTransportSettings mqttTransportSettings, ProductInfo productInfo, ClientOptions options) { IWillMessage willMessage = mqttTransportSettings.HasWill ? _settings.WillMessage : null; return(new MqttIotHubAdapter( iotHubConnectionString.DeviceId, iotHubConnectionString.ModuleId, iotHubConnectionString.HostName, mqttTransportSettings.ClientCertificate != null ? null : iotHubConnectionString, mqttTransportSettings, willMessage, mqttIotHubEventHandler, productInfo, options)); }
private static IMqttClientOptions UnwrapOptions(IClientOptions wrappedOptions, IWillMessage willMessage) { var optionsBuilder = new MqttClientOptionsBuilder(); if (wrappedOptions.ConnectionType == ConnectionType.Tcp) { optionsBuilder.WithTcpServer(wrappedOptions.Uri.Host, wrappedOptions.Uri.Port); } else { optionsBuilder.WithWebSocketServer(wrappedOptions.Uri.AbsoluteUri); } if (wrappedOptions.UseTls) { optionsBuilder .WithTls(new MqttClientOptionsBuilderTlsParameters { AllowUntrustedCertificates = wrappedOptions.AllowUntrustedCertificates, Certificates = UnwrapCertificates(wrappedOptions.Certificates), IgnoreCertificateChainErrors = wrappedOptions.IgnoreCertificateChainErrors, UseTls = wrappedOptions.UseTls }); } return(optionsBuilder .WithWillMessage(WrapWillMessage(willMessage)) .WithCleanSession(wrappedOptions.CleanSession) .WithClientId(wrappedOptions.ClientId ?? Guid.NewGuid().ToString().Replace("-", string.Empty)) .WithProtocolVersion(UnwrapProtocolVersion(wrappedOptions.ProtocolVersion)) .WithCommunicationTimeout(wrappedOptions.DefaultCommunicationTimeout == default ? TimeSpan.FromSeconds(10) : wrappedOptions.DefaultCommunicationTimeout) .WithKeepAlivePeriod(wrappedOptions.KeepAlivePeriod == default ? TimeSpan.FromSeconds(5) : wrappedOptions.KeepAlivePeriod) .WithCredentials(wrappedOptions.UserName, wrappedOptions.Password) .Build()); }
CreateObservableMQTTService( IClientOptions options, IEnumerable <ITopicFilter> topicFilters = null, IWillMessage willMessage = null) { IsConnected = false; var services = new ServiceCollection() .AddMqttClient() .AddLogging() .BuildServiceProvider(); _client = services.GetRequiredService <IMqttClient>(); _wrappedClient = new MQTTClient(_client, this); IsConnected = false; var observable = Observable.Create <IMQTTMessage>( async obs => { var disposableConnect = Observable.FromEventPattern <MqttClientConnectedEventArgs>( h => _client.Connected += h, h => _client.Connected -= h) .Subscribe( async connectEvent => { Debug.WriteLine("Connected"); if (topicFilters?.Any() ?? false) { try { await _wrappedClient.SubscribeAsync(topicFilters); } catch (Exception ex) { obs.OnError(ex); } } }, obs.OnError, obs.OnCompleted); var disposableMessage = Observable.FromEventPattern <MqttApplicationMessageReceivedEventArgs>( h => _client.ApplicationMessageReceived += h, h => _client.ApplicationMessageReceived -= h) .Subscribe( msgEvent => { var message = new MQTTMessage { Payload = msgEvent.EventArgs.ApplicationMessage.Payload, Retain = msgEvent.EventArgs.ApplicationMessage.Retain, QualityOfServiceLevel = ConvertToQoSLevel(msgEvent.EventArgs.ApplicationMessage.QualityOfServiceLevel), Topic = msgEvent.EventArgs.ApplicationMessage.Topic }; obs.OnNext(message); }, obs.OnError, obs.OnCompleted); var disposableDisconnect = Observable.FromEventPattern <MqttClientDisconnectedEventArgs>( h => _client.Disconnected += h, h => _client.Disconnected -= h) .Subscribe( disconnectEvent => { if (!IsConnected) { return; } Debug.WriteLine("Disconnected"); obs.OnCompleted(); }, obs.OnError, obs.OnCompleted); if (!IsConnected) { try { var opt = UnwrapOptions(options, willMessage); await _client.ConnectAsync(opt); IsConnected = true; } catch (Exception ex) { IsConnected = false; obs.OnError(ex); } } return(new CompositeDisposable( Disposable.Create(async() => { await CleanUp(_client); }), disposableMessage, disposableConnect, disposableDisconnect)); }) .FinallyAsync(async() => { await CleanUp(_client); }) .Publish().RefCount(); return(observable, _wrappedClient); }