use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObserveTest method can_observe_timestamped_object.
@Test
public void can_observe_timestamped_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));
// *** HACK send time-stamped notification as Leshan client does not support it *** //
// create time-stamped nodes
TimestampedLwM2mNode mostRecentNode = new TimestampedLwM2mNode(System.currentTimeMillis(), new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Paris"))));
List<TimestampedLwM2mNode> timestampedNodes = new ArrayList<>();
timestampedNodes.add(mostRecentNode);
timestampedNodes.add(new TimestampedLwM2mNode(mostRecentNode.getTimestamp() - 2, new LwM2mObject(3, new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))));
byte[] payload = LwM2mNodeJsonEncoder.encodeTimestampedData(timestampedNodes, new LwM2mPath("/3"), new LwM2mModel(helper.createObjectModels()), new DefaultLwM2mValueConverter());
Response firstCoapResponse = (Response) observeResponse.getCoapResponse();
sendNotification(getConnector(helper.client), payload, firstCoapResponse, ContentFormat.JSON_CODE);
// *** Hack End *** //
// verify result
listener.waitForNotification(2000);
assertTrue(listener.receivedNotify().get());
assertEquals(mostRecentNode.getNode(), listener.getResponse().getContent());
assertEquals(timestampedNodes, listener.getResponse().getTimestampedLwM2mNode());
assertNotNull(listener.getResponse().getCoapResponse());
assertThat(listener.getResponse().getCoapResponse(), is(instanceOf(Response.class)));
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObserveUtil method createLwM2mObservation.
/**
* Create a LWM2M observation from a CoAP request.
*/
public static Observation createLwM2mObservation(Request request) {
String regId = null;
String lwm2mPath = null;
Map<String, String> context = null;
for (Entry<String, String> ctx : request.getUserContext().entrySet()) {
switch(ctx.getKey()) {
case CTX_REGID:
regId = ctx.getValue();
break;
case CTX_LWM2M_PATH:
lwm2mPath = ctx.getValue();
break;
case CTX_ENDPOINT:
break;
default:
if (context == null) {
context = new HashMap<>();
}
context.put(ctx.getKey(), ctx.getValue());
}
}
return new Observation(request.getToken().getBytes(), regId, new LwM2mPath(lwm2mPath), context);
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class ObservationServiceImpl method getObservations.
private Set<Observation> getObservations(String registrationId, String resourcePath) {
if (registrationId == null || resourcePath == null)
return Collections.emptySet();
Set<Observation> result = new HashSet<>();
LwM2mPath lwPath = new LwM2mPath(resourcePath);
for (Observation obs : getObservations(registrationId)) {
if (lwPath.equals(obs.getPath())) {
result.add(obs);
}
}
return result;
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeEncoderTest method json_encode_timestamped_instances.
@Test
public void json_encode_timestamped_instances() throws CodecException {
List<TimestampedLwM2mNode> data = new ArrayList<>();
LwM2mObjectInstance instanceAt110 = new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 22.9));
LwM2mObjectInstance instanceAt120 = new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 22.4), LwM2mSingleResource.newStringResource(0, "a string"));
LwM2mObjectInstance instanceAt130 = new LwM2mObjectInstance(0, LwM2mSingleResource.newFloatResource(1, 24.1));
data.add(new TimestampedLwM2mNode(110L, instanceAt110));
data.add(new TimestampedLwM2mNode(120L, instanceAt120));
data.add(new TimestampedLwM2mNode(130L, instanceAt130));
byte[] encoded = encoder.encodeTimestampedData(data, ContentFormat.JSON, new LwM2mPath(1024, 0), model);
StringBuilder b = new StringBuilder();
b.append("{\"bn\":\"/1024/0\",\"e\":[");
b.append("{\"n\":\"1\",\"v\":22.9,\"t\":110},");
b.append("{\"n\":\"0\",\"sv\":\"a string\",\"t\":120},");
b.append("{\"n\":\"1\",\"v\":22.4,\"t\":120},");
b.append("{\"n\":\"1\",\"v\":24.1,\"t\":130}]}");
String expected = b.toString();
Assert.assertEquals(expected, new String(encoded));
}
use of org.eclipse.leshan.core.node.LwM2mPath in project leshan by eclipse.
the class LwM2mNodeEncoderTest method text_encode_date_as_long.
@Test
public void text_encode_date_as_long() {
byte[] encoded = encoder.encode(LwM2mSingleResource.newStringResource(13, "2010-01-01T12:00:00+01:00"), ContentFormat.TEXT, new LwM2mPath("/3/0/13"), model);
Assert.assertEquals("1262343600", new String(encoded, StandardCharsets.UTF_8));
}
Aggregations