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); }
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; }
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); }
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); }
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; }
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; }