/// <summary>
        /// Fetches a preview image for the specified <code>info</code>.
        /// </summary>
        /// <param name="target">The <see cref="RawDataTargetEntityDto"/> that specifies the raw data object that should be fetched.</param>
        /// <param name="rawDataKey">The unique key that identifies the raw data object for the specified target.</param>
        /// <returns>The preview image as byte array.</returns>
        /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
        /// <exception cref="ArgumentNullException"><paramref name="target"/> is <see langword="null" />.</exception>
        public Task <byte[]> GetRawDataThumbnail(RawDataTargetEntityDto target, int rawDataKey, CancellationToken cancellationToken = default)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            async Task <byte[]> GetRawDataThumbnail()
            {
                try
                {
                    return(await _RestClient
                           .RequestBytes(
                               RequestBuilder.CreateGet($"rawData/{target.Entity}/{target.Uuid}/{rawDataKey}/thumbnail"),
                               cancellationToken)
                           .ConfigureAwait(false));
                }
                catch (WrappedServerErrorException ex)
                {
                    if (ex.StatusCode != HttpStatusCode.NotFound)
                    {
                        throw;
                    }
                }

                return(null);
            }

            return(GetRawDataThumbnail());
        }
 /// <summary>
 /// Deletes the raw data object identified by the compound key <paramref name="measurementUuid"/> and <paramref name="characteristicUuid"/>.
 /// </summary>
 /// <param name="client">The client class to use.</param>
 /// <param name="measurementUuid">The uuid of the measurement the raw data objects belongs to. </param>
 /// <param name="characteristicUuid">The uuid of the characteristic the raw data object belongs to.</param>
 /// <param name="rawDataKey">The key of the raw data object that should be deleted.</param>
 /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 /// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null" />.</exception>
 public static Task DeleteRawDataForValue([NotNull] this IRawDataServiceRestClient client, Guid measurementUuid, Guid characteristicUuid, int?rawDataKey = null, CancellationToken cancellationToken = default)
 {
     if (client == null)
     {
         throw new ArgumentNullException(nameof(client));
     }
     return(client.DeleteRawData(RawDataTargetEntityDto.CreateForValue(measurementUuid, characteristicUuid), rawDataKey, cancellationToken));
 }
 /// <summary>
 /// Fetches raw data for the measurement with <paramref name="measurementUuid"/> and raw data index <paramref name="rawDataKey"/>.
 /// </summary>
 /// <param name="client">The client class to use for fetching the raw data.</param>
 /// <param name="measurementUuid">The uuid of the measurement to fetch the raw data object for.</param>
 /// <param name="rawDataKey">The unique key that identifies the raw data object for the specified measurement.</param>
 /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
 /// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null" />.</exception>
 public static Task <byte[]> GetRawDataForMeasurement([NotNull] this IRawDataServiceRestClient client, Guid measurementUuid, int rawDataKey, CancellationToken cancellationToken = default)
 {
     if (client == null)
     {
         throw new ArgumentNullException(nameof(client));
     }
     return(client.GetRawData(RawDataTargetEntityDto.CreateForMeasurement(measurementUuid), rawDataKey, cancellationToken: cancellationToken));
 }
