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

< BaSyx ‎ | Introductory Examples
Revision as of 05:12, 8 April 2021 by Thomas.kuhn.iese.fraunhofer.de (Talk | contribs) (Created page with "= Step 4: Registering the oven sub model, and providing the oven sub model in the network = In this step, the code of the previous example will be extended to provide the cre...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Step 4: Registering the oven sub model, and providing the oven sub model in the network

In this step, the code of the previous example will be extended to provide the created sub model through a http/REST interface. Sub models 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. Sub models 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. Sub models with dynamic data may be hosted closer to the process to prevent a high network load.

In this example, the AAS of the oven will be hosted on the AAS server in the IT infrastructure, and the oven sub model will be hosted on the oven. The sub model will be registered at the VABRegistry to ensure that it can be found. Once the model is registered, it is accessible by its IT. If the model is re-deployed to a different location, it is sufficient to update the registry entry.


Example Code

The model 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.


/**
 * Expected console output in this HandsOn:
 * - the heater id
 * - oven is activated and deactivated multiple times
 * - temperature values between 30 and 40
 */
public class SubModelProvider {
	// The path to the registry (cf. step 2)
        // - URL and port needs to be adapted
	private static final string REGISTRYPATH = "http://localhost:8082/registry";
 
	// Initializes a logger for the output
	private static final Logger logger = LoggerFactory.getLogger(SubModelProvider.class);
 
	public static void main(String[] args) throws Exception {
		// First, a local model is created that is wrapped by a model provider (see previous step 3)
		Map<String, Object> model = createMyOvenModel(new Oven());
		// Now wrap the model in a VABLambdaProvider that enables the execution of Java code when accessing properties and operations
		IModelProvider modelProvider = new VABLambdaProvider(model);
		// Up to this point, everything is known from the previous step example
 
 
		// Now, create the servlet that will provide the http/REST interface for accessing the oven sub model
		// => Every servlet that is provided by this node is available at http://localhost:4001/handson/
		BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001);
 
		// 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
		// => The model will be published using an HTTP-REST interface
		HttpServlet modelServlet = new VABHTTPInterface<IModelProvider>(modelProvider);
		logger.info("Created a servlet for the oven model");
 
		// The model will be available at http://localhost:4001/handson/oven/
		context.addServletMapping("/oven/*", modelServlet);
 
		// Start the local HTTP server
		BaSyxHTTPServer server = new BaSyxHTTPServer(context);
		server.start();
		logger.info("HTTP server started");
 
 
                // Register the oven sub model at the registry
                AASRegistryProxy registry = new AASRegistryProxy(REGISTRYPATH)
		registry.addMapping("oven", "http://localhost:4001/handson/oven");
	}
}

Back to the top