Пример #1
0
        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);
            }
        }
        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);
        }
 public DeviceInfoEntity(DeviceInfo deviceInfo)
 {
     this.PartitionKey = deviceInfo.DeviceId;
     this.RowKey = deviceInfo.DeviceId;
     this.Status = deviceInfo.Status;
     if (deviceInfo.Metadata != null)
     {
         this.Building = deviceInfo.Metadata.Building;
         this.Room = deviceInfo.Metadata.Room;
     }
 }
        public async Task RestoresDeviceIfRevoked()
        {
            string deviceId = "1";

            DeviceInfo info = new DeviceInfo
            {
                DeviceId = deviceId,
                Status = DeviceStateConstants.RevokedState 
            };

            _registry.Setup(x => x.FindAsync(deviceId))
                .ReturnsAsync(info);

            IHttpActionResult actionResult = await _controller.ProvisionDevice(deviceId);

            _tokenProvider.Verify(x => x.RestoreDeviceAsync(deviceId));
        }
Пример #5
0
        public async Task ReturnsDeviceInfo()
        {
            string deviceId = "1";

            DeviceInfo info = new DeviceInfo { 
                DeviceId = deviceId,
                Status = DeviceStateConstants.RegisteredState
            };

            _registry.Setup(x => x.FindAsync(deviceId))
                .ReturnsAsync(info);

            IHttpActionResult actionResult = await _controller.RevokeDevice(deviceId);

            var contentResult = actionResult as OkNegotiatedContentResult<DeviceInfo>;

            Assert.NotNull(contentResult);
            Assert.NotNull(contentResult.Content);
        }
        public async Task ReturnsToken()
        {
            string deviceId = "1";

            DeviceInfo info = new DeviceInfo { 
                DeviceId = deviceId,
                Status = DeviceStateConstants.RegisteredState
            };

            _registry.Setup(x => x.FindAsync(deviceId))
                .ReturnsAsync(info);

            IHttpActionResult actionResult = await _controller.ProvisionDevice(deviceId);

            var contentResult = actionResult as OkNegotiatedContentResult<DeviceEndpoint>;

            Assert.NotNull(contentResult);
            Assert.NotNull(contentResult.Content);
            Assert.Equal(TOKEN, contentResult.Content.AccessToken);
        }
 public Task<bool> AddOrUpdate(DeviceInfo info)
 {
     var entity = new DeviceInfoEntity(info);
     _table.Execute(TableOperation.InsertOrReplace(entity));
     return Task.FromResult(true);
 }