Search in sources :

Example 1 with LeshanClient

use of org.eclipse.leshan.client.californium.LeshanClient 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)

Example 2 with LeshanClient

use of org.eclipse.leshan.client.californium.LeshanClient in project leshan by eclipse.

the class RegistrationTest method register_with_additional_attributes.

@Test
public void register_with_additional_attributes() throws InterruptedException {
    // Check registration
    helper.assertClientNotRegisterered();
    // HACK to be able to send a Registration request with additional attributes
    LeshanClient lclient = helper.client;
    lclient.getCoapServer().start();
    Endpoint secureEndpoint = lclient.getCoapServer().getEndpoint(lclient.getSecuredAddress());
    Endpoint nonSecureEndpoint = lclient.getCoapServer().getEndpoint(lclient.getUnsecuredAddress());
    CaliforniumLwM2mRequestSender sender = new CaliforniumLwM2mRequestSender(secureEndpoint, nonSecureEndpoint);
    // Create Request with additional attributes
    Map<String, String> additionalAttributes = new HashMap<>();
    additionalAttributes.put("key1", "value1");
    additionalAttributes.put("imei", "2136872368");
    Link[] objectLinks = Link.parse("</>;rt=\"oma.lwm2m\",</1/0>,</2>,</3/0>".getBytes());
    RegisterRequest registerRequest = new RegisterRequest(helper.getCurrentEndpoint(), null, null, null, null, objectLinks, additionalAttributes);
    // Send request
    RegisterResponse resp = sender.send(helper.server.getUnsecuredAddress(), false, registerRequest, 5000l);
    helper.waitForRegistration(1);
    // Check we are registered with the expected attributes
    helper.assertClientRegisterered();
    assertNotNull(helper.last_registration);
    assertEquals(additionalAttributes, helper.last_registration.getAdditionalRegistrationAttributes());
    assertArrayEquals(Link.parse("</>;rt=\"oma.lwm2m\",</1/0>,</2>,</3/0>".getBytes()), helper.getCurrentRegistration().getObjectLinks());
    sender.send(helper.server.getUnsecuredAddress(), false, new DeregisterRequest(resp.getRegistrationID()), 5000l);
    lclient.getCoapServer().stop();
}
Also used : CaliforniumLwM2mRequestSender(org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender) RegisterRequest(org.eclipse.leshan.core.request.RegisterRequest) RegisterResponse(org.eclipse.leshan.core.response.RegisterResponse) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) HashMap(java.util.HashMap) DeregisterRequest(org.eclipse.leshan.core.request.DeregisterRequest) Link(org.eclipse.leshan.Link) LeshanClient(org.eclipse.leshan.client.californium.LeshanClient) Test(org.junit.Test)

Aggregations

LeshanClient (org.eclipse.leshan.client.californium.LeshanClient)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Scanner (java.util.Scanner)1 CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)1 Endpoint (org.eclipse.californium.core.network.Endpoint)1 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)1 Link (org.eclipse.leshan.Link)1 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)1 CaliforniumLwM2mRequestSender (org.eclipse.leshan.client.californium.impl.CaliforniumLwM2mRequestSender)1 Server (org.eclipse.leshan.client.object.Server)1 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)1 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 DeregisterRequest (org.eclipse.leshan.core.request.DeregisterRequest)1 RegisterRequest (org.eclipse.leshan.core.request.RegisterRequest)1 RegisterResponse (org.eclipse.leshan.core.response.RegisterResponse)1 Test (org.junit.Test)1