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 6"
m (Adds link to off-the-shelf components) |
m (Adds imports) |
||
Line 5: | Line 5: | ||
<syntaxhighlight lang="java" style="margin-left: 4em"> | <syntaxhighlight lang="java" style="margin-left: 4em"> | ||
+ | import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager; | ||
+ | import org.eclipse.basyx.aas.metamodel.api.parts.asset.AssetKind; | ||
+ | import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; | ||
+ | import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId; | ||
+ | import org.eclipse.basyx.aas.metamodel.map.parts.Asset; | ||
+ | import org.eclipse.basyx.aas.registration.proxy.AASRegistryProxy; | ||
+ | import org.eclipse.basyx.components.aas.AASServerComponent; | ||
+ | import org.eclipse.basyx.components.aas.configuration.AASServerBackend; | ||
+ | import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration; | ||
+ | import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration; | ||
+ | import org.eclipse.basyx.components.registry.RegistryComponent; | ||
+ | import org.eclipse.basyx.components.registry.configuration.BaSyxRegistryConfiguration; | ||
+ | import org.eclipse.basyx.components.registry.configuration.RegistryBackend; | ||
+ | import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; | ||
+ | import org.eclipse.basyx.submodel.metamodel.map.Submodel; | ||
+ | import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property; | ||
+ | |||
public class Server { | public class Server { | ||
// Server URLs | // Server URLs | ||
Line 73: | Line 90: | ||
<br/><br/>If the server started successfully, the last line of console output will be:<br/> | <br/><br/>If the server started successfully, the last line of console output will be:<br/> | ||
'''''AASRegistryProxy - AAS with Id eclipse.basyx.aas.oven created''''' | '''''AASRegistryProxy - AAS with Id eclipse.basyx.aas.oven created''''' | ||
− | |||
== Client Class == | == Client Class == |
Revision as of 08:16, 23 August 2021
Example 6 - Hello World
The "Hello World" example is a very basic client/server demonstration of BaSyx that utilizes the Off-the-shelf components. It can be found in the BaSyx repository in examples/basyx.hello_world. The "Hello World" project consists of two parts: Client and Server.
Server Class
import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager; import org.eclipse.basyx.aas.metamodel.api.parts.asset.AssetKind; import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId; import org.eclipse.basyx.aas.metamodel.map.parts.Asset; import org.eclipse.basyx.aas.registration.proxy.AASRegistryProxy; import org.eclipse.basyx.components.aas.AASServerComponent; import org.eclipse.basyx.components.aas.configuration.AASServerBackend; import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration; import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration; import org.eclipse.basyx.components.registry.RegistryComponent; import org.eclipse.basyx.components.registry.configuration.BaSyxRegistryConfiguration; import org.eclipse.basyx.components.registry.configuration.RegistryBackend; import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier; import org.eclipse.basyx.submodel.metamodel.map.Submodel; import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property; public class Server { // Server URLs public static final String REGISTRYPATH = "http://localhost:4000/registry"; public static final String AASSERVERPATH = "http://localhost:4001/aasServer"; // AAS/Submodel/Property Ids public static final IIdentifier OVENAASID = new CustomId("eclipse.basyx.aas.oven"); public static final IIdentifier DOCUSMID = new CustomId("eclipse.basyx.submodel.documentation"); public static final String MAXTEMPID = "maxTemp"; public static void main(String[] args) { // Create Infrastructure startRegistry(); startAASServer(); // Create Manager - This manager is used to interact with an AAS server ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(new AASRegistryProxy(REGISTRYPATH)); // Create AAS and push it to server Asset asset = new Asset("ovenAsset", new CustomId("eclipse.basyx.asset.oven"), AssetKind.INSTANCE); AssetAdministrationShell shell = new AssetAdministrationShell("oven", OVENAASID, asset); // The manager uploads the AAS and registers it in the Registry server manager.createAAS(shell, AASSERVERPATH); // Create submodel SubModel documentationSubmodel = new SubModel("documentationSm", DOCUSMID); // - Create property Property maxTemp = new Property(MAXTEMPID, 1000); // Add the property to the Submodel documentationSubmodel.addSubModelElement(maxTemp); // - Push the Submodel to the AAS server manager.createSubModel(shell.getIdentification(), documentationSubmodel); } /** * Starts an empty registry at "http://localhost:4000" */ private static void startRegistry() { BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration(4000, "/registry"); BaSyxRegistryConfiguration registryConfig = new BaSyxRegistryConfiguration(RegistryBackend.INMEMORY); RegistryComponent registry = new RegistryComponent(contextConfig, registryConfig); // Start the created server registry.startComponent(); } /** * Startup an empty server at "http://localhost:4001/" */ private static void startAASServer() { BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration(4001, "/aasServer"); BaSyxAASServerConfiguration aasServerConfig = new BaSyxAASServerConfiguration(AASServerBackend.INMEMORY, "", REGISTRYPATH); AASServerComponent aasServer = new AASServerComponent(contextConfig, aasServerConfig); // Start the created server aasServer.startComponent(); } }
This is the server class. Its main method starts an AAS server and a Registry server. The AAS server gets populated by an AAS and a Submodel, which contains the Property “maxTemp” with the value 1000.
If the server started successfully, the last line of console output will be:
AASRegistryProxy - AAS with Id eclipse.basyx.aas.oven created
Client Class
public class Client { public static void main(String[] args) { // Create Manager ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(new AASRegistryProxy(Server.REGISTRYPATH)); // Retrieve submodel ISubModel submodel = manager.retrieveSubModel(Server.OVENAASID, Server.DOCUSMID); // Retrieve MaxTemp Property ISubmodelElement maxTemp = submodel.getSubmodelElement(Server.MAXTEMPID); // Print value System.out.println(maxTemp.getIdShort() + " is " + maxTemp.getValue()); } }
This is the second part of the “Hello World” example, the client. It connects to the AAS server started in the first part of this project and retrieves the Submodel. Next, it prints the idShort of the contained Property and its value to the console.
If the client is successful in connecting to the server and retrieving the Submodel, the following output will be written to the console:
maxTemp is 1000