Notice: This Wiki is now read only and edits are no longer 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 2b"
(Created page with "= Example 2b = This is a variant of Example 2a. Instead of directly connecting to the the model via TCP, a HTTP/REST to...") |
m (→Remote) |
||
Line 22: | Line 22: | ||
// Initializes a logger for the output | // Initializes a logger for the output | ||
private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class); | private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class); | ||
− | + | ||
public static void main(String[] args) throws Exception { | public static void main(String[] args) throws Exception { | ||
// First, a local model is created that is wrapped by a model provider (see first HandsOn) | // First, a local model is created that is wrapped by a model provider (see first HandsOn) | ||
Line 28: | Line 28: | ||
IModelProvider modelProvider = new VABLambdaProvider(model); | IModelProvider modelProvider = new VABLambdaProvider(model); | ||
// Up to this point, everything is known from the previous HandsOn | // Up to this point, everything is known from the previous HandsOn | ||
− | + | ||
// Now, the model provider is given to a HTTP servlet that gives access to the | // Now, the model provider is given to a HTTP servlet that gives access to the | ||
// model in the next steps | // model in the next steps | ||
// => The model will be published using an HTTP-REST interface | // => The model will be published using an HTTP-REST interface | ||
− | HttpServlet gatewayServlet = new VABHTTPInterface<IModelProvider>(new DelegatingModelProvider(new | + | HttpServlet gatewayServlet = new VABHTTPInterface<IModelProvider>(new DelegatingModelProvider(new BaSyxConnectorFactory())); |
logger.info("Created a servlet for the gateway"); | logger.info("Created a servlet for the gateway"); | ||
− | + | ||
− | // Second, create a | + | // Second, create a registry that can store endpoints for VAB models |
− | + | IVABRegistryService registry = new VABInMemoryRegistry(); | |
− | IModelProvider | + | IModelProvider registryProvider = new VABRegistryModelProvider(registry); |
− | HttpServlet directoryServlet = new VABHTTPInterface<IModelProvider>( | + | HttpServlet directoryServlet = new VABHTTPInterface<IModelProvider>(registryProvider); |
logger.info("Created a servlet for the directory"); | logger.info("Created a servlet for the directory"); | ||
− | + | ||
// Now, define a context to which multiple servlets can be added | // Now, define a context to which multiple servlets can be added | ||
BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001); | BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001); | ||
Line 48: | Line 48: | ||
context.addServletMapping("/directory/*", directoryServlet); | context.addServletMapping("/directory/*", directoryServlet); | ||
// The directory will be available at http://localhost:4001/handson/directory/ | // The directory will be available at http://localhost:4001/handson/directory/ | ||
− | + | BaSyxHTTPServer server = new BaSyxHTTPServer(context); | |
server.start(); | server.start(); | ||
− | + | ||
// Creates a tcp server providing the oven model on port 7000 | // Creates a tcp server providing the oven model on port 7000 | ||
BaSyxTCPServer<IModelProvider> tcpServer = new BaSyxTCPServer<>(modelProvider, 6999); | BaSyxTCPServer<IModelProvider> tcpServer = new BaSyxTCPServer<>(modelProvider, 6999); | ||
tcpServer.start(); | tcpServer.start(); | ||
− | + | ||
− | + | ||
// Register the VAB model at the directory (locally in this case) | // Register the VAB model at the directory (locally in this case) | ||
− | + | registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999"); | |
logger.info("Oven model registered!"); | logger.info("Oven model registered!"); | ||
− | + | ||
logger.info("Server started"); | logger.info("Server started"); | ||
} | } |
Revision as of 20:24, 8 March 2021
Example 2b
This is a variant of Example 2a. Instead of directly connecting to the the model via TCP, a HTTP/REST to TCP Gateway is used. Thus, access to models only available via TCP are made possible for web based apps, e.g. a browser.
Example Code
There's little change necessary to the already existing code. The local code can be reused from Example 2. Only the remote side has to change to start the Gateway.
Remote
In this example variant, the remote code is very similar to that of variant Example 2a. Additionally to the existing TCP server, the Gateway is started.
Due to the usage of the Gateway, the path to the oven model has to be changed.
/** * Expected console output in this HandsOn: * - the heater id * - oven is activated and deactivated multiple times * - temperature values between 30 and 40 */ public class Scenario2Gateway { // Initializes a logger for the output private static final Logger logger = LoggerFactory.getLogger(Scenario2Gateway.class); public static void main(String[] args) throws Exception { // First, a local model is created that is wrapped by a model provider (see first HandsOn) Map<String, Object> model = Scenario1.createMyOvenModel(new Oven()); IModelProvider modelProvider = new VABLambdaProvider(model); // Up to this point, everything is known from the previous HandsOn // Now, the model provider is given to a HTTP servlet that gives access to the // model in the next steps // => The model will be published using an HTTP-REST interface HttpServlet gatewayServlet = new VABHTTPInterface<IModelProvider>(new DelegatingModelProvider(new BaSyxConnectorFactory())); logger.info("Created a servlet for the gateway"); // Second, create a registry that can store endpoints for VAB models IVABRegistryService registry = new VABInMemoryRegistry(); IModelProvider registryProvider = new VABRegistryModelProvider(registry); HttpServlet directoryServlet = new VABHTTPInterface<IModelProvider>(registryProvider); logger.info("Created a servlet for the directory"); // Now, define a context to which multiple servlets can be added BaSyxContext context = new BaSyxContext("/handson", "", "localhost", 4001); // => Every servlet contained in this context is available at http://localhost:4001/handson/ context.addServletMapping("/gateway/*", gatewayServlet); // The model will be available at http://localhost:4001/handson/oven/ context.addServletMapping("/directory/*", directoryServlet); // The directory will be available at http://localhost:4001/handson/directory/ BaSyxHTTPServer server = new BaSyxHTTPServer(context); server.start(); // Creates a tcp server providing the oven model on port 7000 BaSyxTCPServer<IModelProvider> tcpServer = new BaSyxTCPServer<>(modelProvider, 6999); tcpServer.start(); // Register the VAB model at the directory (locally in this case) registry.addMapping("oven", "http://localhost:4001/handson/gateway//basyx://127.0.0.1:6999"); logger.info("Oven model registered!"); logger.info("Server started"); } }
Local
As previously stated, the code for local from Example 2 can be reused.