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();
}
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());
}
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());
}
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)));
}
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());
}
Aggregations