Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "BaSyx.Examples.Snippets.VABAASConnection.Java"

(Created page with "This code snippet illustrates the connection to an Asset Administration Shell (AAS) sub model using the Java SDK with Virtual Automation Bus (VAB) communication. In contrast t...")
 
 
Line 11: Line 11:
  
 
<syntaxhighlight lang="java" style="margin-left: 4em">
 
<syntaxhighlight lang="java" style="margin-left: 4em">
// Connect to sub model using lower-level VAB interface
+
// Retrieve sub model (created by factory) with SDK connector
 +
// - Connect to sub model using lower-level VAB interface
 
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");
 
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");
 +
Map<String, Object> submodel = (Map<String, Object>) connSubModel1.getModelPropertyValue("");
 +
Map<String, Object> smId = (Map<String, Object>) submodel.get(Identifiable.IDENTIFICATION);
  
// - Read property values and compare with expected values
+
// - Read properties
assertTrue((int) connSubModel1.readElementValue("properties/prop1/value") == 234);
+
Map<String, Object> prop1 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
assertTrue((int) connSubModel1.readElementValue("properties/prop3/value") == 17);
+
Map<String, Object> prop2 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop2");
assertTrue(((Map<String, Object>) connSubModel1.readElementValue("properties/prop1")).get("idShort").equals("prop1"));
+
Map<String, Object> prop11 = (Map<String, Object>) connSubModel1.getModelPropertyValue("submodelElements/prop2/prop11");
assertTrue(((Map<String, Object>) connSubModel1.readElementValue("properties/prop2")).get("idShort").equals("prop2"));
+
Map<String, Object> prop3 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop3");
assertTrue((int) connSubModel1.readElementValue("properties/prop2/properties/prop11/value") == 123);
+
 
 +
// - Change property value using VAB primitive
 +
connSubModel1.setModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1/value", 456);
 +
 
 +
// - Read value back using VAB primitive
 +
Map<String, Object> changedProp1 = (Map<String, Object>) connSubModel1
 +
.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 30: Line 39:
 
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");
 
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");
 
</syntaxhighlight>
 
</syntaxhighlight>
 
  
  
Line 43: Line 51:
  
  
The following code accesses the meta property idShort of sub model properties "prop1" and "prop3" and compares its value to the expected values
+
The following code accesses the sub model properties "prop1", "prop2", "prop11" and "prop3" from the connected sub-model.
  
 
<syntaxhighlight lang="java" style="margin-left: 4em">
 
<syntaxhighlight lang="java" style="margin-left: 4em">
// - Retrieve sub model values and compare to expected values
+
// - Read properties
assertTrue(((Map<String, Object>) connSubModel1.readElementValue("properties/prop1")).get("idShort").equals("prop1"));
+
Map<String, Object> prop1 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
assertTrue(((Map<String, Object>) connSubModel1.readElementValue("properties/prop2")).get("idShort").equals("prop2"));
+
Map<String, Object> prop2 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop2");
 +
Map<String, Object> prop11 = (Map<String, Object>) connSubModel1.getModelPropertyValue("submodelElements/prop2/prop11");
 +
Map<String, Object> prop3 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop3");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
The following code change the value of "prop1" to 456 via the VAB, and verify the changed value by reading the property using VAB primative.
 +
<syntaxhighlight lang="java" style="margin-left: 4em">
 +
// - Change property value using VAB primitive
 +
connSubModel1.setModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1/value", 456);
 +
 +
// - Read value back using VAB primitive
 +
Map<String, Object> changedProp1 = (Map<String, Object>) connSubModel1
 +
.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
 +
</syntaxhighlight>
  
  
The following code accesses the value of contained property "prop11" in container "prop2".
+
The following code verifies the id-short and the value of the accessed properties "prop1", "prop2", "prop11" and "prop3".
  
 
<syntaxhighlight lang="java" style="margin-left: 4em">
 
<syntaxhighlight lang="java" style="margin-left: 4em">
assertTrue((int) connSubModel1.readElementValue("properties/prop2/properties/prop11/value") == 123);
+
// - Check results
 +
assertEquals("sm-001", smId.get(Identifier.ID));
 +
assertEquals("smName", submodel.get(Referable.IDSHORT));
 +
assertEquals("prop1", prop1.get(Referable.IDSHORT));
 +
assertEquals(234, prop1.get(Property.VALUE)); // old value of prop1 has been stored locally
 +
assertEquals(456, changedProp1.get(Property.VALUE)); // new value is 456
 +
assertEquals("prop2", prop2.get(Referable.IDSHORT));
 +
assertEquals("prop11", prop11.get(Referable.IDSHORT));
 +
assertEquals(123, prop11.get(Property.VALUE));
 +
assertEquals("prop3", prop3.get(Referable.IDSHORT));
 +
