Example #1
0
 public Model.ObjectDefinition ToModel()
 {
     Model.ObjectDefinition result = new Model.ObjectDefinition();
     if (!string.IsNullOrEmpty(ObjectDefinitionID))
     {
         result.ObjectDefinitionID = StringUtils.GuidDecode(ObjectDefinitionID);
     }
     result.ObjectID          = ObjectID;
     result.Name              = Name;
     result.Description       = Description;
     result.MIMEType          = MIMEType;
     result.SerialisationName = SerialisationName;
     result.Singleton         = Singleton;
     if (Properties != null)
     {
         foreach (PropertyDefinition item in Properties)
         {
             if (result.Properties == null)
             {
                 result.Properties = new List <Model.PropertyDefinition>();
             }
             result.Properties.Add(item.ToModel());
         }
     }
     return(result);
 }
        public IActionResult RemoveObjectDefinition(string id)
        {
            IActionResult result;
            Guid          objectDefinitionID;

            if (StringUtils.GuidTryDecode(id, out objectDefinitionID))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition objectDefinition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, objectDefinitionID);
                if (objectDefinition == null)
                {
                    result = new NotFoundResult();
                }
                else
                {
                    if (!objectDefinition.OrganisationID.HasValue && (organisationID != 0))
                    {
                        result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
                    }
                    else
                    {
                        BusinessLogicFactory.ObjectDefinitions.SaveObjectDefinition(objectDefinition, Model.TObjectState.Delete);
                        result = new NoContentResult();
                    }
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
        public IActionResult UpdateObjectDefinition(string id, [FromBody] ServiceModels.ObjectDefinition objectDefinition)
        {
            IActionResult result;
            Guid          objectDefinitionID;

            if (StringUtils.GuidTryDecode(id, out objectDefinitionID))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition existingObjectDefinition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, objectDefinitionID);
                if (existingObjectDefinition == null)
                {
                    result = new NotFoundResult();
                }
                else
                {
                    if (!existingObjectDefinition.OrganisationID.HasValue && (organisationID != 0))
                    {
                        result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
                    }
                    else
                    {
                        Model.ObjectDefinition updatedObjectDefinition = objectDefinition.ToModel();
                        updatedObjectDefinition.OrganisationID = User.GetOrganisationID();
                        BusinessLogicFactory.ObjectDefinitions.SaveObjectDefinition(updatedObjectDefinition, Model.TObjectState.Update);
                        result = new NoContentResult();
                    }
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
        private IActionResult AddObjectDefinition(ServiceModels.ObjectDefinition objectDefinition)
        {
            IActionResult result;

            if (objectDefinition == null)
            {
                result = new BadRequestResult();
            }
            else
            {
                ResourceCreated        response = new ResourceCreated();
                string                 rootUrl  = Request.GetRootUrl();
                Model.ObjectDefinition item     = objectDefinition.ToModel();
                item.OrganisationID = User.GetOrganisationID();
                try
                {
                    BusinessLogicFactory.ObjectDefinitions.SaveObjectDefinition(item, Model.TObjectState.Add);
                    response.ID = StringUtils.GuidEncode(item.ObjectDefinitionID);
                    response.AddSelfLink(string.Concat(rootUrl, "/objecttypes/definitions/", response.ID), false, false);
                    result = Request.GetObjectResult(response, System.Net.HttpStatusCode.Created);
                }
                catch (ConflictException)
                {
                    result = new StatusCodeResult((int)HttpStatusCode.Conflict);
                }
            }
            return(result);
        }
        public IActionResult GetObjectDefinition(string id)
        {
            IActionResult result;
            Guid          objectDefinitionID;

            if (StringUtils.GuidTryDecode(id, out objectDefinitionID))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition objectDefinition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, objectDefinitionID);
                if (objectDefinition == null)
                {
                    result = new NotFoundResult();
                }
                else
                {
                    ServiceModels.ObjectDefinition response = new ServiceModels.ObjectDefinition(objectDefinition);
                    string rootUrl = Request.GetRootUrl();
                    if (organisationID == 0)
                    {
                        response.AddSelfLink(string.Concat(rootUrl, "/objecttypes/definitions/", response.ObjectDefinitionID), true, true);
                    }
                    else
                    {
                        response.AddSelfLink(string.Concat(rootUrl, "/objecttypes/definitions/", response.ObjectDefinitionID), objectDefinition.OrganisationID.HasValue, objectDefinition.OrganisationID.HasValue);
                    }
                    result = Request.GetObjectResult(response);
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
Example #6
0
        public IActionResult GetObjectType(string clientID, string definitionID)
        {
            IActionResult result = new NotFoundResult();
            Guid          clientIDGuid;
            Guid          definitionIDGuid;

            if (StringUtils.GuidTryDecode(clientID, out clientIDGuid) && StringUtils.GuidTryDecode(definitionID, out definitionIDGuid))
            {
                Model.Client client = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                if ((client != null) && (client.SupportedTypes != null))
                {
                    Model.ObjectDefinition definition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(User.GetOrganisationID(), definitionIDGuid);
                    if (definition != null)
                    {
                        Model.ObjectType objectType = client.SupportedTypes.GetObjectType(int.Parse(definition.ObjectID));
                        if (objectType != null)
                        {
                            ServiceModels.ObjectType response = new ServiceModels.ObjectType(objectType);
                            string rootUrl = Request.GetRootUrl();
                            response.AddSelfLink(string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", definitionID), false, false);
                            response.AddLink <ObjectDefinition>(Request, "definition", string.Concat(rootUrl, "/objecttypes/definitions/", definitionID));
                            response.AddLink("instances", string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", definitionID, "/instances"), Request.GetContentType(definition.MIMEType));
                            result = Request.GetObjectResult(response);
                        }
                    }
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
Example #7
0
        public IActionResult AddObjectInstance(string clientID, string definitionID)
        {
            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.
                    // TODO: could lwm2mObject.instanceID be an optional parameter, allowing a web client to specify?
                    Model.Object lwm2mObject = new ServiceModels.ObjectInstance(definition, Request).Resource;
                    BusinessLogicFactory.Clients.SaveObject(client, lwm2mObject, Model.TObjectState.Add);

                    ServiceModels.ResourceCreated response = new ServiceModels.ResourceCreated();
                    response.ID = lwm2mObject.InstanceID;
                    string rootUrl = Request.GetRootUrl();
                    response.AddSelfLink(string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", definitionID, "/instances/", response.ID), true, true);
                    result = Request.GetObjectResult(response, System.Net.HttpStatusCode.Created);
                }
                else
                {
                    result = new BadRequestResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
Example #8
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;
            }
        }
Example #9
0
        public IActionResult GetObjectInstances(string clientID, string definitionID)
        {
            IActionResult result;

            Guid definitionIDGuid, clientIDGuid;

            if (StringUtils.GuidTryDecode(definitionID, out definitionIDGuid) && StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition definition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, definitionIDGuid);
                if (definition != null)
                {
                    Model.Client client = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                    if (client != null)
                    {
                        List <Model.Object> instances = BusinessLogicFactory.Clients.GetObjects(client, definition.ObjectDefinitionID);
                        if (instances != null)
                        {
                            ObjectInstances response     = new ObjectInstances(definition);
                            string          rootUrl      = Request.GetRootUrl();
                            string          instancesUrl = string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", StringUtils.GuidEncode(definition.ObjectDefinitionID), "/instances");

                            response.AddLink("add", instancesUrl, "");

                            response.PageInfo = Request.GetPageInfo(instances.Count);
                            int endIndex = response.PageInfo.StartIndex + response.PageInfo.ItemsCount;
                            for (int index = response.PageInfo.StartIndex; index < endIndex; index++)
                            {
                                ObjectInstance instance    = new ObjectInstance(definition, instances[index]);
                                string         instanceUrl = string.Concat(instancesUrl, "/", instances[index].InstanceID);
                                AddObjectInstanceLinks(Request, definition, instance, instanceUrl);
                                response.Add(instance);
                            }
                            result = response.GetAction();
                        }
                        else
                        {
                            result = new NotFoundResult();
                        }
                    }
                    else
                    {
                        result = new NotFoundResult();
                    }
                }
                else
                {
                    result = new NotFoundResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
Example #10
0
        public void UpdateModel(Model.ObjectDefinition item)
        {
            if (!string.IsNullOrEmpty(ObjectID))
            {
                item.ObjectID = ObjectID;
            }
            if (!string.IsNullOrEmpty(Name))
            {
                item.Name = Name;
            }
            if (!string.IsNullOrEmpty(MIMEType))
            {
                item.MIMEType = MIMEType;
            }
            if (!string.IsNullOrEmpty(Description))
            {
                item.Description = Description;
            }
            if (!string.IsNullOrEmpty(SerialisationName))
            {
                item.SerialisationName = SerialisationName;
            }
            item.Singleton = Singleton;
            if (Properties != null)
            {
                foreach (PropertyDefinition property in Properties)
                {
                    if (item.Properties == null)
                    {
                        item.Properties = new List <Model.PropertyDefinition>();
                    }
                    bool found = false;

                    Guid propertyMetadataID;
                    if (StringUtils.GuidTryDecode(property.PropertyDefinitionID, out propertyMetadataID))
                    {
                        foreach (Model.PropertyDefinition existingProperty in item.Properties)
                        {
                            if (propertyMetadataID == existingProperty.PropertyDefinitionID)
                            {
                                property.UpdateModel(existingProperty);
                                found = true;
                                break;
                            }
                        }
                    }
                    if (!found)
                    {
                        item.Properties.Add(property.ToModel());
                    }
                }
            }
        }
Example #11
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);
        }
Example #12
0
        public IActionResult GetObjectTypes(string clientID)
        {
            IActionResult result;
            Guid          clientIDGuid;

            if (StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                Model.Client client = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                if (client != null)
                {
                    ServiceModels.ObjectTypes response = new ServiceModels.ObjectTypes();
                    string rootUrl = Request.GetRootUrl();

                    if (client.SupportedTypes != null)
                    {
                        Model.ObjectDefinitionLookups definitions = BusinessLogicFactory.ObjectDefinitions.GetLookups();

                        response.PageInfo = Request.GetPageInfo(client.SupportedTypes.Count);
                        int endIndex = response.PageInfo.StartIndex + response.PageInfo.ItemsCount;
                        for (int index = response.PageInfo.StartIndex; index < endIndex; index++)
                        {
                            ServiceModels.ObjectType objectType = new ServiceModels.ObjectType(client.SupportedTypes[index]);
                            if (definitions != null)
                            {
                                Model.ObjectDefinition definition = definitions.GetObjectDefinition(User.GetOrganisationID(), objectType.ObjectTypeID);
                                if (definition != null)
                                {
                                    objectType.AddSelfLink(string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", StringUtils.GuidEncode(definition.ObjectDefinitionID)), false, false);
                                    objectType.AddLink <ObjectDefinition>(Request, "definition", string.Concat(rootUrl, "/objecttypes/definitions/", StringUtils.GuidEncode(definition.ObjectDefinitionID)));
                                    objectType.AddLink("instances", string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", StringUtils.GuidEncode(definition.ObjectDefinitionID), "/instances"), Request.GetContentType(definition.MIMEType));
                                }
                            }
                            response.Add(objectType);
                        }
                    }
                    result = Request.GetObjectResult(response);
                }
                else
                {
                    result = new NotFoundResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }

            return(result);
        }
Example #13
0
        public IActionResult GetObjectInstance(string clientID, string definitionID, string instanceID)
        {
            IActionResult result;

            Guid definitionIDGuid, clientIDGuid;

            if (StringUtils.GuidTryDecode(definitionID, out definitionIDGuid) && StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition definition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, definitionIDGuid);
                if (definition != null)
                {
                    Model.Client client = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                    if (client != null)
                    {
                        Model.Object instance = BusinessLogicFactory.Clients.GetObject(client, definition.ObjectDefinitionID, instanceID);
                        if (instance != null)
                        {
                            ServiceModels.ObjectInstance response = new ServiceModels.ObjectInstance(definition, instance);
                            string rootUrl     = Request.GetRootUrl();
                            string instanceUrl = string.Concat(rootUrl, "/clients/", clientID, "/objecttypes/", StringUtils.GuidEncode(definition.ObjectDefinitionID), "/instances/", instanceID);

                            AddObjectInstanceLinks(Request, definition, response, instanceUrl);
                            result = response.GetAction();
                        }
                        else
                        {
                            result = new NotFoundResult();
                        }
                    }
                    else
                    {
                        result = new NotFoundResult();
                    }
                }
                else
                {
                    result = new NotFoundResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
Example #14
0
 public ObjectInstances(Model.ObjectDefinition objectDefinition, Stream content, string contentType) : this(objectDefinition)
 {
     if (contentType != null && contentType.Contains("xml"))
     {
         using (XmlReader reader = XmlReader.Create(content))
         {
             Deserialise(reader);
         }
     }
     else
     {
         using (JsonReader reader = new JsonReader(content))
         {
             Deserialise(reader);
         }
     }
 }
Example #15
0
        public ObjectInstance(Model.ObjectDefinition objectDefinition, HttpRequest request)
        {
            ObjectDefinition = objectDefinition;

            if (request.ContentType.Contains("xml"))
            {
                using (XmlReader reader = XmlReader.Create(request.Body))
                {
                    Deserialise(reader);
                }
            }
            else
            {
                using (JsonReader reader = new JsonReader(request.Body))
                {
                    Deserialise(reader);
                }
            }
        }
Example #16
0
        public IActionResult RemoveObjectInstance(string clientID, string definitionID, string instanceID)
        {
            IActionResult result;

            Guid definitionIDGuid, clientIDGuid;

            if (StringUtils.GuidTryDecode(definitionID, out definitionIDGuid) && StringUtils.GuidTryDecode(clientID, out clientIDGuid))
            {
                int organisationID = User.GetOrganisationID();
                Model.ObjectDefinition definition = BusinessLogicFactory.ObjectDefinitions.GetObjectDefinition(organisationID, definitionIDGuid);
                if (definition != null)
                {
                    Model.Client client = BusinessLogicFactory.Clients.GetClient(clientIDGuid);
                    if (client != null)
                    {
                        Model.Object instance = BusinessLogicFactory.Clients.GetObject(client, definition.ObjectDefinitionID, instanceID);
                        if (instance != null)
                        {
                            BusinessLogicFactory.Clients.SaveObject(client, instance, Model.TObjectState.Delete);
                            result = new NoContentResult();
                        }
                        else
                        {
                            result = new NotFoundResult();
                        }
                    }
                    else
                    {
                        result = new NotFoundResult();
                    }
                }
                else
                {
                    result = new NotFoundResult();
                }
            }
            else
            {
                result = new BadRequestResult();
            }
            return(result);
        }
        public ObjectDefinition(Model.ObjectDefinition objectMetadata)
        {
            ObjectDefinitionID = StringUtils.GuidEncode(objectMetadata.ObjectDefinitionID);
            ObjectID           = objectMetadata.ObjectID;
            Name              = objectMetadata.Name;
            MIMEType          = objectMetadata.MIMEType;
            SerialisationName = objectMetadata.SerialisationName;
            Singleton         = objectMetadata.Singleton;
            if (objectMetadata.Properties != null)
            {
                foreach (Model.PropertyDefinition item in objectMetadata.Properties)
                {
                    if (Properties == null)
                    {
                        Properties = new List <PropertyDefinition>();
                    }

                    Properties.Add(new PropertyDefinition(item));
                }
            }
        }
Example #18
0
 public ObjectInstance(Model.ObjectDefinition objectDefinition, JsonReader reader)
 {
     ObjectDefinition = objectDefinition;
     Deserialise(reader);
 }
Example #19
0
        private void AddObjectInstanceLinks(Microsoft.AspNetCore.Http.HttpRequest request, Model.ObjectDefinition objectDefinition, ObjectInstance instance, string instanceUrl)
        {
            instance.AddSelfLink(instanceUrl, true, true);
            instance.AddLink <ServiceModels.Subscriptions>(request, "subscriptions", string.Concat(instanceUrl, "/subscriptions"));

            string rootUrl = request.GetRootUrl();

            instance.AddLink <ObjectDefinition>(Request, "definition", string.Concat(rootUrl, "/objecttypes/definitions/", StringUtils.GuidEncode(objectDefinition.ObjectDefinitionID)));
        }
 public Model.ObjectDefinition ToModel()
 {
     Model.ObjectDefinition result = new Model.ObjectDefinition();
     if (!string.IsNullOrEmpty(ObjectDefinitionID))
         result.ObjectDefinitionID = StringUtils.GuidDecode(ObjectDefinitionID);
     result.ObjectID = ObjectID;
     result.Name = Name;
     result.Description = Description;
     result.MIMEType = MIMEType;
     result.SerialisationName = SerialisationName;
     result.Singleton = Singleton;
     if (Properties != null)
     {
         foreach (PropertyDefinition item in Properties)
         {
             if (result.Properties == null)
                 result.Properties = new List<Model.PropertyDefinition>();
             result.Properties.Add(item.ToModel());
         }
     }
     return result;
 }
Example #21
0
 public ObjectInstance(Model.ObjectDefinition objectDefinition, Model.Object resource)
 {
     ObjectDefinition = objectDefinition;
     _Resource        = resource;
 }
Example #22
0
 public ObjectInstances(Model.ObjectDefinition objectDefinition)
 {
     _ObjectDefinition = objectDefinition;
 }