Search in sources :

Example 11 with LwM2mObjectInstance

use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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 12 with LwM2mObjectInstance

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

the class CreateTest method can_create_specific_instance_of_object_with_json.

@Test
public void can_create_specific_instance_of_object_with_json() throws InterruptedException {
    // create ACL instance
    LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(ContentFormat.JSON, 2, instance));
    // verify result
    assertEquals(ResponseCode.CREATED, response.getCode());
    assertEquals("2/12", response.getLocation());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) Test(org.junit.Test)

Example 13 with LwM2mObjectInstance

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

the class CreateTest method can_create_specific_instance_of_object.

@Test
public void can_create_specific_instance_of_object() throws InterruptedException {
    // create ACL instance
    LwM2mObjectInstance instance = new LwM2mObjectInstance(12, Arrays.<LwM2mResource>asList(LwM2mSingleResource.newIntegerResource(3, 123)));
    CreateResponse response = helper.server.send(helper.getCurrentRegistration(), new CreateRequest(2, instance));
    // verify result
    assertEquals(ResponseCode.CREATED, response.getCode());
    assertEquals("2/12", response.getLocation());
    assertNotNull(response.getCoapResponse());
    assertThat(response.getCoapResponse(), is(instanceOf(Response.class)));
}
Also used : LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) CreateResponse(org.eclipse.leshan.core.response.CreateResponse) CreateRequest(org.eclipse.leshan.core.request.CreateRequest) Test(org.junit.Test)

Example 14 with LwM2mObjectInstance

use of org.eclipse.leshan.core.node.LwM2mObjectInstance 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 15 with LwM2mObjectInstance

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

the class DownlinkRequestSerDes method deserialize.

public static DownlinkRequest<?> deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    String path = o.getString("path", null);
    switch(kind) {
        case "observe":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ObserveRequest(ContentFormat.fromCode(format), path);
            }
        case "delete":
            return new DeleteRequest(path);
        case "discover":
            return new DiscoverRequest(path);
        case "create":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonArray jResources = (JsonArray) o.get("resources");
                for (JsonValue jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
                    resources.add(resource);
                }
                return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
            }
        case "execute":
            String parameters = o.getString("parameters", null);
            return new ExecuteRequest(path, parameters);
        case "writeAttributes":
            {
                String observeSpec = o.getString("observeSpec", null);
                return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
            }
        case "write":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
                LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
                return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
            }
        case "read":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ReadRequest(ContentFormat.fromCode(format), path);
            }
        default:
            throw new IllegalStateException("Invalid request missing kind attribute");
    }
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Collection(java.util.Collection) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Aggregations

LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)49 Test (org.junit.Test)30 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)25 LwM2mObject (org.eclipse.leshan.core.node.LwM2mObject)13 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)13 ReadRequest (org.eclipse.leshan.core.request.ReadRequest)11 CreateRequest (org.eclipse.leshan.core.request.CreateRequest)10 ReadResponse (org.eclipse.leshan.core.response.ReadResponse)9 ArrayList (java.util.ArrayList)8 WriteRequest (org.eclipse.leshan.core.request.WriteRequest)8 TimestampedLwM2mNode (org.eclipse.leshan.core.node.TimestampedLwM2mNode)6 ObserveRequest (org.eclipse.leshan.core.request.ObserveRequest)6 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)5 CreateResponse (org.eclipse.leshan.core.response.CreateResponse)5 DeleteRequest (org.eclipse.leshan.core.request.DeleteRequest)4 ExecuteRequest (org.eclipse.leshan.core.request.ExecuteRequest)4 ObserveResponse (org.eclipse.leshan.core.response.ObserveResponse)4 WriteResponse (org.eclipse.leshan.core.response.WriteResponse)4 JsonObject (com.eclipsesource.json.JsonObject)3 Collection (java.util.Collection)3