blob: e32c7671edd74ca0c917c468df9e5c7b8312fcdd [file] [log] [blame]
package org.eclipse.basyx.examples.snippets.undoc.aas.embedhttp;
import static org.junit.Assert.assertTrue;
import org.eclipse.basyx.aas.api.resources.ISingleProperty;
import org.eclipse.basyx.aas.api.resources.ISubModel;
import org.eclipse.basyx.aas.backend.connected.ConnectedAssetAdministrationShellManager;
import org.eclipse.basyx.aas.backend.connector.http.HTTPConnectorProvider;
import org.eclipse.basyx.aas.metamodel.factory.MetaModelElementFactory;
import org.eclipse.basyx.aas.metamodel.hashmap.aas.SubModel;
import org.eclipse.basyx.aas.metamodel.hashmap.aas.submodelelement.property.Property;
import org.eclipse.basyx.examples.support.directory.ExamplesPreconfiguredDirectory;
import org.eclipse.basyx.vab.core.VABConnectionManager;
import org.eclipse.basyx.vab.core.proxy.VABElementProxy;
import org.eclipse.basyx.vab.provider.lambda.VABLambdaProvider;
import org.junit.Test;
/**
* Illustrate AAS hosted by embedded HTTP server
*
* This example illustrates hosting of an AAS using the Embedded Webserver component. Even if this component is
* in com.sun namespace, it is tagged jdoExport and therefore part of the official JDK API.
*
* @author kuhn
*
*/
public class EmbeddedHTTP {
/**
* Status sub model ID
*/
private static final String STATUS_SM = "de.FHG:devices.es.iese:statusSM:1.0:3:x-509#003";
/**
* VAB connection manager backend
*/
protected VABConnectionManager connManager = new VABConnectionManager(
new ExamplesPreconfiguredDirectory()
// Add example specific mappings
.addMapping(STATUS_SM, "http://localhost:8000/BaSys/1.0/embedHTTP/"),
new HTTPConnectorProvider());
/**
* Example sub model. This example sub model is created with the BaSyx SDK factory and defines the AAS meta model properties
*/
static class SampleSubModel extends SubModel {
/**
* Version number of serialized instance
*/
private static final long serialVersionUID = 1L;
/**
* Constructor - create sub model
*
* This sub model contains static properties, i.e. properties that have a static value assigned.
*/
public SampleSubModel() {
// Create factory that helps with property creation
// - This factory creates sub model properties and ensures presence of all meta data
MetaModelElementFactory fac = new MetaModelElementFactory();
// Set sub model ID
setId(STATUS_SM);
// Create sub model properties
getProperties().put(fac.create(new Property(), 7, "prop1"));
getProperties().put(fac.create(new Property(), "myStr", "prop2"));
}
}
/**
* Create sub model
*/
public void createSubModel() {
// Server connections
// - Connect to sub model. Connecting to a sub model by its ID is discouraged, because a sub
// model ID is not necessarily unique outside the scope of its AAS. If users want to connect
// directly to sub models, the registry needs to support this, and unique identifies (as here)
// must be used. For portability, users should connect to sub models instead via an AAS ID and
// sub model ID tuple, as illustrated in the registry examples.
VABElementProxy connSubModel1 = this.connManager.connectToVABElement(STATUS_SM);
// Instantiate sub model
SubModel submodel = new SampleSubModel();
// Transfer sub model to server
connSubModel1.createElement("aas/submodels/" + STATUS_SM, submodel);
}
/**
* Test CRUD AAS access pattern
*
* - Retrieve sub model with SDK connector
*/
public void testCRUDAAS() throws Exception {
// Create and connect SDK connector
ConnectedAssetAdministrationShellManager manager = new ConnectedAssetAdministrationShellManager(connManager);
// - Retrieve sub model
ISubModel subModel = manager.retrieveSM(STATUS_SM);
// Read sub model properties
String smId = subModel.getId();
String prop1Id = subModel.getProperties().get("prop1").getId();
int prop1Val = (int) ((ISingleProperty) subModel.getProperties().get("prop1")).get();
String prop2Id = subModel.getProperties().get("prop2").getId();
String prop2Val = (String) ((ISingleProperty) subModel.getProperties().get("prop2")).get();
// Compare sub model property values
assertTrue(smId.equals(STATUS_SM));
assertTrue(prop1Id.equals("prop1"));
assertTrue(prop1Val == 7);
assertTrue(prop2Id.equals("prop2"));
assertTrue(prop2Val.equals("myStr"));
}
/**
* Test AAS Invoke
*/
/**
* Main function
*/
@Test
public void runTest() throws Exception {
// Create and start embedded HTTP server
EmbeddedHTTPServer server = new EmbeddedHTTPSubModelServer("/BaSys/1.0/embedHTTP", new VABLambdaProvider(new SampleSubModel()));
// - Start server
server.start();
// Connect to server, create sub model, and test access to sub model
createSubModel();
testCRUDAAS();
// Stop HTTP server
server.stop();
}
}