Пример #1
0
        public static Property ParseProperty(ObjectDefinition objectDefinition, JsonReader reader)
        {
            Property result = new Property();
            string propertyValueID = null;
            while (reader.Read())
            {
                if (reader.State == TJsonReaderState.Member)
                {
                    if (string.Compare(reader.Text, "n") == 0)
                    {
                        if (reader.Read() && !string.IsNullOrEmpty(reader.Text))
                        {
                            string[] fields = reader.Text.Split('/');
                            PropertyDefinition property = objectDefinition.GetProperty(fields[0]);
                            if (property != null)
                            {
                                result.PropertyDefinitionID = property.PropertyDefinitionID;
                                result.PropertyID = property.PropertyID;
                                if (fields.Length > 1)
                                {
                                    propertyValueID = fields[1];
                                    result.Values = new List<PropertyValue>();
                                }
                            }
                        }
                    }
                    else if ((string.Compare(reader.Text, "v") == 0) || (string.Compare(reader.Text, "sv") == 0))
                    {
                        if (reader.Read() && !string.IsNullOrEmpty(reader.Text))
                        {
                            if (string.IsNullOrEmpty(propertyValueID))
                            {
                                result.Value = new PropertyValue(reader.Text);
                            }
                            else
                            {
                                PropertyValue value = new PropertyValue();
                                value.PropertyValueID = propertyValueID;
                                value.Value = reader.Text;
                                result.Values.Add(value);
                            }
                        }
                    }
                    else if (string.Compare(reader.Text, "bv") == 0)
                    {
                        if (reader.Read())
                        {
                            if (string.IsNullOrEmpty(propertyValueID))
                            {
                                result.Value = new PropertyValue(reader.AsBoolean.ToString());
                            }
                            else
                            {
                                PropertyValue value = new PropertyValue();
                                value.PropertyValueID = propertyValueID;
                                value.Value = reader.AsBoolean.ToString();
                                result.Values.Add(value);
                            }

                        }
                    }
                }
                if (reader.State == TJsonReaderState.EndObject)
                    break;
            }
            return result;
        }
Пример #2
0
 private byte[] SerialiseObject(ObjectDefinition objectDefinition, Model.Object item)
 {
     byte[] result = null;
     TlvWriter arrayWriter = null;
     MemoryStream arraystream = null;
     using (MemoryStream steam = new MemoryStream())
     {
         TlvWriter writer = new TlvWriter(steam);
         foreach (Property property in item.Properties)
         {
             PropertyDefinition propertyDefinition = objectDefinition.GetProperty(property.PropertyID);
             if (propertyDefinition != null)
             {
                 if (propertyDefinition.IsCollection)
                 {
                     if (property.Values != null)
                     {
                         ushort identifier;
                         if (ushort.TryParse(propertyDefinition.PropertyID, out identifier))
                         {
                             if (arrayWriter == null)
                             {
                                 arraystream = new MemoryStream();
                                 arrayWriter = new TlvWriter(arraystream);
                             }
                             arraystream.SetLength(0);
                             foreach (PropertyValue propertyValue in property.Values)
                             {
                                 WriteValue(arrayWriter, TTlvTypeIdentifier.ResourceInstance, propertyDefinition.DataType, propertyValue.PropertyValueID, propertyValue.Value);
                             }
                             byte[] arrayItems = arraystream.ToArray();
                             writer.Write(TTlvTypeIdentifier.MultipleResources, identifier, arrayItems);
                         }
                     }
                 }
                 else if (property.Value != null)
                 {
                     WriteValue(writer, TTlvTypeIdentifier.ResourceWithValue, propertyDefinition.DataType, propertyDefinition.PropertyID, property.Value.Value);
                 }
             }
         }
         result = steam.ToArray();
     }
     return result;
 }
Пример #3
0
 public static Model.Object ParseObject(ObjectDefinition objectDefinition, TlvReader reader)
 {
     Model.Object result = null;
     while (reader.Read())
     {
         if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance)
         {
             TlvReader objectReader = new TlvReader(reader.TlvRecord.Value);
             result = ParseObject(objectDefinition, objectReader);
             if (result != null)
             {
                 result.InstanceID = reader.TlvRecord.Identifier.ToString();
             }
             break;
         }
         if ((reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.ObjectInstance) && (reader.TlvRecord.TypeIdentifier != TTlvTypeIdentifier.NotSet))
         {
             if (result == null)
             {
                 result = new Model.Object();
                 result.ObjectID = objectDefinition.ObjectID;
                 result.ObjectDefinitionID = objectDefinition.ObjectDefinitionID;
             }
             if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceWithValue)
             {
                 string propertyID = reader.TlvRecord.Identifier.ToString();
                 PropertyDefinition property = objectDefinition.GetProperty(propertyID);
                 if (property != null)
                 {
                     Property lwm2mProperty = new Property();
                     lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID;
                     lwm2mProperty.PropertyID = property.PropertyID;
                     lwm2mProperty.Value = new PropertyValue(GetValue(reader, property));
                     result.Properties.Add(lwm2mProperty);
                 }
             }
             else if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.MultipleResources)
             {
                 string propertyID = reader.TlvRecord.Identifier.ToString();
                 PropertyDefinition property = objectDefinition.GetProperty(propertyID);
                 if (property != null)
                 {
                     Property lwm2mProperty = new Property();
                     lwm2mProperty.PropertyDefinitionID = property.PropertyDefinitionID;
                     lwm2mProperty.PropertyID = property.PropertyID;
                     result.Properties.Add(lwm2mProperty);
                     TlvReader arrayReader = new TlvReader(reader.TlvRecord.Value);
                     while (arrayReader.Read())
                     {
                         if (arrayReader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ResourceInstance)
                         {
                             string value = GetValue(arrayReader, property);
                             if (value != null)
                             {
                                 if (lwm2mProperty.Values == null)
                                     lwm2mProperty.Values = new List<PropertyValue>();
                                 PropertyValue propertyValue = new PropertyValue();
                                 propertyValue.PropertyValueID = arrayReader.TlvRecord.Identifier.ToString();
                                 propertyValue.Value = value;
                                 lwm2mProperty.Values.Add(propertyValue);
                             }
                         }
                     }
                 }
             }
         }
     }
     return result;
 }