/// <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}"); }
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 } }); } }
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 } }); } }
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()); } }