Example #1
0
        /// <summary>
        /// We only support update twin
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device)
        {
            // validate device module
            var azureDevice = await this.registry.GetDeviceAsync(device.Id);

            if (azureDevice == null)
            {
                azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel());
            }

            Twin azureTwin;

            if (device.Twin == null)
            {
                azureTwin = await this.registry.GetTwinAsync(device.Id);
            }
            else
            {
                azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag);

                // Update the deviceGroupFilter cache, no need to wait
                var unused = this.configService.UpdateDeviceGroupFiltersAsync(device.Twin);
            }

            return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName));
        }
Example #2
0
        public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device)
        {
            if (device.IsEdgeDevice &&
                device.Authentication != null &&
                !device.Authentication.AuthenticationType.Equals(AuthenticationType.Sas))
            {
                throw new InvalidInputException("Edge devices only support symmetric key authentication.");
            }

            // auto generate DeviceId, if missing
            if (string.IsNullOrEmpty(device.Id))
            {
                device.Id = Guid.NewGuid().ToString();
            }

            var azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel());

            Twin azureTwin;

            if (device.Twin == null)
            {
                azureTwin = await this.registry.GetTwinAsync(device.Id);
            }
            else
            {
                azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*");
            }

            return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName));
        }
        public async Task <DeviceServiceModel> CreateWithDesiredAsync(DeviceServiceModel device, string twinPatch)
        {
            // auto generate DeviceId, if missing
            if (string.IsNullOrEmpty(device.Id))
            {
                device.Id = Guid.NewGuid().ToString();
            }

            Device azureDevice = await registry.AddDeviceAsync(device.ToAzureModel());

            Twin azureTwin;

            azureTwin = await registry.UpdateTwinAsync(device.Id, twinPatch, "*");


            return(new DeviceServiceModel(azureDevice, azureTwin, ioTHubHostName));
        }
Example #4
0
        /// <summary>
        /// We only support update twin
        /// </summary>
        /// <param name="device"></param>
        /// <param name="devicePropertyDelegate"></param>
        /// <returns></returns>
        public async Task <DeviceServiceModel> CreateOrUpdateAsync(DeviceServiceModel device, DevicePropertyDelegate devicePropertyDelegate)
        {
            // validate device module
            var azureDevice = await this.registry.GetDeviceAsync(device.Id);

            if (azureDevice == null)
            {
                azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel());
            }

            Twin azureTwin;

            if (device.Twin == null)
            {
                azureTwin = await this.registry.GetTwinAsync(device.Id);
            }
            else
            {
                azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), device.Twin.ETag);

                // Update the deviceGroupFilter cache, no need to wait
                var model = new DevicePropertyServiceModel();

                var tagRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.Tags)) as JToken;
                if (tagRoot != null)
                {
                    model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath());
                }

                var reportedRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(device.Twin.ReportedProperties)) as JToken;
                if (reportedRoot != null)
                {
                    model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath());
                }
                var unused = devicePropertyDelegate(model);
            }

            return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName));
        }
Example #5
0
        public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device)
        {
            // auto generate DeviceId, if missing
            if (string.IsNullOrEmpty(device.Id))
            {
                device.Id = Guid.NewGuid().ToString();
            }

            var azureDevice = await this.registry.AddDeviceAsync(device.ToAzureModel());

            Twin azureTwin;

            if (device.Twin == null)
            {
                azureTwin = await this.registry.GetTwinAsync(device.Id);
            }
            else
            {
                azureTwin = await this.registry.UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*");
            }

            return(new DeviceServiceModel(azureDevice, azureTwin, this.ioTHubHostName));
        }
Example #6
0
        public async Task <DeviceServiceModel> CreateAsync(DeviceServiceModel device)
        {
            if (device.IsEdgeDevice &&
                device.Authentication != null &&
                !device.Authentication.AuthenticationType.Equals(AuthenticationType.Sas))
            {
                throw new InvalidInputException("Edge devices only support symmetric key authentication.");
            }

            // auto generate DeviceId, if missing
            if (string.IsNullOrEmpty(device.Id))
            {
                device.Id = Guid.NewGuid().ToString();
            }

            var azureDevice = await this.tenantConnectionHelper.GetRegistry().AddDeviceAsync(device.ToAzureModel());

            Twin azureTwin;

            if (device.Twin == null)
            {
                azureTwin = await this.tenantConnectionHelper.GetRegistry().GetTwinAsync(device.Id);
            }
            else
            {
                azureTwin = await this.tenantConnectionHelper.GetRegistry().UpdateTwinAsync(device.Id, device.Twin.ToAzureModel(), "*");
            }

            await this.asaManager.BeginDeviceGroupsConversionAsync();

            return(new DeviceServiceModel(azureDevice, azureTwin, this.tenantConnectionHelper.GetIotHubName()));
        }