Ejemplo n.º 1
0
        /// <summary>
        ///     Usefull for updating Twins properties (either tags or desired/reported properties).
        ///     This first version does not include desired/reported properties serialization/deserialization but it could be added
        ///     easily (additional properties in settings classes)
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public async Task<bool> UpdateDeviceSettingsAsync(string deviceId, SIoT.DeviceIoTSettings options)
        {
            if (string.IsNullOrEmpty(deviceId))
                throw new ArgumentNullException(nameof(deviceId));

            if (options == null)
                throw new ArgumentNullException(nameof(options));

            if (options.Twins != null && options.Twins.Tags != null)
                try
                {
                    var twin = await _registryManager.GetTwinAsync(deviceId).ConfigureAwait(false);

                    if (twin != null)
                    {
                        var jsonTags = JsonConvert.SerializeObject(options.Twins,
                            new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore});
                        var updatedTwin = await _registryManager.UpdateTwinAsync(deviceId, jsonTags, twin.ETag).ConfigureAwait(false);

                        return updatedTwin != null;
                    }

                    return false;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                    throw;
                }

            return false;
        }
Ejemplo n.º 2
0
        public async Task<SIoT.Device> AddDeviceAsync(string deviceId, SIoT.DeviceIoTSettings settings)
        {
            if (settings == null)
                throw new ArgumentNullException(nameof(settings));

            var device = await AddDeviceToIoTHubAsync(deviceId).ConfigureAwait(false);

            var twinUpdated = await UpdateDeviceSettingsAsync(deviceId, settings).ConfigureAwait(false);

            if (device != null && twinUpdated)
                return _mapper.Map<SIoT.Device>(device);
            throw new Exception("An error has occurred during the device creation or the twin updates.");
        }
Ejemplo n.º 3
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.");
        }