예제 #1
0
        public Subscription(Model.Subscription item)
        {
            SubscriptionType = item.SubscriptionType.ToString();

            if (item.ObjectDefinitionID != null && item.PropertyDefinitionID != null)
            {
                Model.ObjectDefinition objectDefinition = DataAccessFactory.ObjectDefinitions.GetLookups().GetObjectDefinition(item.ObjectDefinitionID);
                if (objectDefinition != null)
                {
                    Model.PropertyDefinition propertyDefinition = objectDefinition.GetProperty(item.PropertyDefinitionID);
                    if (propertyDefinition != null)
                    {
                        Property = propertyDefinition.SerialisationName;
                    }
                }
            }
            Url = item.Url;
            AcceptContentType = item.AcceptContentType;

            if (item.NotificationParameters != null)
            {
                Attributes             = new SubscriptionAttributes();
                Attributes.Pmin        = item.NotificationParameters.MinimumPeriod;
                Attributes.Pmax        = item.NotificationParameters.MaximumPeriod;
                Attributes.Step        = item.NotificationParameters.Step;
                Attributes.LessThan    = item.NotificationParameters.LessThan;
                Attributes.GreaterThan = item.NotificationParameters.GreaterThan;
            }
        }
예제 #2
0
 private void Deserialise(XmlReader reader)
 {
     while (reader.Read())
     {
         if (reader.NodeType == XmlNodeType.Element)
         {
             if (_Resource == null)
             {
                 if (string.Compare(ObjectDefinition.SerialisationName, reader.Name, true) == 0)
                 {
                     _Resource = new Model.Object();
                     _Resource.ObjectDefinitionID = ObjectDefinition.ObjectDefinitionID;
                     _Resource.ObjectID           = ObjectDefinition.ObjectID;
                 }
             }
             else
             {
                 if (reader.Name.Equals("Links"))
                 {
                     Links = new List <Link>();
                     Links.Deserialise(reader);
                 }
                 else
                 {
                     Model.PropertyDefinition propertyDefinition = ObjectDefinition.GetPropertyBySerialisationName(reader.Name);
                     if (propertyDefinition == null)
                     {
                         if (string.Compare(reader.Name, "InstanceID", true) == 0)
                         {
                             reader.Read();
                             _Resource.InstanceID = reader.Value;
                         }
                     }
                     else
                     {
                         Model.Property property = new Model.Property();
                         property.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                         property.PropertyID           = propertyDefinition.PropertyID;
                         if (propertyDefinition.IsCollection)
                         {
                             XmlReader collectionReader = reader.ReadSubtree();
                             DeserialiseItems(propertyDefinition, property, collectionReader);
                         }
                         else
                         {
                             property.Value       = new Model.PropertyValue();
                             property.Value.Value = GetValue(propertyDefinition.DataType, reader.ReadInnerXml());
                         }
                         _Resource.Properties.Add(property);
                     }
                 }
             }
         }
         else if (reader.NodeType == XmlNodeType.EndElement && string.Compare(ObjectDefinition.SerialisationName, reader.Name, true) == 0)
         {
             break;
         }
     }
 }
