public async Task <IHttpActionResult> ProvisionDevice(string deviceId) { var info = await _registry.FindAsync(deviceId); if (info == null) { return(NotFound()); } // If the device was revoked, restore it. if (info.Status.Equals(DeviceStateConstants.RevokedState, StringComparison.Ordinal)) { await _provisioner.RestoreDeviceAsync(deviceId); } var token = await _provisioner.GetTokenAsync(deviceId); var endpoint = new DeviceEndpoint { Uri = _provisioner.EndpointUri.AbsoluteUri, EventHubName = _provisioner.EventHubName, AccessToken = token }; // Update registry with new provisioning state. info.Status = DeviceStateConstants.ProvisionedState; await _registry.AddOrUpdateAsync(info); return(Ok(endpoint)); }
public async Task <IHttpActionResult> PutDeviceMetadata(string id, [FromBody] DeviceMetadata metadata) { if (metadata == null) { return(BadRequest("metadata is null")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } DeviceInfo info = await _registry.FindAsync(id); if (info == null) { info = new DeviceInfo { DeviceId = id, Status = DeviceStateConstants.RegisteredState, Metadata = metadata }; await _registry.AddOrUpdateAsync(info); return(CreatedAtRoute("DefaultApi", new { id = id }, info)); } else { info.Metadata = metadata; await _registry.AddOrUpdateAsync(info); return(Ok(info)); } }