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 / Step 4"

m (Removes invalid usage of VABRegistry)
m (Adds line numbers)
 
Line 9: Line 9:
  
  
<syntaxhighlight lang="java" style="margin-left: 4em">
+
<syntaxhighlight lang="java" style="margin-left: 4em" line>
 
/**
 
/**
 
  * Expected console output in this HandsOn:
 
  * Expected console output in this HandsOn:

Latest revision as of 08:55, 23 August 2021

Step 4: Providing the Submodel in the network via HTTP

In this step, the code of the previous example will be extended to provide the created Submodel through its HTTP/REST interface. Submodels for the same AAS can be created and hosted on different locations in the network. For example, the AAS may be hosted in the central IT infrastructure to ensure its availability. Submodels may be hosted in the infrastructure as well, or on the device. Static information, e.g. a digital nameplate is often hosted in the IT infrastructure as well. Submodels with dynamic data may be hosted closer to the process to prevent a high network load.


Example Code

The Submodel that has been created in the previous step is made available on a HTTP REST interface by the means of the VAB. This is done by wrapping the provider in a Servlet and providing it via e.g. an Apache Tomcat server. To enable clients to connect to this model, it is registered at the registry.


  1. /**
  2.  * Expected console output in this HandsOn:
  3.  * - the heater id
  4.  * - oven is activated and deactivated multiple times
  5.  * - temperature values between 30 and 40
  6.  */
  7. public class SubModelProvider {
  8. 	// Initializes a logger for the output
  9. 	private static final Logger logger = LoggerFactory.getLogger(SubModelProvider.class);
  10.  
  11. 	public static void main(String[] args) throws Exception {
  12. 		// First, a local model is created that is wrapped by a model provider (see previous step 3)
  13. 		Submodel ovenModel = createMyOvenModel(new Oven());
  14. 		// Now wrap the model in a SubmodelProvider
  15. 		IModelProvider modelProvider = new SubmodelProvider(ovenModel);
  16. 		// Up to this point, everything is known from the previous step example
  17.  
  18.  
  19. 		// Now, create the servlet that will provide the http/REST interface for accessing the oven Submodel
  20. 		// => Every servlet that is provided by this node is available at http://localhost:4001/handson/
  21. 		BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
  22.  
  23. 		// Now, the model provider is attached to a HTTP servlet that enables access to the model in the next steps through a HTTP rest interface
  24. 		// => The model will be published using an HTTP-REST interface
  25. 		HttpServlet modelServlet = new VABHTTPInterface<IModelProvider>(modelProvider);
  26. 		logger.info("Created a servlet for the oven model");
  27.  
  28. 		// The provider will be available at http://localhost:4001/handson/oven/
  29.                 // And submodel can be accessed at: http://localhost:4001/handson/oven/submodel
  30.    		context.addServletMapping("/oven/*", modelServlet);
  31.  
  32. 		// Start the local HTTP server
  33. 		BaSyxHTTPServer server = new BaSyxHTTPServer(context);
  34. 		server.start();
  35. 		logger.info("HTTP server started");
  36. 	}
  37. }

Copyright © Eclipse Foundation, Inc. All Rights Reserved.