/// <summary> /// Decodes the DataSetMessageHeader /// </summary> private void DecodeDataSetMessageHeader(JsonDecoder jsonDecoder) { object token = null; if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.DataSetWriterId) != 0) { if (jsonDecoder.ReadField(nameof(DataSetWriterId), out token)) { DataSetWriterId = Convert.ToUInt16(jsonDecoder.ReadString(nameof(DataSetWriterId))); } } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.SequenceNumber) != 0) { if (jsonDecoder.ReadField(nameof(SequenceNumber), out token)) { SequenceNumber = jsonDecoder.ReadUInt32(nameof(SequenceNumber)); } } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.MetaDataVersion) != 0) { if (jsonDecoder.ReadField(nameof(MetaDataVersion), out token)) { MetaDataVersion = jsonDecoder.ReadEncodeable(nameof(MetaDataVersion), typeof(ConfigurationVersionDataType)) as ConfigurationVersionDataType; } } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.Timestamp) != 0) { if (jsonDecoder.ReadField(nameof(Timestamp), out token)) { Timestamp = jsonDecoder.ReadDateTime(nameof(Timestamp)); } } if ((DataSetMessageContentMask & JsonDataSetMessageContentMask.Status) != 0) { if (jsonDecoder.ReadField(nameof(Status), out token)) { Status = jsonDecoder.ReadStatusCode(nameof(Status)); } } }
/// <summary> /// Decode a scalar type /// </summary> private object DecodeRawScalar(JsonDecoder jsonDecoder, byte builtInType, string fieldName) { try { switch ((BuiltInType)builtInType) { case BuiltInType.Boolean: return(jsonDecoder.ReadBoolean(fieldName)); case BuiltInType.SByte: return(jsonDecoder.ReadSByte(fieldName)); case BuiltInType.Byte: return(jsonDecoder.ReadByte(fieldName)); case BuiltInType.Int16: return(jsonDecoder.ReadInt16(fieldName)); case BuiltInType.UInt16: return(jsonDecoder.ReadUInt16(fieldName)); case BuiltInType.Int32: return(jsonDecoder.ReadInt32(fieldName)); case BuiltInType.UInt32: return(jsonDecoder.ReadUInt32(fieldName)); case BuiltInType.Int64: return(jsonDecoder.ReadInt64(fieldName)); case BuiltInType.UInt64: return(jsonDecoder.ReadUInt64(fieldName)); case BuiltInType.Float: return(jsonDecoder.ReadFloat(fieldName)); case BuiltInType.Double: return(jsonDecoder.ReadDouble(fieldName)); case BuiltInType.String: return(jsonDecoder.ReadString(fieldName)); case BuiltInType.DateTime: return(jsonDecoder.ReadDateTime(fieldName)); case BuiltInType.Guid: return(jsonDecoder.ReadGuid(fieldName)); case BuiltInType.ByteString: return(jsonDecoder.ReadByteString(fieldName)); case BuiltInType.XmlElement: return(jsonDecoder.ReadXmlElement(fieldName)); case BuiltInType.NodeId: return(jsonDecoder.ReadNodeId(fieldName)); case BuiltInType.ExpandedNodeId: return(jsonDecoder.ReadExpandedNodeId(fieldName)); case BuiltInType.QualifiedName: return(jsonDecoder.ReadQualifiedName(fieldName)); case BuiltInType.LocalizedText: return(jsonDecoder.ReadLocalizedText(fieldName)); case BuiltInType.DataValue: return(jsonDecoder.ReadDataValue(fieldName)); case BuiltInType.Enumeration: return(jsonDecoder.ReadInt32(fieldName)); case BuiltInType.Variant: return(jsonDecoder.ReadVariant(fieldName)); case BuiltInType.ExtensionObject: return(jsonDecoder.ReadExtensionObject(fieldName)); case BuiltInType.DiagnosticInfo: return(jsonDecoder.ReadDiagnosticInfo(fieldName)); case BuiltInType.StatusCode: return(jsonDecoder.ReadStatusCode(fieldName)); } } catch (Exception ex) { Utils.Trace(ex, "JsonDataSetMessage - Error decoding field {0}", fieldName); } return(null); }
/// <summary> /// Decode the Content of the Payload and create a DataSet object from it /// </summary> private DataSet DecodePayloadContent(JsonDecoder jsonDecoder, DataSetReaderDataType dataSetReader) { TargetVariablesDataType targetVariablesData = ExtensionObject.ToEncodeable(dataSetReader.SubscribedDataSet) as TargetVariablesDataType; DataSetMetaDataType dataSetMetaData = dataSetReader.DataSetMetaData; object token; List <DataValue> dataValues = new List <DataValue>(); for (int index = 0; index < dataSetMetaData?.Fields.Count; index++) { FieldMetaData fieldMetaData = dataSetMetaData?.Fields[index]; if (jsonDecoder.ReadField(fieldMetaData.Name, out token)) { switch (m_fieldTypeEncoding) { case FieldTypeEncodingMask.Variant: Variant variantValue = jsonDecoder.ReadVariant(fieldMetaData.Name); dataValues.Add(new DataValue(variantValue)); break; case FieldTypeEncodingMask.RawData: object value = DecodeRawData(jsonDecoder, dataSetMetaData?.Fields[index], dataSetMetaData?.Fields[index].Name); dataValues.Add(new DataValue(new Variant(value))); break; case FieldTypeEncodingMask.DataValue: bool wasPush2 = jsonDecoder.PushStructure(fieldMetaData.Name); DataValue dataValue = new DataValue(Variant.Null); try { if (wasPush2 && jsonDecoder.ReadField("Value", out token)) { // the Value was encoded using the non reversible json encoding token = DecodeRawData(jsonDecoder, dataSetMetaData?.Fields[index], "Value"); dataValue = new DataValue(new Variant(token)); } else { // handle Good StatusCode that was not encoded if (dataSetMetaData?.Fields[index].BuiltInType == (byte)BuiltInType.StatusCode) { dataValue = new DataValue(new Variant(new StatusCode(StatusCodes.Good))); } } if ((FieldContentMask & DataSetFieldContentMask.StatusCode) != 0) { if (jsonDecoder.ReadField("StatusCode", out token)) { bool wasPush3 = jsonDecoder.PushStructure("StatusCode"); if (wasPush3) { dataValue.StatusCode = jsonDecoder.ReadStatusCode("Code"); jsonDecoder.Pop(); } } } if ((FieldContentMask & DataSetFieldContentMask.SourceTimestamp) != 0) { dataValue.SourceTimestamp = jsonDecoder.ReadDateTime("SourceTimestamp"); } if ((FieldContentMask & DataSetFieldContentMask.SourcePicoSeconds) != 0) { dataValue.SourcePicoseconds = jsonDecoder.ReadUInt16("SourcePicoseconds"); } if ((FieldContentMask & DataSetFieldContentMask.ServerTimestamp) != 0) { dataValue.ServerTimestamp = jsonDecoder.ReadDateTime("ServerTimestamp"); } if ((FieldContentMask & DataSetFieldContentMask.ServerPicoSeconds) != 0) { dataValue.ServerPicoseconds = jsonDecoder.ReadUInt16("ServerPicoseconds"); } dataValues.Add(dataValue); } finally { if (wasPush2) { jsonDecoder.Pop(); } } break; } } else { switch (m_fieldTypeEncoding) { case FieldTypeEncodingMask.Variant: case FieldTypeEncodingMask.RawData: // handle StatusCodes.Good which is not encoded and therefore must be created at decode if (dataSetMetaData?.Fields[index].BuiltInType == (byte)BuiltInType.StatusCode) { dataValues.Add(new DataValue(new Variant(new StatusCode(StatusCodes.Good)))); } else { // the field is null dataValues.Add(new DataValue(Variant.Null)); } break; } } } if (dataValues.Count != dataSetMetaData?.Fields.Count) { return(null); } //build the DataSet Fields collection based on the decoded values and the target List <Field> dataFields = new List <Field>(); for (int i = 0; i < dataValues.Count; i++) { Field dataField = new Field(); dataField.FieldMetaData = dataSetMetaData?.Fields[i]; dataField.Value = dataValues[i]; if (targetVariablesData != null && targetVariablesData.TargetVariables != null && i < targetVariablesData.TargetVariables.Count) { // remember the target Attribute and target nodeId dataField.TargetAttribute = targetVariablesData.TargetVariables[i].AttributeId; dataField.TargetNodeId = targetVariablesData.TargetVariables[i].TargetNodeId; } dataFields.Add(dataField); } // build the dataset object DataSet dataSet = new DataSet(dataSetMetaData?.Name); dataSet.Fields = dataFields.ToArray(); dataSet.DataSetWriterId = DataSetWriterId; dataSet.SequenceNumber = SequenceNumber; return(dataSet); }