예제 #3
0
        public IActionResult UpdateObjectInstance(string clientID, string definitionID, string instanceID)
        {
            IActionResult result;

            Guid definitionIDGuid, clientIDGuid;

            if (StringUtils.GuidTryDecode(definitionID, out definitionIDGuid) && StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                Model.Client           client         = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                int                    organisationID = User.GetOrganisationID();
                Model.ObjectDefinition definition     = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, definitionIDGuid);
                if (definition != null)
                {
                    // TODO: add error handling around deserialisation.
                    List <Model.Property> executeProperties = new List <Model.Property>();
                    Model.Object          lwm2mObject       = new ServiceModels.ObjectInstance(definition, Request).Resource;
                    lwm2mObject.InstanceID = instanceID;
                    int index = 0;
                    while (index < lwm2mObject.Properties.Count)
                    {
                        Model.PropertyDefinition propertyDefinition = definition.GetProperty(lwm2mObject.Properties[index].PropertyDefinitionID);
                        if (propertyDefinition.Access == Model.TAccessRight.Execute)
                        {
                            executeProperties.Add(lwm2mObject.Properties[index]);
                            lwm2mObject.Properties.RemoveAt(index);
                        }
                        else
                        {
                            index++;
                        }
                    }
                    if (lwm2mObject.Properties.Count > 0)
                    {
                        BusinessLogicFactory.Clients.SaveObject(client, lwm2mObject, Model.TObjectState.Update);
                    }
                    if (executeProperties.Count > 0)
                    {
                        BusinessLogicFactory.Clients.Execute(client, lwm2mObject, executeProperties);
                    }
                    result = new NoContentResult();
                }
                else
                {
                    result = new BadRequestResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
예제 #4
0
 public PropertyDefinition(Model.PropertyDefinition property)
 {
     PropertyDefinitionID = StringUtils.GuidEncode(property.PropertyDefinitionID);
     PropertyID           = property.PropertyID;
     Name              = property.Name;
     DataType          = property.DataType.ToString();
     DataTypeLength    = property.DataTypeLength;
     MIMEType          = property.MIMEType;
     MinValue          = property.MinValue;
     MaxValue          = property.MaxValue;
     Units             = property.Units;
     IsCollection      = property.IsCollection;
     IsMandatory       = property.IsMandatory;
     Access            = property.Access.ToString();
     SortOrder         = property.SortOrder;
     SerialisationName = property.SerialisationName;
     CollectionItemSerialisationName = property.CollectionItemSerialisationName;
 }
예제 #5
0
 private void DeserialiseItems(Model.PropertyDefinition propertyDefinition, Model.Property property, XmlReader reader)
 {
     while (reader.Read())
     {
         if (reader.NodeType == XmlNodeType.Element)
         {
             if (string.Compare(propertyDefinition.CollectionItemSerialisationName, reader.Name, true) == 0)
             {
                 Model.PropertyValue propertyValue = new Model.PropertyValue();
                 propertyValue.Value = reader.ReadInnerXml();
                 if (property.Values == null)
                 {
                     property.Values = new List <Model.PropertyValue>();
                 }
                 property.Values.Add(propertyValue);
             }
         }
     }
 }
예제 #6
0
 public Model.PropertyDefinition ToModel()
 {
     Model.PropertyDefinition result = new Model.PropertyDefinition();
     if (!string.IsNullOrEmpty(PropertyDefinitionID))
     {
         result.PropertyDefinitionID = StringUtils.GuidDecode(PropertyDefinitionID);
     }
     result.PropertyID  = PropertyID;
     result.Name        = Name;
     result.Description = Description;
     Model.TPropertyDataType dataType;
     if (Enum.TryParse(DataType, true, out dataType))
     {
         result.DataType = dataType;
     }
     result.DataTypeLength = DataTypeLength;
     result.MIMEType       = MIMEType;
     result.MinValue       = MinValue;
     result.MaxValue       = MaxValue;
     result.Units          = Units;
     if (IsCollection.HasValue)
     {
         result.IsCollection = IsCollection.Value;
     }
     if (IsMandatory.HasValue)
     {
         result.IsMandatory = IsMandatory.Value;
     }
     Model.TAccessRight access;
     if (Enum.TryParse(Access, true, out access))
     {
         result.Access = access;
     }
     result.SortOrder         = SortOrder;
     result.SerialisationName = SerialisationName;
     result.CollectionItemSerialisationName = CollectionItemSerialisationName;
     return(result);
 }
예제 #7
0
 public void UpdateModel(Model.PropertyDefinition item)
 {
     if (!string.IsNullOrEmpty(PropertyID))
     {
         item.PropertyID = PropertyID;
     }
     if (!string.IsNullOrEmpty(Name))
     {
         item.Name = Name;
     }
     if (!string.IsNullOrEmpty(DataType))
     {
         Model.TPropertyDataType dataType;
         if (Enum.TryParse(DataType, true, out dataType))
         {
             item.DataType = dataType;
         }
     }
     if (DataTypeLength.HasValue)
     {
         item.DataTypeLength = DataTypeLength;
     }
     if (!string.IsNullOrEmpty(MIMEType))
     {
         item.MIMEType = MIMEType;
     }
     if (!string.IsNullOrEmpty(MinValue))
     {
         item.MinValue = MinValue;
     }
     if (!string.IsNullOrEmpty(MaxValue))
     {
         item.MaxValue = MaxValue;
     }
     if (!string.IsNullOrEmpty(Units))
     {
         item.Units = Units;
     }
     if (IsCollection.HasValue)
     {
         item.IsCollection = IsCollection.Value;
     }
     if (IsMandatory.HasValue)
     {
         item.IsMandatory = IsMandatory.Value;
     }
     if (!string.IsNullOrEmpty(Access))
     {
         Model.TAccessRight access;
         if (Enum.TryParse(Access, true, out access))
         {
             item.Access = access;
         }
     }
     if (SortOrder.HasValue)
     {
         item.SortOrder = SortOrder;
     }
     if (!string.IsNullOrEmpty(SerialisationName))
     {
         item.SerialisationName = SerialisationName;
     }
     if (!string.IsNullOrEmpty(CollectionItemSerialisationName))
     {
         item.CollectionItemSerialisationName = CollectionItemSerialisationName;
     }
 }
예제 #8
0
        private void Deserialise(JsonReader reader)
        {
            Model.PropertyDefinition propertyDefinition = null;
            Model.Property           property           = null;
            bool isID = false;

            while (reader.Read())
            {
                switch (reader.State)
                {
                case TJsonReaderState.NotSet:
                    break;

                case TJsonReaderState.Array:
                    break;

                case TJsonReaderState.BOF:
                    break;

                case TJsonReaderState.Boolean:
                    if (propertyDefinition != null)
                    {
                        Model.PropertyValue propertyValue = new Model.PropertyValue(reader.AsBoolean.ToString());
                        if (propertyDefinition.IsCollection)
                        {
                            if (property.Values == null)
                            {
                                property.Values = new List <Model.PropertyValue>();
                            }
                            property.Values.Add(propertyValue);
                        }
                        else
                        {
                            property.Value = new Model.PropertyValue(propertyValue.Value);
                        }
                    }
                    break;

                case TJsonReaderState.EndArray:
                    propertyDefinition = null;
                    break;

                case TJsonReaderState.EndObject:
                    break;

                case TJsonReaderState.EOF:
                    break;

                case TJsonReaderState.Member:
                    if (reader.Text.Equals("Links"))
                    {
                        Links = new List <Link>();
                        Links.Deserialise(reader);
                    }
                    else
                    {
                        propertyDefinition = ObjectDefinition.GetPropertyBySerialisationName(reader.Text);
                        if (propertyDefinition == null)
                        {
                            isID = string.Compare(reader.Text, "InstanceID", true) == 0;
                        }
                        else
                        {
                            isID     = false;
                            property = new Model.Property();
                            property.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                            property.PropertyID           = propertyDefinition.PropertyID;
                            if (_Resource != null)
                            {
                                _Resource.Properties.Add(property);
                            }
                        }
                    }
                    break;

                case TJsonReaderState.Null:
                    break;

                case TJsonReaderState.Number:
                    if (propertyDefinition != null)
                    {
                        Model.PropertyValue propertyValue = new Model.PropertyValue(reader.Text);
                        if (propertyDefinition.IsCollection)
                        {
                            if (property.Values == null)
                            {
                                property.Values = new List <Model.PropertyValue>();
                            }
                            property.Values.Add(propertyValue);
                        }
                        else
                        {
                            property.Value = new Model.PropertyValue(propertyValue.Value);
                        }
                    }
                    break;

                case TJsonReaderState.Object:
                    if (_Resource == null)
                    {
                        _Resource = new Model.Object();
                        _Resource.ObjectDefinitionID = ObjectDefinition.ObjectDefinitionID;
                        _Resource.ObjectID           = ObjectDefinition.ObjectID;
                    }
                    break;

                case TJsonReaderState.String:
                    if (propertyDefinition == null)
                    {
                        if (isID)
                        {
                            _Resource.InstanceID = reader.Text;
                        }
                    }
                    else
                    {
                        string text;
                        if (propertyDefinition.DataType == Model.TPropertyDataType.DateTime)
                        {
                            text = GetDateTimeString(reader.Text);
                        }
                        else
                        {
                            text = reader.Text;
                        }
                        Model.PropertyValue propertyValue = new Model.PropertyValue(text);
                        if (propertyDefinition.IsCollection)
                        {
                            if (property.Values == null)
                            {
                                property.Values = new List <Model.PropertyValue>();
                            }
                            property.Values.Add(propertyValue);
                        }
                        else
                        {
                            property.Value = new Model.PropertyValue(propertyValue.Value);
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }
예제 #9
0
 public Model.PropertyDefinition ToModel()
 {
     Model.PropertyDefinition result = new Model.PropertyDefinition();
     if (!string.IsNullOrEmpty(PropertyDefinitionID))
         result.PropertyDefinitionID = StringUtils.GuidDecode(PropertyDefinitionID);
     result.PropertyID = PropertyID;
     result.Name = Name;
     result.Description = Description;
     Model.TPropertyDataType dataType;
     if (Enum.TryParse(DataType, true, out dataType))
         result.DataType = dataType;
     result.DataTypeLength = DataTypeLength;
     result.MIMEType = MIMEType;
     result.MinValue = MinValue;
     result.MaxValue = MaxValue;
     result.Units = Units;
     if (IsCollection.HasValue)
         result.IsCollection = IsCollection.Value;
     if (IsMandatory.HasValue)
         result.IsMandatory = IsMandatory.Value;
     Model.TAccessRight access;
     if (Enum.TryParse(Access, true, out access))
         result.Access = access;
     result.SortOrder = SortOrder;
     result.SerialisationName = SerialisationName;
     result.CollectionItemSerialisationName = CollectionItemSerialisationName;
     return result;
 }
예제 #10
0
        public Model.Subscription ToModel(HttpRequest request, string clientID, string definitionID, string instanceID)
        {
            Model.Subscription result = new Model.Subscription();
            TSubscriptionType  subscriptionType;

            if (Enum.TryParse <TSubscriptionType>(SubscriptionType, true, out subscriptionType))
            {
                result.SubscriptionType = subscriptionType;
            }
            else
            {
                throw new BadRequestException();
            }

            if (AcceptContentType == null)
            {
                result.AcceptContentType = request.ContentType.Contains("xml") ? "application/xml" : "application/json";
            }
            else
            {
                result.AcceptContentType = AcceptContentType;
            }

            if (clientID != null)
            {
                result.ClientID = StringUtils.GuidDecode(clientID);
            }
            if (definitionID != null)
            {
                result.ObjectDefinitionID = StringUtils.GuidDecode(definitionID);
            }
            if (instanceID != null)
            {
                result.ObjectID = instanceID;
            }

            if (Attributes != null)
            {
                result.NotificationParameters = new NotificationParameters();
                result.NotificationParameters.MinimumPeriod = Attributes.Pmin;
                result.NotificationParameters.MaximumPeriod = Attributes.Pmax;
                result.NotificationParameters.Step          = Attributes.Step;
                result.NotificationParameters.LessThan      = Attributes.LessThan;
                result.NotificationParameters.GreaterThan   = Attributes.GreaterThan;
            }

            if (Url != null)
            {
                result.Url = Url;
            }
            else
            {
                throw new BadRequestException();
            }

            if (Links != null)
            {
                foreach (Link link in Links)
                {
                    if (link.rel.Equals("object"))
                    {
                        string[] fields = link.href.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                        for (int i = 0; i < fields.Length - 1; i++)
                        {
                            if (fields[i].Equals("clients") && result.ClientID == Guid.Empty)
                            {
                                result.ClientID = StringUtils.GuidDecode(fields[i + 1]);
                            }
                            else if (fields[i].Equals("objecttypes") && result.ObjectDefinitionID == Guid.Empty)
                            {
                                result.ObjectDefinitionID = StringUtils.GuidDecode(fields[i + 1]);
                            }
                            else if (fields[i].Equals("instances") && result.ObjectID == null)
                            {
                                result.ObjectID = fields[i + 1];
                                break;
                            }
                        }
                    }
                }
            }

            if (result.SubscriptionType == TSubscriptionType.Observation)
            {
                if (result.ClientID == null || result.ObjectDefinitionID == null)
                {
                    throw new BadRequestException();
                }

                if (Property != null)
                {
                    Model.PropertyDefinition propertyDefinition = DataAccessFactory.ObjectDefinitions.GetLookups().GetPropertyDefinitionFromNameOrID(result.ObjectDefinitionID, Property);
                    if (propertyDefinition != null)
                    {
                        result.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID;
                    }
                    else
                    {
                        throw new BadRequestException();
                    }
                }
            }

            return(result);
        }