示例#1
0
        /// <summary>
        /// Adds the message received handler to handle incoming messages.
        /// </summary>
        private void AddMessageReceivedHandler()
        {
            this.Client.UseApplicationMessageReceivedHandler(
                e =>
            {
                var topic = e.ApplicationMessage.Topic;

                if (topic.Contains(SparkplugMessageType.DeviceCommand.GetDescription()))
                {
                    switch (this.NameSpace)
                    {
                    case SparkplugNamespace.VersionA:
                        var payloadVersionA = PayloadHelper.Deserialize <VersionAPayload>(e.ApplicationMessage.Payload);

                        if (payloadVersionA != null)
                        {
                            this.VersionAPayloadReceived?.Invoke(payloadVersionA);
                        }

                        break;

                    case SparkplugNamespace.VersionB:
                        var payloadVersionB = PayloadHelper.Deserialize <VersionBPayload>(e.ApplicationMessage.Payload);

                        if (payloadVersionB != null)
                        {
                            this.VersionBPayloadReceived?.Invoke(payloadVersionB);
                        }

                        break;
                    }
                }
            });
        }
示例#2
0
        /// <summary>
        /// Adds the message received handler to handle incoming messages.
        /// </summary>
        /// <exception cref="InvalidCastException">The metric cast is invalid.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The namespace is out of range.</exception>
        private void AddMessageReceivedHandler()
        {
            this.Client.UseApplicationMessageReceivedHandler(
                e =>
            {
                var topic = e.ApplicationMessage.Topic;

                switch (this.NameSpace)
                {
                case SparkplugNamespace.VersionA:
                    if (topic.Contains(SparkplugMessageType.DeviceCommand.GetDescription()))
                    {
                        var payloadVersionA = PayloadHelper.Deserialize <VersionAPayload>(e.ApplicationMessage.Payload);

                        if (payloadVersionA != null)
                        {
                            if (!(payloadVersionA is T convertedPayloadVersionA))
                            {
                                throw new InvalidCastException("The metric cast didn't work properly.");
                            }

                            this.DeviceCommandReceived?.Invoke(convertedPayloadVersionA);
                        }
                    }

                    break;

                case SparkplugNamespace.VersionB:

                    if (topic.Contains(SparkplugMessageType.DeviceCommand.GetDescription()))
                    {
                        var payloadVersionB = PayloadHelper.Deserialize <VersionBPayload>(e.ApplicationMessage.Payload);

                        if (payloadVersionB != null)
                        {
                            if (!(payloadVersionB is T convertedPayloadVersionB))
                            {
                                throw new InvalidCastException("The metric cast didn't work properly.");
                            }

                            this.DeviceCommandReceived?.Invoke(convertedPayloadVersionB);
                        }
                    }

                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(this.NameSpace));
                }
            });
        }
    /// <summary>
    /// Adds the message received handler to handle incoming messages.
    /// </summary>
    /// <exception cref="InvalidCastException">The metric cast is invalid.</exception>
    /// <exception cref="ArgumentOutOfRangeException">The namespace is out of range.</exception>
    private void AddMessageReceivedHandler()
    {
        this.Client.UseApplicationMessageReceivedHandler(
            e =>
        {
            var topic = e.ApplicationMessage.Topic;

            // Handle the STATE message before anything else as they're UTF-8 encoded.
            if (topic.Contains(SparkplugMessageType.StateMessage.GetDescription()))
            {
                this.StatusMessageReceived?.Invoke(Encoding.UTF8.GetString(e.ApplicationMessage.Payload));
                return;
            }

            switch (this.NameSpace)
            {
            case SparkplugNamespace.VersionA:
                var payloadVersionA = PayloadHelper.Deserialize <VersionAProtoBuf.ProtoBufPayload>(e.ApplicationMessage.Payload);

                if (payloadVersionA is not null)
                {
                    var convertedPayload = PayloadConverter.ConvertVersionAPayload(payloadVersionA);

                    if (topic.Contains(SparkplugMessageType.DeviceCommand.GetDescription()))
                    {
                        if (convertedPayload is not T convertedPayloadVersionA)
                        {
                            throw new InvalidCastException("The metric cast didn't work properly.");
                        }

                        this.DeviceCommandReceived?.Invoke(convertedPayloadVersionA);
                    }

                    if (topic.Contains(SparkplugMessageType.NodeCommand.GetDescription()))
                    {
                        if (convertedPayload is not T convertedPayloadVersionA)
                        {
                            throw new InvalidCastException("The metric cast didn't work properly.");
                        }

                        this.NodeCommandReceived?.Invoke(convertedPayloadVersionA);
                    }
                }

                break;

            case SparkplugNamespace.VersionB:
                var payloadVersionB = PayloadHelper.Deserialize <VersionBProtoBuf.ProtoBufPayload>(e.ApplicationMessage.Payload);

                if (payloadVersionB is not null)
                {
                    var convertedPayload = PayloadConverter.ConvertVersionBPayload(payloadVersionB);

                    if (topic.Contains(SparkplugMessageType.DeviceCommand.GetDescription()))
                    {
                        if (convertedPayload is not T convertedPayloadVersionB)
                        {
                            throw new InvalidCastException("The metric cast didn't work properly.");
                        }

                        this.DeviceCommandReceived?.Invoke(convertedPayloadVersionB);
                    }

                    if (topic.Contains(SparkplugMessageType.NodeCommand.GetDescription()))
                    {
                        if (convertedPayload is not T convertedPayloadVersionB)
                        {
                            throw new InvalidCastException("The metric cast didn't work properly.");
                        }

                        this.NodeCommandReceived?.Invoke(convertedPayloadVersionB);
                    }
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(this.NameSpace));
            }
        });
    }