Search in sources :

Example 1 with ObserveResponse

use of org.eclipse.leshan.core.response.ObserveResponse 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 ObserveResponse

use of org.eclipse.leshan.core.response.ObserveResponse in project leshan by eclipse.

the class ObservationServiceImpl method onNotification.

// ********** NotificationListener interface **********//
@Override
public void onNotification(Request coapRequest, Response coapResponse) {
    LOG.trace("notification received for request {}: {}", coapRequest, coapResponse);
    if (listeners.isEmpty())
        return;
    // get registration Id
    String regid = coapRequest.getUserContext().get(ObserveUtil.CTX_REGID);
    // get observation for this request
    Observation observation = registrationStore.getObservation(regid, coapResponse.getToken().getBytes());
    if (observation == null) {
        LOG.error("Unexpected error: Unable to find observation with token {} for registration {}", coapResponse.getToken(), regid);
        return;
    }
    // get registration
    Registration registration = registrationStore.getRegistration(observation.getRegistrationId());
    if (registration == null) {
        LOG.error("Unexpected error: There is no registration with id {} for this observation {}", observation.getRegistrationId(), observation);
        return;
    }
    try {
        // get model for this registration
        LwM2mModel model = modelProvider.getObjectModel(registration);
        // create response
        ObserveResponse response = createObserveResponse(observation, model, coapResponse);
        // notify all listeners
        for (ObservationListener listener : listeners) {
            listener.onResponse(observation, registration, response);
        }
    } catch (InvalidResponseException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Invalid notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    } catch (RuntimeException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(String.format("Unable to handle notification for observation [%s]", observation), e);
        }
        for (ObservationListener listener : listeners) {
            listener.onError(observation, registration, e);
        }
    }
}
Also used : ObservationListener(org.eclipse.leshan.server.observation.ObservationListener) Registration(org.eclipse.leshan.server.registration.Registration) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) InvalidResponseException(org.eclipse.leshan.core.request.exception.InvalidResponseException) ObserveResponse(org.eclipse.leshan.core.response.ObserveResponse)

Example 3 with ObserveResponse

use of org.eclipse.leshan.core.response.ObserveResponse 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 ObserveResponse

use of org.eclipse.leshan.core.response.ObserveResponse 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 ObserveResponse

use of org.eclipse.leshan.core.response.ObserveResponse 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

ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)14 Observation (org.eclipse.leshan.core.observation.Observation)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)9 Test (org.junit.Test)8 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)6 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)6 Response (org.eclipse.californium.core.coap.Response)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 JsonObject (com.eclipsesource.json.JsonObject)3 ArrayList (java.util.ArrayList)3 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)3 DiscoverResponse (org.eclipse.leshan.core.response.DiscoverResponse)3 ExecuteResponse (org.eclipse.leshan.core.response.ExecuteResponse)3