Search in sources :

Example 6 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeSerDes method deserialize.

public static LwM2mNode deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
    switch(kind) {
        case "object":
            {
                Collection<LwM2mObjectInstance> instances = new ArrayList<>();
                JsonArray jInstances = (JsonArray) o.get("instances");
                for (JsonValue jInstance : jInstances) {
                    LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
                    instances.add(instance);
                }
                return new LwM2mObject(id, instances);
            }
        case "instance":
            {
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonObject jResources = (JsonObject) o.get("resources");
                for (Member jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
                    resources.add(resource);
                }
                return new LwM2mObjectInstance(id, resources);
            }
        case "singleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Object value = ValueSerDes.deserialize(o.get("value"), type);
                return LwM2mSingleResource.newResource(id, value, type);
            }
        case "multipleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Map<Integer, Object> values = new HashMap<>();
                JsonObject jValues = (JsonObject) o.get("values");
                for (Member jValue : jValues) {
                    Integer valueId = Integer.valueOf(jValue.getName());
                    Object value = ValueSerDes.deserialize(jValue.getValue(), type);
                    values.put(valueId, value);
                }
                return LwM2mMultipleResource.newResource(id, values, type);
            }
        default:
            throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) JsonArray(com.eclipsesource.json.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(org.eclipse.leshan.core.model.ResourceModel.Type) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Member(com.eclipsesource.json.JsonObject.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with LwM2mObject

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

Example 8 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class ReadTest method can_read_object.

@Test
public void can_read_object() throws InterruptedException {
    // read device object
    ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(3));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    LwM2mObject object = (LwM2mObject) response.getContent();
    assertEquals(3, object.getId());
    LwM2mObjectInstance instance = object.getInstance(0);
    assertEquals(0, instance.getId());
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 9 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class ReadTest method can_read_empty_object.

@Test
public void can_read_empty_object() throws InterruptedException {
    // read ACL object
    ReadResponse response = helper.server.send(helper.getCurrentRegistration(), new ReadRequest(2));
    // verify result
    assertEquals(CONTENT, response.getCode());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
    LwM2mObject object = (LwM2mObject) response.getContent();
    assertEquals(2, object.getId());
    assertTrue(object.getInstances().isEmpty());
}
Also used : ReadResponse(org.eclipse.leshan.core.response.ReadResponse) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) ReadRequest(org.eclipse.leshan.core.request.ReadRequest) Test(org.junit.Test)

Example 10 with LwM2mObject

use of org.eclipse.leshan.core.node.LwM2mObject in project leshan by eclipse.

the class LwM2mNodeEncoderTest method tlv_encode_device_object.

@Test
public void tlv_encode_device_object() {
    LwM2mObject object = new LwM2mObject(3, new LwM2mObjectInstance(0, getDeviceResources()));
    byte[] encoded = encoder.encode(object, ContentFormat.TLV, new LwM2mPath("/3"), model);
    // encoded as an array of resource TLVs
    Assert.assertArrayEquals(ENCODED_DEVICE_WITH_INSTANCE, encoded);
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Test(org.junit.Test)

Aggregations

LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)21 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)13 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)6 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)5 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)4 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)3 Tlv (org.eclipse.leshan.tlv.Tlv)3 JsonObject (com.eclipsesource.json.JsonObject)2 Collection (java.util.Collection)2 Entry (java.util.Map.Entry)2 Type (org.eclipse.leshan.core.model.ResourceModel.Type)2 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)2 CodecException (org.eclipse.leshan.core.node.codec.CodecException)2 Observation (org.eclipse.leshan.core.observation.Observation)2 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)2