Example #1
0
        static IEnumerable <ExportImportDevice> GenerateExportImportDeviceListForBulkOperations(IEnumerable <Device> devices, ImportMode importMode)
        {
            if (devices == null)
            {
                throw new ArgumentNullException(nameof(devices));
            }

            if (!devices.Any())
            {
                throw new ArgumentException(nameof(devices));
            }

            var exportImportDeviceList = new List <ExportImportDevice>(devices.Count());

            foreach (Device device in devices)
            {
                ValidateDeviceId(device);

                switch (importMode)
                {
                case ImportMode.Create:
                    if (!string.IsNullOrWhiteSpace(device.ETag))
                    {
                        throw new ArgumentException(ApiResources.ETagSetWhileRegisteringDevice);
                    }
                    break;

                case ImportMode.Update:
                    // No preconditions
                    break;

                case ImportMode.UpdateIfMatchETag:
                    if (string.IsNullOrWhiteSpace(device.ETag))
                    {
                        throw new ArgumentException(ApiResources.ETagNotSetWhileUpdatingDevice);
                    }
                    break;

                case ImportMode.Delete:
                    // No preconditions
                    break;

                case ImportMode.DeleteIfMatchETag:
                    if (string.IsNullOrWhiteSpace(device.ETag))
                    {
                        throw new ArgumentException(ApiResources.ETagNotSetWhileDeletingDevice);
                    }
                    break;

                default:
                    throw new ArgumentException(IotHubApiResources.GetString(ApiResources.InvalidImportMode, importMode));
                }

                var exportImportDevice = new ExportImportDevice(device, importMode);

                exportImportDeviceList.Add(exportImportDevice);
            }

            return(exportImportDeviceList);
        }
Example #2
0
        async Task <QueryResult> ExecuteQueryAsync(string sqlQueryString, int?pageSize, string continuationToken, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            if (string.IsNullOrWhiteSpace(sqlQueryString))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrEmpty, nameof(sqlQueryString)));
            }

            var customHeaders = new Dictionary <string, string>();

            if (!string.IsNullOrWhiteSpace(continuationToken))
            {
                customHeaders.Add(ContinuationTokenHeader, continuationToken);
            }

            if (pageSize != null)
            {
                customHeaders.Add(PageSizeHeader, pageSize.ToString());
            }

            HttpResponseMessage response = await this.httpClientHelper.PostAsync <QuerySpecification>(
                QueryDevicesRequestUri(),
                new QuerySpecification()
            {
                Sql = sqlQueryString
            },
                null,
                customHeaders,
                new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" },
                null,
                cancellationToken);

            return(await QueryResult.FromHttpResponseAsync(response));
        }
        Task <QueryResult> ExecuteQueryAsync(string sqlQueryString, int?pageSize, string continuationToken, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            if (string.IsNullOrWhiteSpace(sqlQueryString))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrEmpty, nameof(sqlQueryString)));
            }

            return(this.httpClientHelper.PostAsync <QuerySpecification, QueryResult>(
                       QueryDevicesRequestUri(),
                       new QuerySpecification()
            {
                Sql = sqlQueryString,
                PageSize = pageSize,
                ContinuationToken = continuationToken
            },
                       null,
                       null,
                       new MediaTypeHeaderValue("application/json")
            {
                CharSet = "utf-8"
            },
                       null,
                       cancellationToken));
        }
Example #4
0
        public override Task <Twin> GetTwinAsync(string deviceId, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(deviceId))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrWhitespace, "deviceId"));
            }

            this.EnsureInstanceNotClosed();
            var errorMappingOverrides = new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >()
            {
                { HttpStatusCode.NotFound, async responseMessage => new DeviceNotFoundException(await ExceptionHandlingHelper.GetExceptionMessageAsync(responseMessage)) }
            };

            return(this.httpClientHelper.GetAsync <Twin>(GetTwinUri(deviceId), errorMappingOverrides, null, false, cancellationToken));
        }
Example #5
0
        static IEnumerable <ExportImportDevice> GenerateExportImportDeviceListForTwinBulkOperations(IEnumerable <Twin> twins, ImportMode importMode)
        {
            if (twins == null)
            {
                throw new ArgumentNullException(nameof(twins));
            }

            if (!twins.Any())
            {
                throw new ArgumentException(nameof(twins));
            }

            var exportImportDeviceList = new List <ExportImportDevice>(twins.Count());

            foreach (Twin twin in twins)
            {
                ValidateTwinId(twin);

                switch (importMode)
                {
                case ImportMode.UpdateTwin:
                    // No preconditions
                    break;

                case ImportMode.UpdateTwinIfMatchETag:
                    if (string.IsNullOrWhiteSpace(twin.ETag))
                    {
                        throw new ArgumentException(ApiResources.ETagNotSetWhileUpdatingTwin);
                    }
                    break;

                default:
                    throw new ArgumentException(IotHubApiResources.GetString(ApiResources.InvalidImportMode, importMode));
                }

                var exportImportDevice = new ExportImportDevice();
                exportImportDevice.Id         = twin.DeviceId;
                exportImportDevice.ImportMode = importMode;
                exportImportDevice.TwinETag   = importMode == ImportMode.UpdateTwinIfMatchETag ? twin.ETag : null;
                exportImportDevice.Tags       = twin.Tags;
                exportImportDevice.Properties = new ExportImportDevice.PropertyContainer();
                exportImportDevice.Properties.DesiredProperties = twin.Properties?.Desired;

                exportImportDeviceList.Add(exportImportDevice);
            }

            return(exportImportDeviceList);
        }
