Search in sources :

Example 46 with LwM2mPath

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)));
}
Also used : ArrayList(java.util.ArrayList) 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) 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) TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Example 47 with LwM2mPath

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);
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation)

Example 48 with LwM2mPath

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;
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Observation(org.eclipse.leshan.core.observation.Observation) HashSet(java.util.HashSet)

Example 49 with LwM2mPath

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));
}
Also used : TimestampedLwM2mNode(org.eclipse.leshan.core.node.TimestampedLwM2mNode) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 50 with LwM2mPath

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));
}
Also used : LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) Test(org.junit.Test)

Aggregations

LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)70 Test (org.junit.Test)51 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 Observation (org.eclipse.leshan.core.observation.Observation)11 ArrayList (java.util.ArrayList)10 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)10 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)9 Tlv (org.eclipse.leshan.tlv.Tlv)8 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)7 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)6 CodecException (org.eclipse.leshan.core.node.codec.CodecException)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)6 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)5 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)5 HashMap (java.util.HashMap)4 Response (org.eclipse.californium.core.coap.Response)4 ResourceModel (org.eclipse.leshan.core.model.ResourceModel)4 DefaultLwM2mValueConverter (org.eclipse.leshan.core.node.codec.DefaultLwM2mValueConverter)4