/// <summary>
        /// Add a device with strongly typed settings (could be usefull in some scenarios)
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public async Task <SIoT.Device> AddDeviceWithTagsAsync(string deviceId, SIoT.DeviceIoTSettings settings)
        {
            TwinJsonConverter converter = new TwinJsonConverter();
            Twin twin = null;

            if (settings != null && settings.Twins != null)
            {
                twin = new Twin(deviceId);

                string jsonTags = JsonConvert.SerializeObject(settings.Twins, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                twin.Tags = new TwinCollection(jsonTags);
            }

            BulkRegistryOperationResult result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin);

            if (result != null && result.IsSuccessful)
            {
                return(await GetDeviceAsync(deviceId));
            }
            else
            {
                throw new Exception(JsonConvert.SerializeObject(result.Errors, Formatting.Indented));
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Add a device with strongly typed settings (could be usefull in some scenarios)
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public async Task<SIoT.Device> AddDeviceWithTagsAsync(string deviceId, SIoT.DeviceIoTSettings settings)
        {
            var converter = new TwinJsonConverter();
            Twin twin = null;

            if (settings != null && settings.Twins != null)
            {
                twin = new Twin(deviceId);

                var jsonTags = JsonConvert.SerializeObject(settings.Twins,
                    new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
                twin.Tags = new TwinCollection(jsonTags);
            }

            var result = await _registryManager.AddDeviceWithTwinAsync(new Device(deviceId), twin).ConfigureAwait(false);

            if (result != null && result.IsSuccessful)
                return await GetDeviceAsync(deviceId).ConfigureAwait(false);
            throw new Exception("An error has occurred during the device creation or the twin updates.");
        }