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.
Difference between revisions of "BaSyx / Introductory Examples / Java / Example 5"
m (Replaces deprecated methods) |
m (→Example Code) |
||
Line 25: | Line 25: | ||
// Initializes a logger for the output | // Initializes a logger for the output | ||
private static final Logger logger = LoggerFactory.getLogger(Scenario5.class); | private static final Logger logger = LoggerFactory.getLogger(Scenario5.class); | ||
− | + | ||
public static void main(String[] args) throws Exception { | public static void main(String[] args) throws Exception { | ||
// Create and provide the asset administration shell from the previous HandsOns | // Create and provide the asset administration shell from the previous HandsOns | ||
Line 31: | Line 31: | ||
Scenario3.startMyControlComponent(myOven); | Scenario3.startMyControlComponent(myOven); | ||
Scenario4.startMyAssetAdministrationShell(myOven); | Scenario4.startMyAssetAdministrationShell(myOven); | ||
− | + | ||
// Return a AASHTTPRegistryProxy for the registry on localhost at port 4000 | // Return a AASHTTPRegistryProxy for the registry on localhost at port 4000 | ||
− | + | IAASRegistry registry = new AASRegistryProxy("http://localhost:4000/handson/registry"); | |
− | + | ||
// Create a ConnectedAssetAdministrationShell using a ConnectedAssetAdministrationShellManager | // Create a ConnectedAssetAdministrationShell using a ConnectedAssetAdministrationShellManager | ||
− | + | IConnectorFactory connectorFactory = new HTTPConnectorFactory(); | |
ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry, | ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry, | ||
− | + | connectorFactory); | |
− | + | ||
// The ID of the oven AAS | // The ID of the oven AAS | ||
ModelUrn aasURN = new ModelUrn("urn:org.eclipse.basyx:OvenAAS"); | ModelUrn aasURN = new ModelUrn("urn:org.eclipse.basyx:OvenAAS"); | ||
ConnectedAssetAdministrationShell connectedAAS = manager.retrieveAAS(aasURN); | ConnectedAssetAdministrationShell connectedAAS = manager.retrieveAAS(aasURN); | ||
− | + | ||
// Connect to the AAS and read the current temperature | // Connect to the AAS and read the current temperature | ||
// Either Create a connected property using the connected facades | // Either Create a connected property using the connected facades | ||
− | Map<String, | + | Map<String, ISubmodel> submodels = connectedAAS.getSubmodels(); |
− | + | ISubmodel connectedSensorSM = submodels.get("Sensor"); | |
Map<String, IProperty> properties = connectedSensorSM.getProperties(); | Map<String, IProperty> properties = connectedSensorSM.getProperties(); | ||
− | IProperty temperatureProperty = properties. | + | IProperty temperatureProperty = properties.get("currentTemperature"); |
− | double temperature = (double) temperatureProperty. | + | double temperature = (double) temperatureProperty.getValue(); |
// Or get a VABElementProxy to directly query the VAB path of the property | // Or get a VABElementProxy to directly query the VAB path of the property | ||
/* | /* | ||
Line 58: | Line 58: | ||
* double temperature = (double) ret.get(Property.VALUE); | * double temperature = (double) ret.get(Property.VALUE); | ||
*/ | */ | ||
− | + | ||
// Connect to the AAS and read the current temperature | // Connect to the AAS and read the current temperature | ||
// Either use the connected variants: | // Either use the connected variants: | ||
Line 70: | Line 70: | ||
* temperatureUnit = (String) ret.get(Property.VALUE); | * temperatureUnit = (String) ret.get(Property.VALUE); | ||
*/ | */ | ||
− | + | ||
// Now depending on the semantics of the temperature, calculate the value in °C | // 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 | // Usually, these semantics will be stored in a context dictionary that is referenced by the semantic attributes | ||
Line 77: | Line 77: | ||
temperature = (temperature - 32.0d) * 5.0d / 9.0d; | temperature = (temperature - 32.0d) * 5.0d / 9.0d; | ||
} | } | ||
− | + | ||
logger.info("The sensor temperature is " + temperature + "°C"); | logger.info("The sensor temperature is " + temperature + "°C"); | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 20:47, 8 March 2021
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 IAASRegistry registry = new AASRegistryProxy("http://localhost:4000/handson/registry"); // Create a ConnectedAssetAdministrationShell using a ConnectedAssetAdministrationShellManager IConnectorFactory connectorFactory = new HTTPConnectorFactory(); ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(registry, connectorFactory); // The ID of the oven AAS ModelUrn aasURN = new ModelUrn("urn:org.eclipse.basyx:OvenAAS"); 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.getValue(); // Or get a VABElementProxy to directly query the VAB path of the property /* * IModelProvider providerProxy = connectedAAS.getProxy(); * String temperatureValuePath = "/submodels/Sensor/submodelElements/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.getValue(); // Or get a VABElementProxy to directly query the VAB path of the property /* * String temperatureUnitPath = * "/submodels/Sensor/submodelElements/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"); } }