Search in sources :

Example 6 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class ObservationServiceImpl method cancelObservations.

@Override
public int cancelObservations(Registration registration) {
    // check registration id
    String registrationId = registration.getId();
    if (registrationId == null)
        return 0;
    Collection<Observation> observations = registrationStore.removeObservations(registrationId);
    if (observations == null)
        return 0;
    for (Observation observation : observations) {
        cancel(observation);
    }
    return observations.size();
}
Also used : Observation(org.eclipse.leshan.core.observation.Observation)

Example 7 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class ObservationServiceTest method cancel_by_path.

@Test
public void cancel_by_path() {
    // create some observations
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
    // check its presence
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(2, observations.size());
    // cancel it
    int nbCancelled = observationService.cancelObservations(support.registration, "/3/0/12");
    Assert.assertEquals(1, nbCancelled);
    // check its absence
    observations = observationService.getObservations(support.registration);
    Assert.assertEquals(1, observations.size());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 8 with Observation

use of org.eclipse.leshan.core.observation.Observation in project leshan by eclipse.

the class ObservationServiceTest method cancel_by_client.

@Test
public void cancel_by_client() {
    // create some observations
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 13));
    givenAnObservation(support.registration.getId(), new LwM2mPath(3, 0, 12));
    givenAnObservation("anotherClient", new LwM2mPath(3, 0, 12));
    // check its presence
    Set<Observation> observations = observationService.getObservations(support.registration);
    Assert.assertEquals(2, observations.size());
    // cancel it
    int nbCancelled = observationService.cancelObservations(support.registration);
    Assert.assertEquals(2, nbCancelled);
    // check its absence
    observations = observationService.getObservations(support.registration);
    Assert.assertTrue(observations.isEmpty());
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) Test(org.junit.Test)

Example 9 with Observation

use of org.eclipse.leshan.core.observation.Observation 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 10 with Observation

use of org.eclipse.leshan.core.observation.Observation 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)

Aggregations

Observation (org.eclipse.leshan.core.observation.Observation)32 Test (org.junit.Test)12 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)11 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)10 Registration (org.eclipse.leshan.server.registration.Registration)10 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)9 ArrayList (java.util.ArrayList)7 LwM2mResponse (org.eclipse.leshan.core.response.LwM2mResponse)7 Token (org.eclipse.californium.core.coap.Token)6 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)6 Jedis (redis.clients.jedis.Jedis)6 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)5 Response (org.eclipse.californium.core.coap.Response)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4 Deregistration (org.eclipse.leshan.server.registration.Deregistration)4 UpdatedRegistration (org.eclipse.leshan.server.registration.UpdatedRegistration)4 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)3 RegistrationListener (org.eclipse.leshan.server.registration.RegistrationListener)3