Exemplo n.º 1
0
 /// <summary>
 /// Decode the jsonDecoder content as a MetaData message
 /// </summary>
 /// <param name="jsonDecoder"></param>
 private void DecodeMetaDataMessage(JsonDecoder jsonDecoder)
 {
     try
     {
         m_metadata = jsonDecoder.ReadEncodeable(kFieldMetaData, typeof(DataSetMetaDataType)) as DataSetMetaDataType;
     }
     catch (Exception ex)
     {
         // Unexpected exception in DecodeMetaDataMessage
         Utils.Trace(ex, "JsonNetworkMessage.DecodeMetaDataMessage");
     }
 }
        /// <inheritdoc/>
        public static DataSetMetadata Decode(ServiceMessageContext context, StreamReader reader)
        {
            var json = reader.ReadToEnd();

            var output = new DataSetMetadata();

            using (var decoder = new JsonDecoder(json, context)) {
                output.MessageId      = decoder.ReadString("MessageId");
                output.MessageType    = decoder.ReadString("MessageType");
                output.PublisherId    = decoder.ReadString("PublisherId");
                output.DataSetClassId = decoder.ReadString("DataSetClassId");
                output.MetaData       = (DataSetMetaDataType)decoder.ReadEncodeable("MetaData", typeof(DataSetMetaDataType));
                decoder.Close();
            }

            return(output);
        }
Exemplo n.º 3
0
        /// <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));
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Decode the stream from decoder parameter and produce a Dataset
        /// </summary>
        /// <param name="jsonDecoder"></param>
        /// <param name="dataSetReaders"></param>
        /// <returns></returns>
        private void DecodeSubscribedDataSets(JsonDecoder jsonDecoder, IList <DataSetReaderDataType> dataSetReaders)
        {
            if (dataSetReaders == null || dataSetReaders.Count == 0)
            {
                return;
            }
            try
            {
                List <DataSetReaderDataType> dataSetReadersFiltered = new List <DataSetReaderDataType>();

                // 1. decode network message header (PublisherId & DataSetClassId)
                DecodeNetworkMessageHeader(jsonDecoder);

                // handle metadata messages.
                if (m_jsonNetworkMessageType == JSONNetworkMessageType.DataSetMetaData)
                {
                    m_metadata = jsonDecoder.ReadEncodeable(kFieldMetaData, typeof(DataSetMetaDataType)) as DataSetMetaDataType;
                    return;
                }

                // ignore network messages that are not dataSet messages
                if (m_jsonNetworkMessageType != JSONNetworkMessageType.DataSetMessage)
                {
                    return;
                }

                //* 6.2.8.1 PublisherId
                // The parameter PublisherId defines the Publisher to receive NetworkMessages from.
                // If the value is null, the parameter shall be ignored and all received NetworkMessages pass the PublisherId filter. */
                foreach (DataSetReaderDataType dataSetReader in dataSetReaders)
                {
                    if (dataSetReader.PublisherId == Variant.Null)
                    {
                        dataSetReadersFiltered.Add(dataSetReader);
                    }
                    // publisher id
                    else if ((NetworkMessageContentMask & JsonNetworkMessageContentMask.PublisherId) != 0 &&
                             PublisherId != null &&
                             PublisherId.Equals(dataSetReader.PublisherId.Value.ToString()))
                    {
                        dataSetReadersFiltered.Add(dataSetReader);
                    }
                }
                if (dataSetReadersFiltered.Count == 0)
                {
                    return;
                }
                dataSetReaders = dataSetReadersFiltered;

                object        messagesToken    = null;
                List <object> messagesList     = null;
                string        messagesListName = string.Empty;
                if (jsonDecoder.ReadField(kFieldMessages, out messagesToken))
                {
                    messagesList = messagesToken as List <object>;
                    if (messagesList == null)
                    {
                        // this is a SingleDataSetMessage encoded as the content of Messages
                        jsonDecoder.PushStructure(kFieldMessages);
                        messagesList = new List <object>();
                    }
                    else
                    {
                        messagesListName = kFieldMessages;
                    }
                }
                else if (jsonDecoder.ReadField(JsonDecoder.RootArrayName, out messagesToken))
                {
                    messagesList     = messagesToken as List <object>;
                    messagesListName = JsonDecoder.RootArrayName;
                }
                else
                {
                    // this is a SingleDataSetMessage encoded as the content json
                    messagesList = new List <object>();
                }
                if (messagesList != null)
                {
                    // atempt decoding for each data set reader
                    foreach (DataSetReaderDataType dataSetReader in dataSetReaders)
                    {
                        JsonDataSetReaderMessageDataType jsonMessageSettings = ExtensionObject.ToEncodeable(dataSetReader.MessageSettings)
                                                                               as JsonDataSetReaderMessageDataType;
                        if (jsonMessageSettings == null)
                        {
                            // The reader MessageSettings is not set up correctly
                            continue;
                        }
                        JsonNetworkMessageContentMask networkMessageContentMask =
                            (JsonNetworkMessageContentMask)jsonMessageSettings.NetworkMessageContentMask;
                        if ((networkMessageContentMask & NetworkMessageContentMask) != NetworkMessageContentMask)
                        {
                            // The reader MessageSettings.NetworkMessageContentMask is not set up correctly
                            continue;
                        }

                        // initialize the dataset message
                        JsonDataSetMessage jsonDataSetMessage = new JsonDataSetMessage();
                        jsonDataSetMessage.DataSetMessageContentMask = (JsonDataSetMessageContentMask)jsonMessageSettings.DataSetMessageContentMask;
                        jsonDataSetMessage.SetFieldContentMask((DataSetFieldContentMask)dataSetReader.DataSetFieldContentMask);
                        // set the flag that indicates if dataset message shall have a header
                        jsonDataSetMessage.HasDataSetMessageHeader = (networkMessageContentMask & JsonNetworkMessageContentMask.DataSetMessageHeader) != 0;

                        jsonDataSetMessage.DecodePossibleDataSetReader(jsonDecoder, messagesList.Count, messagesListName, dataSetReader);
                        if (jsonDataSetMessage.DataSet != null)
                        {
                            m_uaDataSetMessages.Add(jsonDataSetMessage);
                        }
                        else if (jsonDataSetMessage.DecodeErrorReason == DataSetDecodeErrorReason.MetadataMajorVersion)
                        {
                            OnDataSetDecodeErrorOccurred(new DataSetDecodeErrorEventArgs(DataSetDecodeErrorReason.MetadataMajorVersion, this, dataSetReader));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // Unexpected exception in DecodeSubscribedDataSets
                Utils.Trace(ex, "JsonNetworkMessage.DecodeSubscribedDataSets");
            }
        }