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);
        }
Ejemplo n.º 2
0
        public async Task<IHttpActionResult> ProvisionDevice([FromBody] Device device)
        {
            if (device == null)
            {
                return BadRequest("device is null");
            }
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = new DeviceInfo
            {
                DeviceId = device.DeviceId,
                Status = "provisioned"
            };

            await _registry.AddOrUpdate(info);

            var token = await _provisioner.GetTokenAsync(device.DeviceId);
            var endpoint = new DeviceEndpoint
            {
                Uri = _provisioner.EndpointUri.AbsoluteUri,
                EventHubName = _provisioner.EventHubName,
                AccessToken = token
            };

            return Ok(endpoint);
        }