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

Difference between revisions of "BaSyx / Introductory Examples / Java / Example 5"

m (Updates example code with newest iteration of Submodel Interface)
m
Line 54: Line 54:
 
/*
 
/*
 
* IModelProvider providerProxy = connectedAAS.getProxy();
 
* IModelProvider providerProxy = connectedAAS.getProxy();
* String temperatureValuePath = "/submodels/Sensor/dataElements/currentTemperature/value";
+
* String temperatureValuePath = "/submodels/Sensor/properties/currentTemperature/value";
 
* Map<String, Object> ret = (Map<String, Object>) providerProxy.getModelPropertyValue(temperatureValuePath);
 
* Map<String, Object> ret = (Map<String, Object>) providerProxy.getModelPropertyValue(temperatureValuePath);
 
* double temperature = (double) ret.get(Property.VALUE);
 
* double temperature = (double) ret.get(Property.VALUE);
Line 66: Line 66:
 
/*
 
/*
 
* String temperatureUnitPath =
 
* String temperatureUnitPath =
* "/submodels/Sensor/dataElements/temperatureUnit/value"; ret = (Map<String,
+
* "/submodels/Sensor/properties/temperatureUnit/value"; ret = (Map<String,
 
* Object>) providerProxy.getModelPropertyValue(temperatureUnitPath); String
 
* Object>) providerProxy.getModelPropertyValue(temperatureUnitPath); String
 
* temperatureUnit = (String) ret.get(Property.VALUE);
 
* temperatureUnit = (String) ret.get(Property.VALUE);

Revision as of 02:46, 20 August 2020

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


/**
 * Now, the result from the last HandsOn is used to set up the Asset Administration Shell.
 * In this HandsOn, the connected site is demonstrated.
 * 
 * Given SDK components will be used to query the registry and access properties from the AAS.
 * 
 * Expected console output:
 * - the temperature value in °C
 */
public class Scenario5 {
	// Initializes a logger for the output
	private static final Logger logger = LoggerFactory.getLogger(Scenario5.class);
 
	public static void main(String[] args) throws Exception {
		// Create and provide the asset administration shell from the previous HandsOns
		Oven myOven = new Oven();
		Scenario3.startMyControlComponent(myOven);
		Scenario4.startMyAssetAdministrationShell(myOven);
 
		// Return a AASHTTPRegistryProxy for the registry on localhost at port 4000
		IAASRegistryService registry = new AASRegistryProxy("http://localhost:4000/handson/registry");
 
		// Create a ConnectedAssetAdministrationShell using a ConnectedAssetAdministrationShellManager
		IConnectorProvider connectorProvider = new HTTPConnectorProvider();
		ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry,
				connectorProvider);
 
		// The ID of the oven AAS
		ModelUrn aasURN = new ModelUrn("de.FHG", "devices.es.iese", "AAS", "1.0", "1", "oven01", "001");
		ConnectedAssetAdministrationShell connectedAAS = manager.retrieveAAS(aasURN);
 
		// Connect to the AAS and read the current temperature
		// Either Create a connected property using the connected facades
		Map<String, ISubModel> submodels = connectedAAS.getSubModels();
		ISubModel connectedSensorSM = submodels.get("Sensor");
		Map<String, IProperty> properties = connectedSensorSM.getProperties();
		IProperty temperatureProperty = properties.get("currentTemperature");
		double temperature = (double) temperatureProperty.get();
		// Or get a VABElementProxy to directly query the VAB path of the property
		/*
		 * IModelProvider providerProxy = connectedAAS.getProxy();
		 * String temperatureValuePath = "/submodels/Sensor/properties/currentTemperature/value";
		 * Map<String, Object> ret = (Map<String, Object>) providerProxy.getModelPropertyValue(temperatureValuePath);
		 * double temperature = (double) ret.get(Property.VALUE);
		 */
 
		// Connect to the AAS and read the current temperature
		// Either use the connected variants:
		IProperty unitProperty = properties.get("temperatureUnit");
		String temperatureUnit = (String) unitProperty.get();
		// Or get a VABElementProxy to directly query the VAB path of the property
		/*
		 * String temperatureUnitPath =
		 * "/submodels/Sensor/properties/temperatureUnit/value"; ret = (Map<String,
		 * Object>) providerProxy.getModelPropertyValue(temperatureUnitPath); String
		 * temperatureUnit = (String) ret.get(Property.VALUE);
		 */
 
		// Now depending on the semantics of the temperature, calculate the value in °C
		// Usually, these semantics will be stored in a context dictionary that is referenced by the semantic attributes
		// of the property. But this HandsOn demonstrates a simplified scenario.
		if (temperatureUnit.equals("Fahrenheit")) {
			temperature = (temperature - 32.0d) * 5.0d / 9.0d;
		}
 
		logger.info("The sensor temperature is " + temperature + "°C");
	}
}

Back to the top