Ejemplo n.º 1
0
        public void TestFieldValueEscapedVariant(string fieldname, string foo, string expected)
        {
            TestContext.Out.WriteLine("Expected:");
            _ = PrettifyAndValidateJson(expected);

            using (var encodeable = new FooBarEncodeable(fieldname, foo))
            {
                var variant = new Variant(new ExtensionObject(encodeable));
                // non reversible to save some space
                var encoder = new JsonEncoder(Context, false);
                encoder.WriteVariant(encodeable.FieldName, variant);

                var encoded = encoder.CloseAndReturnText();
                TestContext.Out.WriteLine("Encoded:");
                TestContext.Out.WriteLine(encoded);

                TestContext.Out.WriteLine("Formatted Encoded:");
                _ = PrettifyAndValidateJson(encoded);

                Assert.That(encoded, Is.EqualTo(expected));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Encodes a dataSet field
        /// </summary>
        private void EncodeField(JsonEncoder encoder, Field field)
        {
            string fieldName = field.FieldMetaData.Name;

            Variant valueToEncode = field.Value.WrappedValue;

            // The StatusCode.Good value is not encoded cor3ectly then it shall be ommited
            if (valueToEncode == StatusCodes.Good && m_fieldTypeEncoding != FieldTypeEncodingMask.Variant)
            {
                valueToEncode = Variant.Null;
            }
            switch (m_fieldTypeEncoding)
            {
            case FieldTypeEncodingMask.Variant:
                // If the DataSetFieldContentMask results in a Variant representation,
                // the field value is encoded as a Variant encoded using the reversible OPC UA JSON Data Encoding
                // defined in OPC 10000-6.
                encoder.ForceNamespaceUri = false;
                encoder.WriteVariant(fieldName, valueToEncode, true);
                break;

            case FieldTypeEncodingMask.RawData:
                // If the DataSetFieldContentMask results in a RawData representation,
                // the field value is a Variant encoded using the non-reversible OPC UA JSON Data Encoding
                // defined in OPC 10000-6
                encoder.ForceNamespaceUri = true;

                encoder.WriteVariant(fieldName, valueToEncode, false);
                break;

            case FieldTypeEncodingMask.DataValue:
                DataValue dataValue = new DataValue();

                dataValue.WrappedValue = valueToEncode;

                if ((FieldContentMask & DataSetFieldContentMask.StatusCode) != 0)
                {
                    dataValue.StatusCode = field.Value.StatusCode;
                }

                if ((FieldContentMask & DataSetFieldContentMask.SourceTimestamp) != 0)
                {
                    dataValue.SourceTimestamp = field.Value.SourceTimestamp;
                }

                if ((FieldContentMask & DataSetFieldContentMask.SourcePicoSeconds) != 0)
                {
                    dataValue.SourcePicoseconds = field.Value.SourcePicoseconds;
                }

                if ((FieldContentMask & DataSetFieldContentMask.ServerTimestamp) != 0)
                {
                    dataValue.ServerTimestamp = field.Value.ServerTimestamp;
                }

                if ((FieldContentMask & DataSetFieldContentMask.ServerPicoSeconds) != 0)
                {
                    dataValue.ServerPicoseconds = field.Value.ServerPicoseconds;
                }

                // If the DataSetFieldContentMask results in a DataValue representation,
                // the field value is a DataValue encoded using the non-reversible OPC UA JSON Data Encoding
                encoder.ForceNamespaceUri = true;
                encoder.WriteDataValue(fieldName, dataValue, false);
                break;
            }
        }