Example #4
0
        public static async Task Lesson(DataServiceRestClient client, RawDataServiceRestClient rawClient)
        {
            await client.CreateParts(new[] { Part });

            //PiWeb only accepts binary data
            var data   = Encoding.UTF8.GetBytes("Hello RawDataService!");
            var target = RawDataTargetEntityDto.CreateForPart(Part.Uuid);

            //Notes:	- see e.g. http://wiki.selfhtml.org/wiki/Referenz:MIME-Typen for a complete list of mime types
            //			- When using Key = -1, the server will generate a new key. The generated key can be read from the result.

            var createdRawDataInfo = await rawClient.CreateRawData(new RawDataInformationDto
            {
                FileName     = "Hello.txt",
                MimeType     = "text/plain",
                Key          = -1,
                Created      = DateTime.Now,
                LastModified = DateTime.Now,
                MD5          = new Guid(MD5.Create().ComputeHash(data)),
                Size         = data.Length,
                Target       = target
            }, data);

            Console.WriteLine($"RawData created with key: {createdRawDataInfo.Key}");

            //We can simply update raw data information like filename or MIME-type
            if (createdRawDataInfo.Key.HasValue)
            {
                createdRawDataInfo.FileName = "HelloEdit.txt";

                Console.WriteLine($"Renaming raw data file to {createdRawDataInfo.FileName}");

                await rawClient.UpdateRawDataInformation(target, createdRawDataInfo.Key.Value, createdRawDataInfo);
            }

            var rawDataInformation = await rawClient.ListRawData(new[] { target });

            foreach (var information in rawDataInformation)
            {
                Console.WriteLine($"Fetching {information.FileName}: {information.Size} bytes");

                //Fetch the data by providing the correct RawDataInformation
                data = await rawClient.GetRawData(information);

                Console.WriteLine($"Content: {Encoding.UTF8.GetString( data )}");

                //We can use the key we found with the ListRawData function to delete a single file
                await rawClient.DeleteRawDataForPart(Part.Uuid, information.Key);
            }

            //Or we simply delete all raw data for a certain entity
            await rawClient.DeleteRawDataForPart(Part.Uuid);
        }
        /// <summary>
        /// Fetches raw data as a byte array for the raw data item identified by <paramref name="target"/> and <paramref name="rawDataKey"/>.
        /// </summary>
        /// <param name="target">The <see cref="RawDataTargetEntityDto"/> that specifies the raw data object that should be fetched.</param>
        /// <param name="rawDataKey">The unique key that identifies the raw data object for the specified target.</param>
        /// <param name="expectedMd5">The md5 check sum that is expected for the result object. If this value is set, performance is better because server side round trips are reduced.</param>
        /// <param name="cancellationToken">A token to cancel the hronous operation.</param>
        /// <exception cref="ArgumentNullException"><paramref name="target"/> is <see langword="null" />.</exception>
        public Task <byte[]> GetRawData(RawDataTargetEntityDto target, int rawDataKey, Guid?expectedMd5 = null, CancellationToken cancellationToken = default)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (expectedMd5.HasValue)
            {
                return(_RestClient.RequestBytes(RequestBuilder.CreateGet($"rawData/{target.Entity}/{target.Uuid}/{rawDataKey}?expectedMd5={expectedMd5}"), cancellationToken));
            }

            return(_RestClient.RequestBytes(RequestBuilder.CreateGet($"rawData/{target.Entity}/{target.Uuid}/{rawDataKey}"), cancellationToken));
        }
        /// <summary>
        /// Deletes raw data for the element identified by <paramref name="target"/> and <paramref name="rawDataKey"/>.
        /// </summary>
        /// <param name="target">The <see cref="RawDataTargetEntityDto"/> object containing the <see cref="RawDataEntityDto"/> type and the uuid of the raw data that should be deleted.</param>
        /// <param name="rawDataKey">The key of the raw data object which should be deleted.</param>
        /// <param name="cancellationToken">A token to cancel the hronous operation.</param>
        /// <exception cref="ArgumentNullException"><paramref name="target"/> is <see langword="null" />.</exception>
        public Task DeleteRawData(RawDataTargetEntityDto target, int?rawDataKey = null, CancellationToken cancellationToken = default)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            StringUuidTools.CheckUuid(target.Entity, target.Uuid);

            var url = rawDataKey.HasValue
                                ? $"rawData/{target.Entity}/{{{target.Uuid}}}/{rawDataKey}"
                                : $"rawData/{target.Entity}/{{{target.Uuid}}}";

            return(_RestClient.Request(RequestBuilder.CreateDelete(url), cancellationToken));
        }
        /// <summary>
        /// Fetches a list of raw data information.
        /// </summary>
        /// <param name="client">The client class to use.</param>
        /// <param name="target">The target entity to fetch the raw data information.</param>
        /// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
        /// <exception cref="ArgumentNullException"><paramref name="client"/> or <paramref name="target"/> is <see langword="null" />.</exception>
        public static Task <RawDataInformationDto[]> ListRawData([NotNull] this IRawDataServiceRestClient client, [NotNull] RawDataTargetEntityDto target, CancellationToken cancellationToken = default)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            return(client.ListRawData(target.Entity, new[] { target.Uuid }, null, cancellationToken));
        }