コード例 #1
0
        internal MessageData GetNestedData(string fieldName)
        {
            if (values == null)
            {
                ParseValues();
            }

            foreach (var field in format.fields)
            {
                if (field.name == fieldName)
                {
                    object value = values[fieldName];
                    if (field.arraySize > 0)
                    {
                        // cook up a MessageData just for the array.
                        MessageData   array      = new MessageData(this.msgId, null, this.subscription);
                        StringBuilder fieldTypes = new StringBuilder();
                        fieldTypes.Append(fieldName);
                        fieldTypes.Append(':');

                        var    values = new Dictionary <string, object>();
                        bool   first  = true;
                        object ts     = null;
                        // we need to copy the timestamp down so the data renders with time sync.
                        if (this.values.TryGetValue("timestamp", out ts))
                        {
                            fieldTypes.Append("double timestamp");
                            values["timestamp"] = ts;
                            first = false;
                        }

                        Array data = (Array)value;
                        for (int i = 0; i < field.arraySize; i++)
                        {
                            if (!first)
                            {
                                fieldTypes.Append(';');
                            }
                            fieldTypes.Append(MessageField.GetFieldTypeName(field.type));
                            fieldTypes.Append(' ');
                            fieldTypes.Append(i.ToString());
                            values[i.ToString()] = data.GetValue(i);
                            first = false;
                        }
                        array.format = new MessageFormat(fieldTypes.ToString());
                        array.values = values;
                        return(array);
                    }
                    else if (value is MessageData)
                    {
                        return((MessageData)value);
                    }
                    else
                    {
                        throw new Exception(string.Format("Field {0} is not a struct", fieldName));
                    }
                }
            }
            return(null);
        }
コード例 #2
0
        internal MessageData GetNestedData(string fieldName)
        {
            if (values == null)
            {
                ParseValues();
            }

            foreach (var field in format.fields)
            {
                if (field.name == fieldName)
                {
                    object value = values[fieldName];
                    if (field.arraySize > 0)
                    {
                        if (nestedFormats == null)
                        {
                            nestedFormats = new Dictionary <string, MessageFormat>();
                        }

                        MessageFormat format = null;
                        if (!nestedFormats.TryGetValue(fieldName, out format))
                        {
                            List <MessageField> fields = new List <MessageField>();

                            object tv;
                            // we need to copy the timestamp down so the data renders with time sync.
                            if (this.values.TryGetValue("timestamp", out tv))
                            {
                                fields.Add(new MessageField("double timestamp"));
                            }

                            for (int i = 0; i < field.arraySize; i++)
                            {
                                var name = i.ToString();
                                fields.Add(new MessageField(MessageField.GetFieldTypeName(field.type) + " " + name));
                            }

                            format = new MessageFormat(fieldName, fields);
                            nestedFormats[fieldName] = format;
                        }

                        MessageData array = new MessageData(this.msgId, null, this.subscription);
                        array.format = format;
                        var    values = new Dictionary <string, object>();
                        object ts     = null;
                        int    offset = 0;
                        // we need to copy the timestamp down so the data renders with time sync.
                        if (this.values.TryGetValue("timestamp", out ts))
                        {
                            values["timestamp"] = ts;
                            offset++;
                        }

                        Array data = (Array)value;
                        for (int i = 0; i < field.arraySize; i++)
                        {
                            string name = format.fields[i + offset].name;
                            values[name] = data.GetValue(i);
                        }

                        array.values = values;
                        return(array);
                    }
                    else if (value is MessageData)
                    {
                        return((MessageData)value);
                    }
                    else
                    {
                        throw new Exception(string.Format("Field {0} is not a struct", fieldName));
                    }
                }
            }
            return(null);
        }