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 6

< BaSyx ‎ | Introductory Examples
Revision as of 10:11, 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 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

  1. import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager;
  2. import org.eclipse.basyx.aas.metamodel.api.parts.asset.AssetKind;
  3. import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell;
  4. import org.eclipse.basyx.aas.metamodel.map.descriptor.CustomId;
  5. import org.eclipse.basyx.aas.metamodel.map.parts.Asset;
  6. import org.eclipse.basyx.aas.registration.proxy.AASRegistryProxy;
  7. import org.eclipse.basyx.components.aas.AASServerComponent;
  8. import org.eclipse.basyx.components.aas.configuration.AASServerBackend;
  9. import org.eclipse.basyx.components.aas.configuration.BaSyxAASServerConfiguration;
  10. import org.eclipse.basyx.components.configuration.BaSyxContextConfiguration;
  11. import org.eclipse.basyx.components.registry.RegistryComponent;
  12. import org.eclipse.basyx.components.registry.configuration.BaSyxRegistryConfiguration;
  13. import org.eclipse.basyx.components.registry.configuration.RegistryBackend;
  14. import org.eclipse.basyx.submodel.metamodel.api.identifier.IIdentifier;
  15. import org.eclipse.basyx.submodel.metamodel.map.Submodel;
  16. import org.eclipse.basyx.submodel.metamodel.map.submodelelement.dataelement.property.Property;
  17.  
  18. public class Server {
  19. 	// Server URLs
  20. 	public static final String REGISTRYPATH = "http://localhost:4000/registry";
  21. 	public static final String AASSERVERPATH = "http://localhost:4001/aasServer";
  22.  
  23. 	// AAS/Submodel/Property Ids
  24. 	public static final IIdentifier OVENAASID = new CustomId("eclipse.basyx.aas.oven");
  25. 	public static final IIdentifier DOCUSMID = new CustomId("eclipse.basyx.submodel.documentation");
  26. 	public static final String MAXTEMPID = "maxTemp";
  27.  
  28. 	public static void main(String[] args) {
  29. 		// Create Infrastructure
  30. 		startRegistry();
  31. 		startAASServer();
  32.  
  33. 		// Create Manager - This manager is used to interact with an AAS server
  34. 		ConnectedAssetAdministrationShellManager manager = 
  35. 				new ConnectedAssetAdministrationShellManager(new AASRegistryProxy(REGISTRYPATH));
  36.  
  37. 		// Create AAS and push it to server
  38. 		Asset asset = new Asset("ovenAsset", new CustomId("eclipse.basyx.asset.oven"), AssetKind.INSTANCE);
  39. 		AssetAdministrationShell shell = new AssetAdministrationShell("oven", OVENAASID, asset);
  40.  
  41. 		// The manager uploads the AAS and registers it in the Registry server
  42. 		manager.createAAS(shell, AASSERVERPATH);
  43.  
  44. 		// Create submodel
  45. 		SubModel documentationSubmodel = new SubModel("documentationSm", DOCUSMID);
  46.  
  47. 		// - Create property
  48. 		Property maxTemp = new Property(MAXTEMPID, 1000);
  49.  
  50. 		// Add the property to the Submodel
  51. 		documentationSubmodel.addSubModelElement(maxTemp);
  52.  
  53. 		// - Push the Submodel to the AAS server
  54. 		manager.createSubModel(shell.getIdentification(), documentationSubmodel);
  55. 	}
  56.  
  57. 	/**
  58. 	 * Starts an empty registry at "http://localhost:4000"
  59. 	 */
  60. 	private static void startRegistry() {
  61. 		BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration(4000, "/registry");
  62. 		BaSyxRegistryConfiguration registryConfig = new BaSyxRegistryConfiguration(RegistryBackend.INMEMORY);
  63. 		RegistryComponent registry = new RegistryComponent(contextConfig, registryConfig);
  64.  
  65. 		// Start the created server
  66. 		registry.startComponent();
  67. 	}
  68.  
  69. 	/**
  70. 	 * Startup an empty server at "http://localhost:4001/"
  71. 	 */
  72. 	private static void startAASServer() {
  73. 		BaSyxContextConfiguration contextConfig = new BaSyxContextConfiguration(4001, "/aasServer");
  74. 		BaSyxAASServerConfiguration aasServerConfig = new BaSyxAASServerConfiguration(AASServerBackend.INMEMORY, "", REGISTRYPATH);
  75. 		AASServerComponent aasServer = new AASServerComponent(contextConfig, aasServerConfig);
  76.  
  77. 		// Start the created server
  78. 		aasServer.startComponent();
  79. 	}
  80. }



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.

Expected Output

First there will be some info about the registry which will be an InMemoryRegistry, followed by info (maybe in a red text color) about the BaSyx Context and HTTP server (a Tomcat server) which has been started. In between this output there will be information about the registry server and the AASServerComponent. If the server started correctly the last line will be AASRegistryProxy - AAS with Id eclipse.basyx.aas.oven created.

16:05:54.907 [main] INFO  o.e.b.c.r.RegistryComponent - Loading InMemoryRegistry
...
16:05:55.411 [main] INFO  o.e.b.c.r.RegistryComponent - Registry server started
16:05:55.416 [main] INFO  o.e.b.c.a.AASServerComponent - Create the server...
16:05:55.442 [main] INFO  o.e.b.c.a.AASServerComponent - Registry loaded at "http://localhost:4000/registry"
16:05:55.442 [main] INFO  o.e.b.c.a.AASServerComponent - Using InMemory backend
16:05:55.444 [main] INFO  o.e.b.c.a.AASServerComponent - Start the server
...
16:05:55.819 [main] INFO  o.e.b.a.r.p.AASRegistryProxy - AAS with Id eclipse.basyx.aas.oven created

Client Class

  1. import org.eclipse.basyx.aas.manager.ConnectedAssetAdministrationShellManager;
  2. import org.eclipse.basyx.aas.registration.proxy.AASRegistryProxy;
  3. import org.eclipse.basyx.submodel.metamodel.api.ISubmodel;
  4. import org.eclipse.basyx.submodel.metamodel.api.submodelelement.ISubmodelElement;
  5.  
  6. public class Client {
  7. 	public static void main(String[] args) {
  8. 		// Create Manager
  9. 		ConnectedAssetAdministrationShellManager manager =
  10. 				new ConnectedAssetAdministrationShellManager(new AASRegistryProxy(Server.REGISTRYPATH));
  11.  
  12. 		// Retrieve submodel
  13. 		ISubModel submodel = manager.retrieveSubModel(Server.OVENAASID, Server.DOCUSMID);
  14.  
  15. 		// Retrieve MaxTemp Property
  16. 		ISubmodelElement maxTemp = submodel.getSubmodelElement(Server.MAXTEMPID);
  17.  
  18. 		// Print value
  19. 		System.out.println(maxTemp.getIdShort() + " is " + maxTemp.getValue());
  20. 	}
  21. }



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.

Expected Output

If the client is successful in connecting to the server and retrieving the Submodel, its output will show the maxTemp value.

maxTemp is 1000

Copyright © Eclipse Foundation, Inc. All Rights Reserved.