コード例 #1
0
    public void TestConvertVersionBPayloadToProto()
    {
        var payload = PayloadConverter.ConvertVersionBPayload(new VersionB.Data.Payload());

        Assert.IsNotNull(payload);
    }
コード例 #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;

            // 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));
            }
        });
    }
コード例 #3
0
    public void TestConvertVersionBPayloadFromProto()
    {
        var payload = PayloadConverter.ConvertVersionBPayload(new VersionB.ProtoBuf.ProtoBufPayload());

        Assert.IsNotNull(payload);
    }