Exemplo n.º 1
0
        /// <summary>
        /// Adds the disconnected handler and the reconnect functionality to the client.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        private void AddDisconnectedHandler(SparkplugDeviceOptions options)
        {
            this.Client.UseDisconnectedHandler(
                async e =>
            {
                // Wait until the disconnect interval is reached
                await Task.Delay(options.ReconnectInterval);

                // Connect, subscribe to incoming messages and send a state message
                await this.ConnectInternal(options);
                await this.SubscribeInternal(options);
                await this.PublishInternal(options);
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the messages used by the the Sparkplug device.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        private void LoadMessages(SparkplugDeviceOptions options)
        {
            this.willMessage = this.MessageGenerator.CreateSparkplugMessage(
                this.NameSpace,
                options.GroupIdentifier,
                SparkplugMessageType.DeviceDeath,
                options.EdgeNodeIdentifier,
                options.DeviceIdentifier);

            this.deviceOnlineMessage = this.MessageGenerator.CreateSparkplugMessage(
                this.NameSpace,
                options.GroupIdentifier,
                SparkplugMessageType.DeviceBirth,
                options.EdgeNodeIdentifier,
                null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Starts the Sparkplug device.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
        public async Task Start(SparkplugDeviceOptions options)
        {
            // Load messages
            this.LoadMessages(options);

            // Add handlers
            this.AddDisconnectedHandler(options);
            this.AddMessageReceivedHandler();

            // Connect, subscribe to incoming messages and send a state message
            await this.ConnectInternal(options);

            await this.SubscribeInternal(options);

            await this.PublishInternal(options);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Connects the Sparkplug device to the MQTT broker.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
        private async Task ConnectInternal(SparkplugDeviceOptions options)
        {
            options.CancellationToken ??= CancellationToken.None;

            var builder = new MqttClientOptionsBuilder()
                          .WithClientId(options.ClientId)
                          .WithCredentials(options.UserName, options.Password)
                          .WithCleanSession(false)
                          .WithProtocolVersion(MqttProtocolVersion.V311);

            if (options.UseTls)
            {
                builder.WithTls();
            }

            if (options.WebSocketParameters is null)
            {
                builder.WithTcpServer(options.BrokerAddress, options.Port);
            }
            else
            {
                builder.WithWebSocketServer(options.BrokerAddress, options.WebSocketParameters);
            }

            if (options.ProxyOptions != null)
            {
                builder.WithProxy(
                    options.ProxyOptions.Address,
                    options.ProxyOptions.Username,
                    options.ProxyOptions.Password,
                    options.ProxyOptions.Domain,
                    options.ProxyOptions.BypassOnLocal);
            }

            if (this.willMessage != null)
            {
                builder.WithWillMessage(this.willMessage);
            }

            this.ClientOptions = builder.Build();

            await this.Client.ConnectAsync(this.ClientOptions, options.CancellationToken.Value);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Subscribes the client to the device subscribe topic.
        /// </summary>
        /// <param name="options">The configuration option.</param>
        /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
        private async Task SubscribeInternal(SparkplugDeviceOptions options)
        {
            var deviceCommandSubscribeTopic = this.TopicGenerator.GetDeviceCommandSubscribeTopic(this.NameSpace, options.GroupIdentifier, options.EdgeNodeIdentifier, options.DeviceIdentifier);

            await this.Client.SubscribeAsync(deviceCommandSubscribeTopic, MqttQualityOfServiceLevel.AtLeastOnce);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Publishes data to the MQTT broker.
 /// </summary>
 /// <param name="options">The configuration option.</param>
 /// <returns>A <see cref="Task"/> representing any asynchronous operation.</returns>
 private async Task PublishInternal(SparkplugDeviceOptions options)
 {
     options.CancellationToken ??= CancellationToken.None;
     await this.Client.PublishAsync(this.deviceOnlineMessage, options.CancellationToken.Value);
 }