assertEquals(17, prop3.get(Property.VALUE));
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
  
The complete, executable code is available in the basyx.examples project in package <<<>>>.
+
The complete, executable code is available in the basyx.examples project in package org.eclipse.basyx.examples.snippets.aas.submodels.
  
  

Latest revision as of 10:52, 18 August 2020

This code snippet illustrates the connection to an Asset Administration Shell (AAS) sub model using the Java SDK with Virtual Automation Bus (VAB) communication. In contrast to the ConnectedAssetAdministrationShellManager class, the virtual automation bus provides low-level access to VAB objects and therefore requires the user to know about AAS and sub model meta models. In particular, the property names for meta properties, values, and substructures must be explictly known. The VAB is therefore meant as implementation layer for implementing the ConnectedAssetAdministrationShellManager and for implementing gateways to legacy machines using native protocols that are not directly supported by the BaSyx SDK. The following code illustrates the use of VAB communication.

The BaSys setup for this code snippet is the following:

BaSyx.Snippet.AASConnectorConnection.Java.png


The BaSys setup consists of a Apache Tomcat server that runs BaSyx Servlets. It contains a sub model provider that exports an example sub model (see here for example). The snippet code runs in context of an application. The application contains the example code and a precompiled BaSyx directory that is used for resolving AAS and sub model IDs to network addresses. The application code accesses the AAS sub model:

// Retrieve sub model (created by factory) with SDK connector
// - Connect to sub model using lower-level VAB interface
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");
Map<String, Object> submodel = (Map<String, Object>) connSubModel1.getModelPropertyValue("");
Map<String, Object> smId = (Map<String, Object>) submodel.get(Identifiable.IDENTIFICATION);
 
// - Read properties
Map<String, Object> prop1 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
Map<String, Object> prop2 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop2");
Map<String, Object> prop11 = (Map<String, Object>) connSubModel1.getModelPropertyValue("submodelElements/prop2/prop11");
Map<String, Object> prop3 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop3");
 
// - Change property value using VAB primitive
connSubModel1.setModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1/value", 456);
 
// - Read value back using VAB primitive
Map<String, Object> changedProp1 = (Map<String, Object>) connSubModel1
				.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");


The snippet first creates a VABElementProxy that connects to the sub model as VAB object. The connection manager uses HTTP REST communication, as illustrated in <<<>>>. The following code connects to the sub model with ID "sm-001VAB":

// Connect to sub model using lower-level VAB interface
VABElementProxy connSubModel1 = this.connManager.connectToVABElement("sm-001VAB");


The following code accesses the values of properties "prop1" and "prop3" of the connected sub model compares their values to expected values.

// - Retrieve sub model values and compare to expected values
assertTrue((int) connSubModel1.readElementValue("properties/prop1/value") == 234);
assertTrue((int) connSubModel1.readElementValue("properties/prop3/value") == 17);


The following code accesses the sub model properties "prop1", "prop2", "prop11" and "prop3" from the connected sub-model.

// - Read properties
Map<String, Object> prop1 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");
Map<String, Object> prop2 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop2");
Map<String, Object> prop11 = (Map<String, Object>) connSubModel1.getModelPropertyValue("submodelElements/prop2/prop11");
Map<String, Object> prop3 = (Map<String, Object>) connSubModel1.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop3");

The following code change the value of "prop1" to 456 via the VAB, and verify the changed value by reading the property using VAB primative.

// - Change property value using VAB primitive
connSubModel1.setModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1/value", 456);
 
// - Read value back using VAB primitive
Map<String, Object> changedProp1 = (Map<String, Object>) connSubModel1
				.getModelPropertyValue(SubmodelElementProvider.PROPERTIES + "/prop1");


The following code verifies the id-short and the value of the accessed properties "prop1", "prop2", "prop11" and "prop3".

// - Check results
assertEquals("sm-001", smId.get(Identifier.ID));
assertEquals("smName", submodel.get(Referable.IDSHORT));
assertEquals("prop1", prop1.get(Referable.IDSHORT));
assertEquals(234, prop1.get(Property.VALUE)); // old value of prop1 has been stored locally
assertEquals(456, changedProp1.get(Property.VALUE)); // new value is 456
assertEquals("prop2", prop2.get(Referable.IDSHORT));
assertEquals("prop11", prop11.get(Referable.IDSHORT));
assertEquals(123, prop11.get(Property.VALUE));
assertEquals("prop3", prop3.get(Referable.IDSHORT));
assertEquals(17, prop3.get(Property.VALUE));


The complete, executable code is available in the basyx.examples project in package org.eclipse.basyx.examples.snippets.aas.submodels.


BaSyx project links: Project BaSyx main wiki page | What is BaSyx? | BaSyx Developer Documentation

Back to the top