Example #6
0
        public override Task RemoveDeviceAsync(string deviceId, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            if (string.IsNullOrWhiteSpace(deviceId))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrWhitespace, "deviceId"));
            }

            // use wildcard etag
            var eTag = new ETagHolder {
                ETag = "*"
            };

            return(this.RemoveDeviceAsync(deviceId, eTag, cancellationToken));
        }
        public override Task RemoveDeviceAsync(Device device, CancellationToken cancellationToken)
        {
            this.EnsureInstanceNotClosed();

            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (string.IsNullOrWhiteSpace(device.Id))
            {
                throw new ArgumentException(IotHubApiResources.GetString(ApiResources.ParameterCannotBeNullOrWhitespace, "device.Id"));
            }

            if (string.IsNullOrWhiteSpace(device.ETag))
            {
                throw new ArgumentException(ApiResources.ETagNotSetWhileDeletingDevice);
            }

            return(this.RemoveDeviceAsync(device.Id, device, cancellationToken));
        }
Example #8
0
        public static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType mappingType, out object netObject)
        {
            netObject = null;
            if (amqpObject == null)
            {
                return(false);
            }

            switch (SerializationUtilities.GetTypeId(amqpObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Int16:
            case PropertyValueType.Int32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt16:
            case PropertyValueType.UInt32:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Boolean:
            case PropertyValueType.Decimal:
            case PropertyValueType.Char:
            case PropertyValueType.Guid:
            case PropertyValueType.DateTime:
            case PropertyValueType.String:
                netObject = amqpObject;
                break;

            case PropertyValueType.Unknown:
                if (amqpObject is AmqpSymbol)
                {
                    netObject = ((AmqpSymbol)amqpObject).Value;
                }
                else if (amqpObject is ArraySegment <byte> )
                {
                    ArraySegment <byte> binValue = (ArraySegment <byte>)amqpObject;
                    if (binValue.Count == binValue.Array.Length)
                    {
                        netObject = binValue.Array;
                    }
                    else
                    {
                        byte[] buffer = new byte[binValue.Count];
                        Buffer.BlockCopy(binValue.Array, binValue.Offset, buffer, 0, binValue.Count);
                        netObject = buffer;
                    }
                }
                else if (amqpObject is DescribedType)
                {
                    DescribedType describedType = (DescribedType)amqpObject;
                    if (describedType.Descriptor is AmqpSymbol)
                    {
                        AmqpSymbol symbol = (AmqpSymbol)describedType.Descriptor;
                        if (symbol.Equals((AmqpSymbol)UriName))
                        {
                            netObject = new Uri((string)describedType.Value);
                        }
                        else if (symbol.Equals((AmqpSymbol)TimeSpanName))
                        {
                            netObject = new TimeSpan((long)describedType.Value);
                        }
                        else if (symbol.Equals((AmqpSymbol)DateTimeOffsetName))
                        {
                            netObject = new DateTimeOffset(new DateTime((long)describedType.Value, DateTimeKind.Utc));
                        }
                    }
                }
                else if (mappingType == MappingType.ApplicationProperty)
                {
                    throw FxTrace.Exception.AsError(new SerializationException(IotHubApiResources.GetString(ApiResources.FailedToSerializeUnsupportedType, amqpObject.GetType().FullName)));
                }
                else if (amqpObject is AmqpMap)
                {
                    AmqpMap map = (AmqpMap)amqpObject;
                    Dictionary <string, object> dictionary = new Dictionary <string, object>();
                    foreach (var pair in map)
                    {
                        dictionary.Add(pair.Key.ToString(), pair.Value);
                    }

                    netObject = dictionary;
                }
                else
                {
                    netObject = amqpObject;
                }
                break;

            default:
                break;
            }

            return(netObject != null);
        }
Example #9
0
        public static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType mappingType, out object amqpObject)
        {
            amqpObject = null;
            if (netObject == null)
            {
                return(false);
            }

            switch (SerializationUtilities.GetTypeId(netObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Int16:
            case PropertyValueType.Int32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt16:
            case PropertyValueType.UInt32:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Boolean:
            case PropertyValueType.Decimal:
            case PropertyValueType.Char:
            case PropertyValueType.Guid:
            case PropertyValueType.DateTime:
            case PropertyValueType.String:
                amqpObject = netObject;
                break;

            case PropertyValueType.Stream:
                if (mappingType == MappingType.ApplicationProperty)
                {
                    amqpObject = ReadStream((Stream)netObject);
                }
                break;

            case PropertyValueType.Uri:
                amqpObject = new DescribedType((AmqpSymbol)UriName, ((Uri)netObject).AbsoluteUri);
                break;

            case PropertyValueType.DateTimeOffset:
                amqpObject = new DescribedType((AmqpSymbol)DateTimeOffsetName, ((DateTimeOffset)netObject).UtcTicks);
                break;

            case PropertyValueType.TimeSpan:
                amqpObject = new DescribedType((AmqpSymbol)TimeSpanName, ((TimeSpan)netObject).Ticks);
                break;

            case PropertyValueType.Unknown:
                if (netObject is Stream)
                {
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        amqpObject = ReadStream((Stream)netObject);
                    }
                }
                else if (mappingType == MappingType.ApplicationProperty)
                {
                    throw FxTrace.Exception.AsError(new SerializationException(IotHubApiResources.GetString(ApiResources.FailedToSerializeUnsupportedType, netObject.GetType().FullName)));
                }
                else if (netObject is byte[])
                {
                    amqpObject = new ArraySegment <byte>((byte[])netObject);
                }
                else if (netObject is IList)
                {
                    // Array is also an IList
                    amqpObject = netObject;
                }
                else if (netObject is IDictionary)
                {
                    amqpObject = new AmqpMap((IDictionary)netObject);
                }
                break;

            default:
                break;
            }

            return(amqpObject != null);
        }