Search in sources :

Example 6 with LwM2mModel

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

the class CoapRequestBuilderTest method loadModel.

@BeforeClass
public static void loadModel() {
    model = new LwM2mModel(ObjectLoader.loadDefault());
    encoder = new DefaultLwM2mNodeEncoder();
}
Also used : DefaultLwM2mNodeEncoder(org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BeforeClass(org.junit.BeforeClass)

Example 7 with LwM2mModel

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

the class ObserveTest method can_handle_error_on_notification.

@Test
public void can_handle_error_on_notification() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3, 0, 15));
    assertEquals(ResponseCode.CONTENT, observeResponse.getCode());
    assertNotNull(observeResponse.getCoapResponse());
    assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class)));
    // an observation response should have been sent
    Observation observation = observeResponse.getObservation();
    assertEquals("/3/0/15", observation.getPath().toString());
    assertEquals(helper.getCurrentRegistration().getId(), observation.getRegistrationId());
    Set<Observation> observations = helper.server.getObservationService().getObservations(helper.getCurrentRegistration());
    assertTrue("We should have only on observation", observations.size() == 1);
    assertTrue("New observation is not there", observations.contains(observation));
    // *** HACK send a notification with unsupported content format *** //
    byte[] payload = LwM2mNodeJsonEncoder.encode(LwM2mSingleResource.newStringResource(15, "Paris"), new LwM2mPath("/3/0/15"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
    Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
    // 666 is not a supported //
    sendNotification(getConnector(helper.client), payload, firstCoapResponse, 666);
    // contentFormat.
    // *** Hack End *** //
    // verify result
    listener.waitForNotification(2000);
    assertTrue(listener.receivedNotify().get());
    assertNotNull(listener.getError());
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) DefaultLwM2mValueConverter(org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Test(org.junit.Test)

Example 8 with LwM2mModel

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

the class LwM2mNodeEncoderTest method loadModel.

@BeforeClass
public static void loadModel() {
    model = new LwM2mModel(ObjectLoader.loadDefault());
    encoder = new DefaultLwM2mNodeEncoder();
}
Also used : LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BeforeClass(org.junit.BeforeClass)

Example 9 with LwM2mModel

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

the class LwM2mNodeDecoderTest method loadModel.

@BeforeClass
public static void loadModel() {
    // load default object
    List<ObjectModel> objects = ObjectLoader.loadDefault();
    // add object 65 from the LWM2M v1.0.1 specification (figure 28)
    List<ResourceModel> resForObj65 = new ArrayList<>();
    resForObj65.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.OBJLNK, null, null, null));
    resForObj65.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
    resForObj65.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.INTEGER, null, null, null));
    objects.add(new ObjectModel(65, "object link tests 65", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj65));
    // add object 66 from the LWM2M v1.0.1 specification (figure 28)
    List<ResourceModel> resForObj66 = new ArrayList<>();
    resForObj66.add(new ResourceModel(0, "res0", Operations.R, true, false, Type.STRING, null, null, null));
    resForObj66.add(new ResourceModel(1, "res1", Operations.R, false, false, Type.STRING, null, null, null));
    resForObj66.add(new ResourceModel(2, "res2", Operations.R, false, false, Type.OBJLNK, null, null, null));
    objects.add(new ObjectModel(66, "object link tests 66", "", ObjectModel.DEFAULT_VERSION, true, false, resForObj66));
    model = new LwM2mModel(objects);
    decoder = new DefaultLwM2mNodeDecoder();
}
Also used : ObjectModel(org.eclipse.leshan.core.model.ObjectModel) ArrayList(java.util.ArrayList) ResourceModel(org.eclipse.leshan.core.model.ResourceModel) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BeforeClass(org.junit.BeforeClass)

Example 10 with LwM2mModel

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

the class CaliforniumLwM2mRequestSender method send.

@Override
public <T extends LwM2mResponse> void send(final Registration destination, final DownlinkRequest<T> request, long timeout, ResponseCallback<T> responseCallback, ErrorCallback errorCallback) {
    // Retrieve the objects definition
    final LwM2mModel model = modelProvider.getObjectModel(destination);
    // Create the CoAP request from LwM2m request
    CoapRequestBuilder coapRequestBuilder = new CoapRequestBuilder(destination.getIdentity(), destination.getRootPath(), destination.getId(), destination.getEndpoint(), model, encoder);
    request.accept(coapRequestBuilder);
    final Request coapRequest = coapRequestBuilder.getRequest();
    // Add CoAP request callback
    MessageObserver obs = new AsyncRequestObserver<T>(coapRequest, responseCallback, errorCallback, timeout) {

        @Override
        public T buildResponse(Response coapResponse) {
            // Build LwM2m response
            LwM2mResponseBuilder<T> lwm2mResponseBuilder = new LwM2mResponseBuilder<>(coapRequest, coapResponse, destination, model, observationService, decoder);
            request.accept(lwm2mResponseBuilder);
            return lwm2mResponseBuilder.getResponse();
        }
    };
    coapRequest.addMessageObserver(obs);
    // Store pending request to cancel it on de-registration
    addPendingRequest(destination.getId(), coapRequest);
    // Send CoAP request asynchronously
    Endpoint endpoint = getEndpointForClient(destination);
    endpoint.sendRequest(coapRequest);
}
Also used : Response(org.eclipse.californium.core.coap.Response) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) MessageObserver(org.eclipse.californium.core.coap.MessageObserver) Endpoint(org.eclipse.californium.core.network.Endpoint) Request(org.eclipse.californium.core.coap.Request) DownlinkRequest(org.eclipse.leshan.core.request.DownlinkRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) AsyncRequestObserver(org.eclipse.leshan.core.californium.AsyncRequestObserver)

Aggregations

LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)19 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)7 Response (org.eclipse.californium.core.coap.Response)6 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 Observation (org.eclipse.leshan.core.observation.Observation)5 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 ArrayList (java.util.ArrayList)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 LeshanClientBuilder (org.eclipse.leshan.client.californium.LeshanClientBuilder)3 ResourceUtil.extractServerIdentity (org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity)3 Server (org.eclipse.leshan.client.object.Server)3 ServerIdentity (org.eclipse.leshan.client.request.ServerIdentity)3 LwM2mObjectEnabler (org.eclipse.leshan.client.resource.LwM2mObjectEnabler)3 ObjectsInitializer (org.eclipse.leshan.client.resource.ObjectsInitializer)3 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3