Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

BaSyx / Introductory Examples / Java / Example 5

< BaSyx ‎ | Introductory Examples
Revision as of 10:05, 23 August 2021 by Rene-pascal.fischer.iese.fraunhofer.de (Talk | contribs) (Adds line numbers and expected output)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Example 5

In the last example, the AAS was set up and registered. Additionally, the content of the AAS and the registry was explored through the HTTP-Rest API. However, in a real application this exploration and data retrieval should not happen through manual calls but instead through interfaces encapsulating the access. This example shows how to use the provided interfaces and how to alternatively access the AAS directly through the VAB.

Example Code

Please note, that this example is explicitly referencing the code from Example 3 and Example 4.

First, to access the remote registry, a AASRegistryProxy is created. Next, similar to the VABConnectionManager from Example 2, an ConnectedAssetAdministrationShellManager is created using this registry proxy. Next, the AAS is retrieved through its URN from the connection manager. Last, the provided interfaces and classes are used for submodel, property and operation retrieval. Finally, the temperature value is retrieved and based on its unit either converted to Celsius or printed directly.

Additional to the access through the classes interfacing remote AAS and its entities, there's also the


  1. import java.util.Map;
  2.  
  3. import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager;
  4. import org.eclipse.basyx.aas.metamodel.connected.ConnectedAssetAdministrationShell;
  5. import org.eclipse.basyx.aas.metamodel.map.descriptor.ModelUrn;
  6. import org.eclipse.basyx.aas.registration.api.IAASRegistry;
  7. import org.eclipse.basyx.aas.registration.proxy.AASRegistryProxy;
  8. import org.eclipse.basyx.submodel.metamodel.api.ISubmodel;
  9. import org.eclipse.basyx.submodel.metamodel.api.submodelelement.dataelement.IProperty;
  10. import org.eclipse.basyx.vab.protocol.api.IConnectorFactory;
  11. import org.eclipse.basyx.vab.protocol.http.connector.HTTPConnectorFactory;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14.  
  15. /**
  16.  * Now, the result from the last HandsOn is used to set up the Asset Administration Shell.
  17.  * In this HandsOn, the connected site is demonstrated.
  18.  * 
  19.  * Given SDK components will be used to query the registry and access properties from the AAS.
  20.  * 
  21.  * Expected console output:
  22.  * - the temperature value in °C
  23.  */
  24. public class Scenario5 {
  25. 	// Initializes a logger for the output
  26. 	private static final Logger logger = LoggerFactory.getLogger(Scenario5.class);
  27.  
  28. 	public static void main(String[] args) throws Exception {
  29. 		// Create and provide the asset administration shell from the previous HandsOns
  30. 		Oven myOven = new Oven();
  31. 		Scenario3.startMyControlComponent(myOven);
  32. 		Scenario4.startMyAssetAdministrationShell(myOven);
  33.  
  34. 		// Return a AASHTTPRegistryProxy for the registry on localhost at port 4000
  35. 		IAASRegistry registry = new AASRegistryProxy("http://localhost:4000/handson/registry");
  36.  
  37. 		// Create a ConnectedAssetAdministrationShell using a ConnectedAssetAdministrationShellManager
  38. 		IConnectorFactory connectorFactory = new HTTPConnectorFactory();
  39. 		ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry,
  40. 				connectorFactory);
  41.  
  42. 		// The ID of the oven AAS
  43. 		ModelUrn aasURN = new ModelUrn("urn:org.eclipse.basyx:OvenAAS");
  44. 		ConnectedAssetAdministrationShell connectedAAS = manager.retrieveAAS(aasURN);
  45.  
  46. 		// Connect to the AAS and read the current temperature
  47. 		// Either Create a connected property using the connected facades
  48. 		Map<String, ISubmodel> submodels = connectedAAS.getSubmodels();
  49. 		ISubmodel connectedSensorSM = submodels.get("Sensor");
  50. 		Map<String, IProperty> properties = connectedSensorSM.getProperties();
  51. 		IProperty temperatureProperty = properties.get("currentTemperature");
  52. 		double temperature = (double) temperatureProperty.getValue();
  53. 		// Or get a VABElementProxy to directly query the VAB path of the property
  54. 		/*
  55. 		 * IModelProvider providerProxy = connectedAAS.getProxy();
  56. 		 * String temperatureValuePath = "/submodels/Sensor/submodelElements/currentTemperature/value";
  57. 		 * Map<String, Object> ret = (Map<String, Object>) providerProxy.getModelPropertyValue(temperatureValuePath);
  58. 		 * double temperature = (double) ret.get(Property.VALUE);
  59. 		 */
  60.  
  61. 		// Connect to the AAS and read the current temperature
  62. 		// Either use the connected variants:
  63. 		IProperty unitProperty = properties.get("temperatureUnit");
  64. 		String temperatureUnit = (String) unitProperty.getValue();
  65. 		// Or get a VABElementProxy to directly query the VAB path of the property
  66. 		/*
  67. 		 * String temperatureUnitPath =
  68. 		 * "/submodels/Sensor/submodelElements/temperatureUnit/value"; ret = (Map<String,
  69. 		 * Object>) providerProxy.getModelPropertyValue(temperatureUnitPath); String
  70. 		 * temperatureUnit = (String) ret.get(Property.VALUE);
  71. 		 */
  72.  
  73. 		// Now depending on the semantics of the temperature, calculate the value in °C
  74. 		// Usually, these semantics will be stored in a context dictionary that is referenced by the semantic attributes
  75. 		// of the property. But this HandsOn demonstrates a simplified scenario.
  76. 		if (temperatureUnit.equals("Fahrenheit")) {
  77. 			temperature = (temperature - 32.0d) * 5.0d / 9.0d;
  78. 		}
  79.  
  80. 		logger.info("The sensor temperature is " + temperature + "°C");
  81. 	}
  82. }


Expected Output

The output will consist of the info (maybe in a red text color) about the BaSyx Context and HTTP server (a Tomcat server) which has been started. Additionally it will display the current temperature in °C.

...
16:03:32.671 [main] INFO  i.Scenario5 - The sensor temperature is 20.0°C

Back to the top