Search in sources :

Example 1 with ObserveRequest

use of org.eclipse.leshan.core.request.ObserveRequest in project leshan by eclipse.

the class ObjectResource method handleGET.

@Override
public void handleGET(CoapExchange exchange) {
    ServerIdentity identity = extractServerIdentity(exchange, bootstrapHandler);
    String URI = exchange.getRequestOptions().getUriPathString();
    // Manage Discover Request
    if (exchange.getRequestOptions().getAccept() == MediaTypeRegistry.APPLICATION_LINK_FORMAT) {
        DiscoverResponse response = nodeEnabler.discover(identity, new DiscoverRequest(URI));
        if (response.getCode().isError()) {
            exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
        } else {
            exchange.respond(toCoapResponseCode(response.getCode()), Link.serialize(response.getObjectLinks()), MediaTypeRegistry.APPLICATION_LINK_FORMAT);
        }
    } else {
        // handle content format for Read and Observe Request
        // use TLV as default format
        ContentFormat format = ContentFormat.TLV;
        if (exchange.getRequestOptions().hasAccept()) {
            format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
            if (!encoder.isSupported(format)) {
                exchange.respond(ResponseCode.NOT_ACCEPTABLE);
                return;
            }
        }
        // Manage Observe Request
        if (exchange.getRequestOptions().hasObserve()) {
            ObserveResponse response = nodeEnabler.observe(identity, new ObserveRequest(URI));
            if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
                LwM2mPath path = new LwM2mPath(URI);
                LwM2mNode content = response.getContent();
                LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
                exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
                return;
            } else {
                exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                return;
            }
        } else // Manage Read Request
        {
            ReadResponse response = nodeEnabler.read(identity, new ReadRequest(URI));
            if (response.getCode() == org.eclipse.leshan.ResponseCode.CONTENT) {
                LwM2mPath path = new LwM2mPath(URI);
                LwM2mNode content = response.getContent();
                LwM2mModel model = new LwM2mModel(nodeEnabler.getObjectModel());
                exchange.respond(ResponseCode.CONTENT, encoder.encode(content, format, path, model), format.getCode());
                return;
            } else {
                exchange.respond(toCoapResponseCode(response.getCode()), response.getErrorMessage());
                return;
            }
        }
    }
}
Also used : ResourceUtil.extractServerIdentity(org.eclipse.leshan.client.californium.impl.ResourceUtil.extractServerIdentity) ServerIdentity(org.eclipse.leshan.client.request.ServerIdentity) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) ContentFormat(org.eclipse.leshan.core.request.ContentFormat) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) DiscoverResponse(org.eclipse.leshan.core.response.DiscoverResponse) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 2 with ObserveRequest

use of org.eclipse.leshan.core.request.ObserveRequest in project leshan by eclipse.

the class CoapRequestBuilderTest method build_observe_request.

@Test
public void build_observe_request() throws Exception {
    Registration reg = newRegistration();
    // test
    CoapRequestBuilder builder = new CoapRequestBuilder(reg.getIdentity(), reg.getRootPath(), reg.getId(), reg.getEndpoint(), model, encoder);
    ObserveRequest request = new ObserveRequest(12, 0);
    builder.visit(request);
    // verify
    Request coapRequest = builder.getRequest();
    assertEquals(CoAP.Code.GET, coapRequest.getCode());
    assertEquals(0, coapRequest.getOptions().getObserve().intValue());
    assertEquals("127.0.0.1", coapRequest.getDestinationContext().getPeerAddress().getAddress().getHostAddress());
    assertEquals(12354, coapRequest.getDestinationContext().getPeerAddress().getPort());
    assertEquals("coap://127.0.0.1:12354/12/0", coapRequest.getURI());
}
Also used : Registration(org.eclipse.leshan.server.registration.Registration) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) Request(org.eclipse.californium.core.coap.Request) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) Test(org.junit.Test)

Example 3 with ObserveRequest

use of org.eclipse.leshan.core.request.ObserveRequest in project leshan by eclipse.

the class ObserveTest method can_observe_resource.

@Test
public void can_observe_resource() 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));
    // write device timezone
    LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
    // verify result
    listener.waitForNotification(2000);
    assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
    assertTrue(listener.receivedNotify().get());
    assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"), listener.getResponse().getContent());
    assertNotNull(listener.getResponse().getCoapResponse());
    assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Observation(org.eclipse.leshan.core.observation.Observation) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) Test(org.junit.Test)

Example 4 with ObserveRequest

use of org.eclipse.leshan.core.request.ObserveRequest 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 5 with ObserveRequest

use of org.eclipse.leshan.core.request.ObserveRequest in project leshan by eclipse.

the class ObserveTest method can_observe_object.

@Test
public void can_observe_object() throws InterruptedException {
    TestObservationListener listener = new TestObservationListener();
    helper.server.getObservationService().addListener(listener);
    // observe device timezone
    ObserveResponse observeResponse = helper.server.send(helper.getCurrentRegistration(), new ObserveRequest(3));
    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", 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));
    // write device timezone
    LwM2mResponse writeResponse = helper.server.send(helper.getCurrentRegistration(), new WriteRequest(3, 0, 15, "Europe/Paris"));
    // verify result
    listener.waitForNotification(2000);
    assertEquals(ResponseCode.CHANGED, writeResponse.getCode());
    assertTrue(listener.receivedNotify().get());
    assertTrue(listener.getResponse().getContent() instanceof LwM2mObject);
    assertNotNull(listener.getResponse().getCoapResponse());
    assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
    // try to read the object for comparing
    ReadResponse readResp = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
    assertEquals(readResp.getContent(), listener.getResponse().getContent());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResponse(org.eclipse.leshan.core.response.LwM2mResponse) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Aggregations

ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)15 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)10 Test (org.junit.Test)10 Observation (org.eclipse.leshan.core.observation.Observation)9 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)7 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)6 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)6 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)5 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)5 Response (org.eclipse.californium.core.coap.Response)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)4 DiscoverRequest (org.eclipse.leshan.core.request.DiscoverRequest)4 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)4 Registration (org.eclipse.leshan.server.registration.Registration)4 JsonObject (com.eclipsesource.json.JsonObject)3 ArrayList (java.util.ArrayList)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3