Search in sources :

Example 1 with ObjectModel

use of org.eclipse.leshan.core.model.ObjectModel in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_explicit_complex_root_path.

@Test
public void encode_objectModel_to_linkObject_with_explicit_complex_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/r/t/");
    String strLinks = Link.serialize(links);
    assertEquals("</r/t/6>, </r/t/6/0/0>, </r/t/6/0/1>, </r/t/6/0/2>, </r/t/6/0/3>, </r/t/6/0/4>, </r/t/6/0/5>, </r/t/6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 2 with ObjectModel

use of org.eclipse.leshan.core.model.ObjectModel in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_simple_root_path.

@Test
public void encode_objectModel_to_linkObject_with_simple_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "rp");
    String strLinks = Link.serialize(links);
    assertEquals("</rp/6>, </rp/6/0/0>, </rp/6/0/1>, </rp/6/0/2>, </rp/6/0/3>, </rp/6/0/4>, </rp/6/0/5>, </rp/6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 3 with ObjectModel

use of org.eclipse.leshan.core.model.ObjectModel in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_empty_root_path.

@Test
public void encode_objectModel_to_linkObject_with_empty_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "");
    String strLinks = Link.serialize(links);
    assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 4 with ObjectModel

use of org.eclipse.leshan.core.model.ObjectModel in project leshan by eclipse.

the class LinkFormatHelperTest method encode_objectModel_to_linkObject_with_explicit_empty_root_path.

@Test
public void encode_objectModel_to_linkObject_with_explicit_empty_root_path() {
    ObjectModel locationModel = getObjectModel(6);
    Link[] links = LinkFormatHelper.getObjectDescription(locationModel, "/");
    String strLinks = Link.serialize(links);
    assertEquals("</6>, </6/0/0>, </6/0/1>, </6/0/2>, </6/0/3>, </6/0/4>, </6/0/5>, </6/0/6>", strLinks);
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Link(org.eclipse.leshan.Link) Test(org.junit.Test)

Example 5 with ObjectModel

use of org.eclipse.leshan.core.model.ObjectModel in project leshan by eclipse.

the class LeshanClientDemo method createAndStartClient.

public static void createAndStartClient(String endpoint, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, boolean needBootstrap, String serverURI, byte[] pskIdentity, byte[] pskKey, Float latitude, Float longitude, float scaleFactor) {
    locationInstance = new MyLocation(latitude, longitude, scaleFactor);
    // Initialize model
    List<ObjectModel> models = ObjectLoader.loadDefault();
    models.addAll(ObjectLoader.loadDdfResources("/models", modelPaths));
    // Initialize object list
    ObjectsInitializer initializer = new ObjectsInitializer(new LwM2mModel(models));
    if (needBootstrap) {
        if (pskIdentity == null)
            initializer.setInstancesForObject(SECURITY, noSecBootstap(serverURI));
        else
            initializer.setInstancesForObject(SECURITY, pskBootstrap(serverURI, pskIdentity, pskKey));
    } else {
        if (pskIdentity == null) {
            initializer.setInstancesForObject(SECURITY, noSec(serverURI, 123));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        } else {
            initializer.setInstancesForObject(SECURITY, psk(serverURI, 123, pskIdentity, pskKey));
            initializer.setInstancesForObject(SERVER, new Server(123, 30, BindingMode.U, false));
        }
    }
    initializer.setClassForObject(DEVICE, MyDevice.class);
    initializer.setInstancesForObject(LOCATION, locationInstance);
    initializer.setInstancesForObject(OBJECT_ID_TEMPERATURE_SENSOR, new RandomTemperatureSensor());
    List<LwM2mObjectEnabler> enablers = initializer.create(SECURITY, SERVER, DEVICE, LOCATION, OBJECT_ID_TEMPERATURE_SENSOR);
    // Create CoAP Config
    NetworkConfig coapConfig;
    File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
    if (configFile.isFile()) {
        coapConfig = new NetworkConfig();
        coapConfig.load(configFile);
    } else {
        coapConfig = LeshanClientBuilder.createDefaultNetworkConfig();
        coapConfig.store(configFile);
    }
    // Create client
    LeshanClientBuilder builder = new LeshanClientBuilder(endpoint);
    builder.setLocalAddress(localAddress, localPort);
    builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
    builder.setObjects(enablers);
    builder.setCoapConfig(coapConfig);
    // so we can disable the other one.
    if (!needBootstrap) {
        if (pskIdentity == null)
            builder.disableSecuredEndpoint();
        else
            builder.disableUnsecuredEndpoint();
    }
    final LeshanClient client = builder.build();
    LOG.info("Press 'w','a','s','d' to change reported Location ({},{}).", locationInstance.getLatitude(), locationInstance.getLongitude());
    // Start the client
    client.start();
    // De-register on shutdown and stop client.
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            // send de-registration request before destroy
            client.destroy(true);
        }
    });
    // Change the location through the Console
    try (Scanner scanner = new Scanner(System.in)) {
        while (scanner.hasNext()) {
            String nextMove = scanner.next();
            locationInstance.moveLocation(nextMove);
        }
    }
}
Also used : LwM2mObjectEnabler(org.eclipse.leshan.client.resource.LwM2mObjectEnabler) Scanner(java.util.Scanner) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) Server(org.eclipse.leshan.client.object.Server) ObjectsInitializer(org.eclipse.leshan.client.resource.ObjectsInitializer) LeshanClientBuilder(org.eclipse.leshan.client.californium.LeshanClientBuilder) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) File(java.io.File) LeshanClient(org.eclipse.leshan.client.californium.LeshanClient)

Aggregations

ObjectModel (org.eclipse.leshan.core.model.ObjectModel)16 Test (org.junit.Test)7 Link (org.eclipse.leshan.Link)6 File (java.io.File)4 ArrayList (java.util.ArrayList)4 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)3 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)3 URI (java.net.URI)2 Server (org.eclipse.jetty.server.Server)2 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)2 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)2 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)2 JsonArray (com.eclipsesource.json.JsonArray)1 JsonValue (com.eclipsesource.json.JsonValue)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigInteger (java.math.BigInteger)1 AlgorithmParameters (java.security.AlgorithmParameters)1