Ejemplo n.º 1
0
        public static async Task UndoLesson(DataServiceRestClient client)
        {
            //Delete the catalogs first, since deleting attributes which are used by catalogs will cause an error
            await client.DeleteCatalogs(new[] { MachineCatalog.Uuid });

            await client.DeleteAttributeDefinitions(EntityDto.Catalog, new[] { MachineName.Key, MachineNumber.Key, MachineVendor.Key });
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Shows how to work with the attribute configuration.
        /// </summary>
        /// <param name="client">The client.</param>
        public static async Task Lesson(DataServiceRestClient client)
        {
            //Create attributes
            await client.CreateAttributeDefinition(Entity.Part, AttributeDefinition);

            //This will create an attribute which can be used by catalogs. Don't be confused, this is no catalog attribute
            await client.CreateAttributeDefinition(Entity.Catalog, CatalogColumnAttributeDefinition);

            //Create a catalog which we can use for our catalog attribute
            await client.CreateCatalogs(new[] { Catalog });

            //Create the catalog attribute definition.
            await client.CreateAttributeDefinition(Entity.Part, CatalogAttributeDefinition);

            //Notes: - you can't create catalog attributes for catalogs (Entity.Catalog)
            //Notes: - you must obey the shown order of commands!

            //You can update everything except the key, which is the identifier.
            //To change the key, you must delete and recreate the attribute, but be aware: all data stored for this attribute will be lost!
            CatalogAttributeDefinition.Description = "Characteristic catalog attribute";
            await client.UpdateAttributeDefinitions(Entity.Characteristic, new[] { CatalogAttributeDefinition });

            //Get all attributes
            var configuration = await client.GetConfiguration();

            //Attributes are assigned to an entity: part, characteristic, measurement, value or catalog.
            Console.WriteLine($"Attributes for part: {configuration.PartAttributes.Length}");
            Console.WriteLine($"Attributes for characteristic: {configuration.CharacteristicAttributes.Length}");
            Console.WriteLine($"Attributes for measurement: {configuration.MeasurementAttributes.Length}");
            Console.WriteLine($"Attributes for value: {configuration.ValueAttributes.Length}");
            Console.WriteLine($"Attributes for catalog: {configuration.CatalogAttributes.Length}");
        }
Ejemplo n.º 3
0
        public static async Task Lesson(DataServiceRestClient client)
        {
            await client.CreateParts(new[] { Part, SubPart });

            await client.CreateCharacteristics(new[] { Characteristic, Child, SubChild });

            //Depth null will result in a recursive search
            var result = await client.GetCharacteristics(Part.Path, null);

            Console.WriteLine($"Depth null: {result.Count()} characteristics");

            //Depth 0 will return an empty list
            result = await client.GetCharacteristics(Part.Path, 0);

            Console.WriteLine($"Depth 0: {result.Count()} characteristics");

            //Depth 1 will return the direct child characteristics
            result = await client.GetCharacteristics(Part.Path, 1);

            Console.WriteLine($"Depth 1: {result.Count()} characteristics");

            //Depth 2 will return the direct children and their children, but not the children of subparts
            result = await client.GetCharacteristics(Part.Path, 2);

            Console.WriteLine($"Depth 2: {result.Count()} characteristics");

            //Use UpdateParts and UpdateCharacteristics to rename entities, and modify their attributes
            Part.Path = PathHelper.RoundtripString2PathInformation("P:/PartName2/");
            await client.UpdateParts(new[] { Part });
        }
Ejemplo n.º 4
0
        public static void CheckCatalogAttribute(DataServiceRestClient client, EntityDto entity, ushort key, string description,
                                                 Guid catalog)
        {
            var configuration = client.GetConfiguration().Result;
            var definition    = configuration.GetDefinition(key);

            if (definition == null)
            {
                client.CreateAttributeDefinition(entity, new CatalogAttributeDefinitionDto
                {
                    Key         = key,
                    Description = description,
                    Catalog     = catalog
                });
            }
            else if (configuration.GetTypeForKey(key) != entity || definition.Description != description ||
                     (definition as CatalogAttributeDefinitionDto)?.Catalog != catalog)
            {
                client.UpdateAttributeDefinitions(entity, new AbstractAttributeDefinitionDto[]
                {
                    new CatalogAttributeDefinitionDto
                    {
                        Key         = key,
                        Description = description,
                        Catalog     = catalog
                    }
                });
            }
        }
Ejemplo n.º 5
0
        public static void CheckAttribute(DataServiceRestClient client, EntityDto entity, ushort key, string description,
                                          AttributeTypeDto type)
        {
            var configuration = client.GetConfiguration().Result;
            var definition    = configuration.GetDefinition(key);

            if (definition == null)
            {
                client.CreateAttributeDefinition(entity, new AttributeDefinitionDto
                {
                    Key         = key,
                    Description = description,
                    Type        = type
                });
            }
            else if (configuration.GetTypeForKey(key) != entity || definition.Description != description ||
                     (definition as AttributeDefinitionDto)?.Type != type)
            {
                //Don't do this, in case the PiWeb database is already in use. Changing the configuration will cause data-loss!!!
                client.UpdateAttributeDefinitions(entity, new AbstractAttributeDefinitionDto[]
                {
                    new AttributeDefinitionDto
                    {
                        Key         = key,
                        Description = description,
                        Type        = type
                    }
                });
            }
        }
Ejemplo n.º 6
0
        public static InspectionPlanCharacteristicDto GetOrCreateCharacteristic(DataServiceRestClient client, string partName, string characteristicName, Dictionary <string, ushort> mapping, Dictionary <string, object> values)
        {
            var characteristics = client.GetCharacteristics(PathHelper.String2PartPathInformation(partName), 1).Result;
            var attributes      = values.Select(pair => new AttributeDto(mapping[pair.Key], pair.Value)).ToArray();

            var existingCharacteristic = characteristics.FirstOrDefault(p => string.Equals(p.Path.Name, characteristicName));

            if (existingCharacteristic == null)
            {
                existingCharacteristic = new InspectionPlanCharacteristicDto
                {
                    Path       = PathHelper.RoundtripString2PathInformation("PC:/" + partName + " / " + characteristicName + "/"),
                    Uuid       = Guid.NewGuid(),
                    Attributes = attributes
                };

                client.CreateCharacteristics(new[]
                {
                    existingCharacteristic
                }).Wait();
            }
            else
            {
                existingCharacteristic.Attributes = attributes;
                //maybe update the existing characteristic, so the attributes are up-to-date
                client.UpdateCharacteristics(new[]
                {
                    existingCharacteristic
                }).Wait();
            }

            return(existingCharacteristic);
        }
Ejemplo n.º 7
0
        public static async Task Lesson(DataServiceRestClient client)
        {
            //The statistics in serviceInformation are calculated periodically by the server, and are not guaranteed to be always exact.

            var info = await client.GetServiceInformation();

            Console.WriteLine(info.ServerName);
        }
Ejemplo n.º 8
0
        private static void ExportToPiWeb(string serverUri, string inspectionName, IEnumerable <Characteristic> data,
                                          Dictionary <string, ushort> attributeMapping)
        {
            //1. Create the client
            var client = new DataServiceRestClient(new Uri(serverUri));

            //2. Check the server configuration
            foreach (var entry in attributeMapping)
            {
                Configuration.CheckAttribute(client, EntityDto.Characteristic, entry.Value, entry.Key, AttributeTypeDto.Float);
            }

            //2.1 Make sure the essential time and value attributes are present
            Configuration.CheckAttribute(client, EntityDto.Measurement, WellKnownKeys.Measurement.Time, "Time",
                                         AttributeTypeDto.DateTime);
            Configuration.CheckAttribute(client, EntityDto.Value, WellKnownKeys.Value.MeasuredValue, "Value", AttributeTypeDto.Float);

            //3. Check the inspection plan

            //3.1 Check the inspection plan part
            var part = InspectionPlan.GetOrCreatePart(client, inspectionName);
            var characteristicMapping = new Dictionary <Characteristic, InspectionPlanCharacteristicDto>();

            //3.2 Check the inspection plan characteristics
            foreach (var characteristic in data)
            {
                var inspectionPlanCharacteristic = InspectionPlan.GetOrCreateCharacteristic(client, part.Path.Name,
                                                                                            characteristic.Name, attributeMapping, characteristic.Attributes);
                characteristicMapping.Add(characteristic, inspectionPlanCharacteristic);
            }

            //4. Create the measurement
            var dataCharacteristics = characteristicMapping.Select(pair => new DataCharacteristicDto
            {
                Uuid  = pair.Value.Uuid,
                Path  = pair.Value.Path,
                Value = new DataValueDto
                {
                    Attributes = new[] { new AttributeDto(1, pair.Key.Value) }
                }
            }).ToArray();

            var measurement = new DataMeasurementDto
            {
                Uuid            = Guid.NewGuid(),
                PartUuid        = part.Uuid,
                Time            = DateTime.UtcNow,
                Attributes      = new[] { new AttributeDto(WellKnownKeys.Measurement.Time, DateTime.UtcNow) },
                Characteristics = dataCharacteristics
            };

            //4.1 Write the measurement to the database
            client.CreateMeasurementValues(new[] { measurement }).Wait();
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        public static async Task UndoLesson(DataServiceRestClient client)
        {
            //Delete attributes
            await client.DeleteAttributeDefinitions(Entity.Part, new[] { AttributeDefinition.Key });

            await client.DeleteAttributeDefinitions(Entity.Characteristic, new[] { CatalogAttributeDefinition.Key });

            await client.DeleteAttributeDefinitions(Entity.Catalog, new[] { CatalogColumnAttributeDefinition.Key });

            //Delete catalogs
            await client.DeleteCatalogs(new[] { Catalog.Uuid });
        }
Ejemplo n.º 11
0
        public static async Task UndoLesson(DataServiceRestClient client)
        {
            await client.DeleteMeasurementsByUuid(new[] { Measurement.Uuid });

            //The server can be configured to allow or disallow deleting of parts with measurements

            await client.DeleteCharacteristics(new[] { Characteristic.Uuid });

            await client.DeleteParts(new[] { Part.Uuid });

            await client.DeleteAttributeDefinitions(EntityDto.Measurement, new[] { MeasurementAttributeDefinition.Key });

            await client.DeleteAttributeDefinitions(EntityDto.Value, new[] { ValueAttributeDefinition.Key });
        }
Ejemplo n.º 12
0
        public async Task RequestConfiguration()
        {
            // given
            using var server = WebServer.StartNew(Port);
            using var client = new DataServiceRestClient(Uri);

            var config = Fixture.Create <ConfigurationDto>();

            server.RegisterReponse("/DataServiceRest/configuration", JsonConvert.SerializeObject(config));

            // when
            var result = await client.GetConfiguration();

            // then
            Assert.That(JsonConvert.SerializeObject(result), Is.EqualTo(JsonConvert.SerializeObject(config)));
        }
Ejemplo n.º 13
0
        public static InspectionPlanPartDto GetOrCreatePart(DataServiceRestClient client, string name)
        {
            var parts        = client.GetParts(PathInformationDto.Root, null, 1).Result;
            var existingPart = parts.FirstOrDefault(p => string.Equals(p.Path.Name, name));

            if (existingPart == null)
            {
                existingPart = new InspectionPlanPartDto
                {
                    Path = PathHelper.String2PartPathInformation(name),
                    Uuid = Guid.NewGuid()
                };

                client.CreateParts(new[]
                {
                    existingPart
                }).Wait();
            }

            return(existingPart);
        }
Ejemplo n.º 14
0
        static void Main( )
        {
            var client        = new DataServiceRestClient(new System.Uri("http://127.0.0.1:8080/"));
            var rawDataClient = new RawDataServiceRestClient(new System.Uri("http://127.0.0.1:8080/"));

            ServiceInformation.Lesson(client).Wait();

            Configuration.Lesson(client).Wait();
            Configuration.UndoLesson(client).Wait();

            Catalogs.Lesson(client).Wait();
            Catalogs.UndoLesson(client).Wait();

            InspectionPlan.Lesson(client).Wait();
            InspectionPlan.UndoLesson(client).Wait();

            Measurements.Lesson(client).Wait();
            Measurements.UndoLesson(client).Wait();

            RawData.Lesson(client, rawDataClient).Wait();
            RawData.UndoLesson(client, rawDataClient).Wait();
        }
Ejemplo n.º 15
0
        public static async Task Lesson(DataServiceRestClient client)
        {
            //A catalog must have at least one attribute.
            //Attributes with entity 'Catalog' must not be of type 'CatalogAttribute'
            await client.CreateAttributeDefinitions(EntityDto.Catalog, new[] { MachineName, MachineNumber, MachineVendor });

            //Create the catalog
            await client.CreateCatalogs(new[] { MachineCatalog });

            //Add an attribute
            MachineCatalog.ValidAttributes = MachineCatalog.ValidAttributes.Append(MachineVendor.Key);

            await client.UpdateCatalogs(new[] { MachineCatalog });

            //Add an entry
            await client.CreateCatalogEntries(MachineCatalog.Uuid, new[] { EntryXenos });

            //Update existing catalog entries
            EntryAccura.Attributes  = EntryAccura.Attributes.Append(new AttributeDto(MachineVendor.Key, "Zeiss"));
            EntryContura.Attributes = EntryContura.Attributes.Append(new AttributeDto(MachineVendor.Key, "Zeiss"));

            await client.UpdateCatalogs(new[] { MachineCatalog });
        }
Ejemplo n.º 16
0
 private async void checkConnect2PiWebServer()
 {
     _ServerUri             = new Uri(PiWebHost_Data["Main"].ToString());
     _RestDataServiceClient = new DataServiceRestClient(_ServerUri);
     await CheckConnection();
 }
Ejemplo n.º 17
0
 public static async Task UndoLesson(DataServiceRestClient client, RawDataServiceRestClient rawClient)
 {
     await client.DeleteParts(new[] { Part.Uuid });
 }
Ejemplo n.º 18
0
        public static async Task Lesson(DataServiceRestClient client)
        {
            //Create the most commonly used attributes for measurement and measurement value

            var configuration = await client.GetConfiguration();

            if (configuration.GetDefinition(EntityDto.Measurement, MeasurementAttributeDefinition.Key) == null)
            {
                await client.CreateAttributeDefinition(EntityDto.Measurement, MeasurementAttributeDefinition);
            }

            if (configuration.GetDefinition(EntityDto.Value, ValueAttributeDefinition.Key) == null)
            {
                await client.CreateAttributeDefinition(EntityDto.Value, ValueAttributeDefinition);
            }

            //Every measurement is attached to a part, and every value is attached to a characteristic
            await client.CreateParts(new[] { Part });

            await client.CreateCharacteristics(new[] { Characteristic });

            //The function 'CreateMeasurements' will create measurements without any values. You'll much more likely use the 'CreateMeasurementValues' function
            await client.CreateMeasurementValues(new[] { Measurement });

            Value.Value = new DataValueDto(0.0)               //This will result in an unmeasured characteristic, because the attribute array doesn't contain K1
            {
                Attributes = new AttributeDto[] { }
            };

            await client.UpdateMeasurementValues(new[] { Measurement });

            Value.Value = new DataValueDto             //this will work!
            {
                Attributes = new[] { new AttributeDto(ValueAttributeDefinition.Key, 0.5) }
            };

            await client.UpdateMeasurementValues(new[] { Measurement });

            var result = await client.GetMeasurementValues(
                PathInformationDto.Root,                                                           // Part where to search measurements
                new MeasurementValueFilterAttributesDto
            {
                AggregationMeasurements = AggregationMeasurementSelectionDto.All,                      // Decide how to include aggregated measurements in your query
                CharacteristicsUuidList = null,                                                        // Use characteristic uuids to fetch single measurement values
                Deep = true,                                                                           // A deep search will find measurements recursively below the start path
                FromModificationDate = null,                                                           // Will only search measurements with a modification date (LastModified) newer than the specified one
                ToModificationDate   = null,                                                           // Will only search measurements with a modification date (LastModified) older than the specified one
                LimitResult          = 10,                                                             // Will limit the number of returned measurements
                MeasurementUuids     = null,                                                           // Use measurement uuids to search for specific measurements
                OrderBy = new[]                                                                        // Order the returned measurements by specific attributes
                {
                    new OrderDto(WellKnownKeys.Measurement.Time, OrderDirectionDto.Asc, EntityDto.Measurement)
                },
                RequestedMeasurementAttributes = null,                                                 // Specify, which measurement attributes should be returned (default: all)
                RequestedValueAttributes       = null,                                                 // Specify, which value attributes should be returned (default: all)
                SearchCondition = new GenericSearchAttributeConditionDto                               // You can create more complex attribute conditions using the GenericSearchAnd, GenericSearchOr and GenericSearchNot class
                {
                    Attribute = WellKnownKeys.Measurement.Time,                                        //Only measurement attributes are supported
                    Operation = OperationDto.GreaterThan,
                    Value     = XmlConvert.ToString(DateTime.UtcNow - TimeSpan.FromDays(2), XmlDateTimeSerializationMode.Utc)
                }
            });

            foreach (var measurement in result)
            {
                Console.WriteLine(measurement.ToString());
            }
        }
Ejemplo n.º 19
0
        public static async Task UndoLesson(DataServiceRestClient client)
        {
            await client.DeleteParts(new[] { Part.Uuid });

            await client.DeleteCharacteristics(new[] { Characteristic.Uuid, Child.Uuid